plptools
Loading...
Searching...
No Matches
rclip.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
5 * Copyright (C) 2026 Jason Morley <hello@jbmorley.co.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21#include "config.h"
22
23#include "rclip.h"
24
25#include "bufferarray.h"
26#include "bufferstore.h"
27#include "Enum.h"
28#include "tcpsocket.h"
29
30#include <stdlib.h>
31#include <time.h>
32
33rclip* rclip::connect(const std::string &host, int port, Enum<ConnectionError> *error) {
34
35 if (error) {
37 }
38
39 auto socket = std::make_unique<TCPSocket>();
40 if (!socket->connect(host.c_str(), port)) {
41 if (error) {
43 }
44 return nullptr;
45 }
46
47 return new rclip(std::move(socket));
48}
49
50rclip::rclip(std::unique_ptr<TCPSocket> socket) {
51 socket_ = std::move(socket);
52 reset();
53}
54
56 socket_->closeSocket();
57}
58
59//
60// public common API
61//
62void rclip::reconnect(void) {
63 socket_->reconnect();
64 reset();
65}
66
67void rclip::reset(void) {
70 a.addStringT(getConnectName());
71 if (socket_->sendBufferStore(a)) {
72 if (socket_->getBufferStore(a) == 1) {
73 if (!strcmp(a.getString(0), "NAK")) {
75 }
76 if (!strcmp(a.getString(0), "Ok")) {
78 }
79 }
80 }
81}
82
84 return status;
85}
86
87const char *rclip::getConnectName(void) {
88 return "CLIPSVR.RSY";
89}
90
91//
92// protected internals
93//
96 reconnect();
98 return false;
99 }
101 return false;
102
103 bool result;
105 a.addByte(cc);
106 switch (cc) {
107 case RCLIP_INIT:
108 a.addWord(0x100);
109 break;
110 case RCLIP_NOTIFY:
111 a.addByte(0);
112 }
113 result = socket_->sendBufferStore(a);
114 if (!result) {
115 reconnect();
116 result = socket_->sendBufferStore(a);
117 if (!result)
119 }
120 return result;
121}
122
126 } else {
127 return status;
128 }
129}
130
134
135 int r = socket_->getBufferStore(a, false);
136 if (r < 0) {
138 } else {
139 if (r == 0) {
141 } else {
142 if ((a.getLen() != 1) || (a.getByte(0) != 0)) {
144 }
145 }
146 }
147 return ret;
148}
149
152
155 if ((ret = getResponse(a)) == RFSV::E_PSI_GEN_NONE) {
156 if ((a.getLen() != 1) || (a.getByte(0) != 0)) {
158 }
159 }
160 return ret;
161}
162
166
168 if ((ret = getResponse(a)) == RFSV::E_PSI_GEN_NONE) {
169 if ((a.getLen() != 1) || (a.getByte(0) != RCLIP_NOTIFY)) {
171 }
172 }
173 return ret;
174}
175
179
181 return status;
182 }
183
185 if ((ret = getResponse(a)) == RFSV::E_PSI_GEN_NONE) {
186 if ((a.getLen() != 3) || (a.getByte(0) != RCLIP_INIT) || (a.getWord(1) != 0x100)) {
188 }
189 }
190 return ret;
191}
192
195
197 return status;
198 }
199
200 if (socket_->getBufferStore(data) == 1) {
201 return ret;
202 } else {
204 }
205 return status;
206}
A generic container for an array of bytes.
Definition: bufferstore.h:36
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:135
@ E_PSI_GEN_FAIL
Definition: rfsv.h:115
@ E_PSI_GEN_NSUP
Definition: rfsv.h:118
@ E_PSI_FILE_DISC
Definition: rfsv.h:156
@ E_PSI_GEN_NONE
Definition: rfsv.h:114
@ E_PSI_FILE_EOF
Definition: rfsv.h:142
Remote ClipBoard services via PLP.
Definition: rclip.h:44
Enum< RFSV::errs > sendListen()
Send listen request.
Definition: rclip.cc:123
Enum< RFSV::errs > status
The current status of the connection.
Definition: rclip.h:159
Enum< RFSV::errs > checkNotify()
Check for clipboard notification.
Definition: rclip.cc:131
Enum< RFSV::errs > getStatus()
Retrieves the current status of the connection.
Definition: rclip.cc:83
rclip(std::unique_ptr< TCPSocket > socket)
Constructs a new rclip object.
Definition: rclip.cc:50
Enum< RFSV::errs > waitNotify()
Send listen request and wait for notification.
Definition: rclip.cc:150
bool sendCommand(enum commands cc)
Sends a command to the remote side.
Definition: rclip.cc:94
const char * getConnectName()
Definition: rclip.cc:87
Enum< RFSV::errs > getResponse(BufferStore &data)
Definition: rclip.cc:193
void reconnect()
Attempts to re-establish a remote connection by first closing the socket, then connecting again to th...
Definition: rclip.cc:62
~rclip()
Destructor.
Definition: rclip.cc:55
Enum< RFSV::errs > initClipbd()
Send initialization frame.
Definition: rclip.cc:176
commands
The possible commands.
Definition: rclip.h:144
@ RCLIP_LISTEN
Definition: rclip.h:147
@ RCLIP_INIT
Definition: rclip.h:145
@ RCLIP_NOTIFY
Definition: rclip.h:146
static rclip * connect(const std::string &host, int port, Enum< ConnectionError > *error=nullptr)
Definition: rclip.cc:33
Enum< RFSV::errs > notify()
Send a notification to the Psion.
Definition: rclip.cc:163
void reset()
Initializes a connection to the remote machine.
Definition: rclip.cc:67
std::unique_ptr< TCPSocket > socket_
The socket, used for communication with ncpd.
Definition: rclip.h:154
@ FACERR_NONE
@ FACERR_CONNECTION_FAILURE
static RPCS * r
Definition: main.cc:58
static RFSV * a
Definition: main.cc:55
static void error(int line)
Definition: sismain.cpp:44