-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.hpp
126 lines (103 loc) · 3.27 KB
/
message.hpp
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
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <string>
#include <map>
class RecvMessage;
class SendMessage;
class NetworkNode;
class MessageGroup;
#include "network.hpp"
class NetworkNode
{
public:
NetworkNode();
virtual ~NetworkNode();
MessageGroup *getMessages(void);
int numPending(void);
void addMessage(SendMessage *msg);
void addMessage(RecvMessage *msg);
private:
RecvMessage **data;
int messagesPending, messagesAllocated;
Mutex mutex;
void lock(void) { mutex.lock(); }
void unlock(void) { mutex.unlock(); }
};
class RecvMessage
{
public:
RecvMessage(int type, int length, int source, unsigned char *data);
RecvMessage(const SendMessage& send);
~RecvMessage();
// Query attributes
inline int getType() const { return type; }
inline int getLength() const { return length; }
inline int getSource() const { return source; }
// Pull data from an incoming message
char getChar (void);
short getShort (void);
int getInt (void);
std::string getString(void);
float getFloat (void);
inline RecvMessage& operator>> (char &c) {c=getChar(); return*this;}
inline RecvMessage& operator>> (short &s) {s=getShort(); return*this;}
inline RecvMessage& operator>> (int &i) {i=getInt(); return*this;}
inline RecvMessage& operator>> (std::string &s) {s=getString();return*this;}
inline RecvMessage& operator>> (float &f) {f=getFloat(); return*this;}
private:
unsigned char *data;
int pos;
int type, length;
int source;
};
class SendMessage
{
public:
SendMessage(int type);
~SendMessage();
// Query attributes
inline int getType(void) const { return type; }
inline int getLength(void) const { return length; }
inline int getSource(void) const { return source; }
inline const unsigned char *getData(void) const { return data; }
inline int getNetLength() const { return length+4; }
// Add data to an outgoing message
inline SendMessage& operator<< (char c) { putChar(c); return *this; }
inline SendMessage& operator<< (short s) { putShort(s); return *this; }
inline SendMessage& operator<< (int i) { putInt(i); return *this; }
inline SendMessage& operator<< (const char* s) { putString(s); return *this; }
inline SendMessage& operator<< (const std::string& s) { putString(s.c_str()); return *this; }
inline SendMessage& operator<< (float f) { putFloat(f); return *this; }
void putChar(char);
void putShort(short);
void putInt(int);
void putFloat(float);
void putString(const char*);
// Finalize (send) an outgoing message
// - A client cannot send a message directly to other clients - it must go
// through the server first.
// - "Everyone" does not include the sender
void sendToClient(int client);
void sendToServer(void);
void sendToEveryone(void);
void sendToMyself(void);
private:
void expand(int amt);
unsigned char *data;
int length, alloc;
int type;
int source;
};
class MessageGroup
{
public:
MessageGroup(RecvMessage **messages, int numMessages);
~MessageGroup();
inline int numPending(void) const { return numMessages-iteratorPos; }
RecvMessage *next(void);
private:
RecvMessage **messages;
int numMessages;
int iteratorPos;
};
#endif