GCGlib
0.04.228
GCG Graphics Engine
|
Implements methods for peer-to-peer communication using TCP/IP protocol. More...
#include <gcg.h>
Public Member Functions | |
gcgINETPEER () | |
Constructs a valid but disconnected peer. | |
virtual | ~gcgINETPEER () |
Calls disconnect() and frees all resources. | |
bool | connectToHost (const char *host, int port) |
Connects to an INET host described by host using port port. More... | |
bool | send (void *buffer, unsigned int nbytes) |
Sends a byte sequence to the remote peer. More... | |
int | receive (void *buffer, unsigned int nbytes) |
Receives a byte sequence from the remote peer. More... | |
bool | getConnectionFrom (gcgINETPEER *oldpeerobject) |
Gets the connection of oldpeerobject by moving its contents, resulting in a connection migration. More... | |
bool | shutdownSend () |
Stops the functionality of sending messages. More... | |
bool | shutdownReceive () |
Stops the functionality of receiving messages. More... | |
bool | disconnect () |
Safely breaks current connection. More... | |
int | waitEvent (bool checkReceive=true, bool checkSend=false, int timeoutUsec=-1) |
Checks if the connection is ready for sending/receiving messages. More... | |
Public Member Functions inherited from gcgCLASS | |
void * | operator new (size_t size) |
Defines a new operator to be used by instatiations of GCGlib classes instead the global one. More... | |
void * | operator new (size_t size, const std::nothrow_t &) throw () |
Defines a new operator to be used by instantiations of GCGlib classes instead the global one. Returns a NULL pointer instead of throwing an exception if an error occurs. More... | |
void * | operator new[] (size_t size) |
Defines a new operator to be used by GCGlib array allocations instead the global one. More... | |
void * | operator new[] (size_t size, const std::nothrow_t &) throw () |
Defines a new operator to be used by vector allocations instead the global one. More... | |
void | operator delete (void *p) |
Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More... | |
void | operator delete (void *p, const std::nothrow_t &) throw () |
Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More... | |
void | operator delete[] (void *p) |
Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More... | |
void | operator delete[] (void *p, const std::nothrow_t &) throw () |
Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More... | |
Public Attributes | |
int | local_ip [4] |
Local IP. | |
int | local_port |
Local Port. | |
bool | isConnected |
True if this peer is connected. | |
int | remote_ip [4] |
Remote peer IP. | |
int | remote_port |
Remote port. | |
void * | handle |
Internal handle of this peer. | |
Implements methods for peer-to-peer communication using TCP/IP protocol.
After the connection, the local IP and the remote IP are available for the user as well as the local and remote port numbers. The user can wait for events (reading, writing) using waitEvents() method. This is useful for probing if the peer is ready for sending or writing. The shutdownSend() and shutdownReceive() methods are useful for stoping full duplex communication, which is important to save system resources.
bool gcgINETPEER::connectToHost | ( | const char * | host, |
int | port | ||
) |
Connects to an INET host described by host using port port.
[in] | host | Null-terminated string that defines the host address. |
[in] | port | Integer value identifying the target port. |
bool gcgINETPEER::disconnect | ( | ) |
Safely breaks current connection.
Disconnects the local and remote peers.
bool gcgINETPEER::getConnectionFrom | ( | gcgINETPEER * | oldpeerobject | ) |
Gets the connection of oldpeerobject by moving its contents, resulting in a connection migration.
If succesful, oldpeerobject becomes disconnected and, in his place, this object can be used for continuing communication.
[in] | oldpeerobject | already connected peer object. |
int gcgINETPEER::receive | ( | void * | buffer, |
unsigned int | nbytes | ||
) |
Receives a byte sequence from the remote peer.
It may block the caller if the number of bytes nbytes is not available. Use waitEvent() with checkReceive = true to check if there are bytes to be read. Even if the peer is ready to receive, it does not imply that the caller will not block. This also depends on the requested number of bytes.
[out] | buffer | pointer of the data. |
[in] | nbytes | number of bytes to be received. |
bool gcgINETPEER::send | ( | void * | buffer, |
unsigned int | nbytes | ||
) |
Sends a byte sequence to the remote peer.
It may block the caller if the buffer is full. Use waitEvent() with checkSend = true to check if the buffer is completely full. Even if the peer is ready to send, it does not imply that the caller will not block. This also depends on the size of the message.
[in] | buffer | pointer of the data. |
[in] | nbytes | number of bytes to be sent. |
bool gcgINETPEER::shutdownReceive | ( | ) |
Stops the functionality of receiving messages.
This function informs to the system that the connection will only send messages. This is important to save resources. In this case the input queue will not occupy memory space. Further trials of receiving messages will result in error.
bool gcgINETPEER::shutdownSend | ( | ) |
Stops the functionality of sending messages.
This function informs to the system that the connection will only receive messages. This is important to save resources. In this case the output queue will not occupy memory space. Further trials of sending messages will result in error.
int gcgINETPEER::waitEvent | ( | bool | checkReceive = true , |
bool | checkSend = false , |
||
int | timeoutUsec = -1 |
||
) |
Checks if the connection is ready for sending/receiving messages.
Blocks the caller until: an event happens or the timeout is elapsed. Returns 0 (GCG_EVENT_NOTREADYORDISCONNECTED), if the timeout is elapsed (GCG_REPORT_MESSAGE(gcgGetReport()) = GCG_TIMEOUT) or the connection is broken or an unrecoverable error occurs. If checkReceive is true, it waits for incoming messages. Returns 1 (GCG_EVENT_RECEIVEREADY) when a message is available. If checkSend is true, it waits until a message can be sent without blocking a send() call. Returns 2 (GCG_EVENT_SENDREADY) when the peer is ready to send a new message. If checkReceive and checkSend are both true, it may return 1 (GCG_EVENT_RECEIVEREADY) or 2 (GCG_EVENT_SENDREADY) as described above. Returns 3 ( GCG_EVENT_ALLREADY), when a message is available AND the peer is ready to send. If both checkReceive and checkSend are false, then it just checks for errors or disconnection.
[in] | checkReceive | pass true if waitEvent() should check if there are incoming messages. |
[in] | checkSend | pass true if waitEvent() should check if the output buffer is not completely full. Even if the peer is ready to send, it does not imply that calling send() will not block. This also depends on the size of the message. |
[in] | timeoutUsec | maximum time in microseconds to wait if no event occurred. A negative value means that waitEvent() must wait indefinitely for an event or error. A positive value means that after timeoutUsec microseconds it must return ( GCG_REPORT_MESSAGE(gcgGetReport()) = GCG_TIMEOUT) unless an event has occurred. |