out_sim.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <WS2tcpip.h>
  4. #include <WinSock2.h>
  5. #include "Protocol.pb.h"
  6. #include <sstream>
  7. #include "out_sim.h"
  8. #include "../common/iobuffer.h"
  9. #include <random>
  10. #include "EgoClient.h"
  11. COutSim::COutSim(CEgoClient* c, const char* ip, int32_t port) :_head(nullptr),_ip(ip),_port(port),_client(c)
  12. {
  13. }
  14. void COutSim::Start()
  15. {
  16. WSAData data;
  17. WSAStartup(MAKEWORD(2, 2), &data);
  18. _socket = socket(AF_INET, SOCK_DGRAM, 0);
  19. sockaddr_in sin;
  20. sin.sin_family = AF_INET;
  21. sin.sin_port = htons(_port);
  22. inet_pton(AF_INET, _ip.c_str(), &sin.sin_addr);
  23. connect(_socket, (sockaddr*)&sin, sizeof(sin));
  24. _thread = std::thread(std::bind(&COutSim::Run, this));
  25. }
  26. void COutSim::Stop()
  27. {
  28. closesocket(_socket);
  29. if (_run == true)
  30. {
  31. _run = false;
  32. Mtx.lock();
  33. cv.notify_all();
  34. Mtx.unlock();
  35. _thread.join();
  36. }
  37. }
  38. void COutSim::PushSimPack(CIOBuffer* pBuffer)
  39. {
  40. std::unique_lock <std::mutex> lck(Mtx);
  41. //bool bNull = false;
  42. if (_head == nullptr)
  43. {
  44. _head = pBuffer;
  45. }
  46. else {
  47. _head->Release(__FILE__, __LINE__);
  48. _head = pBuffer;
  49. }
  50. }
  51. void COutSim::Run()
  52. {
  53. _run = true;
  54. while (_run)
  55. {
  56. if (!_run) break;
  57. if (_head != nullptr)
  58. SendOutSim(_head);
  59. Sleep(30);
  60. }
  61. }
  62. bool COutSim::SendOutSim(CIOBuffer* pBuffer)
  63. {
  64. int32_t ret = send(_socket, (const char *)pBuffer->Buffer, pBuffer->Length, 0);
  65. return ret == pBuffer->Length;
  66. }
  67. void COutSim::OnPeerMessage(int16_t cmd, int16_t length, const void* data)
  68. {
  69. //创建一个RemoNet::IMuMessage对象rep
  70. //调用rep的ParseFromArray方法,通过传入的数据指针data和长度length来解析消息内容,data应该指向一个序列化的IMuMessage对象
  71. RemoNet::IMuMessage rep;
  72. rep.ParseFromArray(data, length);
  73. //定义一个名为buffer的CIOBuffer对象
  74. CIOBuffer* buffer=CIOBuffer::Alloc(__FILE__, __LINE__);
  75. OutSimPack p;
  76. //= (OutSimPack*)buffer->Buffer;
  77. //p.rx = (rep.rx()*3.1415926) /180.f ;
  78. //p.ry = (rep.ry()*3.1415926)/180.f;
  79. //将解析出的IMuMessage对象rep中的rx和ry值赋给p的对应成员,将p.rz设为 0.0
  80. p.rx = rep.rx();
  81. p.ry = rep.ry();
  82. p.rz = 0.0;
  83. //调用 _client对象的OnImuData方法,将 p.rx 和 p.ry 传递出去
  84. _client->OnImuData(p.rx, p.ry);
  85. //_client->OnImuData(rep.rx(), rep.ry());
  86. //使用 memcpy 将 p 的内容复制到 buffer 的 Buffer 成员中,设置 buffer 的 Length 成员为 p 的大小
  87. memcpy(buffer->Buffer, &p, sizeof(p));
  88. buffer->Length = sizeof(p);
  89. //std::ostringstream str;
  90. //str << p.rx <<","<< p.ry << std::endl;
  91. // OutputDebugStringA(str.str().c_str());
  92. buffer->Length = sizeof(OutSimPack);
  93. PushSimPack(buffer);
  94. }