bool SocketDescriptor::Connect(SocketAddress address) { assert(address.IsDefined()); return ::connect(Get(), address.GetAddress(), address.GetSize()) >= 0; }
int UDPSocket::ReceiveFrom( void* inToReceive, int inMaxLength, SocketAddress& outFromAddress ) { socklen_t fromLength = outFromAddress.GetSize(); int readByteCount = recvfrom( mSocket, static_cast< char* >( inToReceive ), inMaxLength, 0, &outFromAddress.mSockAddr, &fromLength ); if( readByteCount >= 0 ) { return readByteCount; } else { int error = SocketUtil::GetLastError(); if( error == WSAEWOULDBLOCK ) { return 0; } else if( error == WSAECONNRESET ) { //this can happen if a client closed and we haven't DC'd yet. //this is the ICMP message being sent back saying the port on that computer is closed LOG( "Connection reset from %s", outFromAddress.ToString().c_str() ); return -WSAECONNRESET; } else { SocketUtil::ReportError( "UDPSocket::ReceiveFrom" ); return -error; } } }
int UDPSocket::Bind( const SocketAddress& inBindAddress ) { int error = bind( mSocket, &inBindAddress.mSockAddr, inBindAddress.GetSize() ); if( error != 0 ) { SocketUtil::ReportError( "UDPSocket::Bind" ); return SocketUtil::GetLastError(); } return NO_ERROR; }
int UDPSocket::Bind(const SocketAddress& address) { int error = bind(m_socket, &address.m_sockAddr, (int)address.GetSize()); if(error != 0) { #ifdef _DEBUG ReportError("UDPSocket::Bind"); #endif return LastError(); } return NO_ERROR; }
int UDPSocket::ReceiveFrom(void* buffer, int len, SocketAddress& from) { socklen_t fromLen = (socklen_t)from.GetSize(); int byteCount = recvfrom(m_socket, static_cast<char*>(buffer), len, 0, &from.m_sockAddr, &fromLen); if(byteCount < 0) { #ifdef _DEBUG ReportError("UDPSocket::ReceiveFrom"); #endif return -LastError(); } return byteCount; }
ssize_t SocketDescriptor::Write(const void *buffer, size_t length, SocketAddress address) { int flags = 0; #ifdef HAVE_POSIX flags |= MSG_DONTWAIT; #endif #ifdef __linux__ flags |= MSG_NOSIGNAL; #endif return ::sendto(Get(), (const char *)buffer, length, flags, address.GetAddress(), address.GetSize()); }
int UDPSocket::SendTo( const void* inToSend, int inLength, const SocketAddress& inToAddress ) { int byteSentCount = sendto( mSocket, static_cast< const char* >( inToSend ), inLength, 0, &inToAddress.mSockAddr, inToAddress.GetSize() ); if( byteSentCount <= 0 ) { //we'll return error as negative number to indicate less than requested amount of bytes sent... SocketUtil::ReportError( "UDPSocket::SendTo" ); return -SocketUtil::GetLastError(); } else { return byteSentCount; } }
int UDPSocket::SendTo(const void* data, int len, const SocketAddress& to) { int byteCount = sendto(m_socket, static_cast<const char*>(data), len, 0, &to.m_sockAddr, (int)to.GetSize()); if(byteCount < 0) { #ifdef _DEBUG ReportError("UDPSocket::SendTo"); #endif return -LastError(); } return byteCount; }
bool SocketDescriptor::Bind(SocketAddress address) { return bind(Get(), address.GetAddress(), address.GetSize()) == 0; }