project_files/frontlib/ipc/ipcconn.h
changeset 7234 613998625a3c
parent 7230 240620f46dd7
child 7267 710f3ced8934
equal deleted inserted replaced
7230:240620f46dd7 7234:613998625a3c
     1 /*
       
     2  * Low-level protocol support for the IPC connection to the engine.
       
     3  */
       
     4 
       
     5 #ifndef IPCCONN_H_
       
     6 #define IPCCONN_H_
       
     7 
       
     8 #include "../util/buffer.h"
       
     9 
       
    10 #include <stddef.h>
       
    11 #include <stdbool.h>
       
    12 
       
    13 #define IPCCONN_MAPMSG_BYTES 4097
       
    14 
       
    15 typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
       
    16 
       
    17 struct _flib_ipcconn;
       
    18 typedef struct _flib_ipcconn flib_ipcconn;
       
    19 
       
    20 /**
       
    21  * Start an engine connection by listening on a random port. The selected port can
       
    22  * be queried with flib_ipcconn_port and has to be passed to the engine.
       
    23  *
       
    24  * The parameter "recordDemo" can be used to control whether demo recording should
       
    25  * be enabled for this connection. The localPlayerName is needed for demo
       
    26  * recording purposes.
       
    27  *
       
    28  * Returns NULL on error. Destroy the created object with flib_ipcconn_destroy.
       
    29  *
       
    30  * We stop accepting new connections once a connection has been established, so you
       
    31  * need to create a new ipcconn in order to start a new connection.
       
    32  */
       
    33 flib_ipcconn *flib_ipcconn_create();
       
    34 
       
    35 uint16_t flib_ipcconn_port(flib_ipcconn *ipc);
       
    36 
       
    37 /**
       
    38  * Free resources and close sockets.
       
    39  */
       
    40 void flib_ipcconn_destroy(flib_ipcconn *ipc);
       
    41 
       
    42 /**
       
    43  * Determine the current connection state
       
    44  */
       
    45 IpcConnState flib_ipcconn_state(flib_ipcconn *ipc);
       
    46 
       
    47 /**
       
    48  * Receive a single message (up to 256 bytes) and copy it into the data buffer.
       
    49  * Returns the length of the received message, a negative value if no message could
       
    50  * be read.
       
    51  *
       
    52  * The first byte of a message is its content length, which is one less than the returned
       
    53  * value.
       
    54  *
       
    55  * Note: When a connection is closed, you probably want to call this function until
       
    56  * no further message is returned, to ensure you see all messages that were sent
       
    57  * before the connection closed.
       
    58  */
       
    59 int flib_ipcconn_recv_message(flib_ipcconn *ipc, void *data);
       
    60 
       
    61 /**
       
    62  * Try to receive 4097 bytes. This is the size of the reply the engine sends
       
    63  * when successfully queried for map data. The first 4096 bytes are a bit-packed
       
    64  * twocolor image of the map (256x128), the last byte is the number of hogs that
       
    65  * fit on the map.
       
    66  */
       
    67 int flib_ipcconn_recv_map(flib_ipcconn *ipc, void *data);
       
    68 
       
    69 int flib_ipcconn_send_raw(flib_ipcconn *ipc, const void *data, size_t len);
       
    70 
       
    71 /**
       
    72  * Write a single message (up to 255 bytes) to the engine. This call blocks until the
       
    73  * message is completely written or the connection is closed or an error occurs.
       
    74  *
       
    75  * Calling this function in a state other than IPC_CONNECTED will fail immediately.
       
    76  * Returns a negative value on failure.
       
    77  */
       
    78 int flib_ipcconn_send_message(flib_ipcconn *ipc, void *data, size_t len);
       
    79 
       
    80 /**
       
    81  * Convenience function for sending a 0-delimited string.
       
    82  */
       
    83 int flib_ipcconn_send_messagestr(flib_ipcconn *ipc, char *data);
       
    84 
       
    85 /**
       
    86  * Call regularly to allow background work to proceed
       
    87  */
       
    88 void flib_ipcconn_accept(flib_ipcconn *ipc);
       
    89 
       
    90 #endif /* IPCCONN_H_ */
       
    91