ENUt
Public Member Functions | Static Public Member Functions | Protected Member Functions

nut::ENETSocket Class Reference

An ENet based socket. More...

#include <enetsocket.h>

Inheritance diagram for nut::ENETSocket:
Inheritance graph
[legend]
Collaboration diagram for nut::ENETSocket:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ENETSocket ()
 Creates an ENet socket.
 ENETSocket (const std::string &objName)
 Creates an ENet socket with object name objName.
bool createServer (int maxConnections, uint16_t port=0)
 Creates an ENet socket in server mode.
bool createServer (const IPv4Address &bindAddr, int maxConnections, uint16_t port=0)
 Creates an ENet socket in server mode.
bool createClient (int maxConnections)
 Creates an ENet socket in client mode.
bool isServer () const
 Returns a flag indicating if the socket was created in server mode or client mode.
bool close ()
 Closes the socket and all associated connections.
uint16_t getLocalPort () const
 Returns the port number that the socket is bound to.
bool waitForEvent (int milliseconds)
 Waits at most the specified time for an event to occur.
bool poll ()
 Process all currently available events.
bool requestConnection (const IPv4Address &destAddress, uint16_t destPort, uint8_t channelCount)
 Try to establish a connection to the specified server.
bool closeConnection (uint32_t connID)
 Close the connection specified by connId.
int getNumberOfConnections () const
 Returns the current number of established connections.
const uint32_t * getConnectionIDs () const
 Returns a pointer to the connection identifiers of the currently established connections.
bool getConnectionInfo (uint32_t connID, IPv4Address &addr, uint16_t &port) const
 Obtain additional information about a specific connection.
bool write (const void *pData, size_t dataLength, bool reliable=false, uint8_t channel=0, uint32_t connID=0, bool allExceptConnID=false)
 Writes data on a connection or set of connections.
bool getAvailableDataLength (size_t &length, bool &available)
 Obtain information about the data which can be read.
bool read (void *buffer, size_t &bufferSize, uint8_t *channel=0, uint32_t *connID=0)
 Reads data from the socket.

Static Public Member Functions

static bool ENETStartup ()
 Initializes the ENet library.
static void ENETCleanup ()
 De-initializes the ENet library.

Protected Member Functions

virtual void onNewConnection (uint32_t connID, const IPv4Address &sourceAddress, uint16_t sourcePort)
 Override this function to get notified when a new incoming connection is detected or when an outgoing connection is successfully established.
virtual void onCloseConnection (uint32_t connID)
 Override this function to get notified when a connection is closed.

Detailed Description

This socket works according to the ENet protocol. Like UDP, it is a packet-based protocol, but unlike UDP it allows packets to be sent reliably. See http://enet.bespin.org/ for more information about the ENet protocol.


Constructor & Destructor Documentation

nut::ENETSocket::ENETSocket ( )
nut::ENETSocket::ENETSocket ( const std::string &  objName)

Member Function Documentation

bool nut::ENETSocket::close ( )
bool nut::ENETSocket::closeConnection ( uint32_t  connID)
bool nut::ENETSocket::createClient ( int  maxConnections)

Creates an ENet socket in client mode. This means that it can create outgoing connections, but it cannot accept incoming ones.

Parameters:
maxConnectionsThe maximum number of outgoing connections that can be established.
bool nut::ENETSocket::createServer ( int  maxConnections,
uint16_t  port = 0 
)

Creates an ENet socket in server mode. This means that the socket can accept incoming connections as well as outgoing connections.

Parameters:
maxConnectionsThe maximum number of connections allowed (both incoming and outgoing).
portIf not zero, the socket will be bound on a specific port.
bool nut::ENETSocket::createServer ( const IPv4Address bindAddr,
int  maxConnections,
uint16_t  port = 0 
)

Creates an ENet socket in server mode. This means that the socket can accept incoming connections as well as outgoing connections.

Parameters:
bindAddrThe IP address to bind the socket to.
maxConnectionsThe maximum number of connections allowed (both incoming and outgoing).
portIf not zero, the socket will be bound on a specific port.
static void nut::ENETSocket::ENETCleanup ( ) [static]
static bool nut::ENETSocket::ENETStartup ( ) [static]
bool nut::ENETSocket::getAvailableDataLength ( size_t &  length,
bool &  available 
)

Obtain information about the data which can be read.

Parameters:
lengthWill contain the number of bytes of the first packet that can be read (can be zero!)
availableIs set to true if a packet is available, to false otherwise.
const uint32_t* nut::ENETSocket::getConnectionIDs ( ) const [inline]
bool nut::ENETSocket::getConnectionInfo ( uint32_t  connID,
IPv4Address addr,
uint16_t &  port 
) const

Obtain additional information about a specific connection.

Parameters:
connIDThe connection for which additional information is requested.
addrIn this variable, the IP address of the end-point of this connection will be stored.
portIn this variable, the port number of the end-point of this connection will be stored.
uint16_t nut::ENETSocket::getLocalPort ( ) const [inline]
int nut::ENETSocket::getNumberOfConnections ( ) const [inline]
bool nut::ENETSocket::isServer ( ) const [inline]
virtual void nut::ENETSocket::onCloseConnection ( uint32_t  connID) [inline, protected, virtual]

Override this function to get notified when a connection is closed.

Parameters:
connIDIdentifier of the connection that was closed.
virtual void nut::ENETSocket::onNewConnection ( uint32_t  connID,
const IPv4Address sourceAddress,
uint16_t  sourcePort 
) [inline, protected, virtual]

Override this function to get notified when a new incoming connection is detected or when an outgoing connection is successfully established.

Parameters:
connIDThe identifier of the new connection.
sourceAddressIP address of the end-point of this connection.
sourcePortPort number of the end-point of this connection.
bool nut::ENETSocket::poll ( )

Process all currently available events. An event can be a client that wants to connect, data that arrives etc.

bool nut::ENETSocket::read ( void *  buffer,
size_t &  bufferSize,
uint8_t *  channel = 0,
uint32_t *  connID = 0 
)

Reads data from the socket.

Parameters:
bufferA pointer to the buffer in which the data can be stored.
bufferSizeInitially this must be set to the maximum number of bytes that can be stored in buffer. The value will be adjusted to match the actual number of bytes in the first available packet. If bufferSize is smaller than the actual number of bytes in the first packet, the function will fail.
channelIf not null, the channel number on which the data was received will be stored here.
connIDIf not null, the identifier of the connection on which the data was received will be stored here.
bool nut::ENETSocket::requestConnection ( const IPv4Address destAddress,
uint16_t  destPort,
uint8_t  channelCount 
)

Try to establish a connection to the specified server.

Parameters:
destAddressIP address of the server.
destPortPort number on which the server is listening for incoming connections.
channelCountThe number of communication channels that should be allocated.
bool nut::ENETSocket::waitForEvent ( int  milliseconds)

Waits at most the specified time for an event to occur. The first event that occurs is processed as well. An event can be a client that wants to connect, data that arrives etc.

Parameters:
millisecondsThe maximum amount of time to wait (in milliseconds)
bool nut::ENETSocket::write ( const void *  pData,
size_t  dataLength,
bool  reliable = false,
uint8_t  channel = 0,
uint32_t  connID = 0,
bool  allExceptConnID = false 
)

Writes data on a connection or set of connections. If

Parameters:
pDataPointer to the data which should be sent.
dataLengthNumber of bytes which should be sent.
reliableFlag indicating if the data should be sent reliably.
channelThe channel number on which the data should be sent.
connIDThe connection on which the data should be sent. A value of zero specifies all existing connections.
allExceptConnIDIf this flag is true, the data will be sent over all connections except the one specified in connID.

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