Menu

[r3316]: / advanced / src / cpp / server_pstream_conn.cpp  Maximize  Restore  History

Download this file

365 lines (242 with data), 9.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*=============================================================================
server_pstream
===============================================================================
RPC server based on a very simple byte stream and XML-RPC XML
(But this is not an XML-RPC server because it doesn't use HTTP).
The protocol we use is the "packet socket" protocol, which
is an Xmlrpc-c invention. It is an almost trivial representation of
a sequence of packets on a byte stream.
You can create a pstream server from any file descriptor from which
you can read and write a bidirectional character stream. Typically,
it's a TCP socket. Such a server talks to one client its entire life.
Some day, we'll also have a version that you create from a "listening"
socket, which can talk to multiple clients serially (a client connects,
does some RPCs, and disconnects).
By Bryan Henderson 07.05.12.
Contributed to the public domain by its author.
=============================================================================*/
#include <memory>
#include "xmlrpc-c/girerr.hpp"
using girerr::throwf;
#include "xmlrpc-c/packetsocket.hpp"
#include "xmlrpc-c/util_int.h"
#include "xmlrpc-c/server_pstream.hpp"
using namespace std;
namespace xmlrpc_c {
struct serverPstreamConn::constrOpt_impl {
constrOpt_impl();
struct value {
xmlrpc_c::registryPtr registryPtr;
const xmlrpc_c::registry * registryP;
XMLRPC_SOCKET socketFd;
} value;
struct {
bool registryPtr;
bool registryP;
bool socketFd;
} present;
};
serverPstreamConn::constrOpt_impl::constrOpt_impl() {
this->present.socketFd = false;
this->present.registryP = false;
this->present.registryPtr = false;
}
serverPstreamConn::constrOpt::constrOpt() {
this->implP = new constrOpt_impl();
}
serverPstreamConn::constrOpt::~constrOpt() {
delete(this->implP);
}
#define DEFINE_OPTION_SETTER(OPTION_NAME, TYPE) \
serverPstreamConn::constrOpt & \
serverPstreamConn::constrOpt::OPTION_NAME(TYPE const& arg) { \
this->implP->value.OPTION_NAME = arg; \
this->implP->present.OPTION_NAME = true; \
return *this; \
}
DEFINE_OPTION_SETTER(socketFd, XMLRPC_SOCKET);
DEFINE_OPTION_SETTER(registryP, const registry *);
DEFINE_OPTION_SETTER(registryPtr, xmlrpc_c::registryPtr);
#undef DEFINE_OPTION_SETTER
struct serverPstreamConn_impl {
serverPstreamConn_impl(serverPstreamConn::constrOpt_impl const& opt);
~serverPstreamConn_impl();
void
establishRegistry(serverPstreamConn::constrOpt_impl const& opt);
void
establishPacketSocket(serverPstreamConn::constrOpt_impl const& opt);
void
processRecdPacket(packetPtr const callPacketP,
callInfo * const callInfoP);
// 'registryP' is what we actually use; 'registryHolder' just holds a
// reference to 'registryP' so the registry doesn't disappear while
// this server exists. But note that if the creator doesn't supply
// a registryPtr, 'registryHolder' is just a placeholder variable and
// the creator is responsible for making sure the registry doesn't
// go anywhere while the server exists.
registryPtr registryHolder;
const registry * registryP;
packetSocket * packetSocketP;
// The packet socket over which we received RPCs.
// This is permanently connected to our fixed client.
};
serverPstreamConn_impl::serverPstreamConn_impl(
serverPstreamConn::constrOpt_impl const& opt) {
this->establishRegistry(opt);
this->establishPacketSocket(opt);
}
serverPstreamConn_impl::~serverPstreamConn_impl() {
delete(this->packetSocketP);
}
void
serverPstreamConn_impl::establishRegistry(
serverPstreamConn::constrOpt_impl const& opt) {
if (!opt.present.registryP && !opt.present.registryPtr)
throwf("You must specify the 'registryP' or 'registryPtr' option");
else if (opt.present.registryP && opt.present.registryPtr)
throwf("You may not specify both the 'registryP' and "
"the 'registryPtr' options");
else {
if (opt.present.registryP)
this->registryP = opt.value.registryP;
else {
this->registryHolder = opt.value.registryPtr;
this->registryP = opt.value.registryPtr.get();
}
}
}
void
serverPstreamConn_impl::establishPacketSocket(
serverPstreamConn::constrOpt_impl const& opt) {
if (!opt.present.socketFd)
throwf("You must provide a 'socketFd' constructor option.");
UNIQUE_PTR<packetSocket> packetSocketAP;
try {
packetSocketAP.reset(new packetSocket(opt.value.socketFd));
} catch (exception const& e) {
throwf("Unable to create packet socket out of file descriptor %d. %s",
opt.value.socketFd, e.what());
}
this->packetSocketP = packetSocketAP.get();
packetSocketAP.release();
}
serverPstreamConn::serverPstreamConn(constrOpt const& opt) {
this->implP = new serverPstreamConn_impl(*opt.implP);
}
serverPstreamConn::~serverPstreamConn() {
delete(this->implP);
}
static void
processCall(const registry * const registryP,
packetPtr const& callPacketP,
callInfo * const callInfoP,
packetPtr * const responsePacketPP) {
string const callXml(reinterpret_cast<char *>(callPacketP->getBytes()),
callPacketP->getLength());
string responseXml;
registryP->processCall(callXml, callInfoP, &responseXml);
*responsePacketPP = packetPtr(new packet(responseXml.c_str(),
responseXml.length()));
}
void
serverPstreamConn_impl::processRecdPacket(packetPtr const callPacketP,
callInfo * const callInfoP) {
packetPtr responsePacketP;
try {
processCall(this->registryP, callPacketP, callInfoP, &responsePacketP);
} catch (exception const& e) {
throwf("Error executing received packet as an XML-RPC RPC. %s",
e.what());
}
try {
this->packetSocketP->writeWait(responsePacketP);
} catch (exception const& e) {
throwf("Failed to write the response to the packet socket. %s",
e.what());
}
}
void
serverPstreamConn::runOnce(callInfo * const callInfoP,
volatile const int * const interruptP,
bool * const eofP) {
/*----------------------------------------------------------------------------
Get and execute one RPC from the client.
Unless *interruptP gets set nonzero first.
-----------------------------------------------------------------------------*/
bool gotPacket;
packetPtr callPacketP;
try {
this->implP->packetSocketP->readWait(interruptP, eofP, &gotPacket,
&callPacketP);
} catch (exception const& e) {
throwf("Error reading a packet from the packet socket. %s",
e.what());
}
if (gotPacket)
this->implP->processRecdPacket(callPacketP, callInfoP);
}
void
serverPstreamConn::runOnce(volatile const int * const interruptP,
bool * const eofP) {
this->runOnce(NULL, interruptP, eofP);
}
void
serverPstreamConn::runOnce(bool * const eofP) {
/*----------------------------------------------------------------------------
Get and execute one RPC from the client.
-----------------------------------------------------------------------------*/
int const interrupt(0); // Never interrupt
this->runOnce(&interrupt, eofP);
}
void
serverPstreamConn::runOnceNoWait(callInfo * const callInfoP,
bool * const eofP,
bool * const didOneP) {
/*----------------------------------------------------------------------------
Get and execute one RPC from the client, unless none has been
received yet. Return as *didOneP whether or not one has been
received. Unless didOneP is NULL.
-----------------------------------------------------------------------------*/
bool gotPacket;
packetPtr callPacketP;
try {
this->implP->packetSocketP->read(eofP, &gotPacket, &callPacketP);
} catch (exception const& e) {
throwf("Error reading a packet from the packet socket. %s",
e.what());
}
if (gotPacket)
this->implP->processRecdPacket(callPacketP, callInfoP);
if (didOneP)
*didOneP = gotPacket;
}
void
serverPstreamConn::runOnceNoWait(bool * const eofP,
bool * const didOneP) {
this->runOnceNoWait(NULL, eofP, didOneP);
}
void
serverPstreamConn::runOnceNoWait(bool * const eofP) {
/*----------------------------------------------------------------------------
Get and execute one RPC from the client, unless none has been
received yet.
-----------------------------------------------------------------------------*/
this->runOnceNoWait(eofP, NULL);
}
void
serverPstreamConn::run(callInfo * const callInfoP,
volatile const int * const interruptP) {
for (bool clientHasDisconnected = false;
!clientHasDisconnected && !*interruptP;)
this->runOnce(callInfoP, interruptP, &clientHasDisconnected);
}
void
serverPstreamConn::run(volatile const int * const interruptP) {
this->run(NULL, interruptP);
}
void
serverPstreamConn::run() {
int const interrupt(0); // Never interrupt
this->run(&interrupt);
}
} // namespace
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.