--- a/project_files/frontlib/net/netconn.c Mon Jun 25 00:42:07 2012 +0200
+++ b/project_files/frontlib/net/netconn.c Mon Jun 25 15:21:18 2012 +0200
@@ -33,89 +33,6 @@
#include <errno.h>
#include <ctype.h>
-static void defaultCallback_onMessage(void *context, int msgtype, const char *msg) {
- flib_log_i("Net: [%i] %s", msgtype, msg);
-}
-
-static void defaultCallback_void(void *context) {}
-static void defaultCallback_bool(void *context, bool isChief) {}
-static void defaultCallback_str(void *context, const char *str) {}
-static void defaultCallback_int_str(void *context, int i, const char *str) {}
-static void defaultCallback_str_str(void *context, const char *str1, const char *str2) {}
-static void defaultCallback_str_bool(void *context, const char *str, bool b) {}
-static void defaultCallback_str_int(void *context, const char *str, int i) {}
-
-static void defaultCallback_onRoomAdd(void *context, const flib_roomlist_room *room) {}
-static void defaultCallback_onRoomUpdate(void *context, const char *oldName, const flib_roomlist_room *room) {}
-static void defaultCallback_onChat(void *context, const char *nick, const char *msg) {
- flib_log_i("%s: %s", nick, msg);
-}
-
-// Change the name by suffixing it with a number. If it already ends in a number, increase that number by 1.
-static void defaultCallback_onNickTaken(void *context, const char *requestedNick) {
- flib_netconn *conn = context;
- size_t namelen = strlen(requestedNick);
- int digits = 0;
- while(digits<namelen && isdigit(requestedNick[namelen-1-digits])) {
- digits++;
- }
- long suffix = 0;
- if(digits>0) {
- suffix = atol(requestedNick+namelen-digits)+1;
- }
- char *newPlayerName = flib_asprintf("%.*s%li", namelen-digits, requestedNick, suffix);
- if(newPlayerName) {
- flib_netconn_send_nick(conn, newPlayerName);
- } else {
- flib_netconn_send_quit(conn, "Nick already taken.");
- }
- free(newPlayerName);
-}
-
-// Default behavior: Quit
-static void defaultCallback_onPasswordRequest(void *context, const char *requestedNick) {
- flib_netconn_send_quit((flib_netconn*)context, "Authentication failed");
-}
-
-static void defaultCallback_onTeamAdd(void *context, flib_team *team) {}
-static void defaultCallback_onTeamColorChanged(void *context, const char *teamName, uint32_t color) {}
-static void defaultCallback_onCfgScheme(void *context, flib_cfg *scheme) {}
-static void defaultCallback_onMapChanged(void *context, const flib_map *map, int changetype) {}
-static void defaultCallback_onWeaponsetChanged(void *context, flib_weaponset *weaponset) {}
-
-static void clearCallbacks(flib_netconn *conn) {
- flib_netconn_onMessage(conn, NULL, NULL);
- flib_netconn_onConnected(conn, NULL, NULL);
- flib_netconn_onDisconnected(conn, NULL, NULL);
- flib_netconn_onRoomAdd(conn, NULL, NULL);
- flib_netconn_onRoomDelete(conn, NULL, NULL);
- flib_netconn_onRoomUpdate(conn, NULL, NULL);
- flib_netconn_onChat(conn, NULL, NULL);
- flib_netconn_onLobbyJoin(conn, NULL, NULL);
- flib_netconn_onLobbyLeave(conn, NULL, NULL);
- flib_netconn_onRoomJoin(conn, NULL, NULL);
- flib_netconn_onRoomLeave(conn, NULL, NULL);
- flib_netconn_onNickTaken(conn, NULL, NULL);
- flib_netconn_onPasswordRequest(conn, NULL, NULL);
- flib_netconn_onRoomChiefStatus(conn, NULL, NULL);
- flib_netconn_onReadyState(conn, NULL, NULL);
- flib_netconn_onEnterRoom(conn, NULL, NULL);
- flib_netconn_onLeaveRoom(conn, NULL, NULL);
- flib_netconn_onTeamAdd(conn, NULL, NULL);
- flib_netconn_onTeamDelete(conn, NULL, NULL);
- flib_netconn_onRunGame(conn, NULL, NULL);
- flib_netconn_onTeamAccepted(conn, NULL, NULL);
- flib_netconn_onHogCountChanged(conn, NULL, NULL);
- flib_netconn_onTeamColorChanged(conn, NULL, NULL);
- flib_netconn_onEngineMessage(conn, NULL, NULL);
- flib_netconn_onCfgScheme(conn, NULL, NULL);
- flib_netconn_onMapChanged(conn, NULL, NULL);
- flib_netconn_onScriptChanged(conn, NULL, NULL);
- flib_netconn_onWeaponsetChanged(conn, NULL, NULL);
- flib_netconn_onAdminAccess(conn, NULL, NULL);
- flib_netconn_onServerVar(conn, NULL, NULL);
-}
-
flib_netconn *flib_netconn_create(const char *playerName, flib_cfg_meta *metacfg, const char *host, uint16_t port) {
flib_netconn *result = NULL;
if(!playerName || !metacfg || !host) {
@@ -185,286 +102,6 @@
return result;
}
-/*
- * Callback registration functions
- */
-
-void flib_netconn_onMessage(flib_netconn *conn, void (*callback)(void *context, int msgtype, const char *msg), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onMessage");
- } else {
- conn->onMessageCb = callback ? callback : &defaultCallback_onMessage;
- conn->onMessageCtx = context;
- }
-}
-
-void flib_netconn_onConnected(flib_netconn *conn, void (*callback)(void *context), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onConnected");
- } else {
- conn->onConnectedCb = callback ? callback : &defaultCallback_void;
- conn->onConnectedCtx = context;
- }
-}
-
-void flib_netconn_onDisconnected(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onDisconnected");
- } else {
- conn->onDisconnectedCb = callback ? callback : &defaultCallback_int_str;
- conn->onDisconnectedCtx = context;
- }
-}
-
-void flib_netconn_onRoomAdd(flib_netconn *conn, void (*callback)(void *context, const flib_roomlist_room *room), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomAdd");
- } else {
- conn->onRoomAddCb = callback ? callback : &defaultCallback_onRoomAdd;
- conn->onRoomAddCtx = context;
- }
-}
-
-void flib_netconn_onRoomDelete(flib_netconn *conn, void (*callback)(void *context, const char *name), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomDelete");
- } else {
- conn->onRoomDeleteCb = callback ? callback : &defaultCallback_str;
- conn->onRoomDeleteCtx = context;
- }
-}
-
-void flib_netconn_onRoomUpdate(flib_netconn *conn, void (*callback)(void *context, const char *oldName, const flib_roomlist_room *room), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomUpdate");
- } else {
- conn->onRoomUpdateCb = callback ? callback : &defaultCallback_onRoomUpdate;
- conn->onRoomUpdateCtx = context;
- }
-}
-
-void flib_netconn_onChat(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *msg), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onChat");
- } else {
- conn->onChatCb = callback ? callback : &defaultCallback_onChat;
- conn->onChatCtx = context;
- }
-}
-
-void flib_netconn_onLobbyJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onLobbyJoin");
- } else {
- conn->onLobbyJoinCb = callback ? callback : &defaultCallback_str;
- conn->onLobbyJoinCtx = context;
- }
-}
-
-void flib_netconn_onLobbyLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMsg), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onLobbyLeave");
- } else {
- conn->onLobbyLeaveCb = callback ? callback : &defaultCallback_str_str;
- conn->onLobbyLeaveCtx = context;
- }
-}
-
-void flib_netconn_onRoomJoin(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomJoin");
- } else {
- conn->onRoomJoinCb = callback ? callback : &defaultCallback_str;
- conn->onRoomJoinCtx = context;
- }
-}
-
-void flib_netconn_onRoomLeave(flib_netconn *conn, void (*callback)(void *context, const char *nick, const char *partMessage), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomLeave");
- } else {
- conn->onRoomLeaveCb = callback ? callback : &defaultCallback_str_str;
- conn->onRoomLeaveCtx = context;
- }
-}
-
-void flib_netconn_onNickTaken(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onNickTaken");
- } else if(!callback) {
- conn->onNickTakenCb = &defaultCallback_onNickTaken;
- conn->onNickTakenCtx = conn;
- } else {
- conn->onNickTakenCb = callback;
- conn->onNickTakenCtx = context;
- }
-}
-
-void flib_netconn_onPasswordRequest(flib_netconn *conn, void (*callback)(void *context, const char *nick), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onPasswordRequest");
- } else if(!callback) {
- conn->onPasswordRequestCb = &defaultCallback_onPasswordRequest;
- conn->onPasswordRequestCtx = conn;
- } else {
- conn->onPasswordRequestCb = callback;
- conn->onPasswordRequestCtx = context;
- }
-}
-
-void flib_netconn_onRoomChiefStatus(flib_netconn *conn, void (*callback)(void *context, bool chief), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRoomChiefStatus");
- } else {
- conn->onRoomChiefStatusCb = callback ? callback : &defaultCallback_bool;
- conn->onRoomChiefStatusCtx = context;
- }
-}
-
-void flib_netconn_onReadyState(flib_netconn *conn, void (*callback)(void *context, const char *nick, bool ready), void* context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onReadyState");
- } else {
- conn->onReadyStateCb = callback ? callback : &defaultCallback_str_bool;
- conn->onReadyStateCtx = context;
- }
-}
-
-void flib_netconn_onEnterRoom(flib_netconn *conn, void (*callback)(void *context, bool chief), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onEnterRoom");
- } else {
- conn->onEnterRoomCb = callback ? callback : &defaultCallback_bool;
- conn->onEnterRoomCtx = context;
- }
-}
-
-void flib_netconn_onLeaveRoom(flib_netconn *conn, void (*callback)(void *context, int reason, const char *message), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onLeaveRoom");
- } else {
- conn->onLeaveRoomCb = callback ? callback : &defaultCallback_int_str;
- conn->onLeaveRoomCtx = context;
- }
-}
-
-void flib_netconn_onTeamAdd(flib_netconn *conn, void (*callback)(void *context, flib_team *team), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onTeamAdd");
- } else {
- conn->onTeamAddCb = callback ? callback : &defaultCallback_onTeamAdd;
- conn->onTeamAddCtx = context;
- }
-}
-
-void flib_netconn_onTeamDelete(flib_netconn *conn, void (*callback)(void *context, const char *teamname), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onTeamDelete");
- } else {
- conn->onTeamDeleteCb = callback ? callback : &defaultCallback_str;
- conn->onTeamDeleteCtx = context;
- }
-}
-
-void flib_netconn_onRunGame(flib_netconn *conn, void (*callback)(void *context), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onRunGame");
- } else {
- conn->onRunGameCb = callback ? callback : &defaultCallback_void;
- conn->onRunGameCtx = context;
- }
-}
-
-void flib_netconn_onTeamAccepted(flib_netconn *conn, void (*callback)(void *context, const char *teamName), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onTeamAccepted");
- } else {
- conn->onTeamAcceptedCb = callback ? callback : &defaultCallback_str;
- conn->onTeamAcceptedCtx = context;
- }
-}
-
-void flib_netconn_onHogCountChanged(flib_netconn *conn, void (*callback)(void *context, const char *teamName, int hogs), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onHogCountChanged");
- } else {
- conn->onHogCountChangedCb = callback ? callback : &defaultCallback_str_int;
- conn->onHogCountChangedCtx = context;
- }
-}
-
-void flib_netconn_onTeamColorChanged(flib_netconn *conn, void (*callback)(void *context, const char *teamName, uint32_t colorARGB), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onTeamColorChanged");
- } else {
- conn->onTeamColorChangedCb = callback ? callback : &defaultCallback_onTeamColorChanged;
- conn->onTeamColorChangedCtx = context;
- }
-}
-
-void flib_netconn_onEngineMessage(flib_netconn *conn, void (*callback)(void *context, const char *message, int size), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onEngineMessage");
- } else {
- conn->onEngineMessageCb = callback ? callback : &defaultCallback_str_int;
- conn->onEngineMessageCtx = context;
- }
-}
-
-void flib_netconn_onCfgScheme(flib_netconn *conn, void (*callback)(void *context, flib_cfg *scheme), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onCfgScheme");
- } else {
- conn->onCfgSchemeCb = callback ? callback : &defaultCallback_onCfgScheme;
- conn->onCfgSchemeCtx = context;
- }
-}
-
-void flib_netconn_onMapChanged(flib_netconn *conn, void (*callback)(void *context, const flib_map *map, int changetype), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onMapChanged");
- } else {
- conn->onMapChangedCb = callback ? callback : &defaultCallback_onMapChanged;
- conn->onMapChangedCtx = context;
- }
-}
-
-void flib_netconn_onScriptChanged(flib_netconn *conn, void (*callback)(void *context, const char *script), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onScriptChanged");
- } else {
- conn->onScriptChangedCb = callback ? callback : &defaultCallback_str;
- conn->onScriptChangedCtx = context;
- }
-}
-
-void flib_netconn_onWeaponsetChanged(flib_netconn *conn, void (*callback)(void *context, flib_weaponset *weaponset), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onWeaponsetChanged");
- } else {
- conn->onWeaponsetChangedCb = callback ? callback : &defaultCallback_onWeaponsetChanged;
- conn->onWeaponsetChangedCtx = context;
- }
-}
-
-void flib_netconn_onAdminAccess(flib_netconn *conn, void (*callback)(void *context), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onAdminAccess");
- } else {
- conn->onAdminAccessCb = callback ? callback : &defaultCallback_void;
- conn->onAdminAccessCtx = context;
- }
-}
-
-void flib_netconn_onServerVar(flib_netconn *conn, void (*callback)(void *context, const char *name, const char *value), void *context) {
- if(!conn) {
- flib_log_e("null parameter in flib_netconn_onServerVar");
- } else {
- conn->onServerVarCb = callback ? callback : &defaultCallback_str_str;
- conn->onServerVarCtx = context;
- }
-}
-
void leaveRoom(flib_netconn *conn) {
conn->netconnState = NETCONN_STATE_LOBBY;
conn->isChief = false;