project_files/frontlib/ipcconn.c
author Medo <smaxein@googlemail.com>
Sun, 03 Jun 2012 01:24:18 +0200
changeset 7171 906e72caea7b
parent 7162 fe76d24a25d7
child 7173 7c2eb284f9f1
permissions -rw-r--r--
frontlib refactoring socket.h now completely wraps all the lowlevel neworking, so it would be easy to switch away from SDL_net if needed. Also reduced global state by making an IPC connection an object-like thing.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "ipcconn.h"
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "logging.h"
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
     3
#include "socket.h"
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
     5
#include <string.h>
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
     6
#include <stdbool.h>
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
     7
#include <stdlib.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
     8
#include <stdio.h>
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
     9
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    10
typedef struct _flib_ipcconn {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    11
	char playerName[256];
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    12
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    13
	uint8_t readBuffer[256];
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    14
	int readBufferSize;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    16
	flib_acceptor acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    17
	uint16_t port;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    19
	flib_tcpsocket sock;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    20
	flib_vector demoBuffer;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    21
} _flib_ipcconn;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    22
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    23
flib_ipcconn flib_ipcconn_create(bool recordDemo, const char *localPlayerName) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    24
	flib_ipcconn result = malloc(sizeof(_flib_ipcconn));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    25
	flib_acceptor acceptor = flib_acceptor_create(0);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    26
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    27
	if(!result || !acceptor) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    28
		free(result);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    29
		flib_acceptor_close(&acceptor);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    30
		return NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    31
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    32
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    33
	result->acceptor = acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    34
	result->sock = NULL;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    35
	result->readBufferSize = 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    36
	result->port = flib_acceptor_listenport(acceptor);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    38
	if(localPlayerName) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    39
		strncpy(result->playerName, localPlayerName, 255);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    40
	} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    41
		strncpy(result->playerName, "Player", 255);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    42
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    43
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    44
	if(recordDemo) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    45
		result->demoBuffer = flib_vector_create();
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    46
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    47
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    48
	flib_log_i("Started listening for IPC connections on port %u", result->port);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    49
	return result;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    52
uint16_t flib_ipcconn_port(flib_ipcconn ipc) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    53
	return ipc->port;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    56
void flib_ipcconn_destroy(flib_ipcconn *ipcptr) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    57
	if(!ipcptr || !*ipcptr) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    58
		return;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    60
	flib_ipcconn ipc = *ipcptr;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    61
	flib_acceptor_close(&ipc->acceptor);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    62
	flib_socket_close(&ipc->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    63
	flib_vector_destroy(&ipc->demoBuffer);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    64
	free(ipc);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    65
	*ipcptr = NULL;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    68
IpcConnState flib_ipcconn_state(flib_ipcconn ipc) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    69
	if(ipc && ipc->sock) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		return IPC_CONNECTED;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    71
	} else if(ipc && ipc->acceptor) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		return IPC_LISTENING;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
		return IPC_NOT_CONNECTED;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    78
static void demo_record(flib_ipcconn ipc, const void *data, size_t len) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    79
	if(ipc->demoBuffer) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    80
		if(flib_vector_append(ipc->demoBuffer, data, len) < len) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    81
			// Out of memory, fail demo recording
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    82
			flib_vector_destroy(&ipc->demoBuffer);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    83
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    84
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    85
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    86
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    87
static void demo_record_from_engine(flib_ipcconn ipc, const uint8_t *message) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    88
	if(!ipc->demoBuffer || message[0]==0) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    89
		return;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    90
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    91
	if(strchr("?CEiQqHb", message[1])) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    92
		// Those message types are not recorded in a demo.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    93
		return;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    94
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    95
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    96
	if(message[1] == 's') {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    97
		if(message[0] >= 3) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    98
			// Chat messages get a special once-over to make them look as if they were received, not sent.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    99
			// Get the actual chat message as c string
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   100
			char chatMsg[256];
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   101
			memcpy(chatMsg, message+2, message[0]-3);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   102
			chatMsg[message[0]-3] = 0;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   103
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   104
			char converted[257];
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   105
			bool memessage = message[0] >= 7 && !memcmp(message+2, "/me ", 4);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   106
			const char *template = memessage ? "s\x02* %s %s  " : "s\x01%s: %s  ";
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   107
			int size = snprintf(converted+1, 256, template, ipc->playerName, chatMsg);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   108
			converted[0] = size>255 ? 255 : size;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   109
			demo_record(ipc, converted, converted[0]+1);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   110
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   111
	} else {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   112
		demo_record(ipc, message, message[0]+1);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   113
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   114
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   115
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
/**
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
 * Receive a single message and copy it into the data buffer.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
 * Returns the length of the received message, -1 when nothing is received.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   120
int flib_ipcconn_recv_message(flib_ipcconn ipc, void *data) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   121
	flib_ipcconn_tick(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   123
	if(ipc->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   124
		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
   125
		if(size>=0) {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   126
			ipc->readBufferSize += size;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
		} else {
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   128
			flib_socket_close(&ipc->sock);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
		}
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
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   132
	int msgsize = ipc->readBuffer[0];
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   133
	if(ipc->readBufferSize > msgsize) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   134
		demo_record_from_engine(ipc, ipc->readBuffer);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   135
		memcpy(data, ipc->readBuffer+1, msgsize);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   136
		memmove(ipc->readBuffer, ipc->readBuffer+msgsize+1, ipc->readBufferSize-(msgsize+1));
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   137
		ipc->readBufferSize -= (msgsize+1);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
		return msgsize;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   139
	} else if(!ipc->sock && ipc->readBufferSize>0) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   140
		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", ipc->readBufferSize-1, msgsize);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   141
		ipc->readBufferSize = 0;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
	} else {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   148
int flib_ipcconn_send_raw(flib_ipcconn ipc, void *data, size_t len) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   149
	flib_ipcconn_tick(ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   151
	if(!ipc->sock) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
		flib_log_w("flib_ipcconn_send_message: Not connected.");
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   155
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   156
	if(flib_socket_send(ipc->sock, data, len) == len) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   157
		demo_record(ipc, data, len);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   158
		return 0;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   159
	} else {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   160
		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   161
		flib_socket_close(&ipc->sock);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   162
		return -1;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   163
	}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   164
}
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   165
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   166
int flib_ipcconn_send_message(flib_ipcconn ipc, void *data, size_t len) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
	if(len>255) {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
		flib_log_e("Attempt to send too much data to the engine in a single message.");
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
		return -1;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
	uint8_t sendbuf[256];
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
	sendbuf[0] = len;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
	memcpy(sendbuf+1, data, len);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   175
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   176
	return flib_ipcconn_send_raw(ipc, sendbuf, len+1);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   179
int flib_ipcconn_send_messagestr(flib_ipcconn ipc, char *data) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   180
	return flib_ipcconn_send_message(ipc, data, strlen(data));
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   181
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   182
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   183
void flib_ipcconn_tick(flib_ipcconn ipc) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   184
	if(!ipc->sock && ipc->acceptor) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   185
		ipc->sock = flib_socket_accept(ipc->acceptor, true);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   186
		if(ipc->sock) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   187
			flib_acceptor_close(&ipc->acceptor);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
		}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
	}
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
}
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   191
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   192
static void replace_gamemode(flib_buffer buf, char gamemode) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   193
	size_t msgStart = 0;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   194
	char *data = (char*)buf.data;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   195
	while(msgStart+2 < buf.size) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   196
		if(!memcmp(data+msgStart, "\x02T", 2)) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   197
			data[msgStart+2] = gamemode;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   198
		}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   199
		msgStart += (uint8_t)data[msgStart]+1;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   200
	}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   201
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   202
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   203
flib_constbuffer flib_ipcconn_getdemo(flib_ipcconn ipc) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   204
	if(!ipc->demoBuffer) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   205
		flib_constbuffer result = {NULL, 0};
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   206
		return result;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   207
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   208
	replace_gamemode(flib_vector_as_buffer(ipc->demoBuffer), 'D');
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   209
	return flib_vector_as_constbuffer(ipc->demoBuffer);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   210
}
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   211
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   212
flib_constbuffer flib_ipcconn_getsave(flib_ipcconn ipc) {
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   213
	if(!ipc->demoBuffer) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   214
		flib_constbuffer result = {NULL, 0};
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   215
		return result;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   216
	}
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   217
	replace_gamemode(flib_vector_as_buffer(ipc->demoBuffer), 'S');
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
   218
	return flib_vector_as_constbuffer(ipc->demoBuffer);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   219
}