project_files/frontlib/ipc/ipcbase.c
author Medo <smaxein@googlemail.com>
Thu, 05 Jul 2012 00:33:24 +0200
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 7576 65d29988fd3d
permissions -rw-r--r--
frontlib: Improved and unified parameter checking, moved the cmdlineClient out of the frontlib code.
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);
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    77
		free(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    81
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
    82
	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
    83
		return IPC_NOT_CONNECTED;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    84
	} else if(ipc->sock) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
		return IPC_CONNECTED;
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    86
	} else if(ipc->acceptor) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
		return IPC_LISTENING;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
		return IPC_NOT_CONNECTED;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
    93
static void receiveToBuffer(flib_ipcbase *ipc) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    94
	if(ipc->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    95
		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
    96
		if(size>=0) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    97
			ipc->readBufferSize += size;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
		} else {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
    99
			flib_socket_close(ipc->sock);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   100
			ipc->sock = NULL;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
		}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	}
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   103
}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   105
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
   106
	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
   107
}
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
   108
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   109
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
   110
	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
   111
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   112
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   113
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   114
	if(!isMessageReady(ipc)) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   115
		receiveToBuffer(ipc);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   116
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   117
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   118
	if(isMessageReady(ipc)) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   119
		int msgsize = ipc->readBuffer[0]+1;
7173
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
   120
		memcpy(data, ipc->readBuffer, msgsize);
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
   121
		memmove(ipc->readBuffer, ipc->readBuffer+msgsize, ipc->readBufferSize-msgsize);
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
   122
		ipc->readBufferSize -= msgsize;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
		return msgsize;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   124
	} 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
   125
		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
   126
		ipc->readBufferSize = 0;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   133
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
   134
	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
   135
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   136
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   137
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   138
	receiveToBuffer(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   140
	if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   141
		memcpy(data, ipc->readBuffer, IPCBASE_MAPMSG_BYTES);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   142
		memmove(ipc->readBuffer, ipc->readBuffer+IPCBASE_MAPMSG_BYTES, ipc->readBufferSize-IPCBASE_MAPMSG_BYTES);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   143
		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
   144
	} else {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   145
		return -1;
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
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   148
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   149
static void logSentMsg(const uint8_t *data, size_t len) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   150
	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
7182
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   151
		size_t msgStart = 0;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   152
		while(msgStart < len) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   153
			uint8_t msglen = data[msgStart];
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   154
			if(msgStart+msglen < len) {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   155
				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   156
			} else {
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   157
				uint8_t msglen2 = len-msgStart-1;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   158
				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   159
			}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   160
			msgStart += (uint8_t)data[msgStart]+1;
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   161
		}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   162
	}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
diff changeset
   163
}
076aba32abd3 Small improvements to the frontend lib for better debugging
Medo <smaxein@googlemail.com>
parents: 7179
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_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
   166
	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
   167
			|| 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
   168
		return -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   169
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   170
	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
   171
		logSentMsg(data, len);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   172
		return 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   173
	} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   174
		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   175
		flib_socket_close(ipc->sock);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   176
		ipc->sock = NULL;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   177
		return -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   178
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   179
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   180
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   181
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
   182
	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
   183
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
	uint8_t sendbuf[256];
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
	sendbuf[0] = len;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
	memcpy(sendbuf+1, data, len);
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   189
	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
   190
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7227
diff changeset
   192
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
   193
	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   194
		ipc->sock = flib_socket_accept(ipc->acceptor, true);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   195
		if(ipc->sock) {
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   196
			flib_acceptor_close(ipc->acceptor);
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7182
diff changeset
   197
			ipc->acceptor = NULL;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
		}
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
}