frontlib/frontlib.c
author Medo <smaxein@googlemail.com>
Thu, 31 May 2012 00:35:06 +0200
changeset 7155 273ad375d64e
child 7158 a0573014ff4f
permissions -rw-r--r--
Started work on the frontend networking library
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "frontlib.h"
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "logging.h"
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include <SDL.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include <SDL_net.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <stdio.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <stdint.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <stdlib.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <time.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
static int flib_initflags;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
static TCPsocket ipcListenSocket;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
static int ipcPort;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
static uint8_t ipcReadBuffer[256];
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
static int ipcReadBufferSize;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
static TCPsocket ipcConnSocket;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
static SDLNet_SocketSet ipcConnSocketSet;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
static TCPsocket serverConnSocket;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
static SDLNet_SocketSet serverConnSocketSet;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
#include <time.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
void flib_logtime() {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
    time_t timer;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
    char buffer[25];
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
    struct tm* tm_info;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
    time(&timer);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
    tm_info = localtime(&timer);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
    strftime(buffer, 25, "%H:%M:%S", tm_info);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
    printf("%s", buffer);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
int flib_init(int flags) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	flib_initflags = flags;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	ipcListenSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	ipcConnSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	serverConnSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	ipcReadBufferSize = 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
		if(SDL_Init(0)==-1) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
		    flib_log_e("Error in SDL_Init: %s\n", SDL_GetError());
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		    return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	if(SDLNet_Init()==-1) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		flib_log_e("Error in SDLNet_Init: %s\n", SDLNet_GetError());
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
		if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
			SDL_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
		return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	ipcConnSocketSet = SDLNet_AllocSocketSet(1);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	serverConnSocketSet = SDLNet_AllocSocketSet(1);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	return 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
/**
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
 * Free resources associated with the library. Call this function once
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
 * the library is no longer needed. You can re-initialize the library by calling
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
 * flib_init again.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
 */
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
void flib_quit() {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
	// TODO: Send a "quit" message first?
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
	SDLNet_FreeSocketSet(ipcConnSocketSet);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
	SDLNet_FreeSocketSet(serverConnSocketSet);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	if(ipcListenSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
		SDLNet_TCP_Close(ipcListenSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		ipcListenSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	if(ipcConnSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
		SDLNet_TCP_Close(ipcConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		ipcConnSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	if(serverConnSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		SDLNet_TCP_Close(serverConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
		serverConnSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
	SDLNet_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
		SDL_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
/**
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
 * Start listening for a connection from the engine, if we are not listening already.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
 * Returns the port we are listening on, which needs to be passed to the engine,
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
 * or -1 if there is an error.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
 *
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
 * We stop listening once a connection has been established, so if you want to start
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
 * the engine again and talk to it you need to call this function again after the old
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
 * connection is closed.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
 */
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
int flib_ipc_listen() {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
	if(ipcListenSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
		return ipcPort;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	IPaddress addr;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
	addr.host = INADDR_ANY;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
	/* SDL_net does not seem to have a way to listen on a random unused port
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
	   and find out which port that is, so let's try to find one ourselves. */
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
	// TODO: Is socket binding fail-fast on all platforms?
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	srand(time(NULL));
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
	rand();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	for(int i=0; i<1000; i++) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
		// IANA suggests using ports in the range 49152-65535 for things like this
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
		ipcPort = 49152+(rand()%(65535-49152));
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
		SDLNet_Write16(ipcPort, &addr.port);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
		flib_log_i("Attempting to listen on port %i...\n", ipcPort);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
		ipcListenSocket = SDLNet_TCP_Open(&addr);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
		if(!ipcListenSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
			flib_log_w("Unable to listen on port %i: %s\n", ipcPort, SDLNet_GetError());
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
		} else {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
			flib_log_i("ok.\n");
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
			return ipcPort;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
	flib_log_e("Unable to find a usable IPC port.");
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
	return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
void flib_accept_ipc() {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
	if(!ipcListenSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
		flib_log_e("Attempt to accept IPC connection while not listening.");
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
		return;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
	do {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
		TCPsocket sock = SDLNet_TCP_Accept(ipcListenSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
		if(!sock) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
			// No incoming connections
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
			return;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
		// Check if it is a local connection
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
		IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
		uint32_t numip = SDLNet_Read32(&addr->host);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
		if(numip != (uint32_t)((127UL<<24)+1)) { // 127.0.0.1
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
			flib_log_w("Rejected IPC connection attempt from %s\n", flib_format_ip(numip));
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
		} else {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
			ipcConnSocket = sock;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
			SDLNet_AddSocket(ipcConnSocketSet, (SDLNet_GenericSocket)ipcConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
			SDLNet_TCP_Close(ipcListenSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
			ipcListenSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
	} while(!ipcConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
	return;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
/**
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
 * Receive a single message and copy it into the data buffer.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
 * Returns the length of the received message, -1 when nothing is received.
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
 */
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
int flib_engine_read_message(void *data) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
	if(!ipcConnSocket && ipcListenSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
		flib_accept_ipc();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   166
	if(ipcConnSocket && SDLNet_CheckSockets(ipcConnSocketSet, 0)>0) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
		int size = SDLNet_TCP_Recv(ipcConnSocket, ipcReadBuffer+ipcReadBufferSize, sizeof(ipcReadBuffer)-ipcReadBufferSize);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
		if(size>0) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
			ipcReadBufferSize += size;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
		} else {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
			SDLNet_DelSocket(ipcConnSocketSet, (SDLNet_GenericSocket)ipcConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
			SDLNet_TCP_Close(ipcConnSocket);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
			ipcConnSocket = NULL;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
			// TODO trigger "IPC disconnect" event, possibly delayed until after the messages are processed
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	int msgsize = ipcReadBuffer[0];
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
	if(ipcReadBufferSize > msgsize) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
		memcpy(data, ipcReadBuffer+1, msgsize);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
		memmove(ipcReadBuffer, ipcReadBuffer+msgsize+1, ipcReadBufferSize-(msgsize+1));
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
		ipcReadBufferSize -= (msgsize+1);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
		return msgsize;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
	} else if(!ipcConnSocket && ipcReadBufferSize>0) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", ipcReadBufferSize-1, msgsize);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
		ipcReadBufferSize = 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
		return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
	} else {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
		return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
void flib_engine_write_message(void *data, size_t len) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
	uint8_t sendbuf[256];
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
	if(len>255) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
		flib_log_e("Attempt to send too much data to the engine in a single message.");
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
		return;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
	sendbuf[0] = len;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
	memcpy(sendbuf+1, data, len);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
	SDLNet_TCP_Send(ipcConnSocket, sendbuf, len+1);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
int main(int argc, char *argv[]) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
	flib_init(0);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
	int port = flib_ipc_listen();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
	printf("%i", port);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
	fflush(stdout);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
	char data[256];
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
	while(ipcListenSocket||ipcConnSocket) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
		int size = flib_engine_read_message(data);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
		if(size>0) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   213
			if(data[0]=='?') {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
				flib_engine_write_message("!", 1);
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
			}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   218
	return 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
}