project_files/frontlib/ipc/ipcbase.c
author unc0rr
Mon, 10 Jun 2013 21:20:14 +0400
changeset 9170 7c75643d0b66
parent 8071 7d6aaba59505
child 10017 de822cd3df3a
permissions -rw-r--r--
More verbose initialization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7314
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     1
/*
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     4
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     5
 * This program is free software; you can redistribute it and/or
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     6
 * modify it under the terms of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     8
 * of the License, or (at your option) any later version.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
     9
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    13
 * GNU General Public License for more details.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    14
 *
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    15
 * You should have received a copy of the GNU General Public License
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    16
 * along with this program; if not, write to the Free Software
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    18
 */
6171f0bad318 frontlib: Fixes and improvements. Added a GPLv2+ license header to all frontlib
Medo <smaxein@googlemail.com>
parents: 7271
diff changeset
    19
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    20
#include "ipcbase.h"
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    21
#include "../util/logging.h"
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    22
#include "../util/util.h"
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
    23
#include "../socket.h"
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    25
#include <string.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    26
#include <stdbool.h>
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    27
#include <stdlib.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    28
#include <stdio.h>
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    29
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    30
/*
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    31
 * The receive buffer has to be able to hold any message that might be received. Normally
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    32
 * the messages are at most 256 bytes, but the map preview contains 4097 bytes (4096 for a
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    33
 * bitmap, 1 for the number of hogs which fit on the map).
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    34
 *
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    35
 * We don't need to worry about wasting a few kb though, and I like powers of two...
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    36
 */
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    37
struct _flib_ipcbase {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    38
	uint8_t readBuffer[8192];
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    39
	int readBufferSize;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    41
	flib_acceptor *acceptor;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    42
	uint16_t port;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    44
	flib_tcpsocket *sock;
7227
1c859f572d72 frontlib: Rewrote the ini helper code, finished ini reading/writing support for all relevant file types
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    45
};
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    46
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    47
flib_ipcbase *flib_ipcbase_create() {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    48
	flib_ipcbase *result = flib_calloc(1, sizeof(flib_ipcbase));
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    49
	flib_acceptor *acceptor = flib_acceptor_create(0);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    50
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    51
	if(!result || !acceptor) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    52
		free(result);
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    53
		flib_acceptor_close(acceptor);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    54
		return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    55
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    56
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    57
	result->acceptor = acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    58
	result->sock = NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    59
	result->readBufferSize = 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    60
	result->port = flib_acceptor_listenport(acceptor);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
    62
	flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    63
	return result;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    66
uint16_t flib_ipcbase_port(flib_ipcbase *ipc) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
    67
	if(log_badargs_if(ipc==NULL)) {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    68
		return 0;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    69
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    70
	return ipc->port;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    73
void flib_ipcbase_destroy(flib_ipcbase *ipc) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    74
	if(ipc) {
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    75
		flib_acceptor_close(ipc->acceptor);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    76
		flib_socket_close(ipc->sock);
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
    77
		if(ipc->sock) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
    78
			flib_log_d("IPC connection closed.");
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
    79
		}
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    80
		free(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    84
IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
    85
	if(log_badargs_if(ipc==NULL)) {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    86
		return IPC_NOT_CONNECTED;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    87
	} else if(ipc->sock) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
		return IPC_CONNECTED;
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    89
	} else if(ipc->acceptor) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
		return IPC_LISTENING;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
		return IPC_NOT_CONNECTED;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    96
static void receiveToBuffer(flib_ipcbase *ipc) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    97
	if(ipc->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    98
		int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
		if(size>=0) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   100
			ipc->readBufferSize += size;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
		} else {
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   102
			flib_log_d("IPC connection lost.");
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   103
			flib_socket_close(ipc->sock);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   104
			ipc->sock = NULL;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
		}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	}
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   107
}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   109
static bool isMessageReady(flib_ipcbase *ipc) {
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   110
	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   111
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   112
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   113
static void logSentMsg(const uint8_t *data, size_t len) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   114
	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   115
		size_t msgStart = 0;
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   116
		while(msgStart < len) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   117
			uint8_t msglen = data[msgStart];
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   118
			if(msgStart+msglen < len) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   119
				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   120
			} else {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   121
				uint8_t msglen2 = len-msgStart-1;
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   122
				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   123
			}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   124
			msgStart += (uint8_t)data[msgStart]+1;
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   125
		}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   126
	}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   127
}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   128
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   129
static void logRecvMsg(const uint8_t *data) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   130
	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   131
		uint8_t msglen = data[0];
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   132
		flib_log_d("[IPC IN][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+1);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   133
	}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   134
}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   135
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   136
static void popFromReadBuffer(flib_ipcbase *ipc, uint8_t *outbuf, size_t size) {
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   137
	memcpy(outbuf, ipc->readBuffer, size);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   138
	memmove(ipc->readBuffer, ipc->readBuffer+size, ipc->readBufferSize-size);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   139
	ipc->readBufferSize -= size;
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   140
}
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   141
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   142
int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   143
	if(log_badargs_if2(ipc==NULL, data==NULL)) {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   144
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   145
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   146
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   147
	if(!isMessageReady(ipc)) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   148
		receiveToBuffer(ipc);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   149
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   150
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   151
	if(isMessageReady(ipc)) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   152
		int msgsize = ipc->readBuffer[0]+1;
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   153
		popFromReadBuffer(ipc, data, msgsize);
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   154
		logRecvMsg(data);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
		return msgsize;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   156
	} else if(!ipc->sock && ipc->readBufferSize>0) {
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
   157
		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   158
		ipc->readBufferSize = 0;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   165
int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   166
	if(log_badargs_if2(ipc==NULL, data==NULL)) {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   167
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   168
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   169
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   170
	receiveToBuffer(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   172
	if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   173
		popFromReadBuffer(ipc, data, IPCBASE_MAPMSG_BYTES);
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   174
		return IPCBASE_MAPMSG_BYTES;
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   175
	} else {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   176
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   177
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   178
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   179
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   180
int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   181
	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   182
			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   183
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   184
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   185
	if(flib_socket_send(ipc->sock, data, len) == len) {
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   186
		logSentMsg(data, len);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   187
		return 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   188
	} else {
8071
7d6aaba59505 Fix typo
unc0rr
parents: 7576
diff changeset
   189
		flib_log_w("Failed or incomplete IPC write: engine connection lost.");
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   190
		flib_socket_close(ipc->sock);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   191
		ipc->sock = NULL;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   192
		return -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   193
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   194
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   195
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   196
int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   197
	if(log_badargs_if3(ipc==NULL, data==NULL && len>0, len>255)) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
	uint8_t sendbuf[256];
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
	sendbuf[0] = len;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
	memcpy(sendbuf+1, data, len);
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   204
	return flib_ipcbase_send_raw(ipc, sendbuf, len+1);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   207
void flib_ipcbase_accept(flib_ipcbase *ipc) {
7316
f7b49b2c5d84 frontlib: Improved and unified parameter checking, moved the cmdlineClient out
Medo <smaxein@googlemail.com>
parents: 7314
diff changeset
   208
	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   209
		ipc->sock = flib_socket_accept(ipc->acceptor, true);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   210
		if(ipc->sock) {
7576
65d29988fd3d Fix engine crashes on game end
Medo <smaxein@googlemail.com>
parents: 7316
diff changeset
   211
			flib_log_d("IPC connection accepted.");
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   212
			flib_acceptor_close(ipc->acceptor);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   213
			ipc->acceptor = NULL;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
		}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
}