project_files/frontlib/ipc/gameconn.c
author Medo <smaxein@googlemail.com>
Fri, 15 Jun 2012 19:57:25 +0200
changeset 7230 240620f46dd7
parent 7224 5143861c83bd
child 7234 613998625a3c
permissions -rw-r--r--
Changed frontlib to use the existing ini file formats of the QtFrontend

#include "gameconn.h"
#include "ipcconn.h"
#include "ipcprotocol.h"
#include "../util/logging.h"
#include "../util/util.h"
#include "../hwconsts.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

typedef enum {
	AWAIT_CONNECTION,
	CONNECTED,
	FINISHED
} gameconn_state;

struct _flib_gameconn {
	flib_ipcconn *connection;
	flib_vector *configBuffer;
	flib_vector *demoBuffer;
	char *playerName;

	gameconn_state state;
	bool netgame;

	void (*onConnectCb)(void* context);
	void *onConnectCtx;

	void (*onDisconnectCb)(void* context, int reason);
	void *onDisconnectCtx;

	void (*onErrorMessageCb)(void* context, const char *msg);
	void *onErrorMessageCtx;

	void (*onChatCb)(void* context, const char *msg, bool teamchat);
	void *onChatCtx;

	void (*onGameRecordedCb)(void *context, const uint8_t *record, int size, bool isSavegame);
	void *onGameRecordedCtx;

	void (*onNetMessageCb)(void *context, const uint8_t *em, int size);
	void *onNetMessageCtx;

	bool running;
	bool destroyRequested;
};

static void defaultCallback_onConnect(void* context) {}
static void defaultCallback_onDisconnect(void* context, int reason) {}
static void defaultCallback_onErrorMessage(void* context, const char *msg) {
	flib_log_w("Error from engine (no callback set): %s", msg);
}
static void defaultCallback_onChat(void* context, const char *msg, bool teamchat) {}
static void defaultCallback_onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {}
static void defaultCallback_onNetMessage(void *context, const uint8_t *em, int size) {}

static void clearCallbacks(flib_gameconn *conn) {
	conn->onConnectCb = &defaultCallback_onConnect;
	conn->onDisconnectCb = &defaultCallback_onDisconnect;
	conn->onErrorMessageCb = &defaultCallback_onErrorMessage;
	conn->onChatCb = &defaultCallback_onChat;
	conn->onGameRecordedCb = &defaultCallback_onGameRecorded;
	conn->onNetMessageCb = &defaultCallback_onNetMessage;
}

static flib_gameconn *flib_gameconn_create_partial(bool record, const char *playerName, bool netGame) {
	flib_gameconn *result = NULL;
	flib_gameconn *tempConn = flib_calloc(1, sizeof(flib_gameconn));
	if(tempConn) {
		tempConn->connection = flib_ipcconn_create();
		tempConn->configBuffer = flib_vector_create();
		tempConn->playerName = flib_strdupnull(playerName);
		if(tempConn->connection && tempConn->configBuffer && tempConn->playerName) {
			if(record) {
				tempConn->demoBuffer = flib_vector_create();
			}
			tempConn->state = AWAIT_CONNECTION;
			tempConn->netgame = netGame;
			clearCallbacks(tempConn);
			result = tempConn;
			tempConn = NULL;
		}
	}
	flib_gameconn_destroy(tempConn);
	return result;
}

flib_gameconn *flib_gameconn_create(const char *playerName, flib_gamesetup *setup, bool netgame) {
	flib_gameconn *result = NULL;
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, netgame);
	if(tempConn) {
		if(!flib_ipc_append_fullconfig(tempConn->configBuffer, setup, netgame)) {
			result = tempConn;
			tempConn = NULL;
		}
	}
	flib_gameconn_destroy(tempConn);
	return result;
}

flib_gameconn *flib_gameconn_create_playdemo(const uint8_t *demo, int size) {
	flib_gameconn *result = NULL;
	flib_gameconn *tempConn = flib_gameconn_create_partial(false, "Player", false);
	if(tempConn) {
		if(flib_vector_append(tempConn->configBuffer, demo, size) == size) {
			result = tempConn;
			tempConn = NULL;
		}
	}
	flib_gameconn_destroy(tempConn);
	return result;
}

flib_gameconn *flib_gameconn_create_loadgame(const char *playerName, const uint8_t *save, int size) {
	flib_gameconn *result = NULL;
	flib_gameconn *tempConn = flib_gameconn_create_partial(true, playerName, false);
	if(tempConn) {
		if(flib_vector_append(tempConn->configBuffer, save, size) == size) {
			result = tempConn;
			tempConn = NULL;
		}
	}
	flib_gameconn_destroy(tempConn);
	return result;
}

void flib_gameconn_destroy(flib_gameconn *conn) {
	if(conn) {
		if(conn->running) {
			/*
			 * The function was called from a callback, so the tick function is still running
			 * and we delay the actual destruction. We ensure no further callbacks will be
			 * sent to prevent surprises.
			 */
			clearCallbacks(conn);
			conn->destroyRequested = true;
		} else {
			flib_ipcconn_destroy(conn->connection);
			flib_vector_destroy(conn->configBuffer);
			flib_vector_destroy(conn->demoBuffer);
			free(conn->playerName);
			free(conn);
		}
	}
}

int flib_gameconn_getport(flib_gameconn *conn) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_getport");
		return 0;
	} else {
		return flib_ipcconn_port(conn->connection);
	}
}

static void demo_append(flib_gameconn *conn, const void *data, size_t len) {
	if(conn->demoBuffer) {
		if(flib_vector_append(conn->demoBuffer, data, len) < len) {
			flib_log_e("Error recording demo: Out of memory.");
			flib_vector_destroy(conn->demoBuffer);
			conn->demoBuffer = NULL;
		}
	}
}

static int format_chatmessage(uint8_t buffer[257], const char *playerName, const char *message) {
	size_t msglen = strlen(message);

	// If the message starts with /me, it will be displayed differently.
	bool meMessage = msglen >= 4 && !memcmp(message, "/me ", 4);
	const char *template = meMessage ? "s\x02* %s %s  " : "s\x01%s: %s  ";
	int size = snprintf((char*)buffer+1, 256, template, playerName, meMessage ? message+4 : message);
	if(size>0) {
		buffer[0] = size>255 ? 255 : size;
		return 0;
	} else {
		return -1;
	}
}

static void demo_append_chatmessage(flib_gameconn *conn, const char *message) {
	// Chat messages are reformatted to make them look as if they were received, not sent.
	uint8_t converted[257];
	if(!format_chatmessage(converted, conn->playerName, message)) {
		demo_append(conn, converted, converted[0]+1);
	}
}

static void demo_replace_gamemode(flib_buffer buf, char gamemode) {
	size_t msgStart = 0;
	uint8_t *data = (uint8_t*)buf.data;
	while(msgStart+2 < buf.size) {
		if(!memcmp(data+msgStart, "\x02T", 2)) {
			data[msgStart+2] = gamemode;
		}
		msgStart += (uint8_t)data[msgStart]+1;
	}
}

int flib_gameconn_send_enginemsg(flib_gameconn *conn, uint8_t *data, int len) {
	int result = -1;
	if(!conn || (!data && len>0)) {
		flib_log_e("null parameter in flib_gameconn_send_enginemsg");
	} else if(!flib_ipcconn_send_raw(conn->connection, data, len)) {
		demo_append(conn, data, len);
		result = 0;
	}
	return result;
}

int flib_gameconn_send_textmsg(flib_gameconn *conn, int msgtype, const char *msg) {
	int result = -1;
	if(!conn || !msg) {
		flib_log_e("null parameter in flib_gameconn_send_textmsg");
	} else {
		uint8_t converted[257];
		int size = snprintf((char*)converted+1, 256, "s%c%s", (char)msgtype, msg);
		if(size>0) {
			converted[0] = size>255 ? 255 : size;
			if(!flib_ipcconn_send_raw(conn->connection, converted, converted[0]+1)) {
				demo_append(conn, converted, converted[0]+1);
				result = 0;
			}
		}
	}
	return result;
}

int flib_gameconn_send_chatmsg(flib_gameconn *conn, const char *playername, const char *msg) {
	int result = -1;
	uint8_t converted[257];
	if(!conn || !playername || !msg) {
		flib_log_e("null parameter in flib_gameconn_send_chatmsg");
	} else if(format_chatmessage(converted, playername, msg)) {
		flib_log_e("Error formatting message in flib_gameconn_send_chatmsg");
	} else if(!flib_ipcconn_send_raw(conn->connection, converted, converted[0]+1)) {
		demo_append(conn, converted, converted[0]+1);
		result = 0;
	}
	return result;
}

void flib_gameconn_onConnect(flib_gameconn *conn, void (*callback)(void* context), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onConnect");
	} else {
		conn->onConnectCb = callback ? callback : &defaultCallback_onConnect;
		conn->onConnectCtx = context;
	}
}

void flib_gameconn_onDisconnect(flib_gameconn *conn, void (*callback)(void* context, int reason), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onDisconnect");
	} else {
		conn->onDisconnectCb = callback ? callback : &defaultCallback_onDisconnect;
		conn->onDisconnectCtx = context;
	}
}

void flib_gameconn_onErrorMessage(flib_gameconn *conn, void (*callback)(void* context, const char *msg), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onErrorMessage");
	} else {
		conn->onErrorMessageCb = callback ? callback : &defaultCallback_onErrorMessage;
		conn->onErrorMessageCtx = context;
	}
}

void flib_gameconn_onChat(flib_gameconn *conn, void (*callback)(void* context, const char *msg, bool teamchat), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onChat");
	} else {
		conn->onChatCb = callback ? callback : &defaultCallback_onChat;
		conn->onChatCtx = context;
	}
}

void flib_gameconn_onGameRecorded(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *record, int size, bool isSavegame), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onGameRecorded");
	} else {
		conn->onGameRecordedCb = callback ? callback : &defaultCallback_onGameRecorded;
		conn->onGameRecordedCtx = context;
	}
}

void flib_gameconn_onNetMessage(flib_gameconn *conn, void (*callback)(void *context, const uint8_t *em, int size), void* context) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_onNetMessage");
	} else {
		conn->onNetMessageCb = callback ? callback : &defaultCallback_onNetMessage;
		conn->onNetMessageCtx = context;
	}
}

static void flib_gameconn_wrappedtick(flib_gameconn *conn) {
	if(conn->state == AWAIT_CONNECTION) {
		flib_ipcconn_accept(conn->connection);
		switch(flib_ipcconn_state(conn->connection)) {
		case IPC_CONNECTED:
			{
				flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer);
				if(flib_ipcconn_send_raw(conn->connection, configBuffer.data, configBuffer.size)) {
					conn->state = FINISHED;
					conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
					return;
				} else {
					demo_append(conn, configBuffer.data, configBuffer.size);
					conn->state = CONNECTED;
					conn->onConnectCb(conn->onConnectCtx);
					if(conn->destroyRequested) {
						return;
					}
				}
			}
			break;
		case IPC_NOT_CONNECTED:
			conn->state = FINISHED;
			conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
			return;
		default:
			break;
		}
	}

	if(conn->state == CONNECTED) {
		uint8_t msgbuffer[257];
		int len;
		while(!conn->destroyRequested && (len = flib_ipcconn_recv_message(conn->connection, msgbuffer))>=0) {
			if(len<2) {
				flib_log_w("Received short message from IPC (<2 bytes)");
				continue;
			}
			switch(msgbuffer[1]) {
			case '?':	// Ping
				// The pong is already part of the config message
				break;
			case 'C':	// Config query
				// And we already send the config message on connecting.
				break;
			case 'E':	// Error message
				if(len>=3) {
					msgbuffer[len-2] = 0;
					conn->onErrorMessageCb(conn->onErrorMessageCtx, (char*)msgbuffer+2);
				}
				break;
			case 'i':	// Statistics
				// TODO stats
				break;
			case 'Q':	// Game interrupted
			case 'H':	// Game halted
			case 'q':	// game finished
				{
					int reason = msgbuffer[1]=='Q' ? GAME_END_INTERRUPTED : msgbuffer[1]=='H' ? GAME_END_HALTED : GAME_END_FINISHED;
					bool savegame = (reason != GAME_END_FINISHED) && !conn->netgame;
					if(conn->demoBuffer) {
						flib_buffer demoBuffer = flib_vector_as_buffer(conn->demoBuffer);
						demo_replace_gamemode(demoBuffer, savegame ? 'S' : 'D');
						conn->onGameRecordedCb(conn->onGameRecordedCtx, demoBuffer.data, demoBuffer.size, savegame);
						if(conn->destroyRequested) {
							return;
						}
					}
					conn->state = FINISHED;
					conn->onDisconnectCb(conn->onDisconnectCtx, reason);
					return;
				}
			case 's':	// Chat message
				if(len>=3) {
					msgbuffer[len-2] = 0;
					demo_append_chatmessage(conn, (char*)msgbuffer+2);

					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, false);
				}
				break;
			case 'b':	// Teamchat message
				if(len>=3) {
					msgbuffer[len-2] = 0;
					conn->onChatCb(conn->onChatCtx, (char*)msgbuffer+2, true);
				}
				break;
			default:	// Engine message
				demo_append(conn, msgbuffer, len);

				conn->onNetMessageCb(conn->onNetMessageCtx, msgbuffer, len);
				break;
			}
		}
	}

	if(flib_ipcconn_state(conn->connection) == IPC_NOT_CONNECTED) {
		conn->state = FINISHED;
		conn->onDisconnectCb(conn->onDisconnectCtx, GAME_END_ERROR);
	}
}

void flib_gameconn_tick(flib_gameconn *conn) {
	if(!conn) {
		flib_log_e("null parameter in flib_gameconn_tick");
	} else if(conn->running) {
		flib_log_w("Call to flib_gameconn_tick from a callback");
	} else if(conn->state == FINISHED) {
		flib_log_w("Call to flib_gameconn_tick, but we are already done.");
	} else {
		conn->running = true;
		flib_gameconn_wrappedtick(conn);
		conn->running = false;

		if(conn->destroyRequested) {
			flib_gameconn_destroy(conn);
		}
	}
}