udpClient.cpp (1569B)
1 #include "udpClient.h" 2 3 #include <utility> 4 5 #include "bridgeLib/commandWriter.h" 6 #include "bridgeLib/error.h" 7 #include "bridgeLib/types.h" 8 9 namespace bridgeClient 10 { 11 UdpClient::UdpClient(const bridgeLib::PluginDesc& _desc, ServerFoundCallback&& _callback) 12 : networkLib::UdpClient(bridgeLib::g_udpServerPort) 13 , m_callback(std::move(_callback)) 14 { 15 bridgeLib::PluginDesc desc = _desc; 16 17 desc.protocolVersion = bridgeLib::g_protocolVersion; 18 19 bridgeLib::CommandWriter w; 20 desc.write(w.build(bridgeLib::Command::PluginInfo)); 21 22 baseLib::BinaryStream bs; 23 w.write(bs); 24 bs.toVector(m_requestPacket); 25 26 start(); 27 } 28 29 UdpClient::~UdpClient() 30 { 31 stop(); 32 } 33 34 bool UdpClient::validateResponse(const std::string& _host, const std::vector<uint8_t>& _message) 35 { 36 bool ok = false; 37 38 bridgeLib::CommandReader reader([&](const bridgeLib::Command _command, baseLib::BinaryStream& _binaryStream) 39 { 40 if(_command == bridgeLib::Command::ServerInfo) 41 { 42 bridgeLib::ServerInfo si; 43 si.read(_binaryStream); 44 45 if(si.protocolVersion == bridgeLib::g_protocolVersion && si.portTcp > 0) 46 { 47 ok = true; 48 m_callback(_host, si, {}); 49 } 50 else 51 { 52 bridgeLib::Error e; 53 e.code = bridgeLib::ErrorCode::WrongProtocolVersion; 54 e.msg = "Wrong protocol version"; 55 m_callback(_host, si, e); 56 } 57 } 58 else if(_command == bridgeLib::Command::Error) 59 { 60 bridgeLib::Error e; 61 e.read(_binaryStream); 62 m_callback(_host, {}, e); 63 } 64 }); 65 66 baseLib::BinaryStream bs(_message); 67 reader.read(bs); 68 return false; // continue 69 } 70 }