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