SCR_NetworkSessionSocketMgrWrapper Class Reference

The SessionSocketMgr JavaScript class, creates and keeps sockets bounded throughout a session. More...

Public Slots

int bindNewPort () const
 
QScriptValue boundPorts ()
 
void unbindPort (int port) const
 
int acceptAtPort (int port, int timeoutMS, int msgBufferSize=20) const
 
bool clientSocketConnected (int clientSocketId) const
 
void removeSocketConnection (int clientSocketId) const
 
bool send (int clientSocketId, const String &msg, int timeoutMS=1000) const
 
bool recv (int clientSocketId, int timeoutMS) const
 
void flushReceivedMsgsBuffer (int clientSocketId) const
 
QScriptValue receivedMsgs (int clientSocketId) const
 
void closeAll () const
 

Detailed Description

The SessionSocketMgr JavaScript class, creates and keeps sockets bounded throughout a session.

SCR_NetworkSessionSocketMgrWrapper manages non-blocking socket servers (bound ports) which can exchange text messages with clients (connected ports). Messages are encoded in utf-8 and respect a protocol. (The protocol encapsulates messages between bytes 0x2 and 0x3 - the ASCII table bytes for start text and end text respectively.)

// Bind a new socket
var serverPort = SessionSocketMgr.bindNewPort();
var serverPortBound = false;
var serverPorts = SessionSocketMgr.boundPorts();
for(i=0; i<serverPorts.length; i++)
{
if (serverPorts[i] == serverPort)
{
serverPortBound = true;
break;
}
}
if (serverPortBound)
{
// Create a client to connect to the created socket
var client = new RemoteCmd();
if ( client.connect( "localhost", serverPort ) )
{
// Accept client, with a 5 second timeout and support a queue of 10 messages
clientSocketId = SessionSocketMgr.acceptAtPort(serverPort, 5000, 10);
if ( 0 < clientSocketId )
{
// Exchange message
MessageLog.trace("Connection established between client and server!");
var msg = "Message to server";
MessageLog.trace("Client will send to server: '" + msg + "'");
// Encapsulate the message
encMsg = '\2' + msg + '\3';
// Send it
client.send(encMsg)
if (SessionSocketMgr.recv(clientSocketId,5000)) // Wait 5 seconds
{
MessageLog.trace( "Server received: '" + SessionSocketMgr.lastReceivedMsg(clientSocketId) + "'");
}
else
{
MessageLog.trace("Client failed to receive message!");
}
}
else
{
MessageLog.trace("Client accept timeout!");
}
client.disconnect()
}
else
{
MessageLog.trace("Cannot connect to bound socket at port: " + serverPort);
}
}
else
{
MessageLog.trace("Cannot bind socket at port: " + serverPort);
}
// Close all bound sockets
SessionSocketMgr.removeSocketConnection(clientSocketId);

Member Function Documentation

◆ acceptAtPort

int SCR_NetworkSessionSocketMgrWrapper::acceptAtPort ( int  port,
int  timeoutMS,
int  msgBufferSize = 20 
) const
slot

◆ bindNewPort

int SCR_NetworkSessionSocketMgrWrapper::bindNewPort ( ) const
slot

◆ boundPorts

QScriptValue SCR_NetworkSessionSocketMgrWrapper::boundPorts ( )
slot

◆ clientSocketConnected

bool SCR_NetworkSessionSocketMgrWrapper::clientSocketConnected ( int  clientSocketId) const
slot

◆ closeAll

void SCR_NetworkSessionSocketMgrWrapper::closeAll ( ) const
slot

◆ flushReceivedMsgsBuffer

void SCR_NetworkSessionSocketMgrWrapper::flushReceivedMsgsBuffer ( int  clientSocketId) const
slot

◆ receivedMsgs

QScriptValue SCR_NetworkSessionSocketMgrWrapper::receivedMsgs ( int  clientSocketId) const
slot

◆ recv

bool SCR_NetworkSessionSocketMgrWrapper::recv ( int  clientSocketId,
int  timeoutMS 
) const
slot

◆ removeSocketConnection

void SCR_NetworkSessionSocketMgrWrapper::removeSocketConnection ( int  clientSocketId) const
slot

◆ send

bool SCR_NetworkSessionSocketMgrWrapper::send ( int  clientSocketId,
const String &  msg,
int  timeoutMS = 1000 
) const
slot

◆ unbindPort

void SCR_NetworkSessionSocketMgrWrapper::unbindPort ( int  port) const
slot