GCGlib  0.04.228
GCG Graphics Engine
gcgINETPEER Class Reference

Implements methods for peer-to-peer communication using TCP/IP protocol. More...

#include <gcg.h>

Inheritance diagram for gcgINETPEER:
gcgCLASS

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.
 

Detailed Description

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.

Since
0.01.6

Member Function Documentation

◆ connectToHost()

bool gcgINETPEER::connectToHost ( const char *  host,
int  port 
)

Connects to an INET host described by host using port port.

Parameters
[in]hostNull-terminated string that defines the host address.
[in]portInteger value identifying the target port.
Returns
true if the connection is established. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
disconnect()
getConnectionFrom()

◆ disconnect()

bool gcgINETPEER::disconnect ( )

Safely breaks current connection.

Disconnects the local and remote peers.

See also
connectToHost()
getConnectionFrom()
Returns
Returns true if the peer was correctly disconnected. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.

◆ getConnectionFrom()

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.

Parameters
[in]oldpeerobjectalready connected peer object.
Returns
true if the a migration was made. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
connectToHost()
disconnect()

◆ receive()

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.

Parameters
[out]bufferpointer of the data.
[in]nbytesnumber of bytes to be received.
Returns
the number of bytes received. If it returns 0, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem. If a friendly disconnection has ocurred, the code will be GCG_CONNECTIONCLOSED. After the call, always check the attribute isConnected to verify if it is still connected.
See also
waitEvent()
receive()
shutdownReceive()

◆ send()

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.

Parameters
[in]bufferpointer of the data.
[in]nbytesnumber of bytes to be sent.
Returns
true if the message was correctly sent. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem. After the call, always check the attribute isConnected to verify if it is still connected.
See also
waitEvent()
receive()
shutdownSend()

◆ shutdownReceive()

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.

Returns
true if the shutdown is done. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
shutdownSend()

◆ shutdownSend()

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.

Returns
true if the shutdown is done. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
shutdownReceive()

◆ waitEvent()

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.

Parameters
[in]checkReceivepass true if waitEvent() should check if there are incoming messages.
[in]checkSendpass 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]timeoutUsecmaximum 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.
Returns
a bit mask composed of combinations of GCG_EVENT_RECEIVEREADY (0x1) and GCG_EVENT_SENDREADY (0x2) when events occurs, or GCG_EVENT_NOEVENTORDISCONNECTED (0x0) if the peer is not ready for communication. If it returns GCG_EVENT_NOEVENTORDISCONNECTED, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.

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