socket_client.cpp 13 KB

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