equal
deleted
inserted
replaced
1 #include "mapconn.h" |
1 #include "mapconn.h" |
2 #include "ipcconn.h" |
2 #include "ipcbase.h" |
3 #include "ipcprotocol.h" |
3 #include "ipcprotocol.h" |
4 |
4 |
5 #include "../util/logging.h" |
5 #include "../util/logging.h" |
6 #include "../util/buffer.h" |
6 #include "../util/buffer.h" |
7 #include "../util/util.h" |
7 #include "../util/util.h" |
13 AWAIT_REPLY, |
13 AWAIT_REPLY, |
14 FINISHED |
14 FINISHED |
15 } mapconn_state; |
15 } mapconn_state; |
16 |
16 |
17 struct _flib_mapconn { |
17 struct _flib_mapconn { |
18 uint8_t mapBuffer[IPCCONN_MAPMSG_BYTES]; |
18 uint8_t mapBuffer[IPCBASE_MAPMSG_BYTES]; |
19 flib_ipcconn *connection; |
19 flib_ipcbase *ipcBase; |
20 flib_vector *configBuffer; |
20 flib_vector *configBuffer; |
21 |
21 |
22 mapconn_state progress; |
22 mapconn_state progress; |
23 |
23 |
24 void (*onSuccessCb)(void*, const uint8_t*, int); |
24 void (*onSuccessCb)(void*, const uint8_t*, int); |
58 |
58 |
59 flib_mapconn *flib_mapconn_create(char *seed, flib_map *mapdesc) { |
59 flib_mapconn *flib_mapconn_create(char *seed, flib_map *mapdesc) { |
60 flib_mapconn *result = NULL; |
60 flib_mapconn *result = NULL; |
61 flib_mapconn *tempConn = flib_calloc(1, sizeof(flib_mapconn)); |
61 flib_mapconn *tempConn = flib_calloc(1, sizeof(flib_mapconn)); |
62 if(tempConn) { |
62 if(tempConn) { |
63 tempConn->connection = flib_ipcconn_create(); |
63 tempConn->ipcBase = flib_ipcbase_create(); |
64 tempConn->configBuffer = createConfigBuffer(seed, mapdesc); |
64 tempConn->configBuffer = createConfigBuffer(seed, mapdesc); |
65 if(tempConn->connection && tempConn->configBuffer) { |
65 if(tempConn->ipcBase && tempConn->configBuffer) { |
66 tempConn->progress = AWAIT_CONNECTION; |
66 tempConn->progress = AWAIT_CONNECTION; |
67 clearCallbacks(tempConn); |
67 clearCallbacks(tempConn); |
68 result = tempConn; |
68 result = tempConn; |
69 tempConn = NULL; |
69 tempConn = NULL; |
70 } |
70 } |
82 * sent to prevent surprises. |
82 * sent to prevent surprises. |
83 */ |
83 */ |
84 clearCallbacks(conn); |
84 clearCallbacks(conn); |
85 conn->destroyRequested = true; |
85 conn->destroyRequested = true; |
86 } else { |
86 } else { |
87 flib_ipcconn_destroy(conn->connection); |
87 flib_ipcbase_destroy(conn->ipcBase); |
88 flib_vector_destroy(conn->configBuffer); |
88 flib_vector_destroy(conn->configBuffer); |
89 free(conn); |
89 free(conn); |
90 } |
90 } |
91 } |
91 } |
92 } |
92 } |
94 int flib_mapconn_getport(flib_mapconn *conn) { |
94 int flib_mapconn_getport(flib_mapconn *conn) { |
95 if(!conn) { |
95 if(!conn) { |
96 flib_log_e("null parameter in flib_mapconn_getport"); |
96 flib_log_e("null parameter in flib_mapconn_getport"); |
97 return 0; |
97 return 0; |
98 } else { |
98 } else { |
99 return flib_ipcconn_port(conn->connection); |
99 return flib_ipcbase_port(conn->ipcBase); |
100 } |
100 } |
101 } |
101 } |
102 |
102 |
103 void flib_mapconn_onSuccess(flib_mapconn *conn, void (*callback)(void* context, const uint8_t *bitmap, int numHedgehogs), void *context) { |
103 void flib_mapconn_onSuccess(flib_mapconn *conn, void (*callback)(void* context, const uint8_t *bitmap, int numHedgehogs), void *context) { |
104 if(!conn) { |
104 if(!conn) { |
118 } |
118 } |
119 } |
119 } |
120 |
120 |
121 static void flib_mapconn_wrappedtick(flib_mapconn *conn) { |
121 static void flib_mapconn_wrappedtick(flib_mapconn *conn) { |
122 if(conn->progress == AWAIT_CONNECTION) { |
122 if(conn->progress == AWAIT_CONNECTION) { |
123 flib_ipcconn_accept(conn->connection); |
123 flib_ipcbase_accept(conn->ipcBase); |
124 switch(flib_ipcconn_state(conn->connection)) { |
124 switch(flib_ipcbase_state(conn->ipcBase)) { |
125 case IPC_CONNECTED: |
125 case IPC_CONNECTED: |
126 { |
126 { |
127 flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer); |
127 flib_constbuffer configBuffer = flib_vector_as_constbuffer(conn->configBuffer); |
128 if(flib_ipcconn_send_raw(conn->connection, configBuffer.data, configBuffer.size)) { |
128 if(flib_ipcbase_send_raw(conn->ipcBase, configBuffer.data, configBuffer.size)) { |
129 conn->progress = FINISHED; |
129 conn->progress = FINISHED; |
130 conn->onFailureCb(conn->onFailureCtx, "Error sending map information to the engine."); |
130 conn->onFailureCb(conn->onFailureCtx, "Error sending map information to the engine."); |
131 return; |
131 return; |
132 } else { |
132 } else { |
133 conn->progress = AWAIT_REPLY; |
133 conn->progress = AWAIT_REPLY; |
142 break; |
142 break; |
143 } |
143 } |
144 } |
144 } |
145 |
145 |
146 if(conn->progress == AWAIT_REPLY) { |
146 if(conn->progress == AWAIT_REPLY) { |
147 if(flib_ipcconn_recv_map(conn->connection, conn->mapBuffer) >= 0) { |
147 if(flib_ipcbase_recv_map(conn->ipcBase, conn->mapBuffer) >= 0) { |
148 conn->progress = FINISHED; |
148 conn->progress = FINISHED; |
149 conn->onSuccessCb(conn->onSuccessCtx, conn->mapBuffer, conn->mapBuffer[IPCCONN_MAPMSG_BYTES-1]); |
149 conn->onSuccessCb(conn->onSuccessCtx, conn->mapBuffer, conn->mapBuffer[IPCBASE_MAPMSG_BYTES-1]); |
150 return; |
150 return; |
151 } else if(flib_ipcconn_state(conn->connection) != IPC_CONNECTED) { |
151 } else if(flib_ipcbase_state(conn->ipcBase) != IPC_CONNECTED) { |
152 conn->progress = FINISHED; |
152 conn->progress = FINISHED; |
153 conn->onFailureCb(conn->onSuccessCtx, "Engine connection closed unexpectedly."); |
153 conn->onFailureCb(conn->onSuccessCtx, "Engine connection closed unexpectedly."); |
154 return; |
154 return; |
155 } |
155 } |
156 } |
156 } |