ENUt
|
An ENet based socket. More...
#include <enetsocket.h>
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. |
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.
nut::ENETSocket::ENETSocket | ( | ) |
nut::ENETSocket::ENETSocket | ( | const std::string & | objName | ) |
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.
maxConnections | The 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.
maxConnections | The maximum number of connections allowed (both incoming and outgoing). |
port | If 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.
bindAddr | The IP address to bind the socket to. |
maxConnections | The maximum number of connections allowed (both incoming and outgoing). |
port | If 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.
length | Will contain the number of bytes of the first packet that can be read (can be zero!) |
available | Is 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.
connID | The connection for which additional information is requested. |
addr | In this variable, the IP address of the end-point of this connection will be stored. |
port | In 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.
connID | Identifier 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.
connID | The identifier of the new connection. |
sourceAddress | IP address of the end-point of this connection. |
sourcePort | Port 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.
buffer | A pointer to the buffer in which the data can be stored. |
bufferSize | Initially 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. |
channel | If not null, the channel number on which the data was received will be stored here. |
connID | If 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.
destAddress | IP address of the server. |
destPort | Port number on which the server is listening for incoming connections. |
channelCount | The 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.
milliseconds | The 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
pData | Pointer to the data which should be sent. |
dataLength | Number of bytes which should be sent. |
reliable | Flag indicating if the data should be sent reliably. |
channel | The channel number on which the data should be sent. |
connID | The connection on which the data should be sent. A value of zero specifies all existing connections. |
allExceptConnID | If this flag is true, the data will be sent over all connections except the one specified in connID . |