project_files/frontlib/ipc/mapconn.c
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7234 613998625a3c
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
     2 #include "ipcconn.h"
     2 #include "ipcconn.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 
     8 
     8 #include <stdlib.h>
     9 #include <stdlib.h>
     9 
    10 
    10 typedef enum {
    11 typedef enum {
    11 	AWAIT_CONNECTION,
    12 	AWAIT_CONNECTION,
    13 	FINISHED
    14 	FINISHED
    14 } mapconn_state;
    15 } mapconn_state;
    15 
    16 
    16 struct _flib_mapconn {
    17 struct _flib_mapconn {
    17 	uint8_t mapBuffer[IPCCONN_MAPMSG_BYTES];
    18 	uint8_t mapBuffer[IPCCONN_MAPMSG_BYTES];
    18 	flib_ipcconn connection;
    19 	flib_ipcconn *connection;
    19 	flib_vector configBuffer;
    20 	flib_vector *configBuffer;
    20 
    21 
    21 	mapconn_state progress;
    22 	mapconn_state progress;
    22 
    23 
    23 	void (*onSuccessCb)(void*, const uint8_t*, int);
    24 	void (*onSuccessCb)(void*, const uint8_t*, int);
    24 	void *onSuccessCtx;
    25 	void *onSuccessCtx;
    36 static void clearCallbacks(flib_mapconn *conn) {
    37 static void clearCallbacks(flib_mapconn *conn) {
    37 	conn->onSuccessCb = &noop_handleSuccess;
    38 	conn->onSuccessCb = &noop_handleSuccess;
    38 	conn->onFailureCb = &noop_handleFailure;
    39 	conn->onFailureCb = &noop_handleFailure;
    39 }
    40 }
    40 
    41 
    41 static flib_vector createConfigBuffer(char *seed, flib_map *mapdesc) {
    42 static flib_vector *createConfigBuffer(char *seed, flib_map *mapdesc) {
    42 	flib_vector result = NULL;
    43 	flib_vector *result = NULL;
    43 	flib_vector tempbuffer = flib_vector_create();
    44 	flib_vector *tempbuffer = flib_vector_create();
    44 	if(tempbuffer) {
    45 	if(tempbuffer) {
    45 		bool error = false;
    46 		bool error = false;
    46 		error |= flib_ipc_append_seed(tempbuffer, seed);
    47 		error |= flib_ipc_append_seed(tempbuffer, seed);
    47 		error |= flib_ipc_append_mapconf(tempbuffer, mapdesc, true);
    48 		error |= flib_ipc_append_mapconf(tempbuffer, mapdesc, true);
    48 		error |= flib_ipc_append_message(tempbuffer, "!");
    49 		error |= flib_ipc_append_message(tempbuffer, "!");
    49 		if(!error) {
    50 		if(!error) {
    50 			result = tempbuffer;
    51 			result = tempbuffer;
    51 			tempbuffer = NULL;
    52 			tempbuffer = NULL;
    52 		}
    53 		}
    53 	}
    54 	}
    54 	flib_vector_destroy(&tempbuffer);
    55 	flib_vector_destroy(tempbuffer);
    55 	return result;
    56 	return result;
    56 }
    57 }
    57 
    58 
    58 flib_mapconn *flib_mapconn_create(char *seed, flib_map *mapdesc) {
    59 flib_mapconn *flib_mapconn_create(char *seed, flib_map *mapdesc) {
    59 	flib_mapconn *result = NULL;
    60 	flib_mapconn *result = NULL;
    60 	flib_mapconn *tempConn = calloc(1, sizeof(flib_mapconn));
    61 	flib_mapconn *tempConn = flib_calloc(1, sizeof(flib_mapconn));
    61 	if(tempConn) {
    62 	if(tempConn) {
    62 		tempConn->connection = flib_ipcconn_create(false, "Player");
    63 		tempConn->connection = flib_ipcconn_create();
    63 		tempConn->configBuffer = createConfigBuffer(seed, mapdesc);
    64 		tempConn->configBuffer = createConfigBuffer(seed, mapdesc);
    64 		if(tempConn->connection && tempConn->configBuffer) {
    65 		if(tempConn->connection && tempConn->configBuffer) {
    65 			tempConn->progress = AWAIT_CONNECTION;
    66 			tempConn->progress = AWAIT_CONNECTION;
    66 			clearCallbacks(tempConn);
    67 			clearCallbacks(tempConn);
    67 			result = tempConn;
    68 			result = tempConn;
    81 			 * sent to prevent surprises.
    82 			 * sent to prevent surprises.
    82 			 */
    83 			 */
    83 			clearCallbacks(conn);
    84 			clearCallbacks(conn);
    84 			conn->destroyRequested = true;
    85 			conn->destroyRequested = true;
    85 		} else {
    86 		} else {
    86 			flib_ipcconn_destroy(&conn->connection);
    87 			flib_ipcconn_destroy(conn->connection);
    87 			flib_vector_destroy(&conn->configBuffer);
    88 			flib_vector_destroy(conn->configBuffer);
    88 			free(conn);
    89 			free(conn);
    89 		}
    90 		}
    90 	}
    91 	}
    91 }
    92 }
    92 
    93