frontlib/frontlib.c
changeset 7158 a0573014ff4f
parent 7155 273ad375d64e
equal deleted inserted replaced
7155:273ad375d64e 7158:a0573014ff4f
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "logging.h"
     2 #include "logging.h"
       
     3 #include "nonblocksockets.h"
       
     4 #include "ipcconn.h"
     3 
     5 
     4 #include <SDL.h>
     6 #include <SDL.h>
     5 #include <SDL_net.h>
     7 #include <SDL_net.h>
     6 #include <stdio.h>
     8 #include <stdio.h>
     7 #include <stdint.h>
     9 #include <stdint.h>
     8 #include <stdlib.h>
    10 #include <stdlib.h>
     9 #include <time.h>
       
    10 
    11 
    11 static int flib_initflags;
    12 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 
    13 
    37 int flib_init(int flags) {
    14 int flib_init(int flags) {
    38 	flib_initflags = flags;
    15 	flib_initflags = flags;
    39 	ipcListenSocket = NULL;
       
    40 	ipcConnSocket = NULL;
       
    41 	serverConnSocket = NULL;
       
    42 	ipcReadBufferSize = 0;
       
    43 
    16 
    44 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    17 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    45 		if(SDL_Init(0)==-1) {
    18 		if(SDL_Init(0)==-1) {
    46 		    flib_log_e("Error in SDL_Init: %s\n", SDL_GetError());
    19 		    flib_log_e("Error in SDL_Init: %s", SDL_GetError());
    47 		    return -1;
    20 		    return -1;
    48 		}
    21 		}
    49 	}
    22 	}
    50 
    23 
    51 	if(SDLNet_Init()==-1) {
    24 	if(SDLNet_Init()==-1) {
    52 		flib_log_e("Error in SDLNet_Init: %s\n", SDLNet_GetError());
    25 		flib_log_e("Error in SDLNet_Init: %s", SDLNet_GetError());
    53 		if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    26 		if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    54 			SDL_Quit();
    27 			SDL_Quit();
    55 		}
    28 		}
    56 		return -1;
    29 		return -1;
    57 	}
    30 	}
    58 
    31 
    59 	ipcConnSocketSet = SDLNet_AllocSocketSet(1);
    32 	flib_ipcconn_init();
    60 	serverConnSocketSet = SDLNet_AllocSocketSet(1);
       
    61 	return 0;
    33 	return 0;
    62 }
    34 }
    63 
    35 
    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() {
    36 void flib_quit() {
    70 	// TODO: Send a "quit" message first?
    37 	flib_ipcconn_quit();
    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 
    38 
    87 	SDLNet_Quit();
    39 	SDLNet_Quit();
    88 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    40 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    89 		SDL_Quit();
    41 		SDL_Quit();
    90 	}
    42 	}
    91 }
    43 }
    92 
    44 
    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[]) {
    45 int main(int argc, char *argv[]) {
   205 	flib_init(0);
    46 	flib_init(0);
   206 	int port = flib_ipc_listen();
    47 	int port = flib_ipcconn_listen();
   207 	printf("%i", port);
    48 	printf("%i\n", port);
   208 	fflush(stdout);
    49 	fflush(stdout);
   209 	char data[256];
    50 	char data[256];
   210 	while(ipcListenSocket||ipcConnSocket) {
    51 	while(flib_ipcconn_state() != IPC_NOT_CONNECTED) {
   211 		int size = flib_engine_read_message(data);
    52 		flib_ipcconn_tick();
       
    53 		int size = flib_ipcconn_recv_message(data);
   212 		if(size>0) {
    54 		if(size>0) {
       
    55 			data[size]=0;
       
    56 			flib_log_i("IPC IN: %s", data);
   213 			if(data[0]=='?') {
    57 			if(data[0]=='?') {
   214 				flib_engine_write_message("!", 1);
    58 				flib_log_i("IPC OUT: !");
       
    59 				flib_ipcconn_send_message("!", 1);
   215 			}
    60 			}
   216 		}
    61 		}
   217 	}
    62 	}
       
    63 	flib_log_i("IPC connection lost.");
   218 	return 0;
    64 	return 0;
   219 }
    65 }