project_files/frontlib/ipc/ipcbase.c
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 7576 65d29988fd3d
equal deleted inserted replaced
7314:6171f0bad318 7316:f7b49b2c5d84
    62 	flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
    62 	flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
    63 	return result;
    63 	return result;
    64 }
    64 }
    65 
    65 
    66 uint16_t flib_ipcbase_port(flib_ipcbase *ipc) {
    66 uint16_t flib_ipcbase_port(flib_ipcbase *ipc) {
    67 	if(!ipc) {
    67 	if(log_badargs_if(ipc==NULL)) {
    68 		flib_log_e("null parameter in flib_ipcbase_port");
       
    69 		return 0;
    68 		return 0;
    70 	}
    69 	}
    71 	return ipc->port;
    70 	return ipc->port;
    72 }
    71 }
    73 
    72 
    78 		free(ipc);
    77 		free(ipc);
    79 	}
    78 	}
    80 }
    79 }
    81 
    80 
    82 IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
    81 IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
    83 	if(!ipc) {
    82 	if(log_badargs_if(ipc==NULL)) {
    84 		flib_log_e("null parameter in flib_ipcbase_state");
       
    85 		return IPC_NOT_CONNECTED;
    83 		return IPC_NOT_CONNECTED;
    86 	} else if(ipc->sock) {
    84 	} else if(ipc->sock) {
    87 		return IPC_CONNECTED;
    85 		return IPC_CONNECTED;
    88 	} else if(ipc->acceptor) {
    86 	} else if(ipc->acceptor) {
    89 		return IPC_LISTENING;
    87 		return IPC_LISTENING;
   107 static bool isMessageReady(flib_ipcbase *ipc) {
   105 static bool isMessageReady(flib_ipcbase *ipc) {
   108 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
   106 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
   109 }
   107 }
   110 
   108 
   111 int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
   109 int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
   112 	if(!ipc || !data) {
   110 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
   113 		flib_log_e("null parameter in flib_ipcbase_recv_message");
       
   114 		return -1;
   111 		return -1;
   115 	}
   112 	}
   116 
   113 
   117 	if(!isMessageReady(ipc)) {
   114 	if(!isMessageReady(ipc)) {
   118 		receiveToBuffer(ipc);
   115 		receiveToBuffer(ipc);
   132 		return -1;
   129 		return -1;
   133 	}
   130 	}
   134 }
   131 }
   135 
   132 
   136 int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
   133 int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
   137 	if(!ipc || !data) {
   134 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
   138 		flib_log_e("null parameter in flib_ipcbase_recv_map");
       
   139 		return -1;
   135 		return -1;
   140 	}
   136 	}
   141 
   137 
   142 	receiveToBuffer(ipc);
   138 	receiveToBuffer(ipc);
   143 
   139 
   165 		}
   161 		}
   166 	}
   162 	}
   167 }
   163 }
   168 
   164 
   169 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
   165 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
   170 	if(!ipc || (!data && len>0)) {
   166 	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
   171 		flib_log_e("null parameter in flib_ipcbase_send_raw");
   167 			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
   172 		return -1;
   168 		return -1;
   173 	}
   169 	}
   174 	if(!ipc->sock) {
       
   175 		flib_log_w("flib_ipcbase_send_raw: Not connected.");
       
   176 		return -1;
       
   177 	}
       
   178 
       
   179 	if(flib_socket_send(ipc->sock, data, len) == len) {
   170 	if(flib_socket_send(ipc->sock, data, len) == len) {
   180 		logSentMsg(data, len);
   171 		logSentMsg(data, len);
   181 		return 0;
   172 		return 0;
   182 	} else {
   173 	} else {
   183 		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
   174 		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
   186 		return -1;
   177 		return -1;
   187 	}
   178 	}
   188 }
   179 }
   189 
   180 
   190 int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len) {
   181 int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len) {
   191 	if(!ipc || (!data && len>0)) {
   182 	if(log_badargs_if3(ipc==NULL, data==NULL && len>0, len>255)) {
   192 		flib_log_e("null parameter in flib_ipcbase_send_message");
       
   193 		return -1;
       
   194 	} else if(len>255) {
       
   195 		flib_log_e("Overlong message (%zu bytes) in flib_ipcbase_send_message", len);
       
   196 		return -1;
   183 		return -1;
   197 	}
   184 	}
   198 
   185 
   199 	uint8_t sendbuf[256];
   186 	uint8_t sendbuf[256];
   200 	sendbuf[0] = len;
   187 	sendbuf[0] = len;
   201 	memcpy(sendbuf+1, data, len);
   188 	memcpy(sendbuf+1, data, len);
   202 	return flib_ipcbase_send_raw(ipc, sendbuf, len+1);
   189 	return flib_ipcbase_send_raw(ipc, sendbuf, len+1);
   203 }
   190 }
   204 
   191 
   205 void flib_ipcbase_accept(flib_ipcbase *ipc) {
   192 void flib_ipcbase_accept(flib_ipcbase *ipc) {
   206 	if(!ipc) {
   193 	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
   207 		flib_log_e("null parameter in flib_ipcbase_accept");
       
   208 	} else if(!ipc->sock && ipc->acceptor) {
       
   209 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
   194 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
   210 		if(ipc->sock) {
   195 		if(ipc->sock) {
   211 			flib_acceptor_close(ipc->acceptor);
   196 			flib_acceptor_close(ipc->acceptor);
   212 			ipc->acceptor = NULL;
   197 			ipc->acceptor = NULL;
   213 		}
   198 		}