30 |
30 |
31 #include <stdlib.h> |
31 #include <stdlib.h> |
32 #include <string.h> |
32 #include <string.h> |
33 #include <errno.h> |
33 #include <errno.h> |
34 #include <ctype.h> |
34 #include <ctype.h> |
35 |
|
36 static void defaultCallback_onMessage(void *context, int msgtype, const char *msg) { |
|
37 flib_log_i("Net: [%i] %s", msgtype, msg); |
|
38 } |
|
39 |
|
40 static void defaultCallback_void(void *context) {} |
|
41 static void defaultCallback_bool(void *context, bool isChief) {} |
|
42 static void defaultCallback_str(void *context, const char *str) {} |
|
43 static void defaultCallback_int_str(void *context, int i, const char *str) {} |
|
44 static void defaultCallback_str_str(void *context, const char *str1, const char *str2) {} |
|
45 static void defaultCallback_str_bool(void *context, const char *str, bool b) {} |
|
46 static void defaultCallback_str_int(void *context, const char *str, int i) {} |
|
47 |
|
48 static void defaultCallback_onRoomAdd(void *context, const flib_roomlist_room *room) {} |
|
49 static void defaultCallback_onRoomUpdate(void *context, const char *oldName, const flib_roomlist_room *room) {} |
|
50 static void defaultCallback_onChat(void *context, const char *nick, const char *msg) { |
|
51 flib_log_i("%s: %s", nick, msg); |
|
52 } |
|
53 |
|
54 // Change the name by suffixing it with a number. If it already ends in a number, increase that number by 1. |
|
55 static void defaultCallback_onNickTaken(void *context, const char *requestedNick) { |
|
56 flib_netconn *conn = context; |
|
57 size_t namelen = strlen(requestedNick); |
|
58 int digits = 0; |
|
59 while(digits<namelen && isdigit(requestedNick[namelen-1-digits])) { |
|
60 digits++; |
|
61 } |
|
62 long suffix = 0; |
|
63 if(digits>0) { |
|
64 suffix = atol(requestedNick+namelen-digits)+1; |
|
65 } |
|
66 char *newPlayerName = flib_asprintf("%.*s%li", namelen-digits, requestedNick, suffix); |
|
67 if(newPlayerName) { |
|
68 flib_netconn_send_nick(conn, newPlayerName); |
|
69 } else { |
|
70 flib_netconn_send_quit(conn, "Nick already taken."); |
|
71 } |
|
72 free(newPlayerName); |
|
73 } |
|
74 |
|
75 // Default behavior: Quit |
|
76 static void defaultCallback_onPasswordRequest(void *context, const char *requestedNick) { |
|
77 flib_netconn_send_quit((flib_netconn*)context, "Authentication failed"); |
|
78 } |
|
79 |
|
80 static void defaultCallback_onTeamAdd(void *context, flib_team *team) {} |
|
81 static void defaultCallback_onTeamColorChanged(void *context, const char *teamName, uint32_t color) {} |
|
82 static void defaultCallback_onCfgScheme(void *context, flib_cfg *scheme) {} |
|
83 static void defaultCallback_onMapChanged(void *context, const flib_map *map, int changetype) {} |
|
84 static void defaultCallback_onWeaponsetChanged(void *context, flib_weaponset *weaponset) {} |
|
85 |
|
86 static void clearCallbacks(flib_netconn *conn) { |
|
87 flib_netconn_onMessage(conn, NULL, NULL); |
|
88 flib_netconn_onConnected(conn, NULL, NULL); |
|
89 flib_netconn_onDisconnected(conn, NULL, NULL); |
|
90 flib_netconn_onRoomAdd(conn, NULL, NULL); |
|
91 flib_netconn_onRoomDelete(conn, NULL, NULL); |
|
92 flib_netconn_onRoomUpdate(conn, NULL, NULL); |
|
93 flib_netconn_onChat(conn, NULL, NULL); |
|
94 flib_netconn_onLobbyJoin(conn, NULL, NULL); |
|
95 flib_netconn_onLobbyLeave(conn, NULL, NULL); |
|
96 flib_netconn_onRoomJoin(conn, NULL, NULL); |
|
97 flib_netconn_onRoomLeave(conn, NULL, NULL); |
|
98 flib_netconn_onNickTaken(conn, NULL, NULL); |
|
99 flib_netconn_onPasswordRequest(conn, NULL, NULL); |
|
100 flib_netconn_onRoomChiefStatus(conn, NULL, NULL); |
|
101 flib_netconn_onReadyState(conn, NULL, NULL); |
|
102 flib_netconn_onEnterRoom(conn, NULL, NULL); |
|
103 flib_netconn_onLeaveRoom(conn, NULL, NULL); |
|
104 flib_netconn_onTeamAdd(conn, NULL, NULL); |
|
105 flib_netconn_onTeamDelete(conn, NULL, NULL); |
|
106 flib_netconn_onRunGame(conn, NULL, NULL); |
|
107 flib_netconn_onTeamAccepted(conn, NULL, NULL); |
|
108 flib_netconn_onHogCountChanged(conn, NULL, NULL); |
|
109 flib_netconn_onTeamColorChanged(conn, NULL, NULL); |
|
110 flib_netconn_onEngineMessage(conn, NULL, NULL); |
|
111 flib_netconn_onCfgScheme(conn, NULL, NULL); |
|
112 flib_netconn_onMapChanged(conn, NULL, NULL); |
|
113 flib_netconn_onScriptChanged(conn, NULL, NULL); |
|
114 flib_netconn_onWeaponsetChanged(conn, NULL, NULL); |
|
115 flib_netconn_onAdminAccess(conn, NULL, NULL); |
|
116 flib_netconn_onServerVar(conn, NULL, NULL); |
|
117 } |
|
118 |
35 |
119 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *host, uint16_t port) { |
36 flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *host, uint16_t port) { |
120 flib_netconn *result = NULL; |
37 flib_netconn *result = NULL; |
121 if(!playerName || !metacfg || !host) { |
38 if(!playerName || !metacfg || !host) { |
122 flib_log_e("null parameter in flib_netconn_create"); |
39 flib_log_e("null parameter in flib_netconn_create"); |
181 flib_log_e("null parameter in flib_netconn_is_chief"); |
98 flib_log_e("null parameter in flib_netconn_is_chief"); |
182 } else if(conn->netconnState == NETCONN_STATE_ROOM || conn->netconnState == NETCONN_STATE_INGAME) { |
99 } else if(conn->netconnState == NETCONN_STATE_ROOM || conn->netconnState == NETCONN_STATE_INGAME) { |
183 result = conn->isChief; |
100 result = conn->isChief; |
184 } |
101 } |
185 return result; |
102 return result; |
186 } |
|
187 |
|
188 /* |
|
189 * Callback registration functions |
|
190 */ |
|
191 |
|
192 void flib_netconn_onMessage(flib_netconn *conn, void (*callback)(void *context, int msgtype, const char *msg), void* context) { |
|
193 if(!conn) { |
|
194 flib_log_e("null parameter in flib_netconn_onMessage"); |
|
195 } else { |
|
196 conn->onMessageCb = callback ? callback : &defaultCallback_onMessage; |
|
197 conn->onMessageCtx = context; |
|
198 } |
|
199 } |
|
200 |
|
201 void flib_netconn_onConnected(flib_netconn *conn, void (*callback)(void *context), void* context) { |
|
202 if(!conn) { |
|
203 flib_log_e("null parameter in flib_netconn_onConnected"); |
|
204 } else { |
|
205 conn->onConnectedCb = callback ? callback : &defaultCallback_void; |
|
206 conn->onConnectedCtx = context; |
|
207 } |
|
208 } |
|
209 |
|
210 void flib_netconn_onDisconnected(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void* context) { |
|
211 if(!conn) { |
|
212 flib_log_e("null parameter in flib_netconn_onDisconnected"); |
|
213 } else { |
|
214 conn->onDisconnectedCb = callback ? callback : &defaultCallback_int_str; |
|
215 conn->onDisconnectedCtx = context; |
|
216 } |
|
217 } |
|
218 |
|
219 void flib_netconn_onRoomAdd(flib_netconn *conn, void (*callback)(void *context, const flib_roomlist_room *room), void* context) { |
|
220 if(!conn) { |
|
221 flib_log_e("null parameter in flib_netconn_onRoomAdd"); |
|
222 } else { |
|
223 conn->onRoomAddCb = callback ? callback : &defaultCallback_onRoomAdd; |
|
224 conn->onRoomAddCtx = context; |
|
225 } |
|
226 } |
|
227 |
|
228 void flib_netconn_onRoomDelete(flib_netconn *conn, void (*callback)(void *context, const char *name), void* context) { |
|
229 if(!conn) { |
|
230 flib_log_e("null parameter in flib_netconn_onRoomDelete"); |
|
231 } else { |
|
232 conn->onRoomDeleteCb = callback ? callback : &defaultCallback_str; |
|
233 conn->onRoomDeleteCtx = context; |
|
234 } |
|
235 } |
|
236 |
|
237 void flib_netconn_onRoomUpdate(flib_netconn *conn, void (*callback)(void *context, const char *oldName, const flib_roomlist_room *room), void* context) { |
|
238 if(!conn) { |
|
239 flib_log_e("null parameter in flib_netconn_onRoomUpdate"); |
|
240 } else { |
|
241 conn->onRoomUpdateCb = callback ? callback : &defaultCallback_onRoomUpdate; |
|
242 conn->onRoomUpdateCtx = context; |
|
243 } |
|
244 } |
|
245 |
|
246 void flib_netconn_onChat(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *msg), void* context) { |
|
247 if(!conn) { |
|
248 flib_log_e("null parameter in flib_netconn_onChat"); |
|
249 } else { |
|
250 conn->onChatCb = callback ? callback : &defaultCallback_onChat; |
|
251 conn->onChatCtx = context; |
|
252 } |
|
253 } |
|
254 |
|
255 void flib_netconn_onLobbyJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) { |
|
256 if(!conn) { |
|
257 flib_log_e("null parameter in flib_netconn_onLobbyJoin"); |
|
258 } else { |
|
259 conn->onLobbyJoinCb = callback ? callback : &defaultCallback_str; |
|
260 conn->onLobbyJoinCtx = context; |
|
261 } |
|
262 } |
|
263 |
|
264 void flib_netconn_onLobbyLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMsg), void* context) { |
|
265 if(!conn) { |
|
266 flib_log_e("null parameter in flib_netconn_onLobbyLeave"); |
|
267 } else { |
|
268 conn->onLobbyLeaveCb = callback ? callback : &defaultCallback_str_str; |
|
269 conn->onLobbyLeaveCtx = context; |
|
270 } |
|
271 } |
|
272 |
|
273 void flib_netconn_onRoomJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) { |
|
274 if(!conn) { |
|
275 flib_log_e("null parameter in flib_netconn_onRoomJoin"); |
|
276 } else { |
|
277 conn->onRoomJoinCb = callback ? callback : &defaultCallback_str; |
|
278 conn->onRoomJoinCtx = context; |
|
279 } |
|
280 } |
|
281 |
|
282 void flib_netconn_onRoomLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMessage), void* context) { |
|
283 if(!conn) { |
|
284 flib_log_e("null parameter in flib_netconn_onRoomLeave"); |
|
285 } else { |
|
286 conn->onRoomLeaveCb = callback ? callback : &defaultCallback_str_str; |
|
287 conn->onRoomLeaveCtx = context; |
|
288 } |
|
289 } |
|
290 |
|
291 void flib_netconn_onNickTaken(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) { |
|
292 if(!conn) { |
|
293 flib_log_e("null parameter in flib_netconn_onNickTaken"); |
|
294 } else if(!callback) { |
|
295 conn->onNickTakenCb = &defaultCallback_onNickTaken; |
|
296 conn->onNickTakenCtx = conn; |
|
297 } else { |
|
298 conn->onNickTakenCb = callback; |
|
299 conn->onNickTakenCtx = context; |
|
300 } |
|
301 } |
|
302 |
|
303 void flib_netconn_onPasswordRequest(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) { |
|
304 if(!conn) { |
|
305 flib_log_e("null parameter in flib_netconn_onPasswordRequest"); |
|
306 } else if(!callback) { |
|
307 conn->onPasswordRequestCb = &defaultCallback_onPasswordRequest; |
|
308 conn->onPasswordRequestCtx = conn; |
|
309 } else { |
|
310 conn->onPasswordRequestCb = callback; |
|
311 conn->onPasswordRequestCtx = context; |
|
312 } |
|
313 } |
|
314 |
|
315 void flib_netconn_onRoomChiefStatus(flib_netconn *conn, void (*callback)(void *context, bool chief), void* context) { |
|
316 if(!conn) { |
|
317 flib_log_e("null parameter in flib_netconn_onRoomChiefStatus"); |
|
318 } else { |
|
319 conn->onRoomChiefStatusCb = callback ? callback : &defaultCallback_bool; |
|
320 conn->onRoomChiefStatusCtx = context; |
|
321 } |
|
322 } |
|
323 |
|
324 void flib_netconn_onReadyState(flib_netconn *conn, void (*callback)(void *context, const char *nick, bool ready), void* context) { |
|
325 if(!conn) { |
|
326 flib_log_e("null parameter in flib_netconn_onReadyState"); |
|
327 } else { |
|
328 conn->onReadyStateCb = callback ? callback : &defaultCallback_str_bool; |
|
329 conn->onReadyStateCtx = context; |
|
330 } |
|
331 } |
|
332 |
|
333 void flib_netconn_onEnterRoom(flib_netconn *conn, void (*callback)(void *context, bool chief), void *context) { |
|
334 if(!conn) { |
|
335 flib_log_e("null parameter in flib_netconn_onEnterRoom"); |
|
336 } else { |
|
337 conn->onEnterRoomCb = callback ? callback : &defaultCallback_bool; |
|
338 conn->onEnterRoomCtx = context; |
|
339 } |
|
340 } |
|
341 |
|
342 void flib_netconn_onLeaveRoom(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void *context) { |
|
343 if(!conn) { |
|
344 flib_log_e("null parameter in flib_netconn_onLeaveRoom"); |
|
345 } else { |
|
346 conn->onLeaveRoomCb = callback ? callback : &defaultCallback_int_str; |
|
347 conn->onLeaveRoomCtx = context; |
|
348 } |
|
349 } |
|
350 |
|
351 void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, flib_team *team), void *context) { |
|
352 if(!conn) { |
|
353 flib_log_e("null parameter in flib_netconn_onTeamAdd"); |
|
354 } else { |
|
355 conn->onTeamAddCb = callback ? callback : &defaultCallback_onTeamAdd; |
|
356 conn->onTeamAddCtx = context; |
|
357 } |
|
358 } |
|
359 |
|
360 void flib_netconn_onTeamDelete(flib_netconn *conn, void (*callback)(void *context, const char *teamname), void *context) { |
|
361 if(!conn) { |
|
362 flib_log_e("null parameter in flib_netconn_onTeamDelete"); |
|
363 } else { |
|
364 conn->onTeamDeleteCb = callback ? callback : &defaultCallback_str; |
|
365 conn->onTeamDeleteCtx = context; |
|
366 } |
|
367 } |
|
368 |
|
369 void flib_netconn_onRunGame(flib_netconn *conn, void (*callback)(void *context), void *context) { |
|
370 if(!conn) { |
|
371 flib_log_e("null parameter in flib_netconn_onRunGame"); |
|
372 } else { |
|
373 conn->onRunGameCb = callback ? callback : &defaultCallback_void; |
|
374 conn->onRunGameCtx = context; |
|
375 } |
|
376 } |
|
377 |
|
378 void flib_netconn_onTeamAccepted(flib_netconn *conn, void (*callback)(void *context, const char *teamName), void *context) { |
|
379 if(!conn) { |
|
380 flib_log_e("null parameter in flib_netconn_onTeamAccepted"); |
|
381 } else { |
|
382 conn->onTeamAcceptedCb = callback ? callback : &defaultCallback_str; |
|
383 conn->onTeamAcceptedCtx = context; |
|
384 } |
|
385 } |
|
386 |
|
387 void flib_netconn_onHogCountChanged(flib_netconn *conn, void (*callback)(void *context, const char *teamName, int hogs), void *context) { |
|
388 if(!conn) { |
|
389 flib_log_e("null parameter in flib_netconn_onHogCountChanged"); |
|
390 } else { |
|
391 conn->onHogCountChangedCb = callback ? callback : &defaultCallback_str_int; |
|
392 conn->onHogCountChangedCtx = context; |
|
393 } |
|
394 } |
|
395 |
|
396 void flib_netconn_onTeamColorChanged(flib_netconn *conn, void (*callback)(void *context, const char *teamName, uint32_t colorARGB), void *context) { |
|
397 if(!conn) { |
|
398 flib_log_e("null parameter in flib_netconn_onTeamColorChanged"); |
|
399 } else { |
|
400 conn->onTeamColorChangedCb = callback ? callback : &defaultCallback_onTeamColorChanged; |
|
401 conn->onTeamColorChangedCtx = context; |
|
402 } |
|
403 } |
|
404 |
|
405 void flib_netconn_onEngineMessage(flib_netconn *conn, void (*callback)(void *context, const char *message, int size), void *context) { |
|
406 if(!conn) { |
|
407 flib_log_e("null parameter in flib_netconn_onEngineMessage"); |
|
408 } else { |
|
409 conn->onEngineMessageCb = callback ? callback : &defaultCallback_str_int; |
|
410 conn->onEngineMessageCtx = context; |
|
411 } |
|
412 } |
|
413 |
|
414 void flib_netconn_onCfgScheme(flib_netconn *conn, void (*callback)(void *context, flib_cfg *scheme), void *context) { |
|
415 if(!conn) { |
|
416 flib_log_e("null parameter in flib_netconn_onCfgScheme"); |
|
417 } else { |
|
418 conn->onCfgSchemeCb = callback ? callback : &defaultCallback_onCfgScheme; |
|
419 conn->onCfgSchemeCtx = context; |
|
420 } |
|
421 } |
|
422 |
|
423 void flib_netconn_onMapChanged(flib_netconn *conn, void (*callback)(void *context, const flib_map *map, int changetype), void *context) { |
|
424 if(!conn) { |
|
425 flib_log_e("null parameter in flib_netconn_onMapChanged"); |
|
426 } else { |
|
427 conn->onMapChangedCb = callback ? callback : &defaultCallback_onMapChanged; |
|
428 conn->onMapChangedCtx = context; |
|
429 } |
|
430 } |
|
431 |
|
432 void flib_netconn_onScriptChanged(flib_netconn *conn, void (*callback)(void *context, const char *script), void *context) { |
|
433 if(!conn) { |
|
434 flib_log_e("null parameter in flib_netconn_onScriptChanged"); |
|
435 } else { |
|
436 conn->onScriptChangedCb = callback ? callback : &defaultCallback_str; |
|
437 conn->onScriptChangedCtx = context; |
|
438 } |
|
439 } |
|
440 |
|
441 void flib_netconn_onWeaponsetChanged(flib_netconn *conn, void (*callback)(void *context, flib_weaponset *weaponset), void *context) { |
|
442 if(!conn) { |
|
443 flib_log_e("null parameter in flib_netconn_onWeaponsetChanged"); |
|
444 } else { |
|
445 conn->onWeaponsetChangedCb = callback ? callback : &defaultCallback_onWeaponsetChanged; |
|
446 conn->onWeaponsetChangedCtx = context; |
|
447 } |
|
448 } |
|
449 |
|
450 void flib_netconn_onAdminAccess(flib_netconn *conn, void (*callback)(void *context), void *context) { |
|
451 if(!conn) { |
|
452 flib_log_e("null parameter in flib_netconn_onAdminAccess"); |
|
453 } else { |
|
454 conn->onAdminAccessCb = callback ? callback : &defaultCallback_void; |
|
455 conn->onAdminAccessCtx = context; |
|
456 } |
|
457 } |
|
458 |
|
459 void flib_netconn_onServerVar(flib_netconn *conn, void (*callback)(void *context, const char *name, const char *value), void *context) { |
|
460 if(!conn) { |
|
461 flib_log_e("null parameter in flib_netconn_onServerVar"); |
|
462 } else { |
|
463 conn->onServerVarCb = callback ? callback : &defaultCallback_str_str; |
|
464 conn->onServerVarCtx = context; |
|
465 } |
|
466 } |
103 } |
467 |
104 |
468 void leaveRoom(flib_netconn *conn) { |
105 void leaveRoom(flib_netconn *conn) { |
469 conn->netconnState = NETCONN_STATE_LOBBY; |
106 conn->netconnState = NETCONN_STATE_LOBBY; |
470 conn->isChief = false; |
107 conn->isChief = false; |