project_files/frontlib/ipc/gameconn.c
author Wuzzy <Wuzzy2@mail.ru>
Sat, 27 Oct 2018 03:06:28 +0200
changeset 14000 ccb0ef084596
parent 10017 de822cd3df3a
permissions -rw-r--r--
Add credits for shoryuken_hit.ogg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    20
#include "gameconn.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    21
#include "ipcbase.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    22
#include "ipcprotocol.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    23
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    24
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    25
#include "../hwconsts.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    26
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    27
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    28
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    29
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    30
typedef enum {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    31
    AWAIT_CONNECTION,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    32
    CONNECTED,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    33
    FINISHED
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    34
} gameconn_state;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    35
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    36
struct _flib_gameconn {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    37
    flib_ipcbase *ipcBase;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    38
    flib_vector *configBuffer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    39
    flib_vector *demoBuffer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    40
    char *playerName;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    41
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    42
    gameconn_state state;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    43
    bool netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    44
    int disconnectReason;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    46
    void (*onConnectCb)(void* context);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    47
    void *onConnectCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    49
    void (*onDisconnectCb)(void* context, int reason);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    50
    void *onDisconnectCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    52
    void (*onErrorMessageCb)(void* context, const char *msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    53
    void *onErrorMessageCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    54
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    55
    void (*onChatCb)(void* context, const char *msg, bool teamchat);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    56
    void *onChatCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    57
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    58
    void (*onGameRecordedCb)(void *context, const uint8_t *record, size_t size, bool isSavegame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    59
    void *onGameRecordedCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    60
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    61
    void (*onEngineMessageCb)(void *context, const uint8_t *em, size_t size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    62
    void *onEngineMessageCtx;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    63
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    64
    bool running;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    65
    bool destroyRequested;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    66
};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    68
static void defaultCallback_onErrorMessage(void* context, const char *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    69
    flib_log_w("Error from engine (no callback set): %s", msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    70
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    71
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    72
static void clearCallbacks(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    73
    flib_gameconn_onConnect(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    74
    flib_gameconn_onDisconnect(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    75
    flib_gameconn_onErrorMessage(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    76
    flib_gameconn_onChat(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    77
    flib_gameconn_onGameRecorded(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    78
    flib_gameconn_onEngineMessage(conn, NULL, NULL);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    79
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    80
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    81
static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    82
    flib_gameconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    83
    flib_gameconn *tempConn = flib_calloc(1, sizeof(flib_gameconn));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    84
    if(tempConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    85
        tempConn->ipcBase = flib_ipcbase_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    86
        tempConn->configBuffer = flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    87
        tempConn->playerName = flib_strdupnull(playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    88
        if(tempConn->ipcBase && tempConn->configBuffer && tempConn->playerName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    89
            if(record) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    90
                tempConn->demoBuffer = flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    91
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    92
            tempConn->state = AWAIT_CONNECTION;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    93
            tempConn->netgame = netGame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    94
            tempConn->disconnectReason = GAME_END_ERROR;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    95
            clearCallbacks(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    96
            result = tempConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    97
            tempConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    98
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
    99
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   100
    flib_gameconn_destroy(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   101
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   102
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   103
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   104
flib_gameconn *flib_gameconn_create(const char *playerName, const flib_gamesetup *setup, bool netgame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   105
    if(log_badargs_if2(playerName==NULL, setup==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   106
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   107
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   108
    flib_gameconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   109
    flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, netgame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   110
    if(tempConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   111
        if(flib_ipc_append_fullconfig(tempConn->configBuffer, setup, netgame)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   112
            flib_log_e("Error generating full game configuration for the engine.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   113
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   114
            result = tempConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   115
            tempConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   116
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   117
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   118
    flib_gameconn_destroy(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   119
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   120
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   121
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   122
flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demoFileContent, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   123
    if(log_badargs_if(demoFileContent==NULL && size>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   124
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   125
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   126
    flib_gameconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   127
    flib_gameconn *tempConn = flib_gameconn_create_partial(false, "Player", false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   128
    if(tempConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   129
        if(!flib_vector_append(tempConn->configBuffer, demoFileContent, size)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   130
            result = tempConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   131
            tempConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   132
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   133
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   134
    flib_gameconn_destroy(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   135
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   136
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   137
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   138
flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *saveFileContent, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   139
    if(log_badargs_if(saveFileContent==NULL && size>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   140
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   141
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   142
    flib_gameconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   143
    flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   144
    if(tempConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   145
        if(!flib_vector_append(tempConn->configBuffer, saveFileContent, size)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   146
            result = tempConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   147
            tempConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   148
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   149
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   150
    flib_gameconn_destroy(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   151
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   152
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   153
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   154
flib_gameconn *flib_gameconn_create_campaign(const char *playerName, const char *seed, const char *script) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   155
    if(log_badargs_if3(playerName==NULL, seed==NULL, script==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   156
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   157
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   158
    flib_gameconn *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   159
    flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   160
    if(tempConn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   161
        if(!flib_ipc_append_message(tempConn->configBuffer, "TL")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   162
                && !flib_ipc_append_seed(tempConn->configBuffer, seed)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   163
                && !flib_ipc_append_script(tempConn->configBuffer, script)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   164
                && !flib_ipc_append_message(tempConn->configBuffer, "!")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   165
            result = tempConn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   166
            tempConn = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   167
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   168
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   169
    flib_gameconn_destroy(tempConn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   170
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   171
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   172
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   173
void flib_gameconn_destroy(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   174
    if(conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   175
        if(conn->running) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   176
            /*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   177
             * The function was called from a callback, so the tick function is still running
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   178
             * and we delay the actual destruction. We ensure no further callbacks will be
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   179
             * sent to prevent surprises.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   180
             */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   181
            clearCallbacks(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   182
            conn->destroyRequested = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   183
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   184
            flib_ipcbase_destroy(conn->ipcBase);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   185
            flib_vector_destroy(conn->configBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   186
            flib_vector_destroy(conn->demoBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   187
            free(conn->playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   188
            free(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   189
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   190
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   191
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   192
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   193
int flib_gameconn_getport(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   194
    if(log_badargs_if(conn==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   195
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   196
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   197
    return flib_ipcbase_port(conn->ipcBase);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   198
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   199
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   200
static void demo_append(flib_gameconn *conn, const void *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   201
    if(conn->demoBuffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   202
        if(flib_vector_append(conn->demoBuffer, data, len)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   203
            flib_log_e("Error recording demo: Out of memory.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   204
            flib_vector_destroy(conn->demoBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   205
            conn->demoBuffer = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   206
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   207
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   208
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   209
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   210
static int format_chatmessage(uint8_t buffer[257], const char *playerName, const char *message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   211
    size_t msglen = strlen(message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   212
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   213
    // If the message starts with /me, it will be displayed differently.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   214
    bool meMessage = msglen >= 4 && !memcmp(message, "/me ", 4);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   215
    const char *template = meMessage ? "s\x02* %s %s  " : "s\x01%s: %s  ";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   216
    int size = snprintf((char*)buffer+1, 256, template, playerName, meMessage ? message+4 : message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   217
    if(log_e_if(size<=0, "printf error")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   218
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   219
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   220
        buffer[0] = size>255 ? 255 : size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   221
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   222
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   223
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   224
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   225
static void demo_append_chatmessage(flib_gameconn *conn, const char *message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   226
    // Chat messages are reformatted to make them look as if they were received, not sent.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   227
    uint8_t converted[257];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   228
    if(!format_chatmessage(converted, conn->playerName, message)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   229
        demo_append(conn, converted, converted[0]+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   230
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   231
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   232
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   233
static void demo_replace_gamemode(flib_buffer buf, char gamemode) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   234
    size_t msgStart = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   235
    uint8_t *data = (uint8_t*)buf.data;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   236
    while(msgStart+2 < buf.size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   237
        if(!memcmp(data+msgStart, "\x02T", 2)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   238
            data[msgStart+2] = gamemode;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   239
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   240
        msgStart += (uint8_t)data[msgStart]+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   241
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   242
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   243
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   244
int flib_gameconn_send_enginemsg(flib_gameconn *conn, const uint8_t *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   245
    if(log_badargs_if2(conn==NULL, data==NULL && len>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   246
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   247
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   248
    int result = flib_ipcbase_send_raw(conn->ipcBase, data, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   249
    if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   250
        demo_append(conn, data, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   251
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   252
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   253
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   254
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   255
int flib_gameconn_send_textmsg(flib_gameconn *conn, int msgtype, const char *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   256
    if(log_badargs_if2(conn==NULL, msg==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   257
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   258
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   259
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   260
    uint8_t converted[257];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   261
    int size = snprintf((char*)converted+1, 256, "s%c%s", (char)msgtype, msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   262
    if(size>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   263
        converted[0] = size>255 ? 255 : size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   264
        if(!flib_ipcbase_send_raw(conn->ipcBase, converted, converted[0]+1)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   265
            demo_append(conn, converted, converted[0]+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   266
            result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   267
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   268
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   269
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   270
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   271
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   272
int flib_gameconn_send_chatmsg(flib_gameconn *conn, const char *playername, const char *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   273
    if(log_badargs_if3(conn==NULL, playername==NULL, msg==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   274
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   275
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   276
    uint8_t converted[257];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   277
    if(!format_chatmessage(converted, playername, msg)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   278
            && !flib_ipcbase_send_raw(conn->ipcBase, converted, converted[0]+1)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   279
        demo_append(conn, converted, converted[0]+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   280
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   281
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   282
    return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   283
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   284
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   285
int flib_gameconn_send_quit(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   286
    return flib_gameconn_send_cmd(conn, "efinish");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   287
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   288
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   289
int flib_gameconn_send_cmd(flib_gameconn *conn, const char *cmdString) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   290
    if(log_badargs_if2(conn==NULL, cmdString==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   291
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   292
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   293
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   294
    uint8_t converted[256];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   295
    size_t msglen = strlen(cmdString);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   296
    if(!log_e_if(msglen>255, "Message too long: %s", cmdString)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   297
        strcpy((char*)converted+1, cmdString);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   298
        converted[0] = msglen;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   299
        if(!flib_ipcbase_send_raw(conn->ipcBase, converted, msglen+1)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   300
            demo_append(conn, converted, msglen+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   301
            result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   302
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   303
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   304
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   305
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   306
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   307
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   308
 * This macro generates a callback setter function. It uses the name of the callback to
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   309
 * automatically generate the function name and the fields to set, so a consistent naming
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   310
 * convention needs to be enforced (not that that is a bad thing). If null is passed as
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   311
 * callback to the generated function, the defaultCb will be set instead (with conn
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   312
 * as the context).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   313
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   314
#define GENERATE_CB_SETTER(cbName, cbParameterTypes, defaultCb) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   315
    void flib_gameconn_##cbName(flib_gameconn *conn, void (*callback)cbParameterTypes, void *context) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   316
        if(!log_badargs_if(conn==NULL)) { \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   317
            conn->cbName##Cb = callback ? callback : &defaultCb; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   318
            conn->cbName##Ctx = callback ? context : conn; \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   319
        } \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   320
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   321
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   322
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   323
 * Generate a callback setter function like GENERATE_CB_SETTER, and automatically generate a
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   324
 * no-op callback function as well that is used as default.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   325
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   326
#define GENERATE_CB_SETTER_AND_DEFAULT(cbName, cbParameterTypes) \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   327
    static void _noop_callback_##cbName cbParameterTypes {} \
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   328
    GENERATE_CB_SETTER(cbName, cbParameterTypes, _noop_callback_##cbName)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   329
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   330
GENERATE_CB_SETTER_AND_DEFAULT(onConnect, (void *context));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   331
GENERATE_CB_SETTER_AND_DEFAULT(onDisconnect, (void* context, int reason));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   332
GENERATE_CB_SETTER(onErrorMessage, (void* context, const char *msg), defaultCallback_onErrorMessage);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   333
GENERATE_CB_SETTER_AND_DEFAULT(onChat, (void* context, const char *msg, bool teamchat));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   334
GENERATE_CB_SETTER_AND_DEFAULT(onGameRecorded, (void *context, const uint8_t *record, size_t size, bool isSavegame));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   335
GENERATE_CB_SETTER_AND_DEFAULT(onEngineMessage, (void *context, const uint8_t *em, size_t size));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   336
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   337
#undef GENERATE_CB_SETTER_AND_DEFAULT
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   338
#undef GENERATE_CB_SETTER
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   339
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   340
static void flib_gameconn_wrappedtick(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   341
    if(conn->state == AWAIT_CONNECTION) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   342
        flib_ipcbase_accept(conn->ipcBase);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   343
        switch(flib_ipcbase_state(conn->ipcBase)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   344
        case IPC_CONNECTED:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   345
            {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   346
                flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   347
                if(flib_ipcbase_send_raw(conn->ipcBase, configBuffer.data, configBuffer.size)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   348
                    conn->state = FINISHED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   349
                    conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   350
                    return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   351
                } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   352
                    demo_append(conn, configBuffer.data, configBuffer.size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   353
                    conn->state = CONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   354
                    conn->onConnectCb(conn->onConnectCtx);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   355
                    if(conn->destroyRequested) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   356
                        return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   357
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   358
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   359
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   360
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   361
        case IPC_NOT_CONNECTED:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   362
            conn->state = FINISHED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   363
            conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   364
            return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   365
        default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   366
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   367
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   368
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   369
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   370
    if(conn->state == CONNECTED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   371
        uint8_t msgbuffer[257];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   372
        int len;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   373
        while(!conn->destroyRequested && (len = flib_ipcbase_recv_message(conn->ipcBase, msgbuffer))>=0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   374
            if(len<2) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   375
                flib_log_w("Received short message from IPC (<2 bytes)");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   376
                continue;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   377
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   378
            switch(msgbuffer[1]) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   379
            case '?':   // Ping
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   380
                // The pong is already part of the config message
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   381
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   382
            case 'C':   // Config query
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   383
                // And we already send the config message on connecting.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   384
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   385
            case 'E':   // Error message
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   386
                if(len>=3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   387
                    msgbuffer[len-2] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   388
                    conn->onErrorMessageCb(conn->onErrorMessageCtx, (char*)msgbuffer+2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   389
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   390
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   391
            case 'i':   // Statistics
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   392
                // TODO stats
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   393
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   394
            case 'Q':   // Game interrupted
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   395
            case 'H':   // Game halted
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   396
            case 'q':   // game finished
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   397
                {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   398
                    int reason = msgbuffer[1]=='Q' ? GAME_END_INTERRUPTED : msgbuffer[1]=='H' ? GAME_END_HALTED : GAME_END_FINISHED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   399
                    conn->disconnectReason = reason;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   400
                    bool savegame = (reason != GAME_END_FINISHED) && !conn->netgame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   401
                    if(conn->demoBuffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   402
                        flib_buffer demoBuffer = flib_vector_as_buffer(conn->demoBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   403
                        demo_replace_gamemode(demoBuffer, savegame ? 'S' : 'D');
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   404
                        conn->onGameRecordedCb(conn->onGameRecordedCtx, demoBuffer.data, demoBuffer.size, savegame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   405
                        if(conn->destroyRequested) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   406
                            return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   407
                        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   408
                    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   409
                    return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   410
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   411
            case 's':   // Chat message
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   412
                if(len>=3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   413
                    msgbuffer[len-2] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   414
                    demo_append_chatmessage(conn, (char*)msgbuffer+2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   415
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   416
                    conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   417
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   418
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   419
            case 'b':   // Teamchat message
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   420
                if(len>=3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   421
                    msgbuffer[len-2] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   422
                    conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   423
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   424
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   425
            default:    // Engine message
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   426
                demo_append(conn, msgbuffer, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   427
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   428
                conn->onEngineMessageCb(conn->onEngineMessageCtx, msgbuffer, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   429
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   430
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   431
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   432
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   433
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   434
    if(flib_ipcbase_state(conn->ipcBase) == IPC_NOT_CONNECTED) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   435
        conn->state = FINISHED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   436
        conn->onDisconnectCb(conn->onDisconnectCtx, conn->disconnectReason);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   437
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   438
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   439
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   440
void flib_gameconn_tick(flib_gameconn *conn) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   441
    if(!log_badargs_if(conn == NULL)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   442
            && !log_w_if(conn->running, "Call to flib_gameconn_tick from a callback")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   443
            && !log_w_if(conn->state == FINISHED, "We are already done.")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   444
        conn->running = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   445
        flib_gameconn_wrappedtick(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   446
        conn->running = false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   447
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   448
        if(conn->destroyRequested) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   449
            flib_gameconn_destroy(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   450
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   451
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7580
diff changeset
   452
}