socket_client.cpp 12 KB

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