UserSocket.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <WinSock2.h>
  4. #include <mutex>
  5. #include "../common/pool.h"
  6. #include "../common/IOBuffer.h"
  7. #include "../webrtcinterop/include/api.h"
  8. #include "../Protocol/win/Protocol.pb.h"
  9. #include "UserManager.h"
  10. #include "DBConnect.h"
  11. #include "scoped_ptr.h"
  12. #include "UserSocket.h"
  13. #include "WorkAreaManager.h"
  14. FNRedirector<CUserSocket> CUserSocket::Redirector;
  15. CUserSocket::CUserSocket(HANDLE hCom, SOCKET s) :CIOSocket(hCom, s)
  16. {
  17. _uid = -1;
  18. _loginsucc = false;
  19. _egoType = EgoType::None;
  20. _userpeer = -1;
  21. Init(hCom);
  22. TimeTick = GetTickCount64();
  23. }
  24. void CUserSocket::InitFnDirector()
  25. {
  26. Redirector.Insert(RemoNet::CS_Answer, &CUserSocket::OnAnswer);
  27. Redirector.Insert(RemoNet::CS_Candidate, &CUserSocket::OnCadidate);
  28. Redirector.Insert(RemoNet::CS_KeepAlive, &CUserSocket::OnKeepAlive);
  29. Redirector.Insert(RemoNet::CS_Add, &CUserSocket::OnAdd);
  30. Redirector.Insert(RemoNet::CS_Robot, &CUserSocket::OnRobot);
  31. Redirector.Insert(RemoNet::CS_Offer, &CUserSocket::OnOffer);
  32. Redirector.Insert(RemoNet::CS_Rep, &CUserSocket::OnRepVideo);
  33. Redirector.Insert(RemoNet::CS_Req, &CUserSocket::OnReqVideo);
  34. Redirector.Insert(RemoNet::CS_Sign, &CUserSocket::OnSigin);
  35. Redirector.Insert(RemoNet::CS_Leave, &CUserSocket::OnLeave);
  36. Redirector.Insert(RemoNet::CS_MoveBegin, &CUserSocket::OnMoveBegin);
  37. Redirector.Insert(RemoNet::CS_MoveEnd, &CUserSocket::OnMoveEnd);
  38. Redirector.Insert(RemoNet::CS_State, &CUserSocket::OnChangeState);
  39. Redirector.Insert(RemoNet::CS_SwitchDriver, &CUserSocket::OnSwitchDriver);
  40. Redirector.Insert(RemoNet::CS_MoveRet, &CUserSocket::OnMoveRet);
  41. //Redirector.Insert(RemoNet::CS_CloseVideo, &CUserSocket::OnCloseVideo);
  42. }
  43. void CUserSocket::OnAnswer(int8_t* Data, int16_t Size)
  44. {
  45. std::cout << __FUNCTION__ << std::endl;
  46. RemoNet::Answer Req;
  47. Req.ParseFromArray(Data, Size);
  48. int32_t peer = Req.peer();
  49. std::cout << Req.sdp().c_str() << std::endl;
  50. CUserManager::GetInstance().NotifyAnswer(peer, _uid, Req.index(), Req.type().c_str(), Req.sdp().c_str());
  51. // Req.set_peer(_uid);
  52. // CIOBuffer* pBuffer = CIOBuffer::Alloc();
  53. // MessageHead Head;
  54. // Head.Command = RemoNet::SC_Answer;
  55. // Head.Length = Req.ByteSizeLong();
  56. // Head.Serialize(pBuffer->Buffer);
  57. // auto ptr = pBuffer->Buffer + MessageHead::Size();
  58. // Req.SerializeToArray(ptr, Head.Length);
  59. // pBuffer->Length = MessageHead::Size() + Head.Length;
  60. // CUserManager::GetInstance().Write(peer, pBuffer);
  61. // pBuffer->Release();
  62. }
  63. void CUserSocket::OnLeave(int8_t* Data, int16_t Size)
  64. {
  65. RemoNet::Leave Req;
  66. Req.ParseFromArray(Data, Size);
  67. auto type = Req.egotype();
  68. if (_egoType != EgoType::User||type==EgoType::User) return;
  69. int32_t peer = Req.peer();
  70. if (type == EgoType::Car)
  71. {
  72. if (peer != _userpeer) return;
  73. _userpeer = -1;
  74. }
  75. CUserManager::GetInstance().LeavePeerVideo(peer, _uid, _egoType);
  76. }
  77. //
  78. // void CUserSocket::OnCloseVideo(int8_t* Data, int16_t Size)
  79. // {
  80. // std::cout << "OnCloseVideo" << std::endl;
  81. // RemoNet::Close Req;
  82. // Req.ParseFromArray(Data, Size);
  83. // auto type = Req.egotype();
  84. // if (_egoType != EgoType::User || type == EgoType::User) return;
  85. // int32_t peer = Req.peer();
  86. // int32_t index = Req.index();
  87. // if (type == EgoType::Car)
  88. // {
  89. // if (peer != _carpeer) return;
  90. //
  91. //
  92. // }
  93. // else if (type == EgoType::Cave)
  94. // {
  95. // if (peer != _cavepeer) return;
  96. //
  97. // }
  98. // CUserManager::GetInstance().ClosePeerVideo(peer, _uid, _egoType, index);
  99. // }
  100. void CUserSocket::OnCadidate(int8_t* Data, int16_t Size)
  101. {
  102. RemoNet::Candidate Req;
  103. Req.ParseFromArray(Data, Size);
  104. int32_t peer = Req.peer();
  105. CUserManager::GetInstance().NotifyCandidate(peer, _uid, Req.index(), Req.type().c_str(), Req.candidate().c_str(), Req.sdpmlineindex(), Req.sdpmid().c_str());
  106. // Req.set_peer(_uid);
  107. // CIOBuffer* pBuffer = CIOBuffer::Alloc();
  108. // MessageHead Head;
  109. // Head.Command = RemoNet::SC_Candidate;
  110. // Head.Length = Req.ByteSizeLong();
  111. // Head.Serialize(pBuffer->Buffer);
  112. // auto ptr = pBuffer->Buffer + MessageHead::Size();
  113. // Req.SerializeToArray(ptr, Head.Length);
  114. // pBuffer->Length = MessageHead::Size() + Head.Length;
  115. // CUserManager::GetInstance().Write(peer, pBuffer);
  116. // pBuffer->Release();
  117. }
  118. // void CUserSocket::OnCancelReq(int8_t* Data, int16_t Size)
  119. // {
  120. // RemoNet::CSCancelReq Req;
  121. // Req.ParseFromArray(Data, Size);
  122. // if (_peer != Req.peer()) return;
  123. // CUserManager::GetInstance().CancelReq(_peer, _uid);
  124. // _peer = -1;
  125. //
  126. //
  127. // }
  128. void CUserSocket::OnClose()
  129. {
  130. if (_egoType == EgoType::Car)
  131. {
  132. RemoNet::SCDelRobot rot;
  133. rot.set_peer(_uid);
  134. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  135. MessageHead Head;
  136. Head.Command = RemoNet::SC_NotifyDel;
  137. Head.Length = rot.ByteSizeLong();
  138. Head.Serialize(pBuffer->Buffer);
  139. auto ptr = pBuffer->Buffer + MessageHead::Size();
  140. rot.SerializeToArray(ptr, Head.Length);
  141. pBuffer->Length = MessageHead::Size() + Head.Length;
  142. CUserManager::GetInstance().BroadCast(pBuffer);
  143. pBuffer->Release(__FILE__, __LINE__);
  144. }
  145. if (_userpeer != -1)
  146. CUserManager::GetInstance().LeavePeerVideo(_userpeer, _uid, _egoType);
  147. printf("a socket closed %d", _uid);
  148. CUserManager::GetInstance().Remove(this);
  149. Alloc_Pool<CUserSocket>::GetInstance().Free(this);
  150. }
  151. void CUserSocket::OnCreate()
  152. {
  153. Read(0);
  154. }
  155. void CUserSocket::OnKeepAlive(int8_t* Data, int16_t Size)
  156. {
  157. TimeTick = GetTickCount64();
  158. }
  159. void CUserSocket::OnOffer(int8_t* Data, int16_t Size)
  160. {
  161. std::cout << __FUNCTION__ << std::endl;
  162. RemoNet::Offer Req;
  163. Req.ParseFromArray(Data, Size);
  164. int32_t peer = Req.peer();
  165. CUserManager::GetInstance().NotifyOffer(peer,_uid,Req.index(),Req.type().c_str(),Req.sdp().c_str());
  166. // Req.set_peer(_uid);
  167. // CIOBuffer* pBuffer = CIOBuffer::Alloc();
  168. // MessageHead Head;
  169. // Head.Command = RemoNet::SC_Offer;
  170. // Head.Length = Req.ByteSizeLong();
  171. // Head.Serialize(pBuffer->Buffer);
  172. // auto ptr = pBuffer->Buffer + MessageHead::Size();
  173. // Req.SerializeToArray(ptr, Head.Length);
  174. // pBuffer->Length = MessageHead::Size() + Head.Length;
  175. // CUserManager::GetInstance().Write(peer, pBuffer);
  176. // pBuffer->Release();
  177. }
  178. void CUserSocket::OnRead()
  179. {
  180. int16_t length = m_pBuffer->Length;
  181. int8_t* ptr = m_pBuffer->Buffer;
  182. while (true)
  183. {
  184. if (length < MessageHead::Size())
  185. {
  186. Read(length);
  187. return;
  188. }
  189. MessageHead Head;
  190. Head.Deserialize(ptr);// = reinterpret_cast<MessageHead *>(ptr);
  191. int16_t size = Head.Length + MessageHead::Size();
  192. if (length < size)
  193. {
  194. Read(length);
  195. return;
  196. }
  197. int8_t* Data = ptr + MessageHead::Size();
  198. Process(Head.Command, Data, Head.Length);
  199. length -= size;
  200. ptr += size;
  201. }
  202. }
  203. void CUserSocket::OnRepVideo(int8_t* Data, int16_t Size)
  204. {
  205. std::cout << __FUNCTION__ << std::endl;
  206. RemoNet::CSRep Req;
  207. Req.ParseFromArray(Data, Size);
  208. int32_t peer = Req.peer();
  209. if (Req.desc() == RemoNet::VideoDesc::OK)
  210. _userpeer = peer;
  211. CUserManager::GetInstance().ReplyPeerVideo(peer, _uid, Req.desc(), Req.index());
  212. }
  213. void CUserSocket::OnReqVideo(int8_t* Data, int16_t Size)
  214. {
  215. std::cout << __FUNCTION__ << std::endl;
  216. RemoNet::CSReq Req;
  217. Req.ParseFromArray(Data, Size);
  218. int32_t peer = Req.peer();
  219. int32_t index = Req.index();
  220. int32_t type = Req.egotype();
  221. //bool master=Req.master();
  222. if (type==EgoType::Car&&index==RenderPosition::FRONT)
  223. {
  224. if (_userpeer == _uid || peer == _userpeer) return;
  225. _userpeer = peer;
  226. }
  227. auto state = CUserManager::GetInstance().ConnectPeerVideo(peer, _uid,index);
  228. RemoNet::SCReq Rep;
  229. Rep.set_peer(peer);
  230. Rep.set_desc(state);
  231. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  232. MessageHead Head;
  233. Head.Command = RemoNet::SC_Req;
  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. pBuffer->Release(__FILE__, __LINE__);
  241. }
  242. void CUserSocket::OnSigin(int8_t* Data, int16_t Size)
  243. {
  244. RemoNet::CSSign Req;
  245. Req.ParseFromArray(Data, Size);
  246. std::string account = Req.account();
  247. std::string password = Req.password();
  248. CQPtr<CConnectionPtr<sql::Connection>> Conn = CDBConnectPool::GetInstance().QueryConnect();
  249. scoped_ptr<sql::Statement> stmt = (*Conn.get())->createStatement();
  250. char sql[1024];
  251. sprintf_s(sql, "select id,name from user where account=\'%s\' and password=MD5(\'%s\')", account.c_str(), password.c_str());
  252. scoped_ptr<sql::ResultSet> resultSet = stmt->executeQuery(sql);
  253. bool bRet = false;
  254. if (resultSet->next())
  255. {
  256. int32_t uid = resultSet->getInt(1);
  257. CUserManager::GetInstance().Check(uid);
  258. _uid = uid;
  259. _name = resultSet->getString(2).c_str();
  260. _state = RemoNet::UserState::Idle;
  261. _loginsucc = true;
  262. _egoType = EgoType::User;
  263. bRet = true;
  264. }
  265. RemoNet::SCSign Rep;
  266. Rep.set_ret(bRet);
  267. Rep.set_uid(_uid);
  268. Rep.set_name(_name);
  269. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  270. MessageHead Head;
  271. Head.Command = RemoNet::SC_Sign;
  272. Head.Length = Rep.ByteSizeLong();
  273. Head.Serialize(pBuffer->Buffer);
  274. auto ptr = pBuffer->Buffer + MessageHead::Size();
  275. Rep.SerializeToArray(ptr, Head.Length);
  276. pBuffer->Length = MessageHead::Size() + Head.Length;
  277. Write(pBuffer);
  278. pBuffer->Release(__FILE__, __LINE__);
  279. }
  280. void CUserSocket::OnAdd(int8_t* Data, int16_t Size)
  281. {
  282. RemoNet::CSAdd Req;
  283. Req.ParseFromArray(Data, Size);
  284. std::string serial = Req.serial();
  285. _name = Req.name();
  286. CQPtr<CConnectionPtr<sql::Connection>> Conn = CDBConnectPool::GetInstance().QueryConnect();
  287. scoped_ptr<sql::Statement> stmt = (*Conn.get())->createStatement();
  288. char sql[1024];
  289. sprintf_s(sql, "select id from robot where serial=\'%s\'", serial.c_str());
  290. scoped_ptr<sql::ResultSet> resultSet = stmt->executeQuery(sql);
  291. bool bRet = false;
  292. if (resultSet->next())
  293. {
  294. _uid = resultSet->getInt(1);
  295. _loginsucc = true;
  296. _egoType =static_cast<EgoType>(Req.type());
  297. _car = static_cast<CarType>(Req.car());
  298. _state = RemoNet::UserState::Idle;
  299. bRet = true;
  300. }
  301. if (bRet)
  302. {
  303. //_egoType = static_cast<EgoType>(Req.type());
  304. // int32_t size = Req.info().size();
  305. // for (int32_t i = 0; i < size; i++)
  306. // {
  307. // auto& node = Req.info(i);
  308. // CameraInfo info;
  309. // info.index = node.pos();
  310. // info.label = node.label();
  311. //
  312. // info.solution = static_cast<DisplayResolution>(node.solution());
  313. // _cameraArray.push_back(info);
  314. // }
  315. RemoNet::SCAddRobot robot;
  316. auto r=robot.mutable_robot();
  317. r->set_name(_name);
  318. r->set_rid(_uid);
  319. r->set_type(_egoType);
  320. r->set_state(RemoNet::UserState::Idle);
  321. r->set_cartype(_car);
  322. // for (auto& node : _cameraArray)
  323. // {
  324. // auto info = r->add_info();
  325. // info->set_pos(node.index);
  326. // info->set_label(node.label);
  327. // info->set_solution(node.solution);
  328. // }
  329. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  330. MessageHead Head;
  331. Head.Command = RemoNet::SC_NotifyAdd;
  332. Head.Length = robot.ByteSizeLong();
  333. Head.Serialize(pBuffer->Buffer);
  334. auto ptr = pBuffer->Buffer + MessageHead::Size();
  335. robot.SerializeToArray(ptr, Head.Length);
  336. pBuffer->Length = MessageHead::Size() + Head.Length;
  337. CUserManager::GetInstance().BroadCast(pBuffer);
  338. pBuffer->Release(__FILE__, __LINE__);
  339. }
  340. RemoNet::SCAdd Rep;
  341. Rep.set_ret(bRet);
  342. Rep.set_uid(_uid);
  343. Rep.set_name(_name);
  344. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  345. MessageHead Head;
  346. Head.Command = RemoNet::SC_Add;
  347. Head.Length = Rep.ByteSizeLong();
  348. Head.Serialize(pBuffer->Buffer);
  349. auto ptr = pBuffer->Buffer + MessageHead::Size();
  350. Rep.SerializeToArray(ptr, Head.Length);
  351. pBuffer->Length = MessageHead::Size() + Head.Length;
  352. Write(pBuffer);
  353. pBuffer->Release(__FILE__, __LINE__);
  354. }
  355. void CUserSocket::Process(int32_t cmd, int8_t* Data, int32_t Size)
  356. {
  357. if (!_loginsucc)
  358. {
  359. if ((cmd != RemoNet::CS_Sign && cmd != RemoNet::CS_Add)) return;
  360. }
  361. Redirector.Process(this, cmd, Data, Size);
  362. }
  363. RemoNet::VideoDesc CUserSocket::ReqVideo(int32_t peer, int32_t index)
  364. {
  365. if (_egoType == EgoType::User) return RemoNet::VideoDesc::NoFound;
  366. //if(index!=RenderPosition::FRONT_BACK)
  367. if (_userpeer!=-1&&_userpeer != peer) return RemoNet::VideoDesc::Busy;
  368. RemoNet::CSReq Rep;
  369. Rep.set_peer(peer);
  370. Rep.set_index(index);
  371. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  372. MessageHead Head;
  373. Head.Command = RemoNet::SC_NotifyReq;
  374. Head.Length = Rep.ByteSizeLong();
  375. Head.Serialize(pBuffer->Buffer);
  376. auto ptr = pBuffer->Buffer + MessageHead::Size();
  377. Rep.SerializeToArray(ptr, Head.Length);
  378. pBuffer->Length = MessageHead::Size() + Head.Length;
  379. Write(pBuffer);
  380. pBuffer->Release(__FILE__, __LINE__);
  381. return RemoNet::VideoDesc::OK;
  382. }
  383. void CUserSocket::RepVideo(int32_t peer, int32_t index,RemoNet::VideoDesc desc)
  384. {
  385. if (desc != RemoNet::VideoDesc::OK)
  386. {
  387. _userpeer = -1;
  388. }
  389. RemoNet::CSRep Rep;
  390. Rep.set_desc(desc);
  391. Rep.set_peer(peer);
  392. Rep.set_index(index);
  393. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  394. MessageHead Head;
  395. Head.Command = RemoNet::SC_NotifyRep;
  396. Head.Length = Rep.ByteSizeLong();
  397. Head.Serialize(pBuffer->Buffer);
  398. auto ptr = pBuffer->Buffer + MessageHead::Size();
  399. Rep.SerializeToArray(ptr, Head.Length);
  400. pBuffer->Length = MessageHead::Size() + Head.Length;
  401. Write(pBuffer);
  402. pBuffer->Release(__FILE__, __LINE__);
  403. }
  404. int32_t CUserSocket::uid()
  405. {
  406. return _uid;
  407. }
  408. std::string CUserSocket::name()
  409. {
  410. return _name;
  411. }
  412. CarType CUserSocket::car()
  413. {
  414. return _car;
  415. }
  416. int32_t CUserSocket::type()
  417. {
  418. return _egoType;
  419. }
  420. void CUserSocket::CloseVideo(int32_t peer, int32_t index, EgoType type)
  421. {
  422. if (type == EgoType::User)
  423. {
  424. if (_userpeer != peer) return;
  425. }
  426. RemoNet::Close Rep;
  427. Rep.set_egotype(type);
  428. Rep.set_index(index);
  429. Rep.set_peer(peer);
  430. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  431. MessageHead Head;
  432. Head.Command = RemoNet::SC_NotifyCloseVideo;
  433. Head.Length = Rep.ByteSizeLong();
  434. Head.Serialize(pBuffer->Buffer);
  435. auto ptr = pBuffer->Buffer + MessageHead::Size();
  436. Rep.SerializeToArray(ptr, Head.Length);
  437. pBuffer->Length = MessageHead::Size() + Head.Length;
  438. Write(pBuffer);
  439. pBuffer->Release(__FILE__, __LINE__);
  440. }
  441. void CUserSocket::LeaveVideo(int32_t peer,EgoType type)
  442. {
  443. if (_userpeer != peer) return;
  444. _userpeer = -1;
  445. RemoNet::Leave Rep;
  446. Rep.set_egotype(type);
  447. Rep.set_peer(peer);
  448. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  449. MessageHead Head;
  450. Head.Command = RemoNet::SC_NotifyLeave;
  451. Head.Length = Rep.ByteSizeLong();
  452. Head.Serialize(pBuffer->Buffer);
  453. auto ptr = pBuffer->Buffer + MessageHead::Size();
  454. Rep.SerializeToArray(ptr, Head.Length);
  455. pBuffer->Length = MessageHead::Size() + Head.Length;
  456. Write(pBuffer);
  457. pBuffer->Release(__FILE__, __LINE__);
  458. }
  459. void CUserSocket::KickOff()
  460. {
  461. RemoNet::SCKickOff Rep;
  462. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  463. MessageHead Head;
  464. Head.Command = RemoNet::SC_KickOff;
  465. Head.Length = Rep.ByteSizeLong();
  466. Head.Serialize(pBuffer->Buffer);
  467. auto ptr = pBuffer->Buffer + MessageHead::Size();
  468. Rep.SerializeToArray(ptr, Head.Length);
  469. pBuffer->Length = MessageHead::Size() + Head.Length;
  470. Write(pBuffer);
  471. pBuffer->Release(__FILE__, __LINE__);
  472. _loginsucc = false;
  473. _uid = -1;
  474. }
  475. void CUserSocket::OnMoveBegin(int8_t* Data, int16_t Size)
  476. {
  477. RemoNet::CSMoveBegin Req;
  478. Req.ParseFromArray(Data, Size);
  479. auto area = Req.area();
  480. auto no = Req.no();
  481. auto peer = Req.peer();
  482. CUserManager::GetInstance().NotifyMoveBegin(peer, _uid,(WorkArea) area, no);
  483. }
  484. void CUserSocket::OnSwitchDriver(int8_t* Data, int16_t Size)
  485. {
  486. RemoNet::SwitchDriver Req;
  487. Req.ParseFromArray(Data, Size);
  488. auto peer = Req.peer();
  489. CUserManager::GetInstance().NotifySwitchDriver(peer, _uid);
  490. }
  491. void CUserSocket::OnMoveRet(int8_t* Data, int16_t Size)
  492. {
  493. RemoNet::MoveRet Req;
  494. Req.ParseFromArray(Data, Size);
  495. auto peer = Req.peer();
  496. CUserManager::GetInstance().NotifyMoveRet(peer, _uid, Req.desc());
  497. }
  498. void CUserSocket::OnMoveEnd(int8_t* Data, int16_t Size)
  499. {
  500. RemoNet::CSMoveEnd Req;
  501. Req.ParseFromArray(Data, Size);
  502. auto area = Req.area();
  503. auto no = Req.no();
  504. auto peer = Req.uid();
  505. CUserManager::GetInstance().NotifyMoveEnd(peer,_uid, (WorkArea)area, Req.no());
  506. }
  507. void CUserSocket::OnChangeState(int8_t* Data, int16_t Size)
  508. {
  509. RemoNet::CSState Req;
  510. Req.ParseFromArray(Data, Size);
  511. _state = Req.state();
  512. CUserManager::GetInstance().NotifyState(_uid, _egoType,_state);
  513. }
  514. void CUserSocket::OnRobot(int8_t* Data, int16_t Size)
  515. {
  516. RemoNet::CSRobot Req;
  517. Req.ParseFromArray(Data, Size);
  518. std::vector<Robot> ret;
  519. CUserManager::GetInstance().GetRobot(ret);
  520. RemoNet::SCRobot Rep;
  521. for (auto& node : ret)
  522. {
  523. auto robot = Rep.add_robot();
  524. robot->set_name(node.name.c_str());
  525. robot->set_rid(node.uid);
  526. robot->set_type(node.type);
  527. robot->set_state(node.state);
  528. robot->set_cartype(node.car);
  529. }
  530. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  531. MessageHead Head;
  532. Head.Command = RemoNet::SC_Robot;
  533. Head.Length = Rep.ByteSizeLong();
  534. Head.Serialize(pBuffer->Buffer);
  535. auto ptr = pBuffer->Buffer + MessageHead::Size();
  536. Rep.SerializeToArray(ptr, Head.Length);
  537. pBuffer->Length = MessageHead::Size() + Head.Length;
  538. Write(pBuffer);
  539. pBuffer->Release(__FILE__, __LINE__);
  540. }
  541. void CUserSocket::NotifyOffer(int32_t peer, int32_t index,const char * type,const char * sdp)
  542. {
  543. // std::cout << sdp << std::endl;
  544. RemoNet::Offer Rep;
  545. Rep.set_peer(peer);
  546. Rep.set_index(index);
  547. Rep.set_type(type);
  548. Rep.set_sdp(sdp);
  549. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  550. MessageHead Head;
  551. Head.Command = RemoNet::SC_NotifyOffer;
  552. Head.Length = Rep.ByteSizeLong();
  553. Head.Serialize(pBuffer->Buffer);
  554. auto ptr = pBuffer->Buffer + MessageHead::Size();
  555. Rep.SerializeToArray(ptr, Head.Length);
  556. pBuffer->Length = MessageHead::Size() + Head.Length;
  557. Write(pBuffer);
  558. pBuffer->Release(__FILE__, __LINE__);
  559. }
  560. void CUserSocket::NotifyMoveBegin(int32_t peer, WorkArea area, int32_t no)
  561. {
  562. RemoNet::SCMoveBegin Rep;
  563. Rep.set_uid(peer);
  564. Rep.set_area(area);
  565. Rep.set_no(no);
  566. //Point pt = CWorkAreaManager::GetInstance().GetPoint(area, no);
  567. //Rep.set_x(pt.x);
  568. //Rep.set_y(pt.y);
  569. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  570. MessageHead Head;
  571. Head.Command = RemoNet::SC_MoveBegin;
  572. Head.Length = Rep.ByteSizeLong();
  573. Head.Serialize(pBuffer->Buffer);
  574. auto ptr = pBuffer->Buffer + MessageHead::Size();
  575. Rep.SerializeToArray(ptr, Head.Length);
  576. pBuffer->Length = MessageHead::Size() + Head.Length;
  577. Write(pBuffer);
  578. pBuffer->Release(__FILE__, __LINE__);
  579. }
  580. void CUserSocket::NotifyMoveEnd(int32_t peer, WorkArea area, int32_t no)
  581. {
  582. RemoNet::SCMoveEnd Rep;
  583. Rep.set_uid(peer);
  584. Rep.set_area(area);
  585. Rep.set_no(no);
  586. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  587. MessageHead Head;
  588. Head.Command = RemoNet::SC_MoveEnd;
  589. Head.Length = Rep.ByteSizeLong();
  590. Head.Serialize(pBuffer->Buffer);
  591. auto ptr = pBuffer->Buffer + MessageHead::Size();
  592. Rep.SerializeToArray(ptr, Head.Length);
  593. pBuffer->Length = MessageHead::Size() + Head.Length;
  594. Write(pBuffer);
  595. pBuffer->Release(__FILE__, __LINE__);
  596. _state = RemoNet::UserState::Remote;
  597. }
  598. void CUserSocket::NotifyAnswer(int32_t peer, int32_t index, const char* type, const char* sdp)
  599. {
  600. //std::cout << sdp << std::endl;
  601. RemoNet::Answer Rep;
  602. Rep.set_peer(peer);
  603. Rep.set_index(index);
  604. Rep.set_type(type);
  605. Rep.set_sdp(sdp);
  606. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  607. MessageHead Head;
  608. Head.Command = RemoNet::SC_NotifyAnswer;
  609. Head.Length = Rep.ByteSizeLong();
  610. Head.Serialize(pBuffer->Buffer);
  611. auto ptr = pBuffer->Buffer + MessageHead::Size();
  612. Rep.SerializeToArray(ptr, Head.Length);
  613. pBuffer->Length = MessageHead::Size() + Head.Length;
  614. Write(pBuffer);
  615. pBuffer->Release(__FILE__, __LINE__);
  616. }
  617. void CUserSocket::NotifyCandidate(int32_t peer, int32_t index, const char* type, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  618. {
  619. RemoNet::Candidate Rep;
  620. Rep.set_peer(peer);
  621. Rep.set_index(index);
  622. Rep.set_type(type);
  623. Rep.set_sdpmid(sdp_mid);
  624. Rep.set_candidate(candidate);
  625. Rep.set_sdpmlineindex(sdp_mline_index);
  626. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  627. MessageHead Head;
  628. Head.Command = RemoNet::SC_NotifyCandidate;
  629. Head.Length = Rep.ByteSizeLong();
  630. Head.Serialize(pBuffer->Buffer);
  631. auto ptr = pBuffer->Buffer + MessageHead::Size();
  632. Rep.SerializeToArray(ptr, Head.Length);
  633. pBuffer->Length = MessageHead::Size() + Head.Length;
  634. Write(pBuffer);
  635. pBuffer->Release(__FILE__, __LINE__);
  636. }
  637. void CUserSocket::OnTimeout()
  638. {
  639. PostQueuedCompletionStatus(m_hComplete, -1, (DWORD_PTR)this, nullptr);
  640. }
  641. ULONGLONG CUserSocket::GetTimeTick()
  642. {
  643. return TimeTick;
  644. }
  645. RemoNet::UserState CUserSocket::state()
  646. {
  647. return _state;
  648. }