project_files/frontlib/ipc/ipcbase.c
changeset 7576 65d29988fd3d
parent 7316 f7b49b2c5d84
child 8071 7d6aaba59505
equal deleted inserted replaced
7574:b9ec869e624a 7576:65d29988fd3d
    72 
    72 
    73 void flib_ipcbase_destroy(flib_ipcbase *ipc) {
    73 void flib_ipcbase_destroy(flib_ipcbase *ipc) {
    74 	if(ipc) {
    74 	if(ipc) {
    75 		flib_acceptor_close(ipc->acceptor);
    75 		flib_acceptor_close(ipc->acceptor);
    76 		flib_socket_close(ipc->sock);
    76 		flib_socket_close(ipc->sock);
       
    77 		if(ipc->sock) {
       
    78 			flib_log_d("IPC connection closed.");
       
    79 		}
    77 		free(ipc);
    80 		free(ipc);
    78 	}
    81 	}
    79 }
    82 }
    80 
    83 
    81 IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
    84 IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
    94 	if(ipc->sock) {
    97 	if(ipc->sock) {
    95 		int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
    98 		int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
    96 		if(size>=0) {
    99 		if(size>=0) {
    97 			ipc->readBufferSize += size;
   100 			ipc->readBufferSize += size;
    98 		} else {
   101 		} else {
       
   102 			flib_log_d("IPC connection lost.");
    99 			flib_socket_close(ipc->sock);
   103 			flib_socket_close(ipc->sock);
   100 			ipc->sock = NULL;
   104 			ipc->sock = NULL;
   101 		}
   105 		}
   102 	}
   106 	}
   103 }
   107 }
   104 
   108 
   105 static bool isMessageReady(flib_ipcbase *ipc) {
   109 static bool isMessageReady(flib_ipcbase *ipc) {
   106 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
   110 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
   107 }
       
   108 
       
   109 int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
       
   110 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
       
   111 		return -1;
       
   112 	}
       
   113 
       
   114 	if(!isMessageReady(ipc)) {
       
   115 		receiveToBuffer(ipc);
       
   116 	}
       
   117 
       
   118 	if(isMessageReady(ipc)) {
       
   119 		int msgsize = ipc->readBuffer[0]+1;
       
   120 		memcpy(data, ipc->readBuffer, msgsize);
       
   121 		memmove(ipc->readBuffer, ipc->readBuffer+msgsize, ipc->readBufferSize-msgsize);
       
   122 		ipc->readBufferSize -= msgsize;
       
   123 		return msgsize;
       
   124 	} else if(!ipc->sock && ipc->readBufferSize>0) {
       
   125 		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1);
       
   126 		ipc->readBufferSize = 0;
       
   127 		return -1;
       
   128 	} else {
       
   129 		return -1;
       
   130 	}
       
   131 }
       
   132 
       
   133 int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
       
   134 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
       
   135 		return -1;
       
   136 	}
       
   137 
       
   138 	receiveToBuffer(ipc);
       
   139 
       
   140 	if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
       
   141 		memcpy(data, ipc->readBuffer, IPCBASE_MAPMSG_BYTES);
       
   142 		memmove(ipc->readBuffer, ipc->readBuffer+IPCBASE_MAPMSG_BYTES, ipc->readBufferSize-IPCBASE_MAPMSG_BYTES);
       
   143 		return IPCBASE_MAPMSG_BYTES;
       
   144 	} else {
       
   145 		return -1;
       
   146 	}
       
   147 }
   111 }
   148 
   112 
   149 static void logSentMsg(const uint8_t *data, size_t len) {
   113 static void logSentMsg(const uint8_t *data, size_t len) {
   150 	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
   114 	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
   151 		size_t msgStart = 0;
   115 		size_t msgStart = 0;
   160 			msgStart += (uint8_t)data[msgStart]+1;
   124 			msgStart += (uint8_t)data[msgStart]+1;
   161 		}
   125 		}
   162 	}
   126 	}
   163 }
   127 }
   164 
   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 
   165 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
   180 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
   166 	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
   181 	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
   167 			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
   182 			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
   168 		return -1;
   183 		return -1;
   169 	}
   184 	}
   191 
   206 
   192 void flib_ipcbase_accept(flib_ipcbase *ipc) {
   207 void flib_ipcbase_accept(flib_ipcbase *ipc) {
   193 	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
   208 	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
   194 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
   209 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
   195 		if(ipc->sock) {
   210 		if(ipc->sock) {
       
   211 			flib_log_d("IPC connection accepted.");
   196 			flib_acceptor_close(ipc->acceptor);
   212 			flib_acceptor_close(ipc->acceptor);
   197 			ipc->acceptor = NULL;
   213 			ipc->acceptor = NULL;
   198 		}
   214 		}
   199 	}
   215 	}
   200 }
   216 }