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 <ncp_session.h>

Collaboration diagram for NCPSession:

Public Member Functions

 NCPSession (int portNumber, int baudRate, std::string host, std::string serialDevice, bool autoexit, unsigned short nverbose)
 
 ~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_
 
unsigned short nverbose_
 
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 41 of file ncp_session.h.

Constructor & Destructor Documentation

◆ NCPSession()

NCPSession::NCPSession ( int  portNumber,
int  baudRate,
std::string  host,
std::string  serialDevice,
bool  autoexit,
unsigned short  nverbose 
)
inline

Definition at line 44 of file ncp_session.h.

◆ ~NCPSession()

NCPSession::~NCPSession ( )

Definition at line 212 of file ncp_session.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 229 of file ncp_session.cc.

◆ isCancelled()

bool NCPSession::isCancelled ( )
private

Definition at line 234 of file ncp_session.cc.

◆ 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 219 of file ncp_session.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 245 of file ncp_session.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 ncp_session.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 (packet currently has responsibility for performing internal resets when auto-detecting baud rate).

Definition at line 51 of file ncp_session.cc.

◆ ncp_session_main_thread

void * ncp_session_main_thread ( void *  arg)
friend

Definition at line 171 of file ncp_session.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 ncp_session.cc.

Member Data Documentation

◆ autoexit_

bool NCPSession::autoexit_
private

Definition at line 101 of file ncp_session.h.

◆ baudRate_

int NCPSession::baudRate_
private

Definition at line 98 of file ncp_session.h.

◆ cancellationPipe_

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

Definition at line 121 of file ncp_session.h.

◆ host_

std::string NCPSession::host_
private

Definition at line 99 of file ncp_session.h.

◆ ncp_

NCP* NCPSession::ncp_ = nullptr
private

NCP instance.

Definition at line 111 of file ncp_session.h.

◆ nverbose_

unsigned short NCPSession::nverbose_
private

Definition at line 102 of file ncp_session.h.

◆ portNumber_

int NCPSession::portNumber_
private

Definition at line 97 of file ncp_session.h.

◆ serialDevice_

std::string NCPSession::serialDevice_
private

Definition at line 100 of file ncp_session.h.

◆ sessionMainThreadId_

pthread_t NCPSession::sessionMainThreadId_ = 0
private

Definition at line 106 of file ncp_session.h.

◆ skt_

TCPSocket NCPSession::skt_
private

Definition at line 118 of file ncp_session.h.

◆ socketChannelLock_

std::mutex NCPSession::socketChannelLock_
private

Definition at line 119 of file ncp_session.h.

◆ socketChannels_

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

Definition at line 120 of file ncp_session.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 116 of file ncp_session.h.


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