-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.c
More file actions
275 lines (209 loc) · 6.76 KB
/
Copy pathnet.c
File metadata and controls
275 lines (209 loc) · 6.76 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
#include "net.h"
#include "input.h"
#include "protobufs.h"
void net_disconnect(ENetEvent event) {
printf("%s disconnected.\n", event.peer->data);
event.peer->data = NULL;
}
void net_handle_message(ENetEvent event) {
WrapperMessage *msg;
client_t *c = client_find(event.peer);
if ( event.packet->dataLength == 0 ){
printf("no len packet\n");
return;
}
msg = wrapper_message__unpack(
NULL,
event.packet->dataLength,
event.packet->data
);
if ( msg == NULL ) {
fprintf(stderr, "Error decoding message\n");
exit(EXIT_FAILURE);
}
if ( c == NULL ) {
switch(msg->type) {
case WRAPPER_MESSAGE__TYPE__HANDSHAKE:
_net_handle_handshake(event.peer, msg->handshake_message); break;
}
} else {
switch(msg->type) {
case WRAPPER_MESSAGE__TYPE__INPUT:
_net_handle_input(c, msg->input_message); break;
case WRAPPER_MESSAGE__TYPE__LOBBY_CHAT:
_net_handle_lobby_chat(c, msg->lobby_chat_message); break;
}
}
wrapper_message__free_unpacked(msg, NULL);
enet_packet_destroy (event.packet);
}
// client c has said something in the lobby
void _net_handle_lobby_chat(client_t *c, NET__LobbyChat *msg) {
/* NET__LobbyChat msg = NET__LOBBY_CHAT__INIT; */
/* msg.id = c_id; */
/* msg.message = message; */
_net_send_broadcast_message(c->peer->host, WRAPPER_MESSAGE__TYPE__LOBBY_CHAT, msg);
}
void _net_handle_handshake(ENetPeer *peer, HandshakeMessage* msg) {
client_t *c = client_create();
c->id = g_client_ids++;
c->name = malloc(strlen(msg->name));
c->country = malloc(strlen(msg->country));
// client
strcpy(c->name, msg->name);
strcpy(c->country, msg->country);
// network
c->peer = peer;
// let user know who's in the lobby
_net_send_lobby_who(c);
}
void _net_send_lobby_who(client_t *c) {
NET__LobbyWho msg = NET__LOBBY_WHO__INIT;
client_t *tmp_client;
int c_id = -1;
printf("looping over %d clients\n", g_client_count);
msg.n_clients = g_client_count;
msg.clients = malloc(g_client_count * sizeof(NET__Client*));
for(int i = 0, j = 0; i < g_client_count; i++) {
tmp_client = g_clients[i];
if ((tmp_client = g_clients[i]) == NULL) {
continue;
}
c_id = j++;
msg.clients[c_id] = malloc(sizeof(NET__Client));
net__client__init(msg.clients[c_id]);
msg.clients[c_id]->name = tmp_client->name;
msg.clients[c_id]->id = tmp_client->id;
printf("creating %d client\n", c_id);
}
_net_send_message(c->peer, WRAPPER_MESSAGE__TYPE__LOBBY_WHO, &msg);
for(int i = 0; i < c_id; i++ ) {
printf("freeing %d client\n", i+1);
free(msg.clients[i]);
}
free(msg.clients);
}
void _net_broadcast_lobby_join(client_t *c) {
NET__LobbyJoin msg = NET__LOBBY_JOIN__INIT;
msg.id = c->id;
_net_send_broadcast_message(c->peer->host, WRAPPER_MESSAGE__TYPE__LOBBY_JOIN, &msg);
}
void _net_broadcast_lobby_leave(client_t *c, char *reason) {
NET__LobbyLeave msg = NET__LOBBY_JOIN__INIT;
msg.id = c->id;
if ( reason != NULL ) {
msg.reason = reason;
}
_net_send_broadcast_message(c->peer->host, WRAPPER_MESSAGE__TYPE__LOBBY_LEAVE, &msg);
}
void _net_handle_input(client_t *c, InputMessage* msg) {
input_handle_action(c, msg->action, msg->press);
}
void net_client_handle_message(ENetEvent event) {
WrapperMessage *msg;
msg = wrapper_message__unpack(
NULL,
event.packet->dataLength,
event.packet->data
);
if ( msg == NULL ) {
fprintf(stderr, "Error decoding message\n");
exit(EXIT_FAILURE);
}
switch(msg->type) {
case WRAPPER_MESSAGE__TYPE__LOBBY_JOIN:
_net_client_handle_lobby_join(msg->lobby_join_message); break;
case WRAPPER_MESSAGE__TYPE__LOBBY_LEAVE:
_net_client_handle_lobby_leave(msg->lobby_leave_message); break;
case WRAPPER_MESSAGE__TYPE__LOBBY_CHAT:
_net_client_handle_lobby_chat(msg->lobby_chat_message); break;
case WRAPPER_MESSAGE__TYPE__LOBBY_WHO:
_net_client_handle_lobby_who(msg->lobby_who_message); break;
}
wrapper_message__free_unpacked(msg, NULL);
}
void _net_client_handle_lobby_join(NET__LobbyJoin* msg) {
client_t *c = client_find_by_id(msg->id);
printf("%s: has joined the lobby\n", c->name);
}
void _net_client_handle_lobby_leave(NET__LobbyLeave* msg) {
client_t *c = client_find_by_id(msg->id);
printf("%s: has left the lobby\n", c->name);
}
void _net_client_handle_lobby_chat(NET__LobbyChat* msg) {
client_t *c = client_find_by_id(msg->id);
printf("%s: %s\n", c->name, msg->message);
}
void _net_client_handle_lobby_who(NET__LobbyWho* msg) {
NET__Client **clients = msg->clients;
NET__Client *c;
printf("%d clients:\n", msg->n_clients);
for (int i = 0; i < msg->n_clients; i++ ) {
c = msg->clients[i];
printf("%d|%s\n", c->id, c->name);
}
}
void _net_send_message_packet(void *peer_or_host, int type, void *data, int broadcast) {
WrapperMessage msg = WRAPPER_MESSAGE__INIT;
ENetPacket *packet;
int packed_size;
void *packet_data;
msg.type = type;
switch(type) {
case WRAPPER_MESSAGE__TYPE__HANDSHAKE:
msg.handshake_message = data; break;
case WRAPPER_MESSAGE__TYPE__INPUT:
msg.input_message = data; break;
case WRAPPER_MESSAGE__TYPE__LOBBY_JOIN:
msg.lobby_join_message = data; break;
case WRAPPER_MESSAGE__TYPE__LOBBY_LEAVE:
msg.lobby_leave_message = data; break;
case WRAPPER_MESSAGE__TYPE__LOBBY_CHAT:
msg.lobby_chat_message = data; break;
case WRAPPER_MESSAGE__TYPE__LOBBY_WHO:
msg.lobby_who_message = data; break;
}
packed_size = wrapper_message__get_packed_size(&msg);
packet_data = malloc(packed_size);
wrapper_message__pack(&msg, packet_data);
printf("packed_size: %d\n", packed_size);
packet = enet_packet_create(
packet_data,
packed_size,
ENET_PACKET_FLAG_RELIABLE
);
printf("got a packet %p\n", packet);
printf("got a peer %p\n", peer_or_host);
if (broadcast == NET_BROADCAST) {
enet_host_broadcast(peer_or_host, 0, packet);
} else if ( broadcast == NET_SEND ) {
enet_peer_send(peer_or_host, 0, packet);
} else {
printf("_net_send_message_packet receiving NULL value for broadcast\n");
}
free(packet_data);
}
void _net_send_message(ENetPeer *peer, int type, void *data) {
_net_send_message_packet(peer, type, data, NET_SEND);
}
void _net_send_broadcast_message(ENetHost *host, int type, void *data) {
_net_send_message_packet(host, type, data, NET_BROADCAST);
}
void _net_client_send_handshake(ENetPeer *peer, char *name, char *country) {
HandshakeMessage msg = HANDSHAKE_MESSAGE__INIT;
msg.name = name;
msg.country = country;
_net_send_message(peer, WRAPPER_MESSAGE__TYPE__HANDSHAKE, &msg);
}
void _net_client_send_input(ENetPeer *peer, int action, int press) {
InputMessage msg = INPUT_MESSAGE__INIT;
msg.action = action;
msg.press = press;
_net_send_message(peer, WRAPPER_MESSAGE__TYPE__INPUT, &msg);
}
void _net_client_send_lobby_chat(ENetPeer *peer, int c_id, char *message) {
NET__LobbyChat msg = NET__LOBBY_CHAT__INIT;
msg.id = c_id;
msg.message = message;
_net_send_message(peer, WRAPPER_MESSAGE__TYPE__LOBBY_CHAT, &msg);
}