class RNET::IPeer

This interface gives access to REDnet peer configuration and life cycle management.

#include <RNETIPeer.h>

Inherits: IREDObject.

Public functions:

virtual RED_RCCloseConnection ( int iConnectionID ) = 0
virtual RED_RCConnectTo ( const RNET::Address & iAddress ) = 0
virtual RNET::AddressGetConnectionAddress ( int iConnection ) = 0
virtual RED_RCGetConnectionsIDList ( RED::Vector< int > & oConnectionsID ) const = 0
virtual unsigned intGetID ( ) const = 0
virtual intGetMaxConnections ( ) const = 0
virtual intGetPort ( ) const = 0
virtual unsigned intGetVersion ( ) const = 0
virtual const RED::String &GetWebServerPath ( ) const = 0
virtual boolIsConnectedToDispatcher ( ) const = 0
virtual boolIsConnectionAlive ( int iConnection ) = 0
virtual RED_RCSendImage ( unsigned int & oSentBytes, float & ioQuality, int iConnectionID, RED::Object * iImage, unsigned int iMaxFrameSize = 102400 ) = 0
virtual RED_RCSendImage ( unsigned int & oSentBytes, float & ioQuality, const RED::Vector< int > & iConnectionIDs, RED::Object * iImage, unsigned int iMaxFrameSize = 102400 ) = 0
virtual RED_RCSendMessage ( int iConnectionID, RNET::MessageType iType, const char * iData, unsigned int iDataSize ) = 0
virtual RED_RCSendString ( int iConnectionID, const RED::String & iString ) = 0
virtual voidSetOnConnectionClosed ( ON_CONNECTION_CLOSED_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetOnConnectionReady ( ON_CONNECTION_READY_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetOnDataReceived ( ON_DATA_RECEIVED_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetOnDataRequest ( ON_DATA_REQUEST_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetOnPoll ( ON_POLL_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetOnShutdown ( ON_SHUTDOWN_CALLBACK iCallback, void * iUserData ) = 0
virtual voidSetPollTimeout ( unsigned int iPollTimeMS ) = 0
virtual RED_RCSetStatus ( RNET::PeerStatus iStatus, unsigned int iWorkLoad ) = 0
virtual RED_RCShutdown ( ) = 0
virtual RED_RCStart ( unsigned int iID, unsigned int iVersion, const RED::String & iWebServerPath, unsigned int iMaxConnections = 0, int iPort = 18000, const RNET::Address & iDispatcherAddress = RNET::Address() ) = 0

Public static functions:

static RED::CIDGetClassID ( )

Functions documentation

public virtual RED_RC RNET::IPeer::CloseConnection(intiConnectionID) = 0

Closes a connection with a peer.

If the connection does not exist or is already closed, RED_OK is returned. In the case of a peer to peer connection, any message that was sent but that wasn't emitted yet will be lost.

Parameters:

iConnectionIDID of the connection to close.

Returns:

RED_OK on success,
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::ConnectTo(const RNET::Address &iAddress) = 0

Connects to another REDnet peer.

The connection is postponed to next internal call to poll. Success or failure of the connection will be notified to the corresponding peer callback.

Parameters:

iAddressnetwork address of the peer to connect to.

Returns:

RED_OK on success,
RED_FAIL otherwise.
public virtual RNET::Address RNET::IPeer::GetConnectionAddress(intiConnection) = 0

Returns the network address of a connection.

If the connection is not valid anymore, an invalid address is returned.

Returns:

the network address of a connection.
public virtual RED_RC RNET::IPeer::GetConnectionsIDList(RED::Vector< int > &oConnectionsID) const = 0

Gets the list of active connections ID.

A connection may still be active while being closed.

Parameters:

oConnectionsIDlist of active connections ID.

Returns:

RED_OK on success,
RED_ALLOC_FAILURE on a memory allocation error,
RED_FAIL otherwise.
public virtual unsigned int RNET::IPeer::GetID() const = 0

Returns:

the ID of the peer.
public virtual int RNET::IPeer::GetMaxConnections() const = 0

Returns:

the maximum number of allowed connections (or 0 for unlimited number).
public virtual int RNET::IPeer::GetPort() const = 0

Returns:

the network communication port.
public virtual unsigned int RNET::IPeer::GetVersion() const = 0

Returns:

the version of the peer.
public virtual const RED::String & RNET::IPeer::GetWebServerPath() const = 0

Returns:

the path to additional web server files.
public virtual bool RNET::IPeer::IsConnectedToDispatcher() const = 0

Returns:

true if the peer is connected to a dispatcher, false otherwise.
public virtual bool RNET::IPeer::IsConnectionAlive(intiConnection) = 0

Returns true if the input connection is alive, false otherwise.

An alive connection is a connection which is not closed (or being closed).

Returns:

true if the input connection is alive, false otherwise.
public virtual RED_RC RNET::IPeer::SendImage(unsigned int &oSentBytes,
float &ioQuality,
intiConnectionID,
RED::Object *iImage,
unsigned intiMaxFrameSize = 102400
) = 0

Sends a RED image through a connection.

Depending on the value of the ioQuality parameter, the image is compressed using lossy or lossless compression algorithms. 1 stands for full quality (lossless) and 0 stands for the lowest image quality (lossy).

Compression is performed using either JPEG or PNG formats depending on the value of ioQuality.

The iMaxFrameSize parameter lets you control the final size of the compressed image. It takes over the ioQuality parameter. Hence, if the compressed image size at the requested quality is larger than the value of iMaxFrameSize, the quality is decreased and compression is performed again until it satisfies the requirements. The value of the modified quality parameter is then returned in in ioQuality. If iMaxFrameSize equals to 0, the image is compressed targeting only the ioQuality value (i.e ignoring the value of iMaxFrameSize) which is then returned unchanged.

The format of the image must be one of: RED::FMT_RGB or RED::FMT_RGBA. Note that if an alpha channel is present, it is discarded before sending the image.

The local content of iImage may become invalid after the call. If you stored a pointer to the local storage of the image before calling RNET::IPeer::SendImage, you need to call GetPixels and GetLocalPixels again to update it after the call.

Due to optional compression and data re-encoding, the actual number of sent bytes may be bigger or lower than the original size of the data. oSentBytes contains the real number of bytes sent over the network.

Due to delayed network orders processing, the call may return with the RED_OK value even if the data have not been sent immediatly. Therefore, RED_OK means that the order has been successfully buffered.

Parameters:

oSentBytesnumber of bytes sent by the call. If an error occured, this value may be undefined.
ioQualityquality of the compressed image in [0, 1]. If the quality is modified by the call, the modified value is returned in ioQuality.
iConnectionIDID of the connection to use.
iImagepointer to the image to send.
iMaxFrameSizemaximum allowed size in bytes of a compressed image. ioQuality may be adjusted on-the-fly by the method to satisfy the size constraint. Default is 100Kb.

Returns:

RED_OK on success,
RED_BAD_PARAM on an invalid input parameter,
RED_NET_SEND_FAIL if the image has not been send,
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::SendImage(unsigned int &oSentBytes,
float &ioQuality,
const RED::Vector< int > &iConnectionIDs,
RED::Object *iImage,
unsigned intiMaxFrameSize = 102400
) = 0

Sends the same RED image through several connections.

This method lets you perform simple broadcasting of an image to several peers at the same time.

The input image is processed once for all connections. The multiple send operations add only a small processing overhead to the call compared to the other SendImage method. However, there is a network overhead which scales linearly with the number of transmissions.

The method does not check for connections redundancy in the input list. Therefore, if a connection is present multiple times in the list, it will be used as many times to transmit the image.

For more details about the method behavior, please have a look to the documentation of the single connection implementation of SendImage.

Parameters:

oSentBytesnumber of bytes sent by the call. If an error occured, this value may be undefined.
ioQualityquality of the compressed image in [0, 1]. If the quality is modified by the call, the modified value is returned in ioQuality.
iConnectionIDslist of the ID of the connections to use.
iImagepointer to the image to send.
iMaxFrameSizemaximum allowed size in bytes of a compressed image. ioQuality may be adjusted on-the-fly by the method to satisfy the size constraint. Default is 100Kb.

Returns:

RED_OK on success,
RED_BAD_PARAM on an invalid input parameter,
RED_NET_SEND_FAIL if the image has not been send,
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::SendMessage(intiConnectionID,
RNET::MessageTypeiType,
const char *iData,
unsigned intiDataSize
) = 0

Sends a message through a connection.

Parameters:

iConnectionIDID of the connection to use.
iTypemessage type.
iDatapointer to the optional message data to send.
iDataSizesize of the optional message data to send.

Returns:

RED_OK on success,
RED_BAD_PARAM on an invalid input parameter,
RED_NET_SEND_FAIL if the image has not been send,
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::SendString(intiConnectionID,
const RED::String &iString
) = 0

Sends a RED string through a connection.

Due to delayed network orders processing, the call may return with the RED_OK value even if the data have not been sent immediatly. Therefore, RED_OK means that the order has been successfully buffered.

Parameters:

iConnectionIDID of the connection to use.
iStringthe string to send.

Returns:

RED_OK on success,
RED_BAD_PARAM on an invalid input parameter,
RED_NET_SEND_FAIL if the image has not been send,
RED_FAIL otherwise.
public virtual void RNET::IPeer::SetOnConnectionClosed(ON_CONNECTION_CLOSED_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on connection closed' callback.

The callback is called when a connection over the peer communication port is being closed. The reference to the connection must not be used any further after returning from the callback as the object won't reference something valid anymore.

Please, note that this does not include connections with the dispatcher (which is maintained separatly). To know if the peer is connected or not to a dispatcher, call IsConnectedToDispatcher().

Parameters:

iCallbackpointer to the callback to be called when connections get closed.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetOnConnectionReady(ON_CONNECTION_READY_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on connection ready' callback.

The callback is called once a connection has been successfully established over the peer communication port with another machine. Hence, a discarded connection request (due for example to a maximum simultaneous number of connections reached) won't result in a call to the callback.

Parameters:

iCallbackpointer to the callback to be called on new accepted input connections.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetOnDataReceived(ON_DATA_RECEIVED_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on data received' callback.

The callback is called each time data are available from a connection.

Parameters:

iCallbackpointer to the callback to be called when data are received.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetOnDataRequest(ON_DATA_REQUEST_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on data request' callback.

The callback is called each time data are requested by a client. Because each peer acts as a web server, data can be requested by clients such as files, cgi...

Parameters:

iCallbackpointer to the callback to be called when data are received.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetOnPoll(ON_POLL_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on poll' callback.

When set, this function is automatically called each time REDnet performs polling.

This is the perfect place to do your own network management code because you're guaranteed that no other REDnet network operation is ran conccurently.

Parameters:

iCallbackpointer to the on poll callback.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetOnShutdown(ON_SHUTDOWN_CALLBACKiCallback,
void *iUserData
) = 0

Sets the 'on shutdown' callback.

When set, this function is automatically called on peer shutdown.

The callback is called right before the peer is actually shutdown. A last polling operation is performed after returning from this method (however, during that polling, the ON_POLL user callback is not called anymore). Hence, this is the prefered place to send last minute information to connections.

Parameters:

iCallbackpointer to the on shutdown callback.
iUserDatacustom user pointer to additional data to be passed to the callback.
public virtual void RNET::IPeer::SetPollTimeout(unsigned intiPollTimeMS) = 0

Sets the polling timeout.

This method can be set to specify the polling timeout of the peer. By default the peer has a zero timeout, so that it polls actively from its networking thread. Increasing this value will slow down the polling frequency of the peer.

Parameters:

iPollTimeMS:Polling timeout value in milliseconds.
public virtual RED_RC RNET::IPeer::SetStatus(RNET::PeerStatusiStatus,
unsigned intiWorkLoad
) = 0

Informs the dispatcher about the current peer status.

If the peer is not connected to any dispatcher, the call returns immediatly with RED_OK. Otherwise, the passed status is sent to the dispatcher.

Parameters:

iStatuscurrent peer status.
iWorkLoadcurrent work load (in [0, 100]).

Returns:

RED_OK on success,
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::Shutdown() = 0

Shutdowns a REDnet peer.

If you call Start twice, Shutdown is automatically called in-between. During shutdown, all pending sent messages are flushed and effectively transmitted before the shutdown. You'll get called by the RNET::ON_POLL_CALLBACK callback if set.

Returns:

RED_OK on success (or if the peer has already stopped),
RED_FAIL otherwise.
public virtual RED_RC RNET::IPeer::Start(unsigned intiID,
unsigned intiVersion,
const RED::String &iWebServerPath,
unsigned intiMaxConnections = 0,
intiPort = 18000,
const RNET::Address &iDispatcherAddress = RNET::Address()
) = 0

Starts a new REDnet peer.

Each call to Start internally begins with a call to Shutdown, meaning that every existing communications will be closed.

Parameters:

iIDunique ID of the peer (used to identify peers over the network).
iVersionversion of the peer.
iWebServerPathpath to additional data needed by peers running as web servers.
iMaxConnectionsmaximum connections allowed to the peer in [0, +inf]. 0 means that the number of connections is not limited (default is 0).
iPortnet port that will be used for network communications (default is 18000).
iDispatcherAddressnetwork address of the dispatcher (default is undefined).

Returns:

RED_OK on success (or if the peer has already started),
RED_BAD_PARAM on an invalid port number,
RED_NET_CANT_CREATE_SOCKET if the socket can't be initialized,
RED_NET_CANT_RESOLVE_DESTINATION if the destination address can't be resolved,
RED_FAIL otherwise.