Nebula
Loading...
Searching...
No Matches
Posix::PosixSocket Class Reference

#include <posixsocket.h>

Detailed Description

A lowlevel socket wrapper class.

This class is primarily a platform abstraction class, not an "end-user-class". Usually a Nebula application doesn't use this class directly but instead uses the higher level classes like TcpServer, TclClient, etc...

(C) 2006 Radon Labs GmbH (C) 2013-2018 Individual contributors, see AUTHORS file

Inherits Core::RefCounted.

Public Types

enum  Protocol { TCP , UDP }
 protocol types More...
 
enum  Result { Error = 0 , Success , WouldBlock , Closed }
 
enum  ErrorCode {
  ErrorNone , ErrorUnknown , ErrorInterrupted , ErrorBrokenPipe ,
  ErrorPermissionDenied , ErrorBadAddress , ErrorInvalidArgument , ErrorTooManyOpenFiles ,
  ErrorWouldBlock , ErrorInProgress , ErrorAlreadyInProgress , ErrorNotASocket ,
  ErrorDestAddrRequired , ErrorMsgTooLong , ErrorInvalidProtocol , ErrorBadProtocolOption ,
  ErrorProtocolNotSupported , ErrorSocketTypeNotSupported , ErrorOperationNotSupported , ErrorProtFamilyNotSupported ,
  ErrorAddrFamilyNotSupported , ErrorAddrInUse , ErrorAddrNotAvailable , ErrorNetDown ,
  ErrorNetUnreachable , ErrorNetReset , ErrorConnectionAborted , ErrorConnectionReset ,
  ErrorNoBufferSpace , ErrorIsConnected , ErrorNotConnected , ErrorIsShutdown ,
  ErrorIsTimedOut , ErrorConnectionRefused , ErrorHostDown , ErrorHostUnreachable ,
  ErrorSystemNotReady , ErrorVersionNotSupported , ErrorNotInitialized , ErrorDisconnecting ,
  ErrorTypeNotFound , ErrorHostNotFound , ErrorTryAgain , ErrorNoRecovery ,
  ErrorNoData
}
 error codes More...
 

Public Member Functions

 PosixSocket ()
 constructor
 
virtual ~PosixSocket ()
 destructor
 
bool Open (Protocol p)
 open the socket
 
void Close ()
 close the socket
 
bool IsOpen () const
 return true if the socket is open
 
ErrorCode GetErrorCode () const
 get the last error code
 
Util::String GetErrorString () const
 get the last error string
 
void SetAddress (const Net::IpAddress &a)
 set internet address of socket
 
const Net::IpAddressGetAddress () const
 get internet address of socket
 
void SetBroadcast (bool b)
 set the broadcast flag (SO_BROADCAST)
 
bool GetBroadcast ()
 get the broadcast flag
 
void SetDontLinger (bool b)
 set the don't linger flag (SO_DONTLINGER)
 
bool GetDontLinger ()
 get the don't linger flag
 
void SetKeepAlive (bool b)
 set the keepalive flag (SO_KEEPALIVE)
 
bool GetKeepAlive ()
 get the keepalive flag
 
void SetReUseAddr (bool b)
 set reuseaddr flag (SO_REUSEADDR)
 
bool GetReUseAddr ()
 get reuseaddr flag
 
void SetNoDelay (bool b)
 set nodelay flag (TCP_NODELAY)
 
bool GetNoDelay ()
 get nodelay flag
 
void SetRecvBufSize (SizeT s)
 set receive buffer size
 
SizeT GetRecvBufSize ()
 get receive buffer size
 
void SetSendBufSize (SizeT s)
 set send buffer size
 
SizeT GetSendBufSize ()
 get send buffer size
 
void SetBlocking (bool b)
 set blocking mode (FIONBIO)
 
bool GetBlocking () const
 get blocking mode
 
SizeT GetMaxMsgSize ()
 get the maximum message size that can be sent atomically
 
bool Bind ()
 bind socket to ip address
 
bool IsBound () const
 return true if the socket is bound to an address
 
bool Listen ()
 listen for incoming connections (for server sockets)
 
bool Accept (Ptr< Net::Socket > &outSocket)
 accept incoming connection, return a new socket (for server sockets)
 
Result Connect ()
 connect to the sockets address (for client sockets)
 
bool IsConnected ()
 test if the socket is currently connected
 
Result Send (const void *buf, SizeT numBytes, SizeT &bytesSent)
 send raw data into the socket
 
bool HasRecvData ()
 return true if recv data is available at the socket
 
Result Recv (void *buf, SizeT bufSize, SizeT &bytesReceived)
 receive raw data from the socket
 
Result SendTo (const void *buf, SizeT numBytes, uint addr, ushort port, SizeT &bytesSent)
 send raw data to address for connectionless sockets
 
Result RecvFrom (void *buf, SizeT bufSize, uint addr, ushort port, SizeT &bytesReceived)
 receive raw data from address for connectionless sockets
 
- Public Member Functions inherited from Core::RefCounted
 RefCounted ()
 constructor
 
int GetRefCount () const
 get the current refcount
 
void AddRef ()
 increment refcount by one
 
void Release ()
 decrement refcount and destroy object if refcount is zero
 
bool IsInstanceOf (const Rtti &rtti) const
 return true if this object is instance of given class
 
bool IsInstanceOf (const Util::String &className) const
 return true if this object is instance of given class by string
 
bool IsInstanceOf (const Util::FourCC &classFourCC) const
 return true if this object is instance of given class by fourcc
 
bool IsA (const Rtti &rtti) const
 return true if this object is instance of given class, or a derived class
 
bool IsA (const Util::String &rttiName) const
 return true if this object is instance of given class, or a derived class, by string
 
bool IsA (const Util::FourCC &rttiFourCC) const
 return true if this object is instance of given class, or a derived class, by fourcc
 
const Util::StringGetClassName () const
 get the class name
 
Util::FourCC GetClassFourCC () const
 get the class FourCC code
 

Private Member Functions

 __DeclareClass (PosixSocket)
 
void OpenWithExistingSocket (int s)
 open with an existing socket (called by Accept())
 
void ClearError ()
 clear the last error code
 
void SetToLastSocketError ()
 set error code to errno;
 
void SetSocketError (int wsaError)
 set socket error code
 
void SetBoolOption (int optName, bool val)
 set a bool socket option
 
bool GetBoolOption (int optName)
 get a bool socket option
 
void SetIntOption (int optName, int val)
 set an integer socket option
 
int GetIntOption (int optName)
 get an integer socket option
 

Static Private Member Functions

static void InitNetwork ()
 static initializer method (called only once)
 
static ErrorCode SocketErrorToErrorCode (int wsaError)
 convert a socket error code to internal error code
 
static ErrorCode HostErrorToErrorCode (int wsaError)
 convert a host (gethostbyname) error code to internal error code
 
static Util::String ErrorAsString (ErrorCode err)
 convert error code to human readable string
 
static Util::String SocketErrorToString (int wsaError)
 convert socket error code directly to string
 
static Util::String HostErrorToString (int wsaError)
 convert host error code directly to string
 

Private Attributes

ErrorCode error
 
SOCKET sock
 
Net::IpAddress addr
 
bool isBlocking
 
bool isBound
 

Static Private Attributes

static bool NetworkInitialized = false
 

Friends

class PosixIpAddress
 
class SysFunc
 

Additional Inherited Members

- Static Public Member Functions inherited from Core::RefCounted
static void DumpRefCountingLeaks ()
 dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!)
 
- Protected Member Functions inherited from Core::RefCounted
virtual ~RefCounted ()
 destructor (called when refcount reaches zero)
 

Member Enumeration Documentation

◆ ErrorCode

error codes

Enumerator
ErrorNone 
ErrorUnknown 
ErrorInterrupted 
ErrorBrokenPipe 
ErrorPermissionDenied 
ErrorBadAddress 
ErrorInvalidArgument 
ErrorTooManyOpenFiles 
ErrorWouldBlock 
ErrorInProgress 
ErrorAlreadyInProgress 
ErrorNotASocket 
ErrorDestAddrRequired 
ErrorMsgTooLong 
ErrorInvalidProtocol 
ErrorBadProtocolOption 
ErrorProtocolNotSupported 
ErrorSocketTypeNotSupported 
ErrorOperationNotSupported 
ErrorProtFamilyNotSupported 
ErrorAddrFamilyNotSupported 
ErrorAddrInUse 
ErrorAddrNotAvailable 
ErrorNetDown 
ErrorNetUnreachable 
ErrorNetReset 
ErrorConnectionAborted 
ErrorConnectionReset 
ErrorNoBufferSpace 
ErrorIsConnected 
ErrorNotConnected 
ErrorIsShutdown 
ErrorIsTimedOut 
ErrorConnectionRefused 
ErrorHostDown 
ErrorHostUnreachable 
ErrorSystemNotReady 
ErrorVersionNotSupported 
ErrorNotInitialized 
ErrorDisconnecting 
ErrorTypeNotFound 
ErrorHostNotFound 
ErrorTryAgain 
ErrorNoRecovery 
ErrorNoData 

◆ Protocol

protocol types

Enumerator
TCP 
UDP 

◆ Result

Enumerator
Error 
Success 
WouldBlock 
Closed 

Constructor & Destructor Documentation

◆ PosixSocket()

Posix::PosixSocket::PosixSocket ( )

constructor

◆ ~PosixSocket()

Posix::PosixSocket::~PosixSocket ( )
virtual

destructor

Member Function Documentation

◆ __DeclareClass()

Posix::PosixSocket::__DeclareClass ( PosixSocket )
private

◆ Accept()

bool Posix::PosixSocket::Accept ( Ptr< Net::Socket > & outSocket)

accept incoming connection, return a new socket (for server sockets)

Accept an incoming connection to a server socket.

This will spawn a new socket for the connection which will be returned in the provided pointer reference. The address of the returned socket will be set to the address of the "connecting entity".

◆ Bind()

bool Posix::PosixSocket::Bind ( )

bind socket to ip address

Bind the socket to its ip address set with SetAddress() and SetPort().

After binding the socket to an address, call the Listen() method to wait for incoming connections. This method only makes sense for server sockets.

◆ ClearError()

void Posix::PosixSocket::ClearError ( )
private

clear the last error code

Sets the internal error code to NoError.

◆ Close()

void Posix::PosixSocket::Close ( )

close the socket

◆ Connect()

PosixSocket::Result Posix::PosixSocket::Connect ( )

connect to the sockets address (for client sockets)

Connect to a server socket.

This method is called by a client socket to connect to a server socket identified by the socket object's address. A non-blocking socket will return immediately with WouldBlock, since the connection cannot be established immediately. In this case, just continue to call Connect() until the method returns Success, or alternative, check the IsConnected() method, which will also return true once the connection has been establish.

◆ ErrorAsString()

String Posix::PosixSocket::ErrorAsString ( ErrorCode err)
staticprivate

convert error code to human readable string

Convert an error code to a human readable error message.

◆ GetAddress()

const Net::IpAddress & Posix::PosixSocket::GetAddress ( ) const
inline

get internet address of socket

Get internet address of socket.

◆ GetBlocking()

bool Posix::PosixSocket::GetBlocking ( ) const
inline

get blocking mode

◆ GetBoolOption()

bool Posix::PosixSocket::GetBoolOption ( int optName)
private

get a bool socket option

Get a boolean option on the socket.

This is a private helper function.

◆ GetBroadcast()

bool Posix::PosixSocket::GetBroadcast ( )
inline

get the broadcast flag

◆ GetDontLinger()

bool Posix::PosixSocket::GetDontLinger ( )
inline

get the don't linger flag

◆ GetErrorCode()

PosixSocket::ErrorCode Posix::PosixSocket::GetErrorCode ( ) const
inline

get the last error code

◆ GetErrorString()

String Posix::PosixSocket::GetErrorString ( ) const

get the last error string

◆ GetIntOption()

int Posix::PosixSocket::GetIntOption ( int optName)
private

get an integer socket option

Get an int socket option.

This is a private helper function.

◆ GetKeepAlive()

bool Posix::PosixSocket::GetKeepAlive ( )
inline

get the keepalive flag

◆ GetMaxMsgSize()

SizeT Posix::PosixSocket::GetMaxMsgSize ( )
inline

get the maximum message size that can be sent atomically

◆ GetNoDelay()

bool Posix::PosixSocket::GetNoDelay ( )
inline

get nodelay flag

◆ GetRecvBufSize()

SizeT Posix::PosixSocket::GetRecvBufSize ( )
inline

get receive buffer size

◆ GetReUseAddr()

bool Posix::PosixSocket::GetReUseAddr ( )
inline

get reuseaddr flag

◆ GetSendBufSize()

SizeT Posix::PosixSocket::GetSendBufSize ( )
inline

get send buffer size

◆ HasRecvData()

bool Posix::PosixSocket::HasRecvData ( )

return true if recv data is available at the socket

This method checks if the socket has received data available.

Use this method in a loop with Recv() to get all data waiting at the socket. This method will never block.

◆ HostErrorToErrorCode()

PosixSocket::ErrorCode Posix::PosixSocket::HostErrorToErrorCode ( int wsaError)
staticprivate

convert a host (gethostbyname) error code to internal error code

This method converts a host error code into a portable error code used by PosixSocket.

◆ HostErrorToString()

String Posix::PosixSocket::HostErrorToString ( int wsaError)
staticprivate

convert host error code directly to string

◆ InitNetwork()

void Posix::PosixSocket::InitNetwork ( )
staticprivate

static initializer method (called only once)

This is a one-time init for the Windows Sockets system.

◆ IsBound()

bool Posix::PosixSocket::IsBound ( ) const
inline

return true if the socket is bound to an address

◆ IsConnected()

bool Posix::PosixSocket::IsConnected ( )

test if the socket is currently connected

This tests if the socket is actually connected by doing a select() on the socket to probe for writability.

So the IsConnected() method basically checks whether data can be sent through the socket.

◆ IsOpen()

bool Posix::PosixSocket::IsOpen ( ) const
inline

return true if the socket is open

◆ Listen()

bool Posix::PosixSocket::Listen ( )

listen for incoming connections (for server sockets)

Wait for incoming connections to a server socket.

Call this method on server side after binding the socket to its address.

◆ Open()

bool Posix::PosixSocket::Open ( Protocol p)

open the socket

◆ OpenWithExistingSocket()

void Posix::PosixSocket::OpenWithExistingSocket ( int s)
private

open with an existing socket (called by Accept())

Open the socket object with an existing initialized system socket.

This is a private method and only called by Accept() on the new socket created by the accept() function.

◆ Recv()

PosixSocket::Result Posix::PosixSocket::Recv ( void * buf,
SizeT bufSize,
SizeT & bytesReceived )

receive raw data from the socket

Receive raw data from a socket and write the received data into the provided buffer.

On a blocking socket this method will block until data arrives at the socket. A non-blocking socket would immediately return in this case with a WouldBlock result. When valid data has been received the method will return with a Success result and the bytesReceived argument will contain the number of received bytes. It is not guaranteed that a single receive will return all data waiting on the socket. To make sure that the socket is really empty, call Recv() in a loop until HasRecvData() returns false. When the socket has been gracefully closed by the other side, the method will return with a Closed return value. Everything else will return with an Error return code. Call GetErrorCode() or GetErrorString() to find out more in this case.

◆ RecvFrom()

PosixSocket::Result Posix::PosixSocket::RecvFrom ( void * buf,
SizeT bufSize,
uint addr,
ushort port,
SizeT & bytesReceived )

receive raw data from address for connectionless sockets

FIXME: this is the recv method for connectionless socket using the UDP protocol.

◆ Send()

PosixSocket::Result Posix::PosixSocket::Send ( const void * buf,
SizeT numBytes,
SizeT & bytesSent )

send raw data into the socket

Send raw data into the socket.

Note that depending on the buffer size of the underlying socket implementation and other sockets, the method may not be able to send all provided data. In this case, the returned content of bytesSent will be less then numBytes, even though the return value will be Success. It is up to the caller to handle the extra data which hasn't been sent with the current call.

◆ SendTo()

PosixSocket::Result Posix::PosixSocket::SendTo ( const void * buf,
SizeT numBytes,
uint addr,
ushort port,
SizeT & bytesSent )

send raw data to address for connectionless sockets

FIXME: this is the send method for connectionless sockets using the UDP protocol.

◆ SetAddress()

void Posix::PosixSocket::SetAddress ( const Net::IpAddress & a)
inline

set internet address of socket

Set internet address of socket.

◆ SetBlocking()

void Posix::PosixSocket::SetBlocking ( bool b)

set blocking mode (FIONBIO)

Set the socket to blocking mode.

◆ SetBoolOption()

void Posix::PosixSocket::SetBoolOption ( int optName,
bool val )
private

set a bool socket option

Set a boolean option on the socket.

This is a private helper function.

◆ SetBroadcast()

void Posix::PosixSocket::SetBroadcast ( bool b)
inline

set the broadcast flag (SO_BROADCAST)

◆ SetDontLinger()

void Posix::PosixSocket::SetDontLinger ( bool b)
inline

set the don't linger flag (SO_DONTLINGER)

◆ SetIntOption()

void Posix::PosixSocket::SetIntOption ( int optName,
int val )
private

set an integer socket option

Set an int socket option.

This is a private helper function.

◆ SetKeepAlive()

void Posix::PosixSocket::SetKeepAlive ( bool b)
inline

set the keepalive flag (SO_KEEPALIVE)

◆ SetNoDelay()

void Posix::PosixSocket::SetNoDelay ( bool b)
inline

set nodelay flag (TCP_NODELAY)

◆ SetRecvBufSize()

void Posix::PosixSocket::SetRecvBufSize ( SizeT s)
inline

set receive buffer size

◆ SetReUseAddr()

void Posix::PosixSocket::SetReUseAddr ( bool b)
inline

set reuseaddr flag (SO_REUSEADDR)

◆ SetSendBufSize()

void Posix::PosixSocket::SetSendBufSize ( SizeT s)
inline

set send buffer size

◆ SetSocketError()

void Posix::PosixSocket::SetSocketError ( int wsaError)
private

set socket error code

Sets the provided socket error as error code.

◆ SetToLastSocketError()

void Posix::PosixSocket::SetToLastSocketError ( )
private

set error code to errno;

Sets the internal error code to errno.

◆ SocketErrorToErrorCode()

PosixSocket::ErrorCode Posix::PosixSocket::SocketErrorToErrorCode ( int wsaError)
staticprivate

convert a socket error code to internal error code

This method converts a socket error code into a portable error code used by PosixSocket.

◆ SocketErrorToString()

String Posix::PosixSocket::SocketErrorToString ( int wsaError)
staticprivate

convert socket error code directly to string

Friends And Related Symbol Documentation

◆ PosixIpAddress

friend class PosixIpAddress
friend

◆ SysFunc

friend class SysFunc
friend

Member Data Documentation

◆ addr

Net::IpAddress Posix::PosixSocket::addr
private

◆ error

ErrorCode Posix::PosixSocket::error
private

◆ isBlocking

bool Posix::PosixSocket::isBlocking
private

◆ isBound

bool Posix::PosixSocket::isBound
private

◆ NetworkInitialized

bool Posix::PosixSocket::NetworkInitialized = false
staticprivate

◆ sock

SOCKET Posix::PosixSocket::sock
private

The documentation for this class was generated from the following files: