project_files/frontlib/net/netconn_callbacks.c
author nemo
Mon, 22 Dec 2014 17:47:22 -0500
changeset 10695 e295995348f9
parent 10017 de822cd3df3a
permissions -rw-r--r--
this bugs me every time I look at it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    20
#include "netconn_internal.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    22
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    23
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    24
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    25
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    26
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    27
#include <ctype.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    28
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    29
static void defaultCallback_onMessage(void *context, int msgtype, const char *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    30
    flib_log_i("Net: [%i] %s", msgtype, msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    31
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    32
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    33
static void defaultCallback_onChat(void *context, const char *nick, const char *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    34
    flib_log_i("%s: %s", nick, msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    35
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    37
// Change the name by suffixing it with a number. If it already ends in a number, increase that number by 1.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    38
static void defaultCallback_onNickTaken(void *context, const char *requestedNick) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    39
    flib_netconn *conn = context;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    40
    size_t namelen = strlen(requestedNick);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    41
    int digits = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    42
    while(digits<namelen && isdigit(requestedNick[namelen-1-digits])) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    43
        digits++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    44
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    45
    long suffix = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    46
    if(digits>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    47
        suffix = atol(requestedNick+namelen-digits)+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    48
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    49
    char *newPlayerName = flib_asprintf("%.*s%li", namelen-digits, requestedNick, suffix);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    50
    if(newPlayerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    51
        flib_netconn_send_nick(conn, newPlayerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    52
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    53
        flib_netconn_send_quit(conn, "Nick already taken.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    54
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    55
    free(newPlayerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    56
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    57
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    58
// Default behavior: Quit
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    59
static void defaultCallback_onPasswordRequest(void *context, const char *requestedNick) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    60
    flib_netconn_send_quit((flib_netconn*)context, "Authentication failed");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    61
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    62
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    63
void netconn_clearCallbacks(flib_netconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    64
    flib_netconn_onMessage(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    65
    flib_netconn_onConnected(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    66
    flib_netconn_onDisconnected(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    67
    flib_netconn_onRoomlist(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    68
    flib_netconn_onRoomAdd(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    69
    flib_netconn_onRoomDelete(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    70
    flib_netconn_onRoomUpdate(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    71
    flib_netconn_onClientFlags(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    72
    flib_netconn_onChat(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    73
    flib_netconn_onLobbyJoin(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    74
    flib_netconn_onLobbyLeave(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    75
    flib_netconn_onRoomJoin(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    76
    flib_netconn_onRoomLeave(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    77
    flib_netconn_onNickTaken(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    78
    flib_netconn_onPasswordRequest(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    79
    flib_netconn_onEnterRoom(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    80
    flib_netconn_onLeaveRoom(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    81
    flib_netconn_onTeamAdd(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    82
    flib_netconn_onTeamDelete(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    83
    flib_netconn_onRunGame(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    84
    flib_netconn_onTeamAccepted(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    85
    flib_netconn_onHogCountChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    86
    flib_netconn_onTeamColorChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    87
    flib_netconn_onEngineMessage(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    88
    flib_netconn_onSchemeChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    89
    flib_netconn_onMapChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    90
    flib_netconn_onScriptChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    91
    flib_netconn_onWeaponsetChanged(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    92
    flib_netconn_onServerVar(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    93
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    94
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    95
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    96
 * This macro generates a callback setter function. It uses the name of the callback to
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    97
 * automatically generate the function name and the fields to set, so a consistent naming
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    98
 * convention needs to be enforced (not that that is a bad thing). If null is passed as
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    99
 * callback to the generated function, the defaultCb will be set instead (with conn
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   100
 * as the context).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   101
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   102
#define GENERATE_CB_SETTER(cbName, cbParameterTypes, defaultCb) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   103
    void flib_netconn_##cbName(flib_netconn *conn, void (*callback)cbParameterTypes, void *context) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   104
        if(!log_badargs_if(conn==NULL)) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   105
            conn->cbName##Cb = callback ? callback : &defaultCb; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   106
            conn->cbName##Ctx = callback ? context : conn; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   107
        } \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   108
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   109
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   110
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   111
 * Generate a callback setter function like GENERATE_CB_SETTER, and automatically generate a
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   112
 * no-op callback function as well that is used as default.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   113
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   114
#define GENERATE_CB_SETTER_AND_DEFAULT(cbName, cbParameterTypes) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   115
    static void _noop_callback_##cbName cbParameterTypes {} \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   116
    GENERATE_CB_SETTER(cbName, cbParameterTypes, _noop_callback_##cbName)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   117
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   118
GENERATE_CB_SETTER(onMessage, (void *context, int msgtype, const char *msg), defaultCallback_onMessage);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   119
GENERATE_CB_SETTER_AND_DEFAULT(onConnected, (void *context));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   120
GENERATE_CB_SETTER_AND_DEFAULT(onDisconnected, (void *context, int reason, const char *message));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   121
GENERATE_CB_SETTER_AND_DEFAULT(onRoomlist, (void *context, const flib_room **rooms, int roomCount));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   122
GENERATE_CB_SETTER_AND_DEFAULT(onRoomAdd, (void *context, const flib_room *room));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   123
GENERATE_CB_SETTER_AND_DEFAULT(onRoomDelete, (void *context, const char *name));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   124
GENERATE_CB_SETTER_AND_DEFAULT(onRoomUpdate, (void *context, const char *oldName, const flib_room *room));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   125
GENERATE_CB_SETTER_AND_DEFAULT(onClientFlags, (void *context, const char *nick, const char *flags, bool newFlagState));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   126
GENERATE_CB_SETTER(onChat, (void *context, const char *nick, const char *msg), defaultCallback_onChat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   127
GENERATE_CB_SETTER_AND_DEFAULT(onLobbyJoin, (void *context, const char *nick));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   128
GENERATE_CB_SETTER_AND_DEFAULT(onLobbyLeave, (void *context, const char *nick, const char *partMsg));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   129
GENERATE_CB_SETTER_AND_DEFAULT(onRoomJoin, (void *context, const char *nick));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   130
GENERATE_CB_SETTER_AND_DEFAULT(onRoomLeave, (void *context, const char *nick, const char *partMessage));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   131
GENERATE_CB_SETTER(onNickTaken, (void *context, const char *nick), defaultCallback_onNickTaken);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   132
GENERATE_CB_SETTER(onPasswordRequest, (void *context, const char *nick), defaultCallback_onPasswordRequest);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   133
GENERATE_CB_SETTER_AND_DEFAULT(onEnterRoom, (void *context, bool chief));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   134
GENERATE_CB_SETTER_AND_DEFAULT(onLeaveRoom, (void *context, int reason, const char *message));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   135
GENERATE_CB_SETTER_AND_DEFAULT(onTeamAdd, (void *context, const flib_team *team));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   136
GENERATE_CB_SETTER_AND_DEFAULT(onTeamDelete, (void *context, const char *teamname));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   137
GENERATE_CB_SETTER_AND_DEFAULT(onRunGame, (void *context));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   138
GENERATE_CB_SETTER_AND_DEFAULT(onTeamAccepted, (void *context, const char *teamName));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   139
GENERATE_CB_SETTER_AND_DEFAULT(onHogCountChanged, (void *context, const char *teamName, int hogs));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   140
GENERATE_CB_SETTER_AND_DEFAULT(onTeamColorChanged, (void *context, const char *teamName, int colorIndex));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   141
GENERATE_CB_SETTER_AND_DEFAULT(onEngineMessage, (void *context, const uint8_t *message, size_t size));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   142
GENERATE_CB_SETTER_AND_DEFAULT(onSchemeChanged, (void *context, const flib_scheme *scheme));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   143
GENERATE_CB_SETTER_AND_DEFAULT(onMapChanged, (void *context, const flib_map *map, int changetype));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   144
GENERATE_CB_SETTER_AND_DEFAULT(onScriptChanged, (void *context, const char *script));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   145
GENERATE_CB_SETTER_AND_DEFAULT(onWeaponsetChanged, (void *context, const flib_weaponset *weaponset));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   146
GENERATE_CB_SETTER_AND_DEFAULT(onServerVar, (void *context, const char *name, const char *value));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   147
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   148
#undef GENERATE_CB_SETTER_AND_DEFAULT
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   149
#undef GENERATE_CB_SETTER