tcpConnection.cpp (886B)
1 #include "tcpConnection.h" 2 3 #include <utility> // std::move 4 5 #include "logging.h" 6 #include "tcpStream.h" 7 8 #include "../ptypes/pinet.h" 9 10 #ifndef _WIN32 11 # include <netinet/tcp.h> // TCP_NODELAY 12 # include <netinet/in.h> // IPPROTO_TCP 13 #endif 14 15 namespace networkLib 16 { 17 TcpConnection::TcpConnection(OnConnectedFunc _onConnected) : m_onConnected(std::move(_onConnected)) 18 { 19 } 20 21 void TcpConnection::onConnected(ptypes::ipstream* _stream) const 22 { 23 constexpr int opt = 1; 24 const int res = ::setsockopt(_stream->get_handle(), IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&opt), sizeof(opt)); 25 if (res < 0) 26 { 27 const int err = errno; 28 LOGNET(LogLevel::Error, static_cast<const char*>(ptypes::iptostring(_stream->get_myip())) << ": Failed to set socket option TCP_NODELAY, err " << err << ": " << strerror(err)); 29 } 30 31 m_onConnected(std::make_unique<TcpStream>(_stream)); 32 } 33 }