tcpClient.cpp (734B)
1 #include "tcpClient.h" 2 3 #include <utility> 4 5 #include "logging.h" 6 #include "../ptypes/pinet.h" 7 8 namespace networkLib 9 { 10 TcpClient::TcpClient(std::string _host, const uint32_t _port, OnConnectedFunc _onConnected) 11 : TcpConnection(std::move(_onConnected)) 12 , m_host(std::move(_host)) 13 , m_port(_port) 14 { 15 start(); 16 } 17 18 void TcpClient::threadFunc() 19 { 20 auto* stream = new ptypes::ipstream(m_host.c_str(), static_cast<int>(m_port)); 21 22 while(!exit()) 23 { 24 try 25 { 26 stream->open(); 27 28 if (stream->get_active()) 29 { 30 onConnected(stream); 31 break; 32 } 33 } 34 catch (ptypes::exception* e) 35 { 36 LOGNET(LogLevel::Warning, "Network Error: " << static_cast<const char*>(e->get_message())); 37 delete e; 38 } 39 } 40 } 41 }