SCR_NetworkSessionSocketMgrWrapper Class Reference

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

Public Slots

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

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 
)
slot

◆ bindNewPort

int SCR_NetworkSessionSocketMgrWrapper::bindNewPort ( )
slot

◆ boundPorts

QScriptValue SCR_NetworkSessionSocketMgrWrapper::boundPorts ( )
slot

◆ clientSocketConnected

bool SCR_NetworkSessionSocketMgrWrapper::clientSocketConnected ( int  clientSocketId)
slot

◆ closeAll

void SCR_NetworkSessionSocketMgrWrapper::closeAll ( )
slot

◆ flushReceivedMsgsBuffer

void SCR_NetworkSessionSocketMgrWrapper::flushReceivedMsgsBuffer ( int  clientSocketId)
slot

◆ receivedMsgs

QScriptValue SCR_NetworkSessionSocketMgrWrapper::receivedMsgs ( int  clientSocketId)
slot

◆ recv

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

◆ removeSocketConnection

void SCR_NetworkSessionSocketMgrWrapper::removeSocketConnection ( int  clientSocketId)
slot

◆ send

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

◆ unbindPort

void SCR_NetworkSessionSocketMgrWrapper::unbindPort ( int  port)
slot