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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
* The Unix Channel
*
* by Michel Beaudouin-Lafon
*
* Copyright 1990-1993
* Laboratoire de Recherche en Informatique (LRI)
*
* Servers
*
* $Id$
* $CurLog$
*/
#include "Server.h"
/*?class UchServer
The class \typ{UchServer} derives from \typ{UchStream}.
It is associated to a channel set to monitor the connected clients in parallel.
Whenever a new client connects to the server, the virtual function \fun{HandleNew} is called.
This function must return a pointer to an object of class \typ{UchClient} (or a derived class of \typ{UchClient}).
Because the server and the clients may be on different machines with different architectures,
the byte swapping is always done by the server, by convention. (NOTE: byte swapping is not currently implemented)
?*/
/*?class UchClient
Class \typ{UchClient} derives from \typ{UchMsgStream}.
It is still an abstract class because the virtual functions \fun{NewMessage} and \fun{ConvertAnswer}
of \typ{UchMsgStream} are not defined in \typ{UchClient}.
Instances of \fun{UchClient} only know that they may belong to an instance of \typ{UchServer}.
Thus the virtual function \fun{Delete} is redefined to achieve a clean removal from the server's list.
?*/
// server ports
// constructors,
// destructor
// copy
//
/*?
Construct an empty server port.
?*/
UchServer :: UchServer ()
: UchStream (),
Clients (),
Mpx (0)
{
}
/*?nodoc?*/
UchServer :: UchServer (const UchServer& sp)
: UchStream (sp),
Clients (),
Mpx (0)
{
}
/*?
Construct a server port bound to address \var{a}.
When using Internet domain addresses, \var{a} should refer to
the wildcard address so that clients can connect from any machine.
This can be done with the instruction \com{new UchInetAddress(ANYADDR)}.
?*/
UchServer :: UchServer (UchAddress* a)
: UchStream (a, 0),
Clients (),
Mpx (0)
{
}
/*?nodoc?*/
UchServer :: ~UchServer ()
{
UchClient* cl = 0;
while (cl = Clients.First ())
cl->Delete ();
Mpx = 0; // will delete it if necessary
}
/*?nodoc?*/
UchChannel*
UchServer :: Copy () const
{
return new UchServer (*this);
}
/*?
Remove a client from the set of clients connected to the server.
This is normally done automatically whenever the client disconnects itself,
as a side effect of destroying the client.
?*/
void
UchServer :: RemoveClient (UchClient* cl)
{
if (Clients.Remove (cl)) {
cl->MyServer = 0;
HandleRemove (cl);
} else
Error (ErrWarn, "UchServer::RemoveClient", "not owned by this server");
}
/*?
Bind the socket and listen to it.
Initialize the channel set of the server to \var{cs}, or create a new one if \var{cs} is 0.
The channel set is used by the server to read and process messages from its clients:
whenever a client connects to the server, an instance of \typ{UchClient} is added to the channel set.
Whenever data is readable from a client, it is read in an input buffer and processed as soon
as a full message is received.
Whenever a client closes the connection, it is removed from the channel set of the server.
This function returns TRUE if all went well, else it calls \fun{SysError} and returns FALSE.
You may want to pass your own channel set if you are already multiplexing
i/o on that channel set. For instance, you may have several \typ{UchServer} objects
in your application.
A smart pointer to the channel set is actually kept in the server, so that it can be shared
securely.
?*/
bool
UchServer :: Setup (UchBaseMultiplexer* cs)
{
if (Listen () < 0) {
SysError (ErrWarn, "UchServer::Setup");
return FALSE;
}
SetMode (IORead);
if (! cs)
cs = new UchMultiplexer;
Mpx = cs;
Mpx->Add (this);
return TRUE;
}
/*?
This function removes the server from its channel set.
This has the effect of ignoring any incoming connections.
This function will also make \fun{Run} exit, if there is no other channel in the channel set of the server.
Unless you added your own channels to this channel set,
this will be the case if there is no client currently connected.
This is the only way to exit properly from \fun{Run}.
?*/
void
UchServer :: Unlisten ()
{
if (Mpx)
Mpx->Remove (*this);
}
// handle connections on server port
//
/*?nodoc?*/
void
UchServer :: HandleRead ()
{
// cannot redefine Accept to return UchClient*, so this does not work :
// UchClient* cl = Accept ();
// need to delete the channel created by Accept, so this does not work either :
// UchClient* cl = new UchClient (Accept ());
// so we do this (less elegant ...) :
UchChannel* ch = Accept ();
if (! ch) {
SysError (ErrWarn, "UchServer::HandleRead: Accept");
return;
}
UchClient* cl = HandleNew (ch);
delete ch;
cl->SetMode (IOReadSelect);
cl->SetOwner (this);
Clients.Append (cl);
if (Mpx)
Mpx->Add (cl);
}
// Default virtual function for handling new connections
// If a class MY_CLIENT is derived from UchClient,
// a class MY_SERVER must be derived from UchServer,
// with MY_SERVER::HandleNew (UchChannel* ch)
// doing at least a return new MY_CLIENT (ch)
//
/*?
This virtual function is called whenever a new client connects to the server.
The default action is to return \com{new UchClient(ch)}.
This needs to be redefined only if you create a derived class of \typ{UchClient},
say \typ{MY\_CLIENT}, in which case it should be redefined in a derived class
of \typ{UchServer} to return \com{new MY\_CLIENT(ch)}.
?*/
UchClient*
UchServer :: HandleNew (UchChannel* ch)
{
return new UchClient (ch);
}
/*?
This virtual function is called whenever a client is removed (\fun{RemoveClient}).
It is a hook for the application to take whatever action; the default action is to do nothing.
When this function is called, the client is already removed from the client list of the server.
?*/
void
UchServer :: HandleRemove (UchClient*)
{
// nothing
}
// default error functions
//
/*?nextdoc?*/
bool
UchServer :: SysError (errtype how, const char* who, int excl1, int excl2)
{
return ::SysError (how, who, excl1, excl2);
}
/*?
Each server port class can have its own error handling routines.
Their default action is to call the global functions \fun{Error} and \fun{SysError}.
They can be called from the following functions:
\fun{Error} can be called from
\fun{RemoveClient} when the client does not belong to this server;
\fun{SysError} can be called from
\fun{Setup} when the socket could not be setup correctly,
and from \fun{HandleRead}
when the connection could not be accepted.
?*/
void
UchServer :: Error (errtype how, const char* who, const char* what)
{
::Error (how, who, what);
}
/*?
Setup the server if necessary, then scan and process its channel set
by calling \fun{LoopScan} for it.
To have a server active, you need at least to initialize it (and thus know its address),
and then run it.
If you have added your own channels to the channel set of the server,
their \fun{HandleRead} and \fun{HandleWrite} functions will be called normally by \fun{Run}.
?*/
MPX_RES
UchServer :: Run ()
{
if (! Mpx)
if (! Setup ())
Error (ErrFatal, "UchServer::Run", "could not setup");
if (Mpx)
return Mpx->Run ();
else
return isMpxError;
}
/*?
Send a message to all clients currently connected to this server.
If \var{flush} is TRUE, the output buffer of each client is flushed.
?*/
void
UchServer :: Broadcast (UchMessage& msg, bool flush)
{
#ifndef CPLUS_BUG19
CcuListIterOf <UchClient> li (Clients);
while (++li)
(*li)->Send (msg, flush);
#else
CcuListIter li (Clients);
while (++li)
((UchClient*)(*li))->Send (msg, flush);
#endif
}
/*?
Send a message to all clients currently connected to this server, except \var{exclude}.
If \var{flush} is TRUE, the output buffer of each client is flushed.
?*/
void
UchServer :: Broadcast (UchMessage& msg, UchClient* excl, bool flush)
{
#ifndef CPLUS_BUG19
CcuListIterOf <UchClient> li (Clients);
while (++li)
if (*li != excl)
(*li)->Send (msg, flush);
#else
CcuListIter li (Clients);
while (++li) {
UchClient* a = (UchClient*)(*li);
if (a != excl)
a->Send (msg, flush);
}
#endif
}
/*?nextdoc?*/
void
UchServer :: Broadcast (UchMsgBuffer& buf, bool flush)
{
#ifndef CPLUS_BUG19
CcuListIterOf <UchClient> li (Clients);
while (++li)
(*li)->Send (buf, flush);
#else
CcuListIter li (Clients);
while (++li)
((UchClient*)(*li))->Send (buf, flush);
#endif
}
/*?
These functions are similar to the previous ones, except that they take
a buffer instead of a message. The buffer {\em must} contain a converted message.
It is more efficient to broadcast a buffer than a message because there is less
message conversion overhead.
?*/
void
UchServer :: Broadcast (UchMsgBuffer& buf, UchClient* excl, bool flush)
{
#ifndef CPLUS_BUG19
CcuListIterOf <UchClient> li (Clients);
while (++li)
if (*li != excl)
(*li)->Send (buf, flush);
#else
CcuListIter li (Clients);
while (++li) {
UchClient* a = (UchClient*)(*li);
if (a != excl)
a->Send (buf, flush);
}
#endif
}
// clients:
//
/*?nodoc?*/
UchClient :: UchClient ()
: UchMsgStream (),
MyServer (0)
{
}
/*?nodoc?*/
UchClient :: UchClient (const UchClient& cl)
: UchMsgStream (cl),
MyServer (0)
{
}
/*?
Construct a new client connected to the server on channel \var{ch}.
?*/
UchClient :: UchClient (UchChannel* ch)
: UchMsgStream (),
MyServer (0)
{
UchChannel::Open (ch->FilDes ());
}
/*?nodoc?*/
UchClient :: ~UchClient ()
{
if (MyServer) {
UchServer* s = MyServer;
s->Error (ErrWarn, "~UchClient", "client still in a server; deleting anyway ...\n");
s->RemoveClient (this);
if (s->Mpx)
s->Mpx->Remove (*this); // calls the destructor if no more refs
}
}
/*?nodoc?*/
UchChannel*
UchClient :: Copy () const
{
return new UchClient (*this);
}
/*?
This function must be used to delete a client explicitly.
It is not safe to use the operator delete.
This function is called when an end of file is read from the client;
this means that you usually do not need to call it.
?*/
void
UchClient :: Delete ()
{
if (MyServer) {
UchServer* s = MyServer;
s->RemoveClient (this);
if (s->Mpx)
s->Mpx->Remove (*this); // calls the destructor if no more refs
}
}
/*?nodoc?*/
void
UchClient :: SetOwner (UchServer* serv)
{
if (MyServer)
serv->Error (ErrFatal, "UchClient::SetOwner", "already owned");
else
MyServer = serv;
}
#ifdef DOC
//fake entries for inline functions
/*?
Return the channel set used by the server.
This is useful if you want to add your own channels to the channel set.
?*/
UchBaseMultiplexer*
UchServer :: GetMultiplexer ()
{
}
/*?
Return the server corresponding to a given client.
?*/
UchServer*
UchClient :: GetServer ()
{
}
#endif /* DOC */
|