project_files/frontlib/ipc/ipcbase.c
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7576 65d29988fd3d
child 8071 7d6aaba59505
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
       
    20 #include "ipcbase.h"
       
    21 #include "../util/logging.h"
       
    22 #include "../util/util.h"
       
    23 #include "../socket.h"
       
    24 
       
    25 #include <string.h>
       
    26 #include <stdbool.h>
       
    27 #include <stdlib.h>
       
    28 #include <stdio.h>
       
    29 
       
    30 /*
       
    31  * The receive buffer has to be able to hold any message that might be received. Normally
       
    32  * the messages are at most 256 bytes, but the map preview contains 4097 bytes (4096 for a
       
    33  * bitmap, 1 for the number of hogs which fit on the map).
       
    34  *
       
    35  * We don't need to worry about wasting a few kb though, and I like powers of two...
       
    36  */
       
    37 struct _flib_ipcbase {
       
    38 	uint8_t readBuffer[8192];
       
    39 	int readBufferSize;
       
    40 
       
    41 	flib_acceptor *acceptor;
       
    42 	uint16_t port;
       
    43 
       
    44 	flib_tcpsocket *sock;
       
    45 };
       
    46 
       
    47 flib_ipcbase *flib_ipcbase_create() {
       
    48 	flib_ipcbase *result = flib_calloc(1, sizeof(flib_ipcbase));
       
    49 	flib_acceptor *acceptor = flib_acceptor_create(0);
       
    50 
       
    51 	if(!result || !acceptor) {
       
    52 		free(result);
       
    53 		flib_acceptor_close(acceptor);
       
    54 		return NULL;
       
    55 	}
       
    56 
       
    57 	result->acceptor = acceptor;
       
    58 	result->sock = NULL;
       
    59 	result->readBufferSize = 0;
       
    60 	result->port = flib_acceptor_listenport(acceptor);
       
    61 
       
    62 	flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
       
    63 	return result;
       
    64 }
       
    65 
       
    66 uint16_t flib_ipcbase_port(flib_ipcbase *ipc) {
       
    67 	if(log_badargs_if(ipc==NULL)) {
       
    68 		return 0;
       
    69 	}
       
    70 	return ipc->port;
       
    71 }
       
    72 
       
    73 void flib_ipcbase_destroy(flib_ipcbase *ipc) {
       
    74 	if(ipc) {
       
    75 		flib_acceptor_close(ipc->acceptor);
       
    76 		flib_socket_close(ipc->sock);
       
    77 		if(ipc->sock) {
       
    78 			flib_log_d("IPC connection closed.");
       
    79 		}
       
    80 		free(ipc);
       
    81 	}
       
    82 }
       
    83 
       
    84 IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
       
    85 	if(log_badargs_if(ipc==NULL)) {
       
    86 		return IPC_NOT_CONNECTED;
       
    87 	} else if(ipc->sock) {
       
    88 		return IPC_CONNECTED;
       
    89 	} else if(ipc->acceptor) {
       
    90 		return IPC_LISTENING;
       
    91 	} else {
       
    92 		return IPC_NOT_CONNECTED;
       
    93 	}
       
    94 }
       
    95 
       
    96 static void receiveToBuffer(flib_ipcbase *ipc) {
       
    97 	if(ipc->sock) {
       
    98 		int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
       
    99 		if(size>=0) {
       
   100 			ipc->readBufferSize += size;
       
   101 		} else {
       
   102 			flib_log_d("IPC connection lost.");
       
   103 			flib_socket_close(ipc->sock);
       
   104 			ipc->sock = NULL;
       
   105 		}
       
   106 	}
       
   107 }
       
   108 
       
   109 static bool isMessageReady(flib_ipcbase *ipc) {
       
   110 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
       
   111 }
       
   112 
       
   113 static void logSentMsg(const uint8_t *data, size_t len) {
       
   114 	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
       
   115 		size_t msgStart = 0;
       
   116 		while(msgStart < len) {
       
   117 			uint8_t msglen = data[msgStart];
       
   118 			if(msgStart+msglen < len) {
       
   119 				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
       
   120 			} else {
       
   121 				uint8_t msglen2 = len-msgStart-1;
       
   122 				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
       
   123 			}
       
   124 			msgStart += (uint8_t)data[msgStart]+1;
       
   125 		}
       
   126 	}
       
   127 }
       
   128 
       
   129 static void logRecvMsg(const uint8_t *data) {
       
   130 	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
       
   131 		uint8_t msglen = data[0];
       
   132 		flib_log_d("[IPC IN][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+1);
       
   133 	}
       
   134 }
       
   135 
       
   136 static void popFromReadBuffer(flib_ipcbase *ipc, uint8_t *outbuf, size_t size) {
       
   137 	memcpy(outbuf, ipc->readBuffer, size);
       
   138 	memmove(ipc->readBuffer, ipc->readBuffer+size, ipc->readBufferSize-size);
       
   139 	ipc->readBufferSize -= size;
       
   140 }
       
   141 
       
   142 int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
       
   143 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
       
   144 		return -1;
       
   145 	}
       
   146 
       
   147 	if(!isMessageReady(ipc)) {
       
   148 		receiveToBuffer(ipc);
       
   149 	}
       
   150 
       
   151 	if(isMessageReady(ipc)) {
       
   152 		int msgsize = ipc->readBuffer[0]+1;
       
   153 		popFromReadBuffer(ipc, data, msgsize);
       
   154 		logRecvMsg(data);
       
   155 		return msgsize;
       
   156 	} else if(!ipc->sock && ipc->readBufferSize>0) {
       
   157 		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1);
       
   158 		ipc->readBufferSize = 0;
       
   159 		return -1;
       
   160 	} else {
       
   161 		return -1;
       
   162 	}
       
   163 }
       
   164 
       
   165 int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
       
   166 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
       
   167 		return -1;
       
   168 	}
       
   169 
       
   170 	receiveToBuffer(ipc);
       
   171 
       
   172 	if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
       
   173 		popFromReadBuffer(ipc, data, IPCBASE_MAPMSG_BYTES);
       
   174 		return IPCBASE_MAPMSG_BYTES;
       
   175 	} else {
       
   176 		return -1;
       
   177 	}
       
   178 }
       
   179 
       
   180 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
       
   181 	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
       
   182 			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
       
   183 		return -1;
       
   184 	}
       
   185 	if(flib_socket_send(ipc->sock, data, len) == len) {
       
   186 		logSentMsg(data, len);
       
   187 		return 0;
       
   188 	} else {
       
   189 		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
       
   190 		flib_socket_close(ipc->sock);
       
   191 		ipc->sock = NULL;
       
   192 		return -1;
       
   193 	}
       
   194 }
       
   195 
       
   196 int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len) {
       
   197 	if(log_badargs_if3(ipc==NULL, data==NULL && len>0, len>255)) {
       
   198 		return -1;
       
   199 	}
       
   200 
       
   201 	uint8_t sendbuf[256];
       
   202 	sendbuf[0] = len;
       
   203 	memcpy(sendbuf+1, data, len);
       
   204 	return flib_ipcbase_send_raw(ipc, sendbuf, len+1);
       
   205 }
       
   206 
       
   207 void flib_ipcbase_accept(flib_ipcbase *ipc) {
       
   208 	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
       
   209 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
       
   210 		if(ipc->sock) {
       
   211 			flib_log_d("IPC connection accepted.");
       
   212 			flib_acceptor_close(ipc->acceptor);
       
   213 			ipc->acceptor = NULL;
       
   214 		}
       
   215 	}
       
   216 }