Nebula
|
#include <posixsocket.h>
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 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::IpAddress & | GetAddress () 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::String & | GetClassName () 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) | |
error codes
Posix::PosixSocket::PosixSocket | ( | ) |
constructor
|
virtual |
destructor
|
private |
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".
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.
|
private |
clear the last error code
Sets the internal error code to NoError.
void Posix::PosixSocket::Close | ( | ) |
close the socket
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.
convert error code to human readable string
Convert an error code to a human readable error message.
|
inline |
get internet address of socket
Get internet address of socket.
|
inline |
get blocking mode
|
private |
get a bool socket option
Get a boolean option on the socket.
This is a private helper function.
|
inline |
get the broadcast flag
|
inline |
get the don't linger flag
|
inline |
get the last error code
String Posix::PosixSocket::GetErrorString | ( | ) | const |
get the last error string
|
private |
get an integer socket option
Get an int socket option.
This is a private helper function.
|
inline |
get the keepalive flag
|
inline |
get the maximum message size that can be sent atomically
|
inline |
get nodelay flag
|
inline |
get receive buffer size
|
inline |
get reuseaddr flag
|
inline |
get send buffer size
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.
|
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.
|
staticprivate |
convert host error code directly to string
|
staticprivate |
static initializer method (called only once)
This is a one-time init for the Windows Sockets system.
|
inline |
return true if the socket is bound to an address
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.
|
inline |
return true if the socket is open
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.
bool Posix::PosixSocket::Open | ( | Protocol | p | ) |
open the socket
|
private |
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.
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.
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.
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.
|
inline |
set internet address of socket
Set internet address of socket.
void Posix::PosixSocket::SetBlocking | ( | bool | b | ) |
set blocking mode (FIONBIO)
Set the socket to blocking mode.
|
private |
set a bool socket option
Set a boolean option on the socket.
This is a private helper function.
|
inline |
set the broadcast flag (SO_BROADCAST)
|
inline |
set the don't linger flag (SO_DONTLINGER)
|
private |
set an integer socket option
Set an int socket option.
This is a private helper function.
|
inline |
set the keepalive flag (SO_KEEPALIVE)
|
inline |
set nodelay flag (TCP_NODELAY)
|
inline |
set receive buffer size
|
inline |
set reuseaddr flag (SO_REUSEADDR)
|
inline |
set send buffer size
|
private |
set socket error code
Sets the provided socket error as error code.
|
private |
set error code to errno;
Sets the internal error code to errno.
|
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.
|
staticprivate |
convert socket error code directly to string
|
friend |
|
friend |
|
private |
|
private |
|
private |
|
private |
|
staticprivate |
|
private |