#define WIN32_LEAN_AND_MEAN #include #include #include #include "Protocol.pb.h" #include #include "out_sim.h" #include "../common/iobuffer.h" #include #include "EgoClient.h" COutSim::COutSim(CEgoClient* c, const char* ip, int32_t port) :_head(nullptr),_ip(ip),_port(port),_client(c) { } void COutSim::Start() { WSAData data; WSAStartup(MAKEWORD(2, 2), &data); _socket = socket(AF_INET, SOCK_DGRAM, 0); sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(_port); inet_pton(AF_INET, _ip.c_str(), &sin.sin_addr); connect(_socket, (sockaddr*)&sin, sizeof(sin)); _thread = std::thread(std::bind(&COutSim::Run, this)); } void COutSim::Stop() { closesocket(_socket); if (_run == true) { _run = false; Mtx.lock(); cv.notify_all(); Mtx.unlock(); _thread.join(); } } void COutSim::PushSimPack(CIOBuffer* pBuffer) { std::unique_lock lck(Mtx); //bool bNull = false; if (_head == nullptr) { _head = pBuffer; } else { _head->Release(__FILE__, __LINE__); _head = pBuffer; } } void COutSim::Run() { _run = true; while (_run) { if (!_run) break; if (_head != nullptr) SendOutSim(_head); Sleep(30); } } bool COutSim::SendOutSim(CIOBuffer* pBuffer) { int32_t ret = send(_socket, (const char *)pBuffer->Buffer, pBuffer->Length, 0); return ret == pBuffer->Length; } void COutSim::OnPeerMessage(int16_t cmd, int16_t length, const void* data) { //创建一个RemoNet::IMuMessage对象rep //调用rep的ParseFromArray方法,通过传入的数据指针data和长度length来解析消息内容,data应该指向一个序列化的IMuMessage对象 RemoNet::IMuMessage rep; rep.ParseFromArray(data, length); //定义一个名为buffer的CIOBuffer对象 CIOBuffer* buffer=CIOBuffer::Alloc(__FILE__, __LINE__); OutSimPack p; //= (OutSimPack*)buffer->Buffer; //p.rx = (rep.rx()*3.1415926) /180.f ; //p.ry = (rep.ry()*3.1415926)/180.f; //将解析出的IMuMessage对象rep中的rx和ry值赋给p的对应成员,将p.rz设为 0.0 p.rx = rep.rx(); p.ry = rep.ry(); p.rz = 0.0; //调用 _client对象的OnImuData方法,将 p.rx 和 p.ry 传递出去 _client->OnImuData(p.rx, p.ry); //_client->OnImuData(rep.rx(), rep.ry()); //使用 memcpy 将 p 的内容复制到 buffer 的 Buffer 成员中,设置 buffer 的 Length 成员为 p 的大小 memcpy(buffer->Buffer, &p, sizeof(p)); buffer->Length = sizeof(p); //std::ostringstream str; //str << p.rx <<","<< p.ry << std::endl; // OutputDebugStringA(str.str().c_str()); buffer->Length = sizeof(OutSimPack); PushSimPack(buffer); }