This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
forked from adamjimenez/ChaosGroove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.cpp
299 lines (247 loc) · 6.85 KB
/
net.cpp
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
// Net (.Hpp)
// ----------
#include <ptk.h>
#include <vector>
#include <list>
#include <sstream>
#include "KPTK.h"
#include "time.h"
using namespace std;
#include "bridge.hpp"
#include "game.hpp"
#include "log.hpp"
#include "timer.hpp"
#include "scene.hpp"
#include "config.hpp"
#include "net.hpp"
#include "chat.hpp"
/*
#ifdef WIN32
//#include <winsock2.h>
#include <windows.h>
#else
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#endif
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>
#define MAX_CLIENTS 32
#define SERVER_PORT 60000
struct net_t net;
RakPeerInterface *peer;
Packet *packet;
void net_find_ip(void)
{
PHOSTENT host;
char lpszHostName[50]; // will accept the host name
#ifdef WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
#endif
if (gethostname(lpszHostName, 50) != 0)
{
printf("gethostname() generated error %d", WSAGetLastError());
}
else
{
// get the host entry structure for this machine
if ((host = gethostbyname(lpszHostName)) == NULL)
{
printf("gethostname() generated error %d", WSAGetLastError());
}
else
{
sprintf(net.IP, "%s\n", inet_ntoa(*(struct in_addr*)host->h_addr));
}
}
WSACleanup();
}
void PrintMessage(RPCParameters *rpcParameters)
{
char str[512];
int message, m, id;
RestoreWindow();
sprintf(str, "%s\n", rpcParameters->input);
log("%s", str);
log("%d", str[0]);
message = str[0];
for (m = 0 ; m < strlen(str) - 1 ; m++)
{
str[m] = str[m + 1];
}
str[m + 1] = 0;
//log("length new: %d", strlen(str));
//log("message: %d, %s", message, str);
if (message == MESSAGE_NEW_USER)
{
if (net.server)
{
id = find_free_user_spot();
//log("id: %d", id);
add_user(str, id);
//log("sending all existing user info to new user..");
for (m = 0 ; m < MAX_USERS ; m++)
{
if (user[m].id != -1)
{
sprintf(str, "%c%c%s", MESSAGE_ADD_USER, m, user[m].name);
peer->RPC("PrintMessage", str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0,
rpcParameters->sender, false, 0, UNASSIGNED_NETWORK_ID,0);
}
}
// Give new user his/her ID value.
sprintf(str, "%c%c", MESSAGE_USER_ID, id);
peer->RPC("PrintMessage", str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0,
rpcParameters->sender, false, 0, UNASSIGNED_NETWORK_ID,0);
// Tell everyone else to add the new user.
sprintf(str, "%c%c%s", MESSAGE_ADD_USER, id, user[id].name);
peer->RPC("PrintMessage", str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0,
rpcParameters->sender, true, 0, UNASSIGNED_NETWORK_ID,0);
}
}
if (message == MESSAGE_ADD_USER)
{
id = str[0];
//log("ID: %d", id);
add_user(str, id);
}
if (message == MESSAGE_REMOVE_USER)
{
remove_user(str[0]);
}
if (message == MESSAGE_USER_ID)
{
chat.user_id = str[0];
}
if (message == MESSAGE_CHAT)
{
add_chat_line(str, Rgba(1.0, 1.0, 1.0));
}
// If we are the server, send message back to other clients (apart from the one it came from)
if (net.server)
{
peer->RPC("PrintMessage",(const char*)rpcParameters->input, rpcParameters->numberOfBitsOfData,
HIGH_PRIORITY, RELIABLE_ORDERED, 0, rpcParameters->sender, true, 0, UNASSIGNED_NETWORK_ID, 0);
}
}
bool net_setup(bool server, bool first)
{
char str[512];
net.connected = false;
if (first)
{
peer = RakNetworkFactory::GetRakPeerInterface();
REGISTER_STATIC_RPC(peer, PrintMessage);
net.server = server;
if (!server)
{
peer->Startup(1, 30, &SocketDescriptor(), 1);
}
else
{
peer->Startup(MAX_CLIENTS, 30, &SocketDescriptor(SERVER_PORT,0), 1);
}
return true;
}
if (net.server)
{
add_chat_line("Starting the server", Rgba(1.0, 1.0, 1.0));
net_find_ip();
sprintf(str, "IP Address : %s", net.IP);
add_chat_line(str, Rgba(1.0, 1.0, 1.0));
// We need to let the server accept incoming connections from the clients
peer->SetMaximumIncomingConnections(MAX_CLIENTS);
sprintf(str, "%c%s", 1, chat.name);
add_user(str, 1);
chat.user_id = 1;
}
else
{
add_chat_line("Starting the client.\n", Rgba(1.0, 1.0, 1.0));
peer->Connect(net.IP, SERVER_PORT, 0,0);
}
net.connected = true;
return true;
}
void net_send_message(char *str, int message)
{
char text[512];
sprintf(text, "%c%s", message, str);
peer->RPC("PrintMessage", text, (strlen(text)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0,
UNASSIGNED_SYSTEM_ADDRESS, true, 0, UNASSIGNED_NETWORK_ID,0);
}
void net_process(void)
{
char str[512];
int u;
if (!net.connected) return;
packet = peer->Receive();
while(packet)
{
switch (packet->data[0])
{
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
add_chat_line("Another client has disconnected.\n", Rgba(1.0, 1.0, 1.0));
break;
case ID_REMOTE_CONNECTION_LOST:
add_chat_line("Another client has lost the connection.\n", Rgba(1.0, 1.0, 1.0));
break;
case ID_REMOTE_NEW_INCOMING_CONNECTION:
add_chat_line("Another client has connected.\n", Rgba(1.0, 1.0, 1.0));
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
add_chat_line("Our connection request has been accepted.\n", Rgba(1.0, 1.0, 1.0));
sprintf(str, "%c%s", 1, chat.name);
// Two tricky things here. First, you have to remember to send the NULL terminator so you need strlen(str)+1
// Second, if you didn't read the docs you might not realize RPC takes the number of bits rather than the number of bytes.
// You have to multiply the number of bytes by 8
// Don't forget that RPC takes bits as the data length, not bytes!
//peer->RPC("PrintMessage",str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0,
//UNASSIGNED_SYSTEM_ADDRESS, true, 0, UNASSIGNED_NETWORK_ID,0);
net_send_message(str, MESSAGE_NEW_USER);
break;
case ID_NEW_INCOMING_CONNECTION:
//add_chat_line("A connection is incoming.\n");
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
add_chat_line("The server is full.\n", Rgba(1.0, 1.0, 1.0));
break;
case ID_DISCONNECTION_NOTIFICATION:
if (net.server){
sprintf(str, "A client has disconnected", Rgba(1.0, 1.0, 1.0));
add_chat_line(str, Rgba(1.0, 1.0, 1.0));
} else {
add_chat_line("We have been disconnected.\n", Rgba(1.0, 1.0, 1.0));
}
break;
case ID_CONNECTION_LOST:
if (net.server){
add_chat_line("A client lost the connection.\n", Rgba(1.0, 1.0, 1.0));
} else {
add_chat_line("Connection lost.\n", Rgba(1.0, 1.0, 1.0));
}
break;
default:
//add_chat_line("Message with identifier has arrived.\n");
break;
}
peer->DeallocatePacket(packet);
// Stay in the loop as long as there are more packets.
packet = peer->Receive();
}
}
void net_shutdown(void)
{
char str[512];
if (!net.connected) return;
sprintf(str, "%c", chat.user_id);
net_send_message(str, MESSAGE_REMOVE_USER);
net.connected = false;
peer->Shutdown(300);
RakNetworkFactory::DestroyRakPeerInterface(peer);
}
*/