-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.h
executable file
·348 lines (236 loc) · 7.7 KB
/
packets.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
#ifndef PACKETS_H_
#define PACKETS_H_
#include <osg/Vec3>
#include <gnelib/gnetypes.h>
#include <gnelib/Packet.h>
#include <gnelib/Buffer.h>
#include <vector>
#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include "units/Unit.h"
#include "event.h"
#include "ScenarioData.h"
#include "models/InverseModelFormation.h"
#include "Command.h"
/** This custom packet will allow us to pass a position and rotation
* through the network connection.
* Two important things to remember:
* 1) register the packet with GNE using
* GNE::PacketParser::defaultRegisterPacket<PositionPacket>();
* 2) Every custom packet needs a unique ID
*/
class PositionPacket : public GNE::Packet
{
public:
PositionPacket( osg::Vec3 xyz, osg::Vec3 hpr, const std::string &ownerID );
///default constructor
PositionPacket();
///copy constructor
PositionPacket( const PositionPacket &p );
virtual ~PositionPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer &raw ) const;
virtual void readPacket( GNE::Buffer &raw );
osg::Vec3 mXYZ;
osg::Vec3 mHPR;
std::string mOwnerID;
};
/** This custom packet will allow us to pass a player's UniqueID upon
* exit to let server and other clients known a player has quit the game.
*/
class PlayerQuitPacket : public GNE::Packet
{
public:
PlayerQuitPacket( const std::string& playerID );
PlayerQuitPacket();
public:
PlayerQuitPacket( const PlayerQuitPacket& p );
virtual ~PlayerQuitPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
std::string mPlayerID;
};
/** This custom packet will allow us to pass a player's UniqueID upon
* exit to let server and other clients known a player has quit the game.
*/
class InitializingPacket : public GNE::Packet
{
public:
InitializingPacket();
InitializingPacket(ScenarioData* data);
InitializingPacket( const InitializingPacket& p );
virtual ~InitializingPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
ScenarioData* mScenarioData;
unsigned int mNumUnitPackets;
};
/** This packet is for each unit entry in the scenario data
*/
class InitializingUnitsPacket : public GNE::Packet
{
public:
InitializingUnitsPacket();
InitializingUnitsPacket(ScenarioUnitData* data, int side);
InitializingUnitsPacket( const InitializingUnitsPacket& p );
virtual ~InitializingUnitsPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
ScenarioUnitData* mScenarioUnitData;
unsigned int mSide;
};
/** this packet will create a new UnitRemote if sent.
*/
class UnitInitPacket : public GNE::Packet
{
public:
UnitInitPacket();
UnitInitPacket(const Unit* unit);
UnitInitPacket( const UnitInitPacket& p );
virtual ~UnitInitPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
std::string m_UniqueUnitID;
std::vector<std::string> m_ObjectModelNames;
bool m_IsFlying;
int m_Side;
int mClientID;
int mUnitID;
};
/** this packet transmits system-wide events.
*/
class EventPacket : public GNE::Packet
{
public:
//enum EventType {SEE, FIRE, HURT, GOAL, SELECT, PROJECTILE, PAUSE};
EventPacket();
EventPacket(Event::EventType event, std::vector<std::string>& participants, std::vector<double>& data);
EventPacket( const EventPacket& p );
virtual ~EventPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
// dump the packet contents into a stream (used for logging)
virtual void dumpPacket( std::ostringstream& ss ) const;
Event::EventType m_Event;
std::vector<std::string> m_Participants;
std::vector<double> m_Data;
};
/** this packet identifies the client.
*/
class IdPacket : public GNE::Packet
{
public:
IdPacket();
IdPacket(int id);
IdPacket( const IdPacket& p );
virtual ~IdPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
// dump the packet contents into a stream (used for logging)
virtual void dumpPacket( std::ostringstream& ss ) const;
int m_Id;
};
/** this packet requests remote units.
*/
class UnitSavePacketRequest : public GNE::Packet
{
public:
UnitSavePacketRequest();
UnitSavePacketRequest(int id);
UnitSavePacketRequest( const UnitSavePacketRequest& p );
virtual ~UnitSavePacketRequest() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
// dump the packet contents into a stream (used for logging)
virtual void dumpPacket( std::ostringstream& ss ) const;
int m_Client;
};
/** this packet contains the complete state of a unit.
*/
class UnitSavePacket : public GNE::Packet, public osg::Referenced
{
public:
UnitSavePacket();
UnitSavePacket(Unit *u, int num);
UnitSavePacket(std::string str, int num, int id);
UnitSavePacket( const UnitSavePacket& p );
virtual ~UnitSavePacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
// dump the packet contents into a stream (used for logging)
virtual void dumpPacket( std::ostringstream& ss ) const;
std::ostringstream m_Oss;
int m_Id;
int m_Num;
};
/** this packet contains a position/formation command
*/
class CommandPacket : public GNE::Packet
{
public:
CommandPacket(Command::CommandType command, osg::Vec3 pos, std::vector<int>& units, float duration = 1.0, float timescale = 1.0);
CommandPacket(Command::CommandType command, std::vector<int>& units, float duration = 1.0, float timescale = 1.0);
CommandPacket();
CommandPacket( const CommandPacket& p );
virtual ~CommandPacket() {}
static const int ID;
virtual int getSize() const;
virtual void writePacket( GNE::Buffer& raw ) const;
virtual void readPacket( GNE::Buffer& raw );
// dump the packet contents into a stream (used for logging)
virtual void dumpPacket( std::ostringstream& ss ) const;
Command::CommandType mCommand;
osg::Vec3 mPos;
std::vector<int> mUnits;
float mDuration;
float mTimeScale;
};
///// template helper functions //////
template <class T> GNE::Buffer& operator<<(GNE::Buffer& raw, const std::vector<T>& out)
{
raw << (unsigned int)(out.size());
for (unsigned int i = 0; i<out.size(); i++)
{
raw << out[i];
}
return raw;
}
template <class T> GNE::Buffer& operator>>(GNE::Buffer& raw, std::vector<T>& in)
{
unsigned int size;
raw >> size;
in.resize(size);
for (unsigned int i = 0; i<in.size(); i++)
{
raw >> in[i];
}
return raw;
}
template <class T> unsigned int GetPacketSizeOf(const std::vector<T>& in)
{
unsigned int size = 0;
size += sizeof (unsigned int);
for (unsigned int i = 0; i<in.size(); i++)
{
size += GNE::Buffer::getSizeOf(in[i]);
}
return size;
}
#endif //PACKETS_H_