socket_remote.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "pch.h"
  2. #include <thread>
  3. #include "../protocol/win/remote.pb.h"
  4. #include "socket_remote.h"
  5. #include "../common/iobuffer.h"
  6. SocketRemote::SocketRemote(IRemoteNotify* n) :_notify(n)
  7. {
  8. _connected = false;
  9. }
  10. bool SocketRemote::Start(const char* ip)
  11. {
  12. _ip = ip;
  13. FnMap.insert(std::make_pair(remote::sc_robot, &SocketRemote::OnRobotRep));
  14. FnMap.insert(std::make_pair(remote::sc_NotifyRep, &SocketRemote::OnNotifyRep));
  15. FnMap.insert(std::make_pair(remote::sc_NotifyAdd, &SocketRemote::OnNotifyAdd));
  16. FnMap.insert(std::make_pair(remote::sc_NotifyDel, &SocketRemote::OnNotifyDel));
  17. //FnMap.insert(std::make_pair(remote::sc_NotifyState, &SocketRemote::OnNotifyState));
  18. FnMap.insert(std::make_pair(remote::sc_add, &SocketRemote::OnAdd));
  19. FnMap.insert(std::make_pair(remote::sc_NotifyReq, &SocketRemote::OnNotifyReq));
  20. FnMap.insert(std::make_pair(remote::sc_NotifyLeave, &SocketRemote::OnNotifyLeave));
  21. FnMap.insert(std::make_pair(remote::sc_NotifyOffer, &SocketRemote::OnNotifyOffer));
  22. FnMap.insert(std::make_pair(remote::sc_NotifyAnswer, &SocketRemote::OnNotifyAnswer));
  23. FnMap.insert(std::make_pair(remote::sc_NotifyCandidate, &SocketRemote::OnNotifyCandidate));
  24. WSAData data;
  25. WSAStartup(MAKEWORD(2, 2), &data);
  26. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  27. sockaddr_in sin;
  28. sin.sin_family = AF_INET;
  29. sin.sin_port = htons(20916);
  30. sin.sin_addr.s_addr = inet_addr(ip);
  31. if (connect(sockfd, (const sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
  32. {
  33. DWORD error = WSAGetLastError();
  34. closesocket(sockfd);
  35. sockfd = INVALID_SOCKET;
  36. _connected = false;
  37. }
  38. else
  39. {
  40. _connected = true;
  41. }
  42. _notify->OnConnected(_connected);
  43. _thread = std::thread(&SocketRemote::Run, this);
  44. return true;
  45. }
  46. void SocketRemote::OnRobotRep(int8_t* Data, int16_t Size)
  47. {
  48. remote::SCRobot Rep;
  49. Rep.ParseFromArray(Data, Size);
  50. for (int32_t i = 0; i < Rep.robot_size(); i++)
  51. {
  52. auto& node = Rep.robot(i);
  53. _notify->OnRobot(node);
  54. }
  55. }
  56. void SocketRemote::OnAdd(int8_t* Data, int16_t Size)
  57. {
  58. remote::SCAdd Rep;
  59. Rep.ParseFromArray(Data, Size);
  60. if (Rep.ret() == true)
  61. {
  62. _uid = Rep.uid();
  63. }
  64. _notify->OnAdd(_uid, Rep.ret());
  65. }
  66. void SocketRemote::OnNotifyAnswer(int8_t* Data, int16_t Size)
  67. {
  68. remote::Answer Rep;
  69. Rep.ParseFromArray(Data, Size);
  70. _notify->OnVideoAnswer(Rep.index(), Rep.type().c_str(), Rep.sdp().c_str());
  71. }
  72. void SocketRemote::OnNotifyCandidate(int8_t* Data, int16_t Size)
  73. {
  74. remote::Candidate Rep;
  75. Rep.ParseFromArray(Data, Size);
  76. _notify->OnVideoCandidate(Rep.index(), Rep.candidate().c_str(), Rep.sdpmlineindex(), Rep.sdpmid().c_str());
  77. }
  78. void SocketRemote::OnNotifyOffer(int8_t* Data, int16_t Size)
  79. {
  80. remote::Offer Rep;
  81. Rep.ParseFromArray(Data, Size);
  82. _notify->OnVideoOffer(Rep.index(), Rep.type().c_str(), Rep.sdp().c_str());
  83. }
  84. void SocketRemote::OnNotifyRep(int8_t* Data, int16_t Size)
  85. {
  86. remote::CSRep Rep;
  87. Rep.ParseFromArray(Data, Size);
  88. auto ok = Rep.desc() == remote::VideoDesc::OK;
  89. _notify->OnVideoRep(ok, Rep.index(), Rep.peer());
  90. }
  91. void SocketRemote::OnNotifyAdd(int8_t* Data, int16_t Size)
  92. {
  93. remote::SCAddRobot Rep;
  94. Rep.ParseFromArray(Data, Size);
  95. _notify->OnRobot(Rep.robot());
  96. }
  97. void SocketRemote::OnNotifyDel(int8_t* Data, int16_t Size)
  98. {
  99. remote::SCDelRobot Rep;
  100. Rep.ParseFromArray(Data, Size);
  101. _notify->OnNotifyDel(Rep.peer(), (EgoType)(Rep.egotype()));
  102. }
  103. void SocketRemote::OnNotifyReq(int8_t* Data, int16_t Size)
  104. {
  105. remote::CSReq Rep;
  106. Rep.ParseFromArray(Data, Size);
  107. _notify->OnVideoReq(Rep.index(), Rep.peer());
  108. }
  109. void SocketRemote::OnNotifyLeave(int8_t* Data, int16_t Size)
  110. {
  111. remote::Leave Req;
  112. Req.ParseFromArray(Data, Size);
  113. int32_t peer = Req.peer();
  114. EgoType type = static_cast<EgoType>(Req.egotype());
  115. _notify->OnVideoLeave(peer, type);
  116. }
  117. void SocketRemote::Run()
  118. {
  119. _run = true;
  120. int32_t Offset = 0;
  121. CIOBuffer Buffer;
  122. while (_run)
  123. {
  124. if (_connected)
  125. {
  126. auto ret = recv(sockfd, (char*)&Buffer.Buffer[Offset], CIOBuffer::IO_BUFFER_SIZE - Offset, 0);
  127. if (ret <= 0)
  128. {
  129. DWORD error = WSAGetLastError();
  130. closesocket(sockfd);
  131. _connected = false;
  132. _notify->OnConnected(_connected);
  133. }
  134. else
  135. {
  136. Offset += ret;
  137. if (Offset >= MessageHead::Size())
  138. {
  139. bool bNeedMove = false;
  140. MessageHead head;
  141. int8_t* ptr = Buffer.Buffer;
  142. while (true)
  143. {
  144. if (MessageHead::Size() <= Offset)
  145. {
  146. head.Deserialize(ptr);
  147. int32_t length = MessageHead::Size() + head.Length;
  148. if (Offset >= length)
  149. {
  150. int8_t* Data = ptr + MessageHead::Size();
  151. NetProcess(head.Command, Data, head.Length);
  152. ptr += length;
  153. Offset -= length;
  154. }
  155. else
  156. {
  157. bNeedMove = Offset > 0;
  158. if (bNeedMove)
  159. {
  160. std::cout << "need Move " << Offset << std::endl;
  161. }
  162. break;
  163. }
  164. }
  165. else
  166. {
  167. break;
  168. }
  169. }
  170. if (bNeedMove)
  171. {
  172. memmove(Buffer.Buffer, ptr, Offset);
  173. }
  174. }
  175. }
  176. }
  177. else
  178. {
  179. std::this_thread::sleep_for(std::chrono::seconds(1));
  180. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  181. sockaddr_in sin;
  182. sin.sin_family = AF_INET;
  183. sin.sin_port = htons(20916);
  184. sin.sin_addr.s_addr = inet_addr(_ip.c_str());
  185. if (connect(sockfd, (const sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
  186. {
  187. closesocket(sockfd);
  188. sockfd = INVALID_SOCKET;
  189. continue;
  190. }
  191. _connected = true;
  192. _notify->OnConnected(_connected);
  193. }
  194. }
  195. }
  196. void SocketRemote::NetProcess(int16_t cmd, int8_t* Data, int16_t Size)
  197. {
  198. auto it = FnMap.find(cmd);
  199. if (it != FnMap.end())
  200. {
  201. (this->*it->second)(Data, Size);
  202. }
  203. }