-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathNetworkClient.h
More file actions
282 lines (181 loc) · 8.45 KB
/
NetworkClient.h
File metadata and controls
282 lines (181 loc) · 8.45 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
#ifndef _RTENETWORKCLIENT_
#define _RTENETWORKCLIENT_
//////////////////////////////////////////////////////////////////////////////////////////
// File: NrtworkClient.h
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Header file for the NetworkClient class.
// Project: Retro Terrain Engine
// Author(s):
// Without this nested includes somewhere deep inside Allegro will summon winsock.h and it will conflict with winsock2.h from RakNet
// and we can't move "Network.h" here because for whatever reasons everything will collapse
#define WIN32_LEAN_AND_MEAN
#include <string.h>
#include "Singleton.h"
#include "UInputMan.h"
#include "Sound.h"
#include <map>
#include "Network.h"
#include "NatPunchthroughClient.h"
//////////////////////////////////////////////////////////////////////////////////////////
// Inclusions of header files
#define g_NetworkClient NetworkClient::Instance()
namespace RTE
{
//////////////////////////////////////////////////////////////////////////////////////////
// Class: NetworkClient
//////////////////////////////////////////////////////////////////////////////////////////
// Description: The centralized singleton manager of all Timer:s and overall
// timekeeping in RTE.
// Parent(s): Singleton
// Class history:
class NetworkClient :
public Singleton<NetworkClient>//,
// public Serializable
{
//////////////////////////////////////////////////////////////////////////////////////////
// Public member variable, method and friend function declarations
public:
//////////////////////////////////////////////////////////////////////////////////////////
// Constructor: NetworkClient
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Constructor method used to instantiate a NetworkClient object in system
// memory. Create() should be called before using the object.
// Arguments: None.
NetworkClient() { Clear(); Create(); }
//////////////////////////////////////////////////////////////////////////////////////////
// Destructor: ~NetworkClient
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Destructor method used to clean up a NetworkClient object before deletion
// from system memory.
// Arguments: None.
virtual ~NetworkClient() { Destroy(); }
//////////////////////////////////////////////////////////////////////////////////////////
// Method: Create
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Makes the NetworkClient object ready for use.
// Arguments: None.
// Return value: An error return value signaling sucess or any particular failure.
// Anything below 0 is an error signal.
virtual int Create();
//////////////////////////////////////////////////////////////////////////////////////////
// Virtual method: Reset
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Resets the entire NetworkClient, including its inherited members, to
// their default settings or values.
// Arguments: None.
// Return value: None.
virtual void Reset() { Clear(); }
//////////////////////////////////////////////////////////////////////////////////////////
// Method: Destroy
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Destroys and resets (through Clear()) the NetworkClient object.
// Arguments: None.
// Return value: None.
void Destroy();
//////////////////////////////////////////////////////////////////////////////////////////
// Virtual method: GetClassName
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Gets the class name of this Entity.
// Arguments: None.
// Return value: A string with the friendly-formatted type name of this object.
virtual const std::string & GetClassName() const { return m_ClassName; }
void Update();
void Connect(std::string serverName, unsigned short serverPort, std::string playerName);
void ConnectNAT(RakNet::SystemAddress addr);
void PerformNATPunchThrough(std::string serviceServerName, unsigned short serverPort, std::string playerName, std::string serverName, std::string serverPassword);
RakNet::SystemAddress ConnectBlocking(RakNet::RakPeerInterface *rakPeer, const char *address, unsigned short int port);
void Disconnect();
bool IsConnectedAndRegistred() { return m_IsConnected && m_IsRegistered; }
struct MsgInput
{
unsigned char Id;
int MouseX;
int MouseY;
bool MouseButtonPressed[UInputMan::MAX_MOUSE_BUTTONS];
bool MouseButtonReleased[UInputMan::MAX_MOUSE_BUTTONS];
bool MouseButtonHeld[UInputMan::MAX_MOUSE_BUTTONS];
bool ResetActivityVote;
int MouseWheelMoved;
unsigned int InputElementPressed;
unsigned int InputElementReleased;
unsigned int InputElementHeld;
};
//////////////////////////////////////////////////////////////////////////////////////////
// Protected member variable and method declarations
protected:
// Member variables
static const std::string m_ClassName;
RakNet::SystemAddress m_ClientID;
RakNet::RakPeerInterface *m_Client;
RakNet::NatPunchthroughClient m_NatPunchthroughClient;
RakNet::SystemAddress m_NATServiceServerID;
RakNet::SystemAddress m_ServerID;
RakNet::RakNetGUID m_ServerGuid;
std::string m_PlayerName;
//////////////////////////////////////////////////////////////////////////////////////////
// Private member variable and method declarations
private:
//////////////////////////////////////////////////////////////////////////////////////////
// Method: Clear
//////////////////////////////////////////////////////////////////////////////////////////
// Description: Clears all the member variables of this NetworkClient, effectively
// resetting the members of this abstraction level only.
// Arguments: None.
// Return value: None.
void Clear();
void SendRegisterMsg();
void SendDisconnectMsg();
void SendInputMsg();
void ReceiveFrameSetupMsg(RakNet::Packet * p);
void ReceiveFrameLineMsg(RakNet::Packet * p);
void ReceiveFrameBoxMsg(RakNet::Packet * p);
void ReceiveSceneMsg(RakNet::Packet * p);
void ReceiveAcceptedMsg();
void ReceiveSceneSetupMsg(RakNet::Packet * p);
void SendSceneSetupAcceptedMsg();
void ReceiveTerrainChangeMsg(RakNet::Packet * p);
void SendSceneAcceptedMsg();
void ReceiveSceneEndMsg();
void DrawBackgrounds(BITMAP * pTargetBitmap);
void DrawFrame();
void SendServerGuidRequest(RakNet::SystemAddress addr, std::string serverName, std::string serverPassword);
void ReceiveServerGiudAnswer(RakNet::Packet * p);
void ReceivePosteEffectsMsg(RakNet::Packet * p);
void ReceiveSoundEventsMsg(RakNet::Packet * p);
void ReceiveMusicEventsMsg(RakNet::Packet * p);
void DrawPostEffects(int frame);
unsigned int GetPing();
int64_t m_LastInputSentTime;
unsigned char m_aPixelLineBuffer[MAX_PIXEL_LINE_BUFFER_SIZE];
long int m_ReceivedData;
long int m_CompressedData;
bool m_IsConnected;
bool m_IsRegistered;
bool m_IsNATPunched;
int m_ClientInputFps;
//int m_LastLineReceived;
int m_CurrentSceneLayerReceived;
unsigned char m_SceneId;
int m_CurrentFrame;
Vector m_TargetPos[FRAMES_TO_REMEMBER];
std::list<PostEffect> m_PostEffects[FRAMES_TO_REMEMBER];
// List of sounds received from server. OWNED!!!
std::map<short int, Sound *> m_Sounds;
BITMAP * m_pSceneBackgroundBitmap;
BITMAP * m_pSceneForegroundBitmap;
BITMAP * m_BackgroundBitmaps[MAX_BACKGROUND_LAYERS_TRANSMITTED];
LightweightSceneLayer m_aBackgroundLayers[FRAMES_TO_REMEMBER][MAX_BACKGROUND_LAYERS_TRANSMITTED];
int m_ActiveBackgroundLayers;
bool m_SceneWrapsX;
int m_SceneWidth;
int m_SceneHeight;
int m_aMouseButtonPressedState[3];
int m_aMouseButtonReleasedState[3];
bool m_UseNATPunchThroughService;
// Disallow the use of some implicit methods.
NetworkClient(const NetworkClient &reference);
NetworkClient & operator=(const NetworkClient &rhs);
unsigned char GetPacketIdentifier(RakNet::Packet *p);
};
} // namespace RTE
#endif // File