Nebula
|
#include <win32socket.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...
Inherits Core::RefCounted.
Public Member Functions | |
Win32Socket () | |
constructor | |
virtual | ~Win32Socket () |
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 | 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 | |
Static Public Member Functions | |
static void | InitNetwork () |
static initializer method (called by SysFunc::Setup()) | |
static bool | IsNetworkInitialized () |
is network initialized | |
Static Public Member Functions inherited from Core::RefCounted | |
static void | DumpRefCountingLeaks () |
dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!) | |
Private Member Functions | |
__DeclareClass (Win32Socket) | |
void | OpenWithExistingSocket (SOCKET s) |
open with an existing socket (called by Accept()) | |
void | ClearError () |
clear the last error code | |
void | SetToLastWSAError () |
set error code to WSAGetLastError(); | |
void | SetWSAError (int wsaError) |
set WSA 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 ErrorCode | WSAErrorToErrorCode (int wsaError) |
convert a WSA error code to internal error code | |
static Util::String | ErrorAsString (ErrorCode err) |
convert error code to human readable string | |
static Util::String | WSAErrorToString (int wsaError) |
convert WSA 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 | Win32IpAddress |
Additional Inherited Members | |
Protected Member Functions inherited from Core::RefCounted | |
virtual | ~RefCounted () |
destructor (called when refcount reaches zero) | |
error codes
Win32::Win32Socket::Win32Socket | ( | ) |
constructor
|
virtual |
destructor
|
private |
bool Win32::Win32Socket::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 Win32::Win32Socket::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 Win32::Win32Socket::Close | ( | ) |
close the socket
Win32Socket::Result Win32::Win32Socket::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 last error code
String Win32::Win32Socket::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 Win32::Win32Socket::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.
|
static |
static initializer method (called by SysFunc::Setup())
This is a one-time init for the Windows Sockets system.
The method is called from SysFunc::Setup() once at startup before any threads are launched.
|
inline |
return true if the socket is bound to an address
bool Win32::Win32Socket::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.
|
static |
is network initialized
|
inline |
return true if the socket is open
bool Win32::Win32Socket::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 Win32::Win32Socket::Open | ( | Protocol | p | ) |
open the socket
|
private |
Win32Socket::Result Win32::Win32Socket::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.
Win32Socket::Result Win32::Win32Socket::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.
Win32Socket::Result Win32::Win32Socket::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.
Win32Socket::Result Win32::Win32Socket::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 Win32::Win32Socket::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)
|
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 error code to WSAGetLastError();
Sets the internal error code to WSAGetLastError().
|
private |
set WSA error code
Sets the provided WSA error as error code.
|
staticprivate |
convert a WSA error code to internal error code
This method converts an Windows Socket error code into a portable error code used by Win32Socket.
|
staticprivate |
convert WSA error code directly to string
|
friend |
|
private |
|
private |
|
private |
|
private |
|
staticprivate |
|
private |