1 #include "ipcconn.h" |
|
2 #include "../util/logging.h" |
|
3 #include "../util/util.h" |
|
4 #include "../socket.h" |
|
5 |
|
6 #include <string.h> |
|
7 #include <stdbool.h> |
|
8 #include <stdlib.h> |
|
9 #include <stdio.h> |
|
10 |
|
11 /* |
|
12 * The receive buffer has to be able to hold any message that might be received. Normally |
|
13 * the messages are at most 256 bytes, but the map preview contains 4097 bytes (4096 for a |
|
14 * bitmap, 1 for the number of hogs which fit on the map). |
|
15 * |
|
16 * We don't need to worry about wasting a few kb though, and I like powers of two... |
|
17 */ |
|
18 struct _flib_ipcconn { |
|
19 uint8_t readBuffer[8192]; |
|
20 int readBufferSize; |
|
21 |
|
22 flib_acceptor *acceptor; |
|
23 uint16_t port; |
|
24 |
|
25 flib_tcpsocket *sock; |
|
26 }; |
|
27 |
|
28 flib_ipcconn *flib_ipcconn_create() { |
|
29 flib_ipcconn *result = flib_malloc(sizeof(flib_ipcconn)); |
|
30 flib_acceptor *acceptor = flib_acceptor_create(0); |
|
31 |
|
32 if(!result || !acceptor) { |
|
33 free(result); |
|
34 flib_acceptor_close(acceptor); |
|
35 return NULL; |
|
36 } |
|
37 |
|
38 result->acceptor = acceptor; |
|
39 result->sock = NULL; |
|
40 result->readBufferSize = 0; |
|
41 result->port = flib_acceptor_listenport(acceptor); |
|
42 |
|
43 flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port); |
|
44 return result; |
|
45 } |
|
46 |
|
47 uint16_t flib_ipcconn_port(flib_ipcconn *ipc) { |
|
48 if(!ipc) { |
|
49 flib_log_e("null parameter in flib_ipcconn_port"); |
|
50 return 0; |
|
51 } |
|
52 return ipc->port; |
|
53 } |
|
54 |
|
55 void flib_ipcconn_destroy(flib_ipcconn *ipc) { |
|
56 if(ipc) { |
|
57 flib_acceptor_close(ipc->acceptor); |
|
58 flib_socket_close(ipc->sock); |
|
59 free(ipc); |
|
60 } |
|
61 } |
|
62 |
|
63 IpcConnState flib_ipcconn_state(flib_ipcconn *ipc) { |
|
64 if(!ipc) { |
|
65 flib_log_e("null parameter in flib_ipcconn_state"); |
|
66 return IPC_NOT_CONNECTED; |
|
67 } else if(ipc->sock) { |
|
68 return IPC_CONNECTED; |
|
69 } else if(ipc->acceptor) { |
|
70 return IPC_LISTENING; |
|
71 } else { |
|
72 return IPC_NOT_CONNECTED; |
|
73 } |
|
74 } |
|
75 |
|
76 static bool isMessageReady(flib_ipcconn *ipc) { |
|
77 return ipc->readBufferSize >= ipc->readBuffer[0]+1; |
|
78 } |
|
79 |
|
80 static void receiveToBuffer(flib_ipcconn *ipc) { |
|
81 if(ipc->sock) { |
|
82 int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize); |
|
83 if(size>=0) { |
|
84 ipc->readBufferSize += size; |
|
85 } else { |
|
86 flib_socket_close(ipc->sock); |
|
87 ipc->sock = NULL; |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 int flib_ipcconn_recv_message(flib_ipcconn *ipc, void *data) { |
|
93 if(!ipc || !data) { |
|
94 flib_log_e("null parameter in flib_ipcconn_recv_message"); |
|
95 return -1; |
|
96 } |
|
97 |
|
98 if(!isMessageReady(ipc)) { |
|
99 receiveToBuffer(ipc); |
|
100 } |
|
101 |
|
102 if(isMessageReady(ipc)) { |
|
103 int msgsize = ipc->readBuffer[0]+1; |
|
104 memcpy(data, ipc->readBuffer, msgsize); |
|
105 memmove(ipc->readBuffer, ipc->readBuffer+msgsize, ipc->readBufferSize-msgsize); |
|
106 ipc->readBufferSize -= msgsize; |
|
107 return msgsize; |
|
108 } else if(!ipc->sock && ipc->readBufferSize>0) { |
|
109 flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1); |
|
110 ipc->readBufferSize = 0; |
|
111 return -1; |
|
112 } else { |
|
113 return -1; |
|
114 } |
|
115 } |
|
116 |
|
117 int flib_ipcconn_recv_map(flib_ipcconn *ipc, void *data) { |
|
118 if(!ipc || !data) { |
|
119 flib_log_e("null parameter in flib_ipcconn_recv_map"); |
|
120 return -1; |
|
121 } |
|
122 |
|
123 receiveToBuffer(ipc); |
|
124 |
|
125 if(ipc->readBufferSize >= IPCCONN_MAPMSG_BYTES) { |
|
126 memcpy(data, ipc->readBuffer, IPCCONN_MAPMSG_BYTES); |
|
127 memmove(ipc->readBuffer, ipc->readBuffer+IPCCONN_MAPMSG_BYTES, ipc->readBufferSize-IPCCONN_MAPMSG_BYTES); |
|
128 return IPCCONN_MAPMSG_BYTES; |
|
129 } else { |
|
130 return -1; |
|
131 } |
|
132 } |
|
133 |
|
134 static void logSentMsg(const uint8_t *data, size_t len) { |
|
135 if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) { |
|
136 size_t msgStart = 0; |
|
137 while(msgStart < len) { |
|
138 uint8_t msglen = data[msgStart]; |
|
139 if(msgStart+msglen < len) { |
|
140 flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1); |
|
141 } else { |
|
142 uint8_t msglen2 = len-msgStart-1; |
|
143 flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1); |
|
144 } |
|
145 msgStart += (uint8_t)data[msgStart]+1; |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 int flib_ipcconn_send_raw(flib_ipcconn *ipc, const void *data, size_t len) { |
|
151 if(!ipc || (!data && len>0)) { |
|
152 flib_log_e("null parameter in flib_ipcconn_send_raw"); |
|
153 return -1; |
|
154 } |
|
155 if(!ipc->sock) { |
|
156 flib_log_w("flib_ipcconn_send_raw: Not connected."); |
|
157 return -1; |
|
158 } |
|
159 |
|
160 if(flib_socket_send(ipc->sock, data, len) == len) { |
|
161 logSentMsg(data, len); |
|
162 return 0; |
|
163 } else { |
|
164 flib_log_w("Failed or incomplete ICP write: engine connection lost."); |
|
165 flib_socket_close(ipc->sock); |
|
166 ipc->sock = NULL; |
|
167 return -1; |
|
168 } |
|
169 } |
|
170 |
|
171 int flib_ipcconn_send_message(flib_ipcconn *ipc, void *data, size_t len) { |
|
172 if(!ipc || (!data && len>0)) { |
|
173 flib_log_e("null parameter in flib_ipcconn_send_message"); |
|
174 return -1; |
|
175 } else if(len>255) { |
|
176 flib_log_e("Overlong message (%zu bytes) in flib_ipcconn_send_message", len); |
|
177 return -1; |
|
178 } |
|
179 |
|
180 uint8_t sendbuf[256]; |
|
181 sendbuf[0] = len; |
|
182 memcpy(sendbuf+1, data, len); |
|
183 return flib_ipcconn_send_raw(ipc, sendbuf, len+1); |
|
184 } |
|
185 |
|
186 int flib_ipcconn_send_messagestr(flib_ipcconn *ipc, char *data) { |
|
187 return flib_ipcconn_send_message(ipc, data, strlen(data)); |
|
188 } |
|
189 |
|
190 void flib_ipcconn_accept(flib_ipcconn *ipc) { |
|
191 if(!ipc) { |
|
192 flib_log_e("null parameter in flib_ipcconn_accept"); |
|
193 } else if(!ipc->sock && ipc->acceptor) { |
|
194 ipc->sock = flib_socket_accept(ipc->acceptor, true); |
|
195 if(ipc->sock) { |
|
196 flib_acceptor_close(ipc->acceptor); |
|
197 ipc->acceptor = NULL; |
|
198 } |
|
199 } |
|
200 } |
|