Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

VCF::Socket Class Reference

#include <vcf/NetworkKit/Socket.h>

Inheritance diagram for VCF::Socket:

VCF::Object VCF::TCPSocket VCF::UDPSocket List of all members.

Public Types

enum  SocketType { stStream, stStreamAsync, stDatagram, stDatagramAsync }
enum  { SelectNoWait = (uint32)0, SelectWaitForever = (uint32)-1 }
enum  SocketState {
  ssError = 0x0001, ssReadable = 0x0002, ssWriteable = 0x0004, ssConnected = 0x0010,
  ssListening = 0x0020, ssOpen = 0x0100, ssClosed = 0x0000
}
enum  SocketEvents { seClientConnected = 112491, seClientDisconnected, seReadyToRead, seReadyToWrite }

Public Member Functions

 Socket ()
 Creates an unconnected socket in stStream mode.
 Socket (const SocketType &socketType)
 Creates an unconnected socket of the requested type.
 Socket (unsigned short port)
 Creates a bound and listening socket in stStream mode.
 Socket (const String &host, unsigned short port)
 Creates a connected socket in stStream mode.
virtual ~Socket ()
void open ()
 Opens the socket for use and creates allocates any neccessary OS resoruces for the socket (depending on the peer implementation).
void close ()
 Closes the socket and frees up any underlying OS resources that were allocated for the socket by the previous call to open().
void connect (const String &host, unsigned short port)
 Connects to a specified host and port.
void listen (unsigned short port)
 This will start the socket listening on the specified port.
TCPSocketaccept ()
 Accepts a new connection and returns a new TCP socket instance.
void setOptions (Dictionary &options)
 Sets the options for a socket.
Dictionary getOptions ()
 Returns a dictionary containing the current options for the socket.
IPAddress getLocalHostIPAddress ()
 Returns the local ip address for this socket.
IPAddress getRemoteHostIPAddress ()
 Returns the remote ip address that this socket is connected to.
unsigned short getLocalPort ()
unsigned short getRemotePort ()
SocketPeergetPeer ()
SocketType getSocketType ()
 Returns the socket type.
bool isReadable () const
bool hasError () const
bool isWriteable () const
bool isConnected () const
bool isListening () const
bool isOpen () const
bool isClosed () const
bool pending ()
 Does a quick test to see if there is any readable or writeable data available for the socket.
void internal_setErrorState (bool val)
 Do not call this method - for internal use only.
void internal_setReadable (bool val)
 Do not call this method - for internal use only.
void internal_setWriteable (bool val)
 Do not call this method - for internal use only.
bool wouldOperationBlock ()
 Indicates whether the current operation would block.
void selectFor (uint32 timeout, uint32 flags)
 Perform a select, on this socket, for a specified timeout period, checking against some set of flags that will indicate whether or not we should look for readable, writeable, or error states.

Static Public Member Functions

static bool validateOptions (Dictionary &options)

Public Attributes

VCF::Delegate SocketEventDelegate
 ClientConnected
 ClientDisconnected
VCF::Delegate EventDelegate
 ReadyToRead
 ReadyToWrite

Static Public Attributes

static const String soBlocking
 This key indicates whether or not the socket is blocking or not.
static const String soBroadcast
 Bool value.
static const String soDontLinger
 Bool value.
static const String soDontRoute
 Bool value.
static const String soKeepAlive
 Bool value.
static const String soRecvBuffer
 int value
static const String soReuseAddress
 Bool value.
static const String soSendBuffer
 int value

Protected Member Functions

 Socket (SocketPeer *peer)
 Creates a socket from an existing peer intance.

Protected Attributes

SocketPeerpeer_
SocketType type_
int state_

Member Enumeration Documentation

anonymous enum
 

Enumerator:
SelectNoWait  Select should check and then return immediately.
SelectWaitForever  Select will wait indefinitely, until one of the sockets passed in is flagged with data.

enum VCF::Socket::SocketEvents
 

Enumerator:
seClientConnected 
seClientDisconnected 
seReadyToRead 
seReadyToWrite 

enum VCF::Socket::SocketState
 

Enumerator:
ssError 
ssReadable 
ssWriteable 
ssConnected 
ssListening 
ssOpen 
ssClosed 

enum VCF::Socket::SocketType
 

Enumerator:
stStream  Creates a standard stream based socket peer used for TCP connections.
stStreamAsync  Creates a stream based socket peer used for TCP connections.

This is different than the the stStream type in that it is specifically designed to support asynchronous IO. The implementation of this is platform dependant, assuming the platform supports it at all. On Win32 systems this uses IO Completion ports under the hood to allow this to occur. This means that, at a minimum, the application will have at least one more thread running, and possibly more than that depending on the number of CPUs available.

stDatagram  Creates a standard socket used for UDP connections.
stDatagramAsync  Creates a standard socket used for UDP connections.

This is different than the the stDatagram type in that it is specifically designed to support asynchronous IO. The implementation of this is platform dependant, assuming the platform supports it at all. On Win32 systems this uses IO Completion ports under the hood to allow this to occur. This means that, at a minimum, the application will have at least one more thread running, and possibly more than that depending on the number of CPUs available.


Constructor & Destructor Documentation

VCF::Socket::Socket  ) 
 

Creates an unconnected socket in stStream mode.

VCF::Socket::Socket const SocketType socketType  ) 
 

Creates an unconnected socket of the requested type.

Parameters:
SocketType indicates the type of socket to create. If the host operating system doesn't support the requested type, the underlying peer instance will be NULL and an exception will be thrown by the constructor.
See also:
SocketType

VCF::Socket::Socket unsigned short  port  ) 
 

Creates a bound and listening socket in stStream mode.

VCF::Socket::Socket const String host,
unsigned short  port
 

Creates a connected socket in stStream mode.

A connection is made to the specified host and port

virtual VCF::Socket::~Socket  )  [virtual]
 

VCF::Socket::Socket SocketPeer peer  )  [protected]
 

Creates a socket from an existing peer intance.

This is used primarily by the accept() method.


Member Function Documentation

TCPSocket* VCF::Socket::accept  ) 
 

Accepts a new connection and returns a new TCP socket instance.

Assumes the listen() call has already occurred. If it has not then an exception is thrown by the underlying peer implementation. This method will block until a socket connection has been detected unless the instance has been set to non blocking. This is only valid when called by a TCP socket.

Returns:
TCPSocket* a new instance of a TCPSocket. The caller of this method is responsible for the lifetime and clean up of this socket instance.
See also:
soBlocking

setOptions

void VCF::Socket::close  ) 
 

Closes the socket and frees up any underlying OS resources that were allocated for the socket by the previous call to open().

void VCF::Socket::connect const String host,
unsigned short  port
 

Connects to a specified host and port.

If the peer is unable to connect, then a SocketException exception is thrown.

Parameters:
String the host name to connect to. This may a host name, like "google.com" or a valid IP address, such as "123.32.101.1".
unsigned short port the port to connect to.

IPAddress VCF::Socket::getLocalHostIPAddress  ) 
 

Returns the local ip address for this socket.

unsigned short VCF::Socket::getLocalPort  ) 
 

Dictionary VCF::Socket::getOptions  ) 
 

Returns a dictionary containing the current options for the socket.

SocketPeer* VCF::Socket::getPeer  )  [inline]
 

IPAddress VCF::Socket::getRemoteHostIPAddress  ) 
 

Returns the remote ip address that this socket is connected to.

unsigned short VCF::Socket::getRemotePort  ) 
 

SocketType VCF::Socket::getSocketType  )  [inline]
 

Returns the socket type.

Can be one of the SocketType enumeration values.

See also:
SocketType

bool VCF::Socket::hasError  )  const [inline]
 

void VCF::Socket::internal_setErrorState bool  val  )  [inline]
 

Do not call this method - for internal use only.

void VCF::Socket::internal_setReadable bool  val  )  [inline]
 

Do not call this method - for internal use only.

void VCF::Socket::internal_setWriteable bool  val  )  [inline]
 

Do not call this method - for internal use only.

bool VCF::Socket::isClosed  )  const [inline]
 

bool VCF::Socket::isConnected  )  const [inline]
 

bool VCF::Socket::isListening  )  const [inline]
 

bool VCF::Socket::isOpen  )  const [inline]
 

bool VCF::Socket::isReadable  )  const [inline]
 

bool VCF::Socket::isWriteable  )  const [inline]
 

void VCF::Socket::listen unsigned short  port  ) 
 

This will start the socket listening on the specified port.

This can be called for either a UDP or TCP socket, with the appropriate underlying calls to bind and/or listen made as neccessary.

void VCF::Socket::open  ) 
 

Opens the socket for use and creates allocates any neccessary OS resoruces for the socket (depending on the peer implementation).

If it was alread open, then it is first closed and then opened. Once this is called the socket's state will be set to Socket::ssOpen.

bool VCF::Socket::pending  ) 
 

Does a quick test to see if there is any readable or writeable data available for the socket.

Returns:
bool returns true if the socket is marked for writing or marked for reading. Otherwise returns false.

void VCF::Socket::selectFor uint32  timeout,
uint32  flags
 

Perform a select, on this socket, for a specified timeout period, checking against some set of flags that will indicate whether or not we should look for readable, writeable, or error states.

When this function returns, the socket's state may change depending on the flags specified and the result of the internal call to select. After returning you may call the isReadable(), isWriteable(), or hasError() methods to determine the current state. For example:

        Socket s("foobar.com", 1234 );

        s.selectFor( 120, Socket::ssReadable );
        if ( s.isReadable() ) {
            //do something, such as reading data from the 
            //socket...
        }

Parameters:
uint32 the maximum number of milliseconds to wait for.
See also:
Socket::SelectNoWait,

Socket::SelectWaitForever

Parameters:
uint flags these must be some combination of Socket::ssError, Socket::ssReadable, or Socket::ssWriteable. If flags is 0, the function will return immediately and do nothing.

void VCF::Socket::setOptions Dictionary options  ) 
 

Sets the options for a socket.

The options are stored as a dictionary of items, with the keys being 1 or more values represented by the Socket::soXXX const string variables. The values are VariantData instances whose precise meaning depends on the key. For example, if you specify the Socket::soBlocking key, then the value is bool, indicating whether or not the socket should be set to blocking mode.

Parameters:
Dictionary a dictionary of options with key/value pairs that specify various socket settings.
See also:
Dictionary

static bool VCF::Socket::validateOptions Dictionary options  )  [static]
 

A static utility function to verify that the socket options have valid key-value pairings. The function iterates through the options until it finds the first invalid key-value pairing.
For example, if you used the key Socket::soBlocking with a int type, then the function would return a false value.
Returns:
bool returns true if the options are valid, otherwise it returns false at the first key-value pair that is invalid.

bool VCF::Socket::wouldOperationBlock  ) 
 

Indicates whether the current operation would block.

Assumes a non blocking socket. This is roughly equivalent to getting a an EWOULDBLOCK or WSAEWOULDBLOCK error number.

Returns:
bool True if the operation would block, otherwise false if it would not.


Member Data Documentation

VCF::Socket::ClientConnected
 

VCF::Socket::ClientDisconnected
 

VCF::Delegate VCF::Socket::EventDelegate
 

SocketPeer* VCF::Socket::peer_ [protected]
 

VCF::Socket::ReadyToRead
 

VCF::Socket::ReadyToWrite
 

const String VCF::Socket::soBlocking [static]
 

This key indicates whether or not the socket is blocking or not.

It's value type is a bool.

const String VCF::Socket::soBroadcast [static]
 

Bool value.

VCF::Delegate VCF::Socket::SocketEventDelegate
 

const String VCF::Socket::soDontLinger [static]
 

Bool value.

const String VCF::Socket::soDontRoute [static]
 

Bool value.

const String VCF::Socket::soKeepAlive [static]
 

Bool value.

const String VCF::Socket::soRecvBuffer [static]
 

int value

const String VCF::Socket::soReuseAddress [static]
 

Bool value.

const String VCF::Socket::soSendBuffer [static]
 

int value

int VCF::Socket::state_ [protected]
 

SocketType VCF::Socket::type_ [protected]
 


The documentation for this class was generated from the following file:
   Comments or Suggestions?    License Information