plptools
Loading...
Searching...
No Matches
NCPSession Class Reference

Responsible for orchestrating the high-level life cycle of a daemon-side NCP server and multiplexing connections over a single hardware comms channel (serial port, etc). More...

#include <ncpsession.h>

Collaboration diagram for NCPSession:

Public Member Functions

 NCPSession (int portNumber, int baudRate, std::string host, std::string serialDevice, bool autoexit, bool noDSRCheck, unsigned short nverbose, NCPStatusCallback statusCallback=nullptr, void *callbackContext=nullptr)
 
 NCPSession (const NCPSession &)=delete
 
NCPSessionoperator= (const NCPSession &)=delete
 
 ~NCPSession ()
 
int start ()
 Creates and manages all the threads necessary to run a full session for communicating with a Psion and exposing that to clients via the a TCP socket. More...
 
void cancel ()
 Mark the session as cancelled. More...
 
void wait ()
 Wait for the session to terminate. More...
 

Private Member Functions

bool isCancelled ()
 

Private Attributes

int portNumber_
 
int baudRate_
 
std::string host_
 
std::string serialDevice_
 
bool autoexit_
 
bool noDSRCheck_
 
unsigned short nverbose_
 
NCPStatusCallback statusCallback_
 
void * callbackContext_
 
pthread_t sessionMainThreadId_ = 0
 
NCPncp_ = nullptr
 NCP instance. More...
 
IOWatch socketChannelWatch_
 Used to watch all active SocketChannel instances (stored in socketChannels_) to see if they're readable. More...
 
TCPSocket skt_
 
std::mutex socketChannelLock_
 
std::vector< SocketChannel * > socketChannels_
 
int cancellationPipe_ [2] = { -1, -1 }
 

Friends

void * ncp_session_main_thread (void *arg)
 
void * link_thread (void *arg)
 
void * socket_connection_polling_thread (void *arg)
 Responsible for driving the SocketChannel instances (incoming TCP connections) by means of SocketChannel::socketPoll. More...
 
void check_for_new_socket_connection (NCPSession *session)
 

Detailed Description

Responsible for orchestrating the high-level life cycle of a daemon-side NCP server and multiplexing connections over a single hardware comms channel (serial port, etc).

Creates and manages three threads (ncp_session_main_thread, link_thread, and socket_connection_polling_thread) that drive the serial ports, accept incoming TCP connections from clients, and poll connected TCP sockets.

Definition at line 42 of file ncpsession.h.

Constructor & Destructor Documentation

◆ NCPSession() [1/2]

NCPSession::NCPSession ( int  portNumber,
int  baudRate,
std::string  host,
std::string  serialDevice,
bool  autoexit,
bool  noDSRCheck,
unsigned short  nverbose,
NCPStatusCallback  statusCallback = nullptr,
void *  callbackContext = nullptr 
)
inline

Definition at line 45 of file ncpsession.h.

◆ NCPSession() [2/2]

NCPSession::NCPSession ( const NCPSession )
delete

◆ ~NCPSession()

NCPSession::~NCPSession ( )

Definition at line 218 of file ncpsession.cc.

Member Function Documentation

◆ cancel()

void NCPSession::cancel ( )

Mark the session as cancelled.

It is anticipated that this method be called from within an interrupt handler in CLI apps. When using cancel to initiate session shutdown, it should be paired with wait.

Definition at line 235 of file ncpsession.cc.

◆ isCancelled()

bool NCPSession::isCancelled ( )
private

Definition at line 240 of file ncpsession.cc.

◆ operator=()

NCPSession & NCPSession::operator= ( const NCPSession )
delete

◆ start()

int NCPSession::start ( )

Creates and manages all the threads necessary to run a full session for communicating with a Psion and exposing that to clients via the a TCP socket.

This is a non-blocking function. The session should be stopped by calling stop or cancelling the session using cancel interrupting the session thread with SIGINT.

Definition at line 225 of file ncpsession.cc.

◆ wait()

void NCPSession::wait ( )

Wait for the session to terminate.

A typical usage pattern might call stop, followed by, wait to ensure the session is fully terminated and cleaned up before doing further work (e.g., starting a new session with a different configuration).

Definition at line 251 of file ncpsession.cc.

Friends And Related Function Documentation

◆ check_for_new_socket_connection

void check_for_new_socket_connection ( NCPSession session)
friend

Definition at line 123 of file ncpsession.cc.

◆ link_thread

void * link_thread ( void *  arg)
friend
Todo:
There's something really nuanced going on with the use of the NCPSession::socketChannelWatch_ here. Specifically, while it might look like it's just being used as a timeout (which might be the intent), new TCP sockets are added to it on a successful accept, meaning that this will wake up frequently whenever there's activity on the socket. This will cause frequent NCP::hasFailed checks, and very timely resets (NCP::reset) whenever a client is connected. It's possible this was introduced as a work-around to connectivity issues. It is also worth noting that IOWatch is not thread-safe, so using it in link_thread, and adding to it in ncp_session_main_thread (as we are doing) is definitely a bad thing.

The NCP::reset call here is currently required (even though it feels like it shouldn't be) as it's responsible for preparing the stack after a successful connection has ended (DataLink currently has responsibility for performing internal resets when auto-detecting baud rate).

Definition at line 51 of file ncpsession.cc.

◆ ncp_session_main_thread

void * ncp_session_main_thread ( void *  arg)
friend

Definition at line 171 of file ncpsession.cc.

◆ socket_connection_polling_thread

void * socket_connection_polling_thread ( void *  arg)
friend

Responsible for driving the SocketChannel instances (incoming TCP connections) by means of SocketChannel::socketPoll.

This isn't likely to scale particularly well as it polls all connected sockets whenever a single one wakes up, but it seems to work (as we never have that many connected clients).

Definition at line 78 of file ncpsession.cc.

Member Data Documentation

◆ autoexit_

bool NCPSession::autoexit_
private

Definition at line 111 of file ncpsession.h.

◆ baudRate_

int NCPSession::baudRate_
private

Definition at line 108 of file ncpsession.h.

◆ callbackContext_

void* NCPSession::callbackContext_
private

Definition at line 115 of file ncpsession.h.

◆ cancellationPipe_

int NCPSession::cancellationPipe_[2] = { -1, -1 }
private

Definition at line 134 of file ncpsession.h.

◆ host_

std::string NCPSession::host_
private

Definition at line 109 of file ncpsession.h.

◆ ncp_

NCP* NCPSession::ncp_ = nullptr
private

NCP instance.

Definition at line 124 of file ncpsession.h.

◆ noDSRCheck_

bool NCPSession::noDSRCheck_
private

Definition at line 112 of file ncpsession.h.

◆ nverbose_

unsigned short NCPSession::nverbose_
private

Definition at line 113 of file ncpsession.h.

◆ portNumber_

int NCPSession::portNumber_
private

Definition at line 107 of file ncpsession.h.

◆ serialDevice_

std::string NCPSession::serialDevice_
private

Definition at line 110 of file ncpsession.h.

◆ sessionMainThreadId_

pthread_t NCPSession::sessionMainThreadId_ = 0
private

Definition at line 119 of file ncpsession.h.

◆ skt_

TCPSocket NCPSession::skt_
private

Definition at line 131 of file ncpsession.h.

◆ socketChannelLock_

std::mutex NCPSession::socketChannelLock_
private

Definition at line 132 of file ncpsession.h.

◆ socketChannels_

std::vector<SocketChannel *> NCPSession::socketChannels_
private

Definition at line 133 of file ncpsession.h.

◆ socketChannelWatch_

IOWatch NCPSession::socketChannelWatch_
private

Used to watch all active SocketChannel instances (stored in socketChannels_) to see if they're readable.

Definition at line 129 of file ncpsession.h.

◆ statusCallback_

NCPStatusCallback NCPSession::statusCallback_
private

Definition at line 114 of file ncpsession.h.


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