socket_client.cpp 14 KB

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