plptools
Loading...
Searching...
No Matches
ncpsession.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
6 * Copyright (C) 2026 Jason Morley <hello@jbmorley.co.uk>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22#pragma once
23
24#include "config.h"
25
26#include <mutex>
27#include <string>
28#include <bufferstore.h>
29#include <tcpsocket.h>
30#include <iowatch.h>
31
32#include "ncp.h"
33#include "ncpstatuscallback.h"
34#include "socketchannel.h"
35
43public:
44
45 NCPSession(int portNumber,
46 int baudRate,
47 std::string host,
48 std::string serialDevice,
49 bool autoexit,
50 bool noDSRCheck,
51 unsigned short nverbose,
52 NCPStatusCallback statusCallback = nullptr,
53 void *callbackContext = nullptr)
54 : portNumber_(portNumber)
55 , baudRate_(baudRate)
56 , host_(host)
57 , serialDevice_(serialDevice)
58 , autoexit_(autoexit)
59 , noDSRCheck_(noDSRCheck)
60 , nverbose_(nverbose)
61 , statusCallback_(statusCallback)
62 , callbackContext_(callbackContext) {}
63
64 NCPSession(const NCPSession&) = delete;
65 NCPSession& operator=(const NCPSession&) = delete;
66
68
76 int start();
77
84 void cancel();
85
92 void wait();
93
94private:
95
96 // Thread entry points and helpers.
97
98 friend void *ncp_session_main_thread(void *arg);
99 friend void *link_thread(void *arg);
100 friend void *socket_connection_polling_thread(void *arg);
101 friend void check_for_new_socket_connection(NCPSession *session);
102
103 bool isCancelled();
104
105 // Configuration.
106
109 std::string host_;
110 std::string serialDevice_;
113 unsigned short nverbose_;
116
117 // State.
118
119 pthread_t sessionMainThreadId_ = 0;
120
124 NCP *ncp_ = nullptr;
125
130
133 std::vector<SocketChannel *>socketChannels_;
134 int cancellationPipe_[2] = { -1, -1 };
135};
A simple thread-safe wrapper for select()
Definition: iowatch.h:34
Responsible for orchestrating the high-level life cycle of a daemon-side NCP server and multiplexing ...
Definition: ncpsession.h:42
IOWatch socketChannelWatch_
Used to watch all active SocketChannel instances (stored in socketChannels_) to see if they're readab...
Definition: ncpsession.h:129
int baudRate_
Definition: ncpsession.h:108
TCPSocket skt_
Definition: ncpsession.h:131
void * callbackContext_
Definition: ncpsession.h:115
int cancellationPipe_[2]
Definition: ncpsession.h:134
std::string host_
Definition: ncpsession.h:109
bool isCancelled()
Definition: ncpsession.cc:240
void cancel()
Mark the session as cancelled.
Definition: ncpsession.cc:235
std::string serialDevice_
Definition: ncpsession.h:110
int portNumber_
Definition: ncpsession.h:107
NCP * ncp_
NCP instance.
Definition: ncpsession.h:124
bool autoexit_
Definition: ncpsession.h:111
NCPStatusCallback statusCallback_
Definition: ncpsession.h:114
std::mutex socketChannelLock_
Definition: ncpsession.h:132
unsigned short nverbose_
Definition: ncpsession.h:113
pthread_t sessionMainThreadId_
Definition: ncpsession.h:119
void wait()
Wait for the session to terminate.
Definition: ncpsession.cc:251
friend void check_for_new_socket_connection(NCPSession *session)
Definition: ncpsession.cc:123
NCPSession & operator=(const NCPSession &)=delete
bool noDSRCheck_
Definition: ncpsession.h:112
NCPSession(int portNumber, int baudRate, std::string host, std::string serialDevice, bool autoexit, bool noDSRCheck, unsigned short nverbose, NCPStatusCallback statusCallback=nullptr, void *callbackContext=nullptr)
Definition: ncpsession.h:45
friend void * link_thread(void *arg)
Definition: ncpsession.cc:51
std::vector< SocketChannel * > socketChannels_
Definition: ncpsession.h:133
int start()
Creates and manages all the threads necessary to run a full session for communicating with a Psion an...
Definition: ncpsession.cc:225
friend void * socket_connection_polling_thread(void *arg)
Responsible for driving the SocketChannel instances (incoming TCP connections) by means of SocketChan...
Definition: ncpsession.cc:78
friend void * ncp_session_main_thread(void *arg)
Definition: ncpsession.cc:171
NCPSession(const NCPSession &)=delete
Definition: ncp.h:55
A class for dealing with sockets.
Definition: tcpsocket.h:37
void(* NCPStatusCallback)(void *context, bool isConnected, int protocolVersion)