socket_client.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include "../common/iobuffer.h"
  2. #include "protocol.pb.h"
  3. #include "socket_client.h"
  4. #include <iostream>
  5. #include <memory.h>
  6. SocketClient::SocketClient(INativeNotify* n):_notify(n)
  7. {
  8. _connected = false;
  9. }
  10. bool SocketClient::Start(const char * ip)
  11. {
  12. #ifdef WIN32
  13. FnMap.insert(std::make_pair(RemoNet::SC_Sign,&SocketClient::OnSigin));
  14. FnMap.insert(std::make_pair(RemoNet::SC_Robot, &SocketClient::OnRobotRep));
  15. FnMap.insert(std::make_pair(RemoNet::SC_NotifyRep, &SocketClient::OnNotifyRep));
  16. FnMap.insert(std::make_pair(RemoNet::SC_NotifyAdd, &SocketClient::OnNotifyAdd));
  17. FnMap.insert(std::make_pair(RemoNet::SC_NotifyDel, &SocketClient::OnNotifyDel));
  18. FnMap.insert(std::make_pair(RemoNet::SC_KickOff, &SocketClient::OnNotifyKick));
  19. FnMap.insert(std::make_pair(RemoNet::SC_MoveEnd, &SocketClient::OnNotifyMoveEnd));
  20. FnMap.insert(std::make_pair(RemoNet::SC_MoveRet, &SocketClient::OnNotifyMoveRet));
  21. FnMap.insert(std::make_pair(RemoNet::SC_State, &SocketClient::OnNotifyState));
  22. #else
  23. FnMap.insert(std::make_pair(RemoNet::SC_Add, &SocketClient::OnAdd));
  24. FnMap.insert(std::make_pair(RemoNet::SC_NotifyReq, &SocketClient::OnNotifyReq));
  25. FnMap.insert(std::make_pair(RemoNet::SC_MoveBegin, &SocketClient::OnNotifyMoveBegin));
  26. FnMap.insert(std::make_pair(RemoNet::SC_SwitchDriver, &SocketClient::OnNotifySwitchDriver));
  27. #endif
  28. FnMap.insert(std::make_pair(RemoNet::SC_NotifyLeave, &SocketClient::OnNotifyLeave));
  29. FnMap.insert(std::make_pair(RemoNet::SC_NotifyOffer, &SocketClient::OnNotifyOffer));
  30. FnMap.insert(std::make_pair(RemoNet::SC_NotifyAnswer, &SocketClient::OnNotifyAnswer));
  31. FnMap.insert(std::make_pair(RemoNet::SC_NotifyCandidate, &SocketClient::OnNotifyCandidate));
  32. _ip=ip;
  33. #ifdef WIN32
  34. WSAData data;
  35. WSAStartup(MAKEWORD(2, 2), &data);
  36. #endif
  37. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  38. sockaddr_in sin;
  39. sin.sin_family = AF_INET;
  40. sin.sin_port = htons(20916);
  41. sin.sin_addr.s_addr = inet_addr(ip);
  42. if (connect(sockfd, (const sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
  43. {
  44. #ifdef WIN32
  45. DWORD error = WSAGetLastError();
  46. closesocket(sockfd);
  47. #else
  48. close(sockfd);
  49. #endif
  50. sockfd = INVALID_SOCKET;
  51. _connected=false;
  52. // return false;
  53. }
  54. else
  55. {
  56. _connected = true;
  57. }
  58. _notify->OnConnected(_connected);
  59. // int flag = 1;
  60. // setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag,
  61. // sizeof(flag)); // Disable Nagle's algorithm
  62. #ifdef WIN32
  63. // u_long on = 1;
  64. // ioctlsocket(sockfd, FIONBIO, &on);
  65. #else
  66. // fcntl(sockfd, F_SETFL, O_NONBLOCK);
  67. #endif
  68. _thread = std::thread(&SocketClient::Run, this);
  69. return true;
  70. }
  71. void SocketClient::Run()
  72. {
  73. _run = true;
  74. int32_t Offset = 0;
  75. CIOBuffer Buffer;
  76. while (_run)
  77. {
  78. if (_connected)
  79. {
  80. auto ret = recv(sockfd, (char*)&Buffer.Buffer[Offset], CIOBuffer::IO_BUFFER_SIZE - Offset, 0);
  81. if (ret <= 0)
  82. {
  83. #ifdef WIN32
  84. DWORD error = WSAGetLastError();
  85. closesocket(sockfd);
  86. #else
  87. close(sockfd);
  88. #endif
  89. _connected = false;
  90. _notify->OnConnected(_connected);
  91. }
  92. else
  93. {
  94. Offset += ret;
  95. if (Offset >= MessageHead::Size())
  96. {
  97. bool bNeedMove = false;
  98. MessageHead head;
  99. int8_t* ptr = Buffer.Buffer;
  100. while (true)
  101. {
  102. if (MessageHead::Size() <= Offset)
  103. {
  104. head.Deserialize(ptr);
  105. int32_t length = MessageHead::Size() + head.Length;
  106. if (Offset >= length)
  107. {
  108. int8_t* Data = ptr + MessageHead::Size();
  109. NetProcess(head.Command, Data, head.Length);
  110. ptr += length;
  111. Offset -= length;
  112. }
  113. else
  114. {
  115. bNeedMove = Offset > 0;
  116. if(bNeedMove)
  117. {
  118. std::cout<<"need Move "<<Offset<<std::endl;
  119. }
  120. break;
  121. }
  122. }
  123. else
  124. {
  125. break;
  126. }
  127. }
  128. if (bNeedMove)
  129. {
  130. memmove(Buffer.Buffer, ptr, Offset);
  131. }
  132. }
  133. }
  134. }
  135. else
  136. {
  137. std::this_thread::sleep_for(std::chrono::seconds(1));
  138. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  139. sockaddr_in sin;
  140. sin.sin_family = AF_INET;
  141. sin.sin_port = htons(20916);
  142. sin.sin_addr.s_addr = inet_addr(_ip.c_str());
  143. if (connect(sockfd, (const sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
  144. {
  145. #ifdef WIN32
  146. closesocket(sockfd);
  147. #else
  148. close(sockfd);
  149. #endif
  150. sockfd = INVALID_SOCKET;
  151. continue;
  152. }
  153. _connected = true;
  154. _notify->OnConnected(_connected);
  155. }
  156. }
  157. }
  158. void SocketClient::Stop()
  159. {
  160. _run = false;
  161. #ifdef WIN32
  162. closesocket(sockfd);
  163. #else
  164. close(sockfd);
  165. #endif
  166. sockfd = INVALID_SOCKET;
  167. _thread.join();
  168. }
  169. void SocketClient::NetProcess(int16_t cmd, int8_t* Data, int16_t Size)
  170. {
  171. auto it = FnMap.find(cmd);
  172. if (it != FnMap.end())
  173. {
  174. (this->*it->second)(Data, Size);
  175. }
  176. }
  177. #ifdef WIN32
  178. void SocketClient::OnSigin(int8_t* Data, int16_t Size)
  179. {
  180. RemoNet::SCSign Rep;
  181. Rep.ParseFromArray(Data, Size);
  182. if (Rep.ret() == true)
  183. {
  184. _uid = Rep.uid();
  185. }
  186. _notify->OnSigin(_uid,Rep.ret());
  187. }
  188. void SocketClient::OnRobotRep(int8_t* Data, int16_t Size)
  189. {
  190. RemoNet::SCRobot Rep;
  191. Rep.ParseFromArray(Data, Size);
  192. for (int32_t i = 0; i < Rep.robot_size(); i++)
  193. {
  194. auto& node = Rep.robot(i);
  195. _notify->OnRobot(node);
  196. }
  197. }
  198. void SocketClient::WriteRobotReq()
  199. {
  200. RemoNet::CSRobot Req;
  201. MessageHead Head;
  202. CIOBuffer pBuffer;
  203. Head.Command = RemoNet::CS_Robot;
  204. Head.Length = Req.ByteSizeLong();
  205. Head.Serialize(pBuffer.Buffer);
  206. auto ptr = pBuffer.Buffer + MessageHead::Size();
  207. Req.SerializeToArray(ptr, Head.Length);
  208. pBuffer.Length = MessageHead::Size() + Head.Length;
  209. Write(&pBuffer);
  210. }
  211. void SocketClient::WriteSign(const char* account, const char* password)
  212. {
  213. RemoNet::CSSign Req;
  214. Req.set_account(account);
  215. Req.set_password(password);
  216. MessageHead Head;
  217. CIOBuffer pBuffer;
  218. Head.Command = RemoNet::CS_Sign;
  219. Head.Length = Req.ByteSizeLong();
  220. Head.Serialize(pBuffer.Buffer);
  221. auto ptr = pBuffer.Buffer + MessageHead::Size();
  222. Req.SerializeToArray(ptr, Head.Length);
  223. pBuffer.Length = MessageHead::Size() + Head.Length;
  224. Write(&pBuffer);
  225. }
  226. void SocketClient::WriteVideoLeave(EgoType type, int32_t peer)
  227. {
  228. RemoNet::Leave Req;
  229. Req.set_peer(peer);
  230. Req.set_egotype(type);
  231. MessageHead Head;
  232. CIOBuffer pBuffer;
  233. Head.Command = RemoNet::CS_Leave;
  234. Head.Length = Req.ByteSizeLong();
  235. Head.Serialize(pBuffer.Buffer);
  236. auto ptr = pBuffer.Buffer + MessageHead::Size();
  237. Req.SerializeToArray(ptr, Head.Length);
  238. pBuffer.Length = MessageHead::Size() + Head.Length;
  239. Write(&pBuffer);
  240. }
  241. #else
  242. void SocketClient::OnAdd(int8_t* Data, int16_t Size)
  243. {
  244. RemoNet::SCAdd Rep;
  245. Rep.ParseFromArray(Data, Size);
  246. if (Rep.ret() == true)
  247. {
  248. _uid = Rep.uid();
  249. }
  250. _notify->OnAdd(_uid,Rep.ret());
  251. }
  252. void SocketClient::WriteAddRobot(std::string& serial,std::string& name,std::string url,int32_t type,int32_t car)
  253. {
  254. RemoNet::CSAdd Req;
  255. Req.set_serial(serial.c_str());
  256. Req.set_name(name.c_str());
  257. Req.set_type(type);
  258. Req.set_car(car);
  259. MessageHead Head;
  260. CIOBuffer pBuffer;
  261. Head.Command = RemoNet::CS_Add;
  262. Head.Length = Req.ByteSizeLong();
  263. Head.Serialize(pBuffer.Buffer);
  264. auto ptr = pBuffer.Buffer + MessageHead::Size();
  265. Req.SerializeToArray(ptr, Head.Length);
  266. pBuffer.Length = MessageHead::Size() + Head.Length;
  267. Write(&pBuffer);
  268. }
  269. void SocketClient::OnNotifySwitchDriver(int8_t* Data, int16_t Size)
  270. {
  271. _notify->OnSwitchDriver();
  272. }
  273. #endif
  274. void SocketClient::OnNotifyAnswer(int8_t* Data, int16_t Size)
  275. {
  276. RemoNet::Answer Rep;
  277. Rep.ParseFromArray(Data, Size);
  278. _notify->OnVideoAnswer(Rep.index(), Rep.type().c_str(), Rep.sdp().c_str());
  279. }
  280. void SocketClient::OnNotifyCandidate(int8_t* Data, int16_t Size)
  281. {
  282. RemoNet::Candidate Rep;
  283. Rep.ParseFromArray(Data, Size);
  284. _notify->OnVideoCandidate(Rep.index(), Rep.candidate().c_str(), Rep.sdpmlineindex(), Rep.sdpmid().c_str());
  285. }
  286. #ifdef WIN32
  287. void SocketClient::OnNotifyState(int8_t* Data, int16_t Size)
  288. {
  289. RemoNet::SCState Rep;
  290. Rep.ParseFromArray(Data, Size);
  291. _notify->OnNotifyState(Rep.uid(), (UserState)Rep.state());
  292. }
  293. #endif
  294. void SocketClient::OnNotifyOffer(int8_t* Data, int16_t Size)
  295. {
  296. RemoNet::Offer Rep;
  297. Rep.ParseFromArray(Data, Size);
  298. _notify->OnVideoOffer(Rep.index(), Rep.type().c_str(), Rep.sdp().c_str());
  299. }
  300. #ifdef WIN32
  301. void SocketClient::OnNotifyRep(int8_t* Data, int16_t Size)
  302. {
  303. RemoNet::CSRep Rep;
  304. Rep.ParseFromArray(Data, Size);
  305. auto ok = Rep.desc() == RemoNet::VideoDesc::OK;
  306. _notify->OnVideoRep(ok,Rep.index(),Rep.peer());
  307. }
  308. void SocketClient::OnNotifyAdd(int8_t* Data, int16_t Size)
  309. {
  310. RemoNet::SCAddRobot Rep;
  311. Rep.ParseFromArray(Data, Size);
  312. _notify->OnRobot(Rep.robot());
  313. }
  314. void SocketClient::OnNotifyDel(int8_t* Data, int16_t Size)
  315. {
  316. RemoNet::SCDelRobot Rep;
  317. Rep.ParseFromArray(Data, Size);
  318. _notify->OnNotifyDel(Rep.peer(),(EgoType)(Rep.egotype()));
  319. }
  320. void SocketClient::OnNotifyKick(int8_t* Data, int16_t Size)
  321. {
  322. _notify->OnNotifyKick();
  323. }
  324. void SocketClient::OnNotifyMoveEnd(int8_t* Data, int16_t Size)
  325. {
  326. RemoNet::SCMoveEnd Rep;
  327. Rep.ParseFromArray(Data, Size);
  328. int32_t uid = Rep.uid();
  329. WorkArea area = static_cast<WorkArea>(Rep.area());
  330. int32_t no = Rep.no();
  331. _notify->OnMoveEnd(uid, area, no);
  332. }
  333. void SocketClient::OnNotifyMoveRet(int8_t* Data, int16_t Size)
  334. {
  335. RemoNet::MoveRet Rep;
  336. Rep.ParseFromArray(Data, Size);
  337. MoveDesc desc = (MoveDesc)Rep.desc();
  338. _notify->OnNotifyMoveRet(desc);
  339. }
  340. #else
  341. void SocketClient::OnNotifyReq(int8_t* Data, int16_t Size)
  342. {
  343. RemoNet::CSReq Rep;
  344. Rep.ParseFromArray(Data, Size);
  345. _notify->OnVideoReq(Rep.index(),Rep.peer());
  346. }
  347. void SocketClient::OnNotifyMoveBegin(int8_t* Data, int16_t Size)
  348. {
  349. RemoNet::SCMoveBegin Rep;
  350. Rep.ParseFromArray(Data, Size);
  351. int32_t are=Rep.area();
  352. int32_t no=Rep.no();
  353. _notify->OnMoveBegin(static_cast<WorkArea>(Rep.area()), Rep.no());
  354. }
  355. #endif
  356. void SocketClient::OnNotifyLeave(int8_t* Data, int16_t Size)
  357. {
  358. RemoNet::Leave Req;
  359. Req.ParseFromArray(Data, Size);
  360. int32_t peer = Req.peer();
  361. EgoType type = static_cast<EgoType>(Req.egotype());
  362. _notify->OnVideoLeave(peer,type);
  363. }
  364. void SocketClient::WriteVideoReq(int32_t peer,int32_t index)
  365. {
  366. //_peer = peer;
  367. RemoNet::CSReq Req;
  368. Req.set_peer(peer);
  369. Req.set_index(index);
  370. Req.set_egotype(EgoType::Car);
  371. MessageHead Head;
  372. CIOBuffer pBuffer;
  373. Head.Command = RemoNet::CS_Req;
  374. Head.Length = Req.ByteSizeLong();
  375. Head.Serialize(pBuffer.Buffer);
  376. auto ptr = pBuffer.Buffer + MessageHead::Size();
  377. Req.SerializeToArray(ptr, Head.Length);
  378. pBuffer.Length = MessageHead::Size() + Head.Length;
  379. Write(&pBuffer);
  380. }
  381. void SocketClient::WriteVideoRep(int32_t peer,RemoNet::VideoDesc desc,int32_t index)
  382. {
  383. // _peer = peer;
  384. RemoNet::CSRep Req;
  385. Req.set_peer(peer);
  386. Req.set_desc(desc);
  387. Req.set_index(index);
  388. MessageHead Head;
  389. CIOBuffer pBuffer;
  390. Head.Command = RemoNet::CS_Rep;
  391. Head.Length = Req.ByteSizeLong();
  392. Head.Serialize(pBuffer.Buffer);
  393. auto ptr = pBuffer.Buffer + MessageHead::Size();
  394. Req.SerializeToArray(ptr, Head.Length);
  395. pBuffer.Length = MessageHead::Size() + Head.Length;
  396. Write(&pBuffer);
  397. }
  398. void SocketClient::Write(CIOBuffer* pBuffer)
  399. {
  400. if (_connected)
  401. {
  402. int32_t ret=::send(sockfd, (const char *)pBuffer->Buffer, pBuffer->Length, 0);
  403. if (ret <= 0)
  404. {
  405. #ifdef WIN32
  406. closesocket(sockfd);
  407. #else
  408. close(sockfd);
  409. #endif
  410. _connected = false;
  411. }
  412. }
  413. }
  414. void SocketClient::WriteOffer(int32_t peer,int32_t index,const char* type, const char* sdp)
  415. {
  416. RemoNet::Offer Req;
  417. Req.set_peer(peer);
  418. Req.set_sdp(sdp);
  419. Req.set_type(type);
  420. Req.set_index(index);
  421. MessageHead Head;
  422. CIOBuffer pBuffer;
  423. Head.Command = RemoNet::CS_Offer;
  424. Head.Length = Req.ByteSizeLong();
  425. Head.Serialize(pBuffer.Buffer);
  426. auto ptr = pBuffer.Buffer + MessageHead::Size();
  427. Req.SerializeToArray(ptr, Head.Length);
  428. pBuffer.Length = MessageHead::Size() + Head.Length;
  429. Write(&pBuffer);
  430. }
  431. void SocketClient::WriteAnswer(int32_t peer,int32_t index, const char* type, const char* sdp)
  432. {
  433. RemoNet::Answer Req;
  434. Req.set_peer(peer);
  435. Req.set_sdp(sdp);
  436. Req.set_type(type);
  437. Req.set_index(index);
  438. MessageHead Head;
  439. CIOBuffer pBuffer;
  440. Head.Command = RemoNet::CS_Answer;
  441. Head.Length = Req.ByteSizeLong();
  442. Head.Serialize(pBuffer.Buffer);
  443. auto ptr = pBuffer.Buffer + MessageHead::Size();
  444. Req.SerializeToArray(ptr, Head.Length);
  445. pBuffer.Length = MessageHead::Size() + Head.Length;
  446. Write(&pBuffer);
  447. }
  448. void SocketClient::WriteCandidate(int32_t peer,int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  449. {
  450. RemoNet::Candidate Req;
  451. Req.set_peer(peer);
  452. Req.set_candidate(candidate);
  453. Req.set_index(index);
  454. Req.set_sdpmid(sdp_mid);
  455. Req.set_sdpmlineindex(sdp_mline_index);
  456. MessageHead Head;
  457. CIOBuffer pBuffer;
  458. Head.Command = RemoNet::CS_Candidate;
  459. Head.Length = Req.ByteSizeLong();
  460. Head.Serialize(pBuffer.Buffer);
  461. auto ptr = pBuffer.Buffer + MessageHead::Size();
  462. Req.SerializeToArray(ptr, Head.Length);
  463. pBuffer.Length = MessageHead::Size() + Head.Length;
  464. Write(&pBuffer);
  465. }
  466. /*
  467. void SocketClient::MessageCallback(void * user_data,const void * data,const int32_t size)
  468. {
  469. SocketClient* lhs=static_cast<SocketClient*>(user_data);
  470. lhs->OnPeerMessage(data,size);
  471. }*/
  472. void SocketClient::OnPeerMessage(ChannelType type,int16_t cmd,int16_t length,const void * data)
  473. {
  474. _notify->OnMessageFrameNotify(type,cmd,length,data);
  475. }
  476. void SocketClient::WriteKeepAlive()
  477. {
  478. MessageHead Head;
  479. CIOBuffer pBuffer;
  480. Head.Command = RemoNet::CS_KeepAlive;
  481. Head.Length = 0;
  482. Head.Serialize(pBuffer.Buffer);
  483. pBuffer.Length = MessageHead::Size() + Head.Length;
  484. Write(&pBuffer);
  485. }