Nebula
Loading...
Searching...
No Matches
Win32::Win32Socket Class Reference

#include <win32socket.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...

Inherits Core::RefCounted.

Public Types

enum  Protocol { TCP , UDP }
 protocol types More...
 
enum  Result { Error = 0 , Success , WouldBlock , Closed }
 
enum  ErrorCode {
  ErrorNone , ErrorUnknown , ErrorInterrupted , 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 , ErrorTooManyProcesses ,
  ErrorSystemNotReady , ErrorVersionNotSupported , ErrorNotInitialized , ErrorDisconnecting ,
  ErrorTypeNotFound , ErrorHostNotFound , ErrorTryAgain , ErrorNoRecovery ,
  ErrorNoData
}
 error codes More...
 

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::IpAddressGetAddress () 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::StringGetClassName () 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)
 

Member Enumeration Documentation

◆ ErrorCode

error codes

Enumerator
ErrorNone 
ErrorUnknown 
ErrorInterrupted 
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 
ErrorTooManyProcesses 
ErrorSystemNotReady 
ErrorVersionNotSupported 
ErrorNotInitialized 
ErrorDisconnecting 
ErrorTypeNotFound 
ErrorHostNotFound 
ErrorTryAgain 
ErrorNoRecovery 
ErrorNoData 

◆ Protocol

protocol types

Enumerator
TCP 
UDP 

◆ Result

Enumerator
Error 
Success 
WouldBlock 
Closed 

Constructor & Destructor Documentation

◆ Win32Socket()

Win32::Win32Socket::Win32Socket ( )

constructor

◆ ~Win32Socket()

Win32::Win32Socket::~Win32Socket ( )
virtual

destructor

Member Function Documentation

◆ __DeclareClass()

Win32::Win32Socket::__DeclareClass ( Win32Socket )
private

◆ Accept()

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".

◆ Bind()

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.

◆ ClearError()

void Win32::Win32Socket::ClearError ( )
private

clear the last error code

Sets the internal error code to NoError.

◆ Close()

void Win32::Win32Socket::Close ( )

close the socket

◆ Connect()

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.

◆ ErrorAsString()

String Win32::Win32Socket::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 & Win32::Win32Socket::GetAddress ( ) const
inline

get internet address of socket

Get internet address of socket.

◆ GetBlocking()

bool Win32::Win32Socket::GetBlocking ( ) const
inline

get blocking mode

◆ GetBoolOption()

bool Win32::Win32Socket::GetBoolOption ( int optName)
private

get a bool socket option

Get a boolean option on the socket.

This is a private helper function.

◆ GetBroadcast()

bool Win32::Win32Socket::GetBroadcast ( )
inline

get the broadcast flag

◆ GetErrorCode()

Win32Socket::ErrorCode Win32::Win32Socket::GetErrorCode ( ) const
inline

get the last error code

◆ GetErrorString()

String Win32::Win32Socket::GetErrorString ( ) const

get the last error string

◆ GetIntOption()

int Win32::Win32Socket::GetIntOption ( int optName)
private

get an integer socket option

Get an int socket option.

This is a private helper function.

◆ GetKeepAlive()

bool Win32::Win32Socket::GetKeepAlive ( )
inline

get the keepalive flag

◆ GetMaxMsgSize()

SizeT Win32::Win32Socket::GetMaxMsgSize ( )
inline

get the maximum message size that can be sent atomically

◆ GetNoDelay()

bool Win32::Win32Socket::GetNoDelay ( )
inline

get nodelay flag

◆ GetRecvBufSize()

SizeT Win32::Win32Socket::GetRecvBufSize ( )
inline

get receive buffer size

◆ GetReUseAddr()

bool Win32::Win32Socket::GetReUseAddr ( )
inline

get reuseaddr flag

◆ GetSendBufSize()

SizeT Win32::Win32Socket::GetSendBufSize ( )
inline

get send buffer size

◆ HasRecvData()

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.

◆ InitNetwork()

void Win32::Win32Socket::InitNetwork ( )
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.

◆ IsBound()

bool Win32::Win32Socket::IsBound ( ) const
inline

return true if the socket is bound to an address

◆ IsConnected()

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.

◆ IsNetworkInitialized()

bool Win32::Win32Socket::IsNetworkInitialized ( )
static

is network initialized

◆ IsOpen()

bool Win32::Win32Socket::IsOpen ( ) const
inline

return true if the socket is open

◆ Listen()

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.

◆ Open()

bool Win32::Win32Socket::Open ( Protocol p)

open the socket

◆ OpenWithExistingSocket()

void Win32::Win32Socket::OpenWithExistingSocket ( SOCKET 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()

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.

◆ RecvFrom()

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.

◆ Send()

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.

◆ SendTo()

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.

◆ SetAddress()

void Win32::Win32Socket::SetAddress ( const Net::IpAddress & a)
inline

set internet address of socket

Set internet address of socket.

◆ SetBlocking()

void Win32::Win32Socket::SetBlocking ( bool b)

set blocking mode (FIONBIO)

Set the socket to blocking mode.

◆ SetBoolOption()

void Win32::Win32Socket::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 Win32::Win32Socket::SetBroadcast ( bool b)
inline

set the broadcast flag (SO_BROADCAST)

◆ SetIntOption()

void Win32::Win32Socket::SetIntOption ( int optName,
int val )
private

set an integer socket option

Set an int socket option.

This is a private helper function.

◆ SetKeepAlive()

void Win32::Win32Socket::SetKeepAlive ( bool b)
inline

set the keepalive flag (SO_KEEPALIVE)

◆ SetNoDelay()

void Win32::Win32Socket::SetNoDelay ( bool b)
inline

set nodelay flag (TCP_NODELAY)

◆ SetRecvBufSize()

void Win32::Win32Socket::SetRecvBufSize ( SizeT s)
inline

set receive buffer size

◆ SetReUseAddr()

void Win32::Win32Socket::SetReUseAddr ( bool b)
inline

set reuseaddr flag (SO_REUSEADDR)

◆ SetSendBufSize()

void Win32::Win32Socket::SetSendBufSize ( SizeT s)
inline

set send buffer size

◆ SetToLastWSAError()

void Win32::Win32Socket::SetToLastWSAError ( )
private

set error code to WSAGetLastError();

Sets the internal error code to WSAGetLastError().

◆ SetWSAError()

void Win32::Win32Socket::SetWSAError ( int wsaError)
private

set WSA error code

Sets the provided WSA error as error code.

◆ WSAErrorToErrorCode()

Win32Socket::ErrorCode Win32::Win32Socket::WSAErrorToErrorCode ( int wsaError)
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.

◆ WSAErrorToString()

String Win32::Win32Socket::WSAErrorToString ( int wsaError)
staticprivate

convert WSA error code directly to string

Friends And Related Symbol Documentation

◆ Win32IpAddress

friend class Win32IpAddress
friend

Member Data Documentation

◆ addr

Net::IpAddress Win32::Win32Socket::addr
private

◆ error

ErrorCode Win32::Win32Socket::error
private

◆ isBlocking

bool Win32::Win32Socket::isBlocking
private

◆ isBound

bool Win32::Win32Socket::isBound
private

◆ NetworkInitialized

bool Win32::Win32Socket::NetworkInitialized = false
staticprivate

◆ sock

SOCKET Win32::Win32Socket::sock
private

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