-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.h
1717 lines (1336 loc) · 45.5 KB
/
server.h
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __SERVER_H
#define __SERVER_H
#include <poll.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <cassert>
#include <string>
#include <cstring>
#include <limits>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <memory>
#include <stdexcept>
#include "idpool.h"
#include "trunknode.h"
class TaskPool;
// These symbols will help us port the code to several Unix operating
// systems that we use. We're trying to keep the conditional code to a
// minimum. All maintainers should try to find the most portable
// solution before resorting to platform-specific solutions.
#define Linux_Target 1
#define Darwin_Target 2
#define NetBSD_Target 3
#define FreeBSD_Target 4
#define SunOS_Target 5
#if !defined(THIS_TARGET)
#error Missing THIS_TARGET definition.
#endif
// Determine platform endianess
#include <inttypes.h>
#if THIS_TARGET == SunOS_Target
#include <sys/byteorder.h>
#define BIG_ENDIAN 1234
#define LITTLE_ENDIAN 4321
#if defined(_BIG_ENDIAN)
#define BYTE_ORDER BIG_ENDIAN
#elif defined(_LITTLE_ENDIAN)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#elif (!defined(BYTE_ORDER) || !defined(BIG_ENDIAN))
#include <endian.h>
#endif
#if !defined(BYTE_ORDER) || !defined(BIG_ENDIAN)
#error Missing important endian-defining symbols -- compilation halted.
#endif
#if BYTE_ORDER == BIG_ENDIAN
inline uint16_t htoas(uint16_t v) throw()
{
return (v >> 8) | (v << 8);
}
inline uint32_t htoal(uint32_t v) throw()
{
return (v >> 24) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 24);
}
inline uint16_t atohs(uint16_t v) throw()
{
return (v >> 8) | (v << 8);
}
inline uint32_t atohl(uint32_t v) throw()
{
return (v >> 24) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 24);
}
#define be16(v) uint16_t(v)
#else
#define htoas(v) ((uint16_t) (v))
#define htoal(v) ((uint32_t) (v))
#define atohs(v) ((uint16_t) (v))
#define atohl(v) ((uint32_t) (v))
#define be16(v) (uint8_t(v >> 8) | uint16_t(v << 8))
#endif
#if THIS_TARGET == Darwin_Target
#define NO_DAEMON 1
#endif
// These version numbers store the major number in the upper byte and
// the minor in the lower byte. They're in hexadecimal so a version
// number like 0x915 means v9.21.
// Defines the version number of the on-the-wire layout of an ACNET
// packet. There hasn't been an official numbering system (each ACNET
// implementation seemed to choose their own versioning.) We picked
// this number because it was the largest of all the implementations.
#define ACNET_PROTOCOL 0x0915
// Defines the version of the internals of ACNET. This is a
// project-specific version number. For this project, it is the
// version of the source to acnetd. We've been lax at doing this so,
// after 15 years, we're just starting to be more formal about
// releasing new versions.
#define ACNET_INTERNALS 0x0103
// Defines the API that local clients use to communicate with ACNET so
// it's another project-specific version number. For acnetd, this is
// the version number of the command API we use over the local
// loopback socket. In MOOC, it would be the C language API.
#define ACNET_API 0x0900
// Project-wide types and functions
uint32_t ator(char const *);
char const* rtoa(uint32_t, char * = 0);
char const* rtoa_strip(uint32_t, char * = 0);
int64_t currentTimeMillis();
struct time48_t {
uint16_t t[3];
} __attribute__((packed));
class reqid_t {
uint16_t id_;
public:
explicit reqid_t() : id_(0) {}
explicit reqid_t(uint16_t id) : id_(id) {}
bool operator< (reqid_t const o) const { return id_ < o.id_; }
bool operator== (reqid_t const o) const { return id_ == o.id_; }
bool operator!= (reqid_t const o) const { return id_ != o.id_; }
uint16_t raw() const { return id_; }
};
class rpyid_t {
uint16_t id_;
public:
explicit rpyid_t() : id_(0) {}
explicit rpyid_t(uint16_t id) : id_(id) {}
bool operator< (rpyid_t const o) const { return id_ < o.id_; }
bool operator== (rpyid_t const o) const { return id_ == o.id_; }
bool operator!= (rpyid_t const o) const { return id_ != o.id_; }
uint16_t raw() const { return id_; }
};
class taskid_t {
uint16_t id_;
public:
explicit taskid_t(uint16_t id) : id_(id) {}
bool operator== (taskid_t const o) const { return id_ == o.id_; }
bool operator!= (taskid_t const o) const { return id_ != o.id_; }
uint16_t raw() const { return id_; }
};
class status_t {
int16_t s;
public:
status_t() : s(0) {}
status_t(int16_t f, int16_t e) : s(f + e * 256) {}
explicit status_t(int16_t const sts) : s(sts) {}
bool operator< (status_t const o) const { return s < o.s; }
bool operator== (status_t const o) const { return s == o.s; }
bool operator!= (status_t const o) const { return s != o.s; }
bool isFatal() const { return s < 0; }
int16_t raw() const { return s; }
};
class taskhandle_t {
uint32_t h;
public:
taskhandle_t() : h(0) {}
explicit taskhandle_t(uint32_t const handle) : h(handle) {}
bool operator< (taskhandle_t const o) const { return h < o.h; }
bool operator== (taskhandle_t const o) const { return h == o.h; }
bool operator!= (taskhandle_t const o) const { return h != o.h; }
bool isBlank() const { return h == 0; }
uint32_t raw() const { return h; }
char const* str(char *buf = 0) { return rtoa_strip(h, buf); }
};
class nodename_t {
uint32_t h;
public:
nodename_t() : h(0) {}
explicit nodename_t(uint32_t const handle) : h(handle) {}
explicit nodename_t(taskhandle_t const o) : h(o.raw()) {}
bool operator< (nodename_t const o) const { return h < o.h; }
bool operator== (nodename_t const o) const { return h == o.h; }
bool operator!= (nodename_t const o) const { return h != o.h; }
bool isBlank() const { return h == 0; }
uint32_t raw() const { return h; }
char const* str(char *buf = 0) { return rtoa_strip(h, buf); }
};
class ipaddr_t {
uint32_t a;
friend std::ostream& operator<<(std::ostream& os, ipaddr_t v);
public:
ipaddr_t() : a(0) {}
explicit ipaddr_t(uint32_t const addr) : a(addr) {}
bool isMulticast() const { return IN_MULTICAST(a); }
bool isValid() const { return a != 0; }
uint32_t value() const { return a; }
std::string str() const
{
std::ostringstream os;
os << *this;
return os.str();
}
bool operator< (ipaddr_t const o) const { return a < o.a; }
bool operator== (ipaddr_t const o) const { return a == o.a; }
bool operator!= (ipaddr_t const o) const { return a != o.a; }
};
class StatCounter {
uint32_t counter;
public:
inline explicit StatCounter(uint32_t initialValue = 0)
{
counter = initialValue;
}
inline StatCounter& operator++()
{
if (counter < std::numeric_limits<uint32_t>::max())
++counter;
return *this;
}
inline StatCounter& operator+=(StatCounter const& c)
{
uint32_t tmp = counter + c.counter;
counter = tmp < counter ? (uint32_t) std::numeric_limits<uint32_t>::max() : tmp;
return *this;
}
inline void reset()
{
counter = 0;
}
inline operator uint16_t() const
{
return (uint16_t) counter;
}
inline operator uint32_t() const
{
return counter;
}
};
struct TaskStats {
StatCounter usmRcv;
StatCounter reqRcv;
StatCounter rpyRcv;
StatCounter usmXmt;
StatCounter reqXmt;
StatCounter rpyXmt;
StatCounter lostPkt;
};
struct NodeStats {
StatCounter usmRcv;
StatCounter reqRcv;
StatCounter rpyRcv;
StatCounter usmXmt;
StatCounter reqXmt;
StatCounter rpyXmt;
StatCounter reqQLimit;
};
std::string rtos(nodename_t);
typedef std::queue<void*> BufferQueue;
template <class T>
class QueueAdaptor : protected BufferQueue {
public:
bool empty() const { return BufferQueue::empty(); }
size_type size() const { return BufferQueue::size(); }
T* peek() { return empty() ? 0 : reinterpret_cast<T*>(front()); }
T* current() { return empty() ? 0 : reinterpret_cast<T*>(back()); }
T* pop()
{
if (!empty()) {
T* const ptr = reinterpret_cast<T*>(front());
BufferQueue::pop();
return ptr;
}
return 0;
}
void push(T* ptr)
{
BufferQueue::push(ptr);
}
};
// Acnet header macros
#define ACNET_PORT (6801) // Standard ACNET port number (UDP)
#define ACNET_CLIENT_PORT (ACNET_PORT + 1)
#define UTI_VERSION (0x0800)
#define REPLY_DELAY 5u
#define REQUEST_TIMEOUT 390u
#ifdef NO_SWAP
#define MSG_LENGTH(s) (size_t)(s)
#else
#define MSG_LENGTH(s) (size_t)((s) + (s) % 2)
#endif
#define ACNET_FLG_USM (0x0)
#define ACNET_FLG_REQ (0x2)
#define ACNET_FLG_RPY (0x4)
#define ACNET_FLG_CAN (0x200 | ACNET_FLG_USM)
#define ACNET_FLG_TYPE (ACNET_FLG_USM | ACNET_FLG_REQ | ACNET_FLG_RPY)
#define ACNET_FLG_MLT (0x1)
#define ACNET_FLG_CHK (0x400)
#define ACNET_FLG_NBW (0x100)
#define PKT_TYPE(f) ((f) & ACNET_FLG_TYPE)
#define PKT_USM_FLDS(f) ((f) & (ACNET_FLG_TYPE | ACNET_FLG_CAN))
#define PKT_IS_REPLY(f) (PKT_TYPE(f) == ACNET_FLG_RPY)
#define PKT_IS_REQUEST(f) (PKT_TYPE(f) == ACNET_FLG_REQ)
#define PKT_IS_USM(f) (PKT_USM_FLDS(f) == ACNET_FLG_USM)
#define PKT_IS_CANCEL(f) (PKT_USM_FLDS(f) == ACNET_FLG_CAN)
#define ACNET_MULTICAST trunknode_t(255) // Multicast to all nodes
#define RPY_M_ENDMULT (0x02) // terminate multiple reply request
#define REQ_M_MULTRPY (0x01) // multiple reply request
#define N_REQID 8192
#define N_RPYID 8192
// ACNET protocol packet header
class AcnetHeader {
uint16_t flags_;
int16_t status_;
uint8_t sTrunk_;
uint8_t sNode_;
uint8_t cTrunk_;
uint8_t cNode_;
uint32_t svrTaskName_;
uint16_t clntTaskId_;
uint16_t msgId_;
uint16_t msgLen_;
uint8_t msg_[];
public:
AcnetHeader();
AcnetHeader(uint16_t, status_t, trunknode_t, trunknode_t, taskhandle_t, taskid_t, reqid_t, uint16_t);
uint16_t flags() const { return atohs(flags_); }
status_t status() const { return status_t(atohs(status_)); }
trunknode_t client() const { return trunknode_t(trunk_t(size_t(cTrunk_)), node_t(size_t(cNode_))); }
trunknode_t server() const { return trunknode_t(trunk_t(size_t(sTrunk_)), node_t(size_t(sNode_))); }
taskhandle_t svrTaskName() const { return taskhandle_t(atohl(svrTaskName_)); }
taskid_t clntTaskId() const { return taskid_t(atohs(clntTaskId_)); }
reqid_t msgId() const { return reqid_t(atohs(msgId_)); }
uint16_t msgLen() const { return atohs(msgLen_); }
uint8_t const *msg() const { return msg_; }
void setStatus(status_t status) { status_ = htoas(status.raw()); }
void setStatus(rpyid_t rpyId) { status_ = htoas(rpyId.raw()); }
void setFlags(uint16_t flags) { flags_ = htoas(flags); }
void setClient(trunknode_t tn) { cTrunk_ = tn.trunk().raw(); cNode_ = tn.node().raw(); }
bool isEMR();
} __attribute((packed));
#define INTERNAL_ACNET_PACKET_SIZE int(65534 - sizeof(ip) - sizeof(udphdr))
#define INTERNAL_ACNET_USER_PACKET_SIZE (INTERNAL_ACNET_PACKET_SIZE - sizeof(AcnetHeader))
extern const status_t ACNET_ENDMULT;
extern const status_t ACNET_PEND;
extern const status_t ACNET_SUCCESS;
extern const status_t ACNET_NLM;
extern const status_t ACNET_NOREMMEM;
extern const status_t ACNET_TMO;
extern const status_t ACNET_FUL;
extern const status_t ACNET_BUSY;
extern const status_t ACNET_NCN;
extern const status_t ACNET_IVM;
extern const status_t ACNET_NSR;
extern const status_t ACNET_NAME_IN_USE;
extern const status_t ACNET_NCR;
extern const status_t ACNET_NO_NODE;
extern const status_t ACNET_TRP;
extern const status_t ACNET_NOTASK;
extern const status_t ACNET_DISCONNECTED;
extern const status_t ACNET_LEVEL2;
extern const status_t ACNET_NODE_DOWN;
extern const status_t ACNET_BUG;
extern const status_t ACNET_INVARG;
extern const status_t ACNET_REQREJ;
// This section starts a hierarchy of classes that describe the layout
// of commands passed between the acnet task and the clients. We use
// inheritance rather than nested structures to clean up the syntax
// when referring to nested fields. Another benefit from inheritance
// is that we can use the template mechanisms of the language to stuff
// the 'cmd' field so it is always correct. Finally, inheritance
// allows us to implicitly upcast to CommandHeader*, which further
// cleans up the syntax (one of the ugly things about socket
// programming is the constantly having to typecast sockaddr_un or
// sockaddr_in pointers to sockaddr.)
enum class CommandList : uint16_t {
cmdKeepAlive = be16(0),
cmdConnect = be16(1),
cmdConnectExt = be16(16),
cmdRenameTask = be16(2),
cmdDisconnect = be16(3),
cmdSend = be16(4),
cmdSendRequest = be16(5),
cmdReceiveRequests = be16(6),
cmdSendReply = be16(7),
cmdCancel = be16(8),
cmdRequestAck = be16(9),
cmdAddNode = be16(10),
cmdNameLookup = be16(11),
cmdNodeLookup = be16(12),
cmdLocalNode = be16(13),
cmdTaskPid = be16(14),
cmdNodeStats = be16(15),
cmdDisconnectSingle = be16(17),
cmdSendRequestWithTimeout = be16(18),
cmdIgnoreRequest = be16(19),
cmdBlockRequests = be16(20),
cmdTcpConnect = be16(21),
cmdTcpConnectExt = be16(23),
cmdDefaultNode = be16(22)
};
enum class AckList : uint16_t {
ackAck = be16(0),
ackConnect = be16(1),
ackConnectExt = be16(16),
ackSendRequest = be16(2),
ackSendReply = be16(3),
ackNameLookup = be16(4),
ackNodeLookup = be16(5),
ackTaskPid = be16(6),
ackNodeStats = be16(7),
};
// This is the command header for all commands send from the client to
// the acnet task.
#define ASSERT_SIZE(C, S) static_assert(sizeof(C) == S, "Size of "#C" is incorrect")
struct CommandHeader {
private:
CommandList const cmd_;
uint32_t clientName_;
uint32_t virtualNodeName_;
CommandHeader();
public:
CommandHeader(CommandList Cmd) : cmd_(Cmd), clientName_(0),
virtualNodeName_(0) { }
inline CommandList cmd() const { return cmd_; }
inline nodename_t virtualNodeName() const { return nodename_t(ntohl(virtualNodeName_)); }
inline taskhandle_t clientName() const { return taskhandle_t(ntohl(clientName_)); }
inline void setClientName(taskhandle_t clientName)
{
clientName_ = htonl(clientName.raw());
}
inline void setVirtualNodeName(nodename_t virtualNodeName)
{
virtualNodeName_ = htonl(virtualNodeName.raw());
}
} __attribute__((packed));
ASSERT_SIZE(CommandHeader, 10);
template<CommandList Cmd>
struct CommandHeaderBase : public CommandHeader {
CommandHeaderBase() : CommandHeader(Cmd) { }
} __attribute__((packed));
// Sent by a client when it wants to connect to the network. An
// AckConnect is sent back to the client.
struct ConnectCommand : public CommandHeaderBase<CommandList::cmdConnect> {
private:
uint32_t pid_;
uint16_t dataPort_;
public:
inline pid_t pid() const { return ntohl(pid_); }
inline uint16_t dataPort() const { return ntohs(dataPort_); }
inline void setPid(pid_t pid)
{
pid_ = htonl(pid);
}
inline void setDataPort(uint16_t port){
dataPort_ = htons(port);
}
} __attribute__((packed));
ASSERT_SIZE(ConnectCommand, 16);
struct TcpConnectCommand : public ConnectCommand {
private:
uint32_t remoteAddr_;
public:
inline ipaddr_t remoteAddr() const { return ipaddr_t(ntohl(remoteAddr_)); }
inline void setRemoteAddr(ipaddr_t remoteAddr)
{
remoteAddr_ = htonl(remoteAddr.value());
}
} __attribute__((packed));
ASSERT_SIZE(TcpConnectCommand, 20);
// Temporary command until all clients are updated to 16bit taskid
struct TcpConnectCommandExt : public CommandHeaderBase<CommandList::cmdTcpConnectExt> {
private:
uint32_t pid_;
uint16_t dataPort_;
uint32_t remoteAddr_;
public:
inline pid_t pid() const { return ntohl(pid_); }
inline uint16_t dataPort() const { return ntohs(dataPort_); }
inline void setPid(pid_t pid)
{
pid_ = htonl(pid);
}
inline void setDataPort(uint16_t port){
dataPort_ = htons(port);
}
inline ipaddr_t remoteAddr() const { return ipaddr_t(ntohl(remoteAddr_)); }
inline void setRemoteAddr(ipaddr_t remoteAddr)
{
remoteAddr_ = htonl(remoteAddr.value());
}
} __attribute__((packed));
ASSERT_SIZE(TcpConnectCommandExt, 20);
// Sent by a client periodicly to keep it's Acnet connection. An
// AckCommand is sent back to the client.
struct KeepAliveCommand : public CommandHeaderBase<CommandList::cmdKeepAlive> {
} __attribute__((packed));
ASSERT_SIZE(KeepAliveCommand, 10);
// Sent by a client when it wants to rename a connected task. An
// AckConnect is sent back to the client.
struct RenameTaskCommand : public CommandHeaderBase<CommandList::cmdRenameTask> {
private:
uint32_t newName_;
public:
inline taskhandle_t newName() const { return taskhandle_t(ntohl(newName_)); }
} __attribute__((packed));
ASSERT_SIZE(RenameTaskCommand, 14);
// Sent by a client when it wants to disconnect from the network. An
// AckCommand is sent back to the client.
struct DisconnectCommand :
public CommandHeaderBase<CommandList::cmdDisconnect> {
} __attribute__((packed));
ASSERT_SIZE(DisconnectCommand, 10);
struct DisconnectSingleCommand :
public CommandHeaderBase<CommandList::cmdDisconnectSingle> {
} __attribute__((packed));
ASSERT_SIZE(DisconnectSingleCommand, 10);
// Sent by a client wanting to send an USM. An AckCommand is sent to
// the client.
struct SendCommand : public CommandHeaderBase<CommandList::cmdSend> {
private:
uint32_t task_;
uint16_t addr_;
uint8_t data_[];
public:
inline trunknode_t addr() const { return trunknode_t(ntohs(addr_)); }
inline taskhandle_t task() const { return taskhandle_t(ntohl(task_)); }
inline uint8_t const *data() const { return data_; }
} __attribute__((packed));
ASSERT_SIZE(SendCommand, 16);
// Sent by a client that wants to be a "RUM listener". The acnet task
// marks the task handle (previously registered with a ConnectCommand)
// as able to receive.
struct ReceiveRequestCommand :
public CommandHeaderBase<CommandList::cmdReceiveRequests> {
} __attribute__((packed));
ASSERT_SIZE(ReceiveRequestCommand, 10);
// Sent by a client that wants to do a lookup of a node name to its
// trunk/node combo.
struct NameLookupCommand :
public CommandHeaderBase<CommandList::cmdNameLookup> {
private:
uint32_t name_;
public:
inline nodename_t name() const { return nodename_t(ntohl(name_)); }
} __attribute__((packed));
ASSERT_SIZE(NameLookupCommand, 14);
// Sent by a client that wants to do a lookup of a node name from its
// trunk/node combo.
struct NodeLookupCommand :
public CommandHeaderBase<CommandList::cmdNodeLookup> {
private:
uint16_t addr_;
public:
inline trunknode_t addr() const { return trunknode_t(ntohs(addr_)); }
} __attribute__((packed));
ASSERT_SIZE(NodeLookupCommand, 12);
struct LocalNodeCommand :
public CommandHeaderBase<CommandList::cmdLocalNode> {
} __attribute__((packed));
ASSERT_SIZE(LocalNodeCommand, 10);
struct DefaultNodeCommand :
public CommandHeaderBase<CommandList::cmdDefaultNode> {
} __attribute__((packed));
ASSERT_SIZE(DefaultNodeCommand, 10);
struct SendRequestCommand :
public CommandHeaderBase<CommandList::cmdSendRequest> {
private:
uint32_t task_;
uint16_t addr_;
uint16_t flags_;
uint8_t data_[];
public:
inline trunknode_t addr() const { return trunknode_t(ntohs(addr_)); }
inline taskhandle_t task() const { return taskhandle_t(ntohl(task_)); }
inline uint16_t flags() const { return ntohs(flags_); }
inline uint8_t const *data() const { return data_; }
} __attribute__((packed));
ASSERT_SIZE(SendRequestCommand, 18);
struct SendRequestWithTimeoutCommand :
public CommandHeaderBase<CommandList::cmdSendRequestWithTimeout> {
private:
uint32_t task_;
uint16_t addr_;
uint16_t flags_;
uint32_t timeout_;
uint8_t data_[];
public:
inline trunknode_t addr() const { return trunknode_t(ntohs(addr_)); }
inline taskhandle_t task() const { return taskhandle_t(ntohl(task_)); }
inline uint16_t flags() const { return ntohs(flags_); }
inline uint32_t timeout() const { return ntohl(timeout_); }
inline uint8_t const *data() const { return data_; }
} __attribute__((packed));
ASSERT_SIZE(SendRequestWithTimeoutCommand, 22);
struct SendReplyCommand : public CommandHeaderBase<CommandList::cmdSendReply> {
private:
uint16_t rpyid_;
uint16_t flags_;
int16_t status_;
uint8_t data_[];
public:
inline rpyid_t rpyid() const { return rpyid_t(ntohs(rpyid_)); }
inline uint16_t flags() const { return ntohs(flags_); }
inline status_t status() const { return status_t(ntohs(status_)); }
inline uint8_t const *data() const { return data_; }
} __attribute__((packed));
ASSERT_SIZE(SendReplyCommand, 16);
struct IgnoreRequestCommand :
public CommandHeaderBase<CommandList::cmdIgnoreRequest> {
private:
uint16_t rpyid_;
public:
inline rpyid_t rpyid() const { return rpyid_t(ntohs(rpyid_)); }
} __attribute__((packed));
ASSERT_SIZE(IgnoreRequestCommand, 12);
struct CancelCommand : public CommandHeaderBase<CommandList::cmdCancel> {
private:
uint16_t reqid_;
public:
inline reqid_t reqid() const { return reqid_t(ntohs(reqid_)); }
} __attribute__((packed));
ASSERT_SIZE(CancelCommand, 12);
struct BlockRequestCommand :
public CommandHeaderBase<CommandList::cmdBlockRequests> {
} __attribute__((packed));
ASSERT_SIZE(BlockRequestCommand, 10);
struct RequestAckCommand :
public CommandHeaderBase<CommandList::cmdRequestAck> {
private:
uint16_t rpyid_;
public:
inline rpyid_t rpyid() const { return rpyid_t(ntohs(rpyid_)); }
} __attribute__((packed));
ASSERT_SIZE(RequestAckCommand, 12);
struct AddNodeCommand : public CommandHeaderBase<CommandList::cmdAddNode> {
private:
uint32_t ipAddr_;
uint32_t flags_;
uint16_t addr_;
uint32_t nodeName_;
public:
inline ipaddr_t ipAddr() const { return ipaddr_t(ntohl(ipAddr_)); }
inline uint32_t flags() const { return ntohl(flags_); }
inline trunknode_t addr() const { return trunknode_t(ntohs(addr_)); }
inline nodename_t nodeName() const { return nodename_t(ntohl(nodeName_)); }
} __attribute__((packed));
ASSERT_SIZE(AddNodeCommand, 24);
struct TaskPidCommand : public CommandHeaderBase<CommandList::cmdTaskPid> {
uint32_t task;
} __attribute__((packed));
ASSERT_SIZE(TaskPidCommand, 14);
struct NodeStatsCommand :
public CommandHeaderBase<CommandList::cmdNodeStats> {
} __attribute__((packed));
ASSERT_SIZE(NodeStatsCommand, 10);
class AckHeader {
private:
AckList const cmd_;
int16_t status_;
AckHeader();
public:
AckHeader(AckList cmd) : cmd_(cmd) { setStatus(ACNET_SUCCESS); }
AckList cmd() const { return cmd_; }
status_t status() const { return status_t(ntohs(status_)); }
void setStatus(status_t status) { status_ = htons(status.raw()); }
} __attribute__((packed));
ASSERT_SIZE(AckHeader, 4);
// This command is only sent from the acnet task to the clients to
// acknowledge a command. This class contains a status field to pass
// the success or failure codes back to the client.
struct Ack : public AckHeader {
Ack() : AckHeader(AckList::ackAck) { }
} __attribute__((packed));
ASSERT_SIZE(Ack, 4);
struct AckConnect : public AckHeader {
private:
uint8_t id_;
uint32_t clientName_;
public:
AckConnect() : AckHeader(AckList::ackConnect) { }
void setTaskId(taskid_t id) { id_ = (uint8_t) id.raw(); }
void setClientName(taskhandle_t clientName) { clientName_ = htonl(clientName.raw()); }
} __attribute__((packed));
ASSERT_SIZE(AckConnect, 9);
struct AckConnectExt : public AckHeader {
private:
uint16_t id_;
uint32_t clientName_;
public:
AckConnectExt() : AckHeader(AckList::ackConnectExt) { }
void setTaskId(taskid_t id) { id_ = htons(id.raw()); }
void setClientName(taskhandle_t clientName) { clientName_ = htonl(clientName.raw()); }
} __attribute__((packed));
ASSERT_SIZE(AckConnectExt, 10);
struct AckSendRequest : public AckHeader {
private:
uint16_t reqid_;
public:
AckSendRequest() : AckHeader(AckList::ackSendRequest) { }
void setRequestId(reqid_t reqid) { reqid_ = htons(reqid.raw()); }
} __attribute__((packed));
ASSERT_SIZE(AckSendRequest, 6);
struct AckSendReply : public AckHeader {
private:
uint16_t _flags;
public:
AckSendReply() : AckHeader(AckList::ackSendReply) { }
} __attribute__((packed));
ASSERT_SIZE(AckSendReply, 6);
struct AckNameLookup : public AckHeader {
private:
uint8_t trunk;
uint8_t node;
public:
AckNameLookup() : AckHeader(AckList::ackNameLookup) { }
void setTrunkNode(trunknode_t addr) { trunk = addr.trunk().raw(); node = addr.node().raw(); }
} __attribute__((packed));
ASSERT_SIZE(AckNameLookup, 6);
struct AckNodeLookup : public AckHeader {
private:
uint32_t name_;
public:
AckNodeLookup() : AckHeader(AckList::ackNodeLookup) { }
void setNodeName(nodename_t name) { name_ = htonl(name.raw()); }
} __attribute__((packed));
ASSERT_SIZE(AckNodeLookup, 8);
struct AckTaskPid : public AckHeader {
private:
uint32_t pid_;
public:
AckTaskPid() : AckHeader(AckList::ackTaskPid) { }
void setPid(pid_t pid) { pid_ = htonl(pid); }
} __attribute__((packed));
ASSERT_SIZE(AckTaskPid, 8);
struct AckNodeStats : public AckHeader {
private:
uint32_t statUsmRcv;
uint32_t statReqRcv;
uint32_t statRpyRcv;
uint32_t statUsmXmt;
uint32_t statReqXmt;
uint32_t statRpyXmt;
uint32_t statReqQLimit;
public:
AckNodeStats() : AckHeader(AckList::ackNodeStats) { }
void setStats(NodeStats& stats)
{
statUsmRcv = htonl((uint32_t) stats.usmRcv);
statReqRcv = htonl((uint32_t) stats.reqRcv);
statRpyRcv = htonl((uint32_t) stats.rpyRcv);
statUsmXmt = htonl((uint32_t) stats.usmXmt);
statReqXmt = htonl((uint32_t) stats.reqXmt);
statRpyXmt = htonl((uint32_t) stats.rpyXmt);
statReqQLimit = htonl((uint32_t) stats.reqQLimit);
}
} __attribute__((packed));
ASSERT_SIZE(AckNodeStats, 32);
// Asynchronous message passing to client processes
struct AcnetClientMessage {
private:
uint32_t pid_;
uint32_t task_;
uint8_t type;
public:
enum {
Ping,
DumpProcessIncomingPacketsOn,
DumpProcessIncomingPacketsOff,
DumpTaskIncomingPacketsOn,
DumpTaskIncomingPacketsOff,
};
AcnetClientMessage() : pid_(0), task_(0), type(Ping) {}
AcnetClientMessage(taskhandle_t task, uint8_t type) : task_(htoal(task.raw())), type(type) {}
void setPid(pid_t pid) { pid_ = htoal(pid); }
taskhandle_t task() { return taskhandle_t(atohl(task_)); }
} __attribute__((packed));
ASSERT_SIZE(AcnetClientMessage, 9);
// Project-wide types...
class TaskInfo;
struct reqDetail {
uint16_t id;