project_files/frontlib/ipc/gameconn.c
author Medo <smaxein@googlemail.com>
Sat, 09 Jun 2012 03:28:38 +0200
changeset 7179 f84805e6df03
child 7224 5143861c83bd
permissions -rw-r--r--
Implemented game launching API for the frontlib. It is still buggy though, and not all game settings can be conveniently created/modified yet.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "gameconn.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "ipcconn.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "ipcprotocol.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../util/logging.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include "../hwconsts.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <stdbool.h>
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <stdlib.h>
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
typedef enum {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
	AWAIT_CONNECTION,
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
	CONNECTED,
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	FINISHED
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
} gameconn_state;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
struct _flib_gameconn {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
	flib_ipcconn connection;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	flib_vector configBuffer;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
	gameconn_state state;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
	bool netgame;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	void (*onConnectCb)(void* context);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	void *onConnectCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
	void (*onDisconnectCb)(void* context, int reason);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
	void *onDisconnectCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	void (*onErrorMessageCb)(void* context, const char *msg);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
	void *onErrorMessageCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
	void (*onChatCb)(void* context, const char *msg, bool teamchat);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	void *onChatCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	void (*onGameRecordedCb)(void *context, const uint8_t *record, int size, bool isSavegame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
	void *onGameRecordedCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	void (*onNetMessageCb)(void *context, const uint8_t *em, int size);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	void *onNetMessageCtx;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	bool running;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	bool destroyRequested;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
};
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
static void defaultCallback_onConnect(void* context) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
static void defaultCallback_onDisconnect(void* context, int reason) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
static void defaultCallback_onErrorMessage(void* context, const char *msg) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	flib_log_w("Error from engine (no callback set): %s", msg);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
static void defaultCallback_onChat(void* context, const char *msg, bool teamchat) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
static void defaultCallback_onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
static void defaultCallback_onNetMessage(void *context, const uint8_t *em, int size) {}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
static void clearCallbacks(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	conn->onConnectCb = &defaultCallback_onConnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
	conn->onDisconnectCb = &defaultCallback_onDisconnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	conn->onErrorMessageCb = &defaultCallback_onErrorMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	conn->onChatCb = &defaultCallback_onChat;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	conn->onGameRecordedCb = &defaultCallback_onGameRecorded;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	conn->onNetMessageCb = &defaultCallback_onNetMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
static bool getGameMod(flib_cfg_meta *meta, flib_cfg *conf, int maskbit) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	for(int i=0; i<meta->modCount; i++) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		if(meta->mods[i].bitmaskIndex == maskbit) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
			return conf->mods[i];
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	flib_log_e("Unable to find game mod with mask bit %i", maskbit);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	return false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
static int fillConfigBuffer(flib_vector configBuffer, const char *playerName, flib_cfg_meta *metaconf, flib_gamesetup *setup, bool netgame) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	bool error = false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	bool perHogAmmo = false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	bool sharedAmmo = false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
	error |= flib_ipc_append_message(configBuffer, netgame ? "TN" : "TL");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	error |= flib_ipc_append_seed(configBuffer, setup->seed);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
	if(setup->map) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		error |= flib_ipc_append_mapconf(configBuffer, setup->map, false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	if(setup->script) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		error |= flib_ipc_append_message(configBuffer, "escript %s", setup->script);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	if(setup->gamescheme) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
		error |= flib_ipc_append_gamescheme(configBuffer, setup->gamescheme, metaconf);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
		perHogAmmo = getGameMod(metaconf, setup->gamescheme, GAMEMOD_PERHOGAMMO_MASKBIT);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
		sharedAmmo = getGameMod(metaconf, setup->gamescheme, GAMEMOD_SHAREDAMMO_MASKBIT);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	if(setup->teams) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
		for(int i=0; i<setup->teamcount; i++) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
			error |= flib_ipc_append_addteam(configBuffer, &setup->teams[i], perHogAmmo, sharedAmmo);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
	error |= flib_ipc_append_message(configBuffer, "!");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
	return error ? -1 : 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
	flib_gameconn *tempConn = calloc(1, sizeof(flib_gameconn));
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
		tempConn->connection = flib_ipcconn_create(record, playerName);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
		tempConn->configBuffer = flib_vector_create();
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
		if(tempConn->connection && tempConn->configBuffer) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
			tempConn->state = AWAIT_CONNECTION;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
			tempConn->netgame = netGame;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
			clearCallbacks(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
flib_gameconn *flib_gameconn_create(const char *playerName, flib_cfg_meta *metaconf, flib_gamesetup *setup, bool netgame) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, netgame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
		if(fillConfigBuffer(tempConn->configBuffer, playerName, metaconf, setup, netgame) == 0) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demo, int size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
	flib_gameconn *tempConn = flib_gameconn_create_partial(false, "Player", false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
		if(flib_vector_append(tempConn->configBuffer, demo, size) == size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *save, int size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
	flib_gameconn *result = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
	if(tempConn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
		if(flib_vector_append(tempConn->configBuffer, save, size) == size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
			result = tempConn;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
			tempConn = NULL;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
	flib_gameconn_destroy(tempConn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
	return result;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
void flib_gameconn_destroy(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
	if(conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
		if(conn->running) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
			/*
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
			 * The function was called from a callback, so the tick function is still running
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
			 * and we delay the actual destruction. We ensure no further callbacks will be
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
			 * sent to prevent surprises.
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
			 */
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
			clearCallbacks(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
			conn->destroyRequested = true;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   166
		} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
			flib_ipcconn_destroy(&conn->connection);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
			flib_vector_destroy(&conn->configBuffer);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
			free(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
int flib_gameconn_getport(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
		flib_log_e("null parameter in flib_gameconn_getport");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
		return 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
		return flib_ipcconn_port(conn->connection);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
void flib_gameconn_onConnect(flib_gameconn *conn, void (*callback)(void* context), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
		flib_log_e("null parameter in flib_gameconn_onConnect");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
		conn->onConnectCb = callback ? callback : &defaultCallback_onConnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
		conn->onConnectCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
void flib_gameconn_onDisconnect(flib_gameconn *conn, void (*callback)(void* context, int reason), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
		flib_log_e("null parameter in flib_gameconn_onDisconnect");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
		conn->onDisconnectCb = callback ? callback : &defaultCallback_onDisconnect;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
		conn->onDisconnectCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
void flib_gameconn_onErrorMessage(flib_gameconn *conn, void (*callback)(void* context, const char *msg), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
		flib_log_e("null parameter in flib_gameconn_onErrorMessage");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
		conn->onErrorMessageCb = callback ? callback : &defaultCallback_onErrorMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
		conn->onErrorMessageCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
void flib_gameconn_onChat(flib_gameconn *conn, void (*callback)(void* context, const char *msg, bool teamchat), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
		flib_log_e("null parameter in flib_gameconn_onChat");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   213
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
		conn->onChatCb = callback ? callback : &defaultCallback_onChat;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
		conn->onChatCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   218
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, int size, bool isSavegame), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   220
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   221
		flib_log_e("null parameter in flib_gameconn_onGameRecorded");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   222
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   223
		conn->onGameRecordedCb = callback ? callback : &defaultCallback_onGameRecorded;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   224
		conn->onGameRecordedCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   225
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   226
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   227
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   228
void flib_gameconn_onNetMessage(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *em, int size), void* context) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   229
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   230
		flib_log_e("null parameter in flib_gameconn_onNetMessage");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   231
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   232
		conn->onNetMessageCb = callback ? callback : &defaultCallback_onNetMessage;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   233
		conn->onNetMessageCtx = context;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   234
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   235
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   236
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   237
static void flib_gameconn_wrappedtick(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   238
	if(conn->state == AWAIT_CONNECTION) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   239
		flib_ipcconn_accept(conn->connection);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   240
		switch(flib_ipcconn_state(conn->connection)) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   241
		case IPC_CONNECTED:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   242
			{
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   243
				flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   244
				if(flib_ipcconn_send_raw(conn->connection, configBuffer.data, configBuffer.size)) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   245
					conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   246
					conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   247
					return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   248
				} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   249
					conn->state = CONNECTED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   250
					conn->onConnectCb(conn->onConnectCtx);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   251
					if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   252
						return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   253
					}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   254
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   255
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   256
			break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   257
		case IPC_NOT_CONNECTED:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   258
			conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   259
			conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   260
			return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   261
		default:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   262
			break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   263
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   264
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   265
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   266
	if(conn->state == CONNECTED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   267
		uint8_t msgbuffer[257];
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   268
		int len;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   269
		while(!conn->destroyRequested && (len = flib_ipcconn_recv_message(conn->connection, msgbuffer))>=0) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   270
			if(len<2) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   271
				flib_log_w("Received short message from IPC (<2 bytes)");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   272
				continue;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   273
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   274
			switch(msgbuffer[1]) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   275
			case '?':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   276
				// The pong is already part of the config message
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   277
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   278
			case 'C':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   279
				// And we already send the config message on connecting.
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   280
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   281
			case 'E':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   282
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   283
					msgbuffer[len-2] = 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   284
					conn->onErrorMessageCb(conn->onErrorMessageCtx, (char*)msgbuffer+2);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   285
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   286
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   287
			case 'i':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   288
				// TODO stats
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   289
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   290
			case 'Q':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   291
			case 'H':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   292
			case 'q':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   293
				{
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   294
					int reason = msgbuffer[1]=='Q' ? GAME_END_INTERRUPTED : msgbuffer[1]=='H' ? GAME_END_HALTED : GAME_END_FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   295
					bool savegame = (reason != GAME_END_FINISHED) && !conn->netgame;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   296
					flib_constbuffer record = flib_ipcconn_getrecord(conn->connection, savegame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   297
					if(record.size) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   298
						conn->onGameRecordedCb(conn->onGameRecordedCtx, record.data, record.size, savegame);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   299
						if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   300
							return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   301
						}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   302
					}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   303
					conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   304
					conn->onDisconnectCb(conn->onDisconnectCtx, reason);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   305
					return;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   306
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   307
			case 's':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   308
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   309
					msgbuffer[len-2] = 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   310
					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, false);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   311
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   312
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   313
			case 'b':
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   314
				if(len>=3) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   315
					msgbuffer[len-2] = 0;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   316
					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, true);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   317
				}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   318
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   319
			default:
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   320
				conn->onNetMessageCb(conn->onNetMessageCtx, msgbuffer, len);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   321
				break;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   322
			}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   323
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   324
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   325
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   326
	if(flib_ipcconn_state(conn->connection) == IPC_NOT_CONNECTED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   327
		conn->state = FINISHED;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   328
		conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   329
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   330
}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   331
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   332
void flib_gameconn_tick(flib_gameconn *conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   333
	if(!conn) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   334
		flib_log_e("null parameter in flib_gameconn_tick");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   335
	} else if(conn->running) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   336
		flib_log_w("Call to flib_gameconn_tick from a callback");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   337
	} else if(conn->state == FINISHED) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   338
		flib_log_w("Call to flib_gameconn_tick, but we are already done.");
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   339
	} else {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   340
		conn->running = true;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   341
		flib_gameconn_wrappedtick(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   342
		conn->running = false;
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   343
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   344
		if(conn->destroyRequested) {
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   345
			flib_gameconn_destroy(conn);
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   346
		}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   347
	}
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   348
}