project_files/frontlib/ipc/ipcconn.h
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
    13 #define IPCCONN_MAPMSG_BYTES 4097
    13 #define IPCCONN_MAPMSG_BYTES 4097
    14 
    14 
    15 typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
    15 typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
    16 
    16 
    17 struct _flib_ipcconn;
    17 struct _flib_ipcconn;
    18 typedef struct _flib_ipcconn *flib_ipcconn;
    18 typedef struct _flib_ipcconn flib_ipcconn;
    19 
    19 
    20 /**
    20 /**
    21  * TODO move demo recording up by one layer?
       
    22  *
       
    23  * Start an engine connection by listening on a random port. The selected port can
    21  * Start an engine connection by listening on a random port. The selected port can
    24  * be queried with flib_ipcconn_port and has to be passed to the engine.
    22  * be queried with flib_ipcconn_port and has to be passed to the engine.
    25  *
    23  *
    26  * The parameter "recordDemo" can be used to control whether demo recording should
    24  * The parameter "recordDemo" can be used to control whether demo recording should
    27  * be enabled for this connection. The localPlayerName is needed for demo
    25  * be enabled for this connection. The localPlayerName is needed for demo
    30  * Returns NULL on error. Destroy the created object with flib_ipcconn_destroy.
    28  * Returns NULL on error. Destroy the created object with flib_ipcconn_destroy.
    31  *
    29  *
    32  * We stop accepting new connections once a connection has been established, so you
    30  * We stop accepting new connections once a connection has been established, so you
    33  * need to create a new ipcconn in order to start a new connection.
    31  * need to create a new ipcconn in order to start a new connection.
    34  */
    32  */
    35 flib_ipcconn flib_ipcconn_create(bool recordDemo, const char *localPlayerName);
    33 flib_ipcconn *flib_ipcconn_create();
    36 
    34 
    37 uint16_t flib_ipcconn_port(flib_ipcconn ipc);
    35 uint16_t flib_ipcconn_port(flib_ipcconn *ipc);
    38 
    36 
    39 /**
    37 /**
    40  * Free resources, close sockets, and set the pointer to NULL.
    38  * Free resources and close sockets.
    41  */
    39  */
    42 void flib_ipcconn_destroy(flib_ipcconn *ipcptr);
    40 void flib_ipcconn_destroy(flib_ipcconn *ipc);
    43 
    41 
    44 /**
    42 /**
    45  * Determine the current connection state
    43  * Determine the current connection state
    46  */
    44  */
    47 IpcConnState flib_ipcconn_state(flib_ipcconn ipc);
    45 IpcConnState flib_ipcconn_state(flib_ipcconn *ipc);
    48 
    46 
    49 /**
    47 /**
    50  * Receive a single message (up to 256 bytes) and copy it into the data buffer.
    48  * Receive a single message (up to 256 bytes) and copy it into the data buffer.
    51  * Returns the length of the received message, a negative value if no message could
    49  * Returns the length of the received message, a negative value if no message could
    52  * be read.
    50  * be read.
    56  *
    54  *
    57  * Note: When a connection is closed, you probably want to call this function until
    55  * Note: When a connection is closed, you probably want to call this function until
    58  * no further message is returned, to ensure you see all messages that were sent
    56  * no further message is returned, to ensure you see all messages that were sent
    59  * before the connection closed.
    57  * before the connection closed.
    60  */
    58  */
    61 int flib_ipcconn_recv_message(flib_ipcconn ipc, void *data);
    59 int flib_ipcconn_recv_message(flib_ipcconn *ipc, void *data);
    62 
    60 
    63 /**
    61 /**
    64  * Try to receive 4097 bytes. This is the size of the reply the engine sends
    62  * Try to receive 4097 bytes. This is the size of the reply the engine sends
    65  * when successfully queried for map data. The first 4096 bytes are a bit-packed
    63  * when successfully queried for map data. The first 4096 bytes are a bit-packed
    66  * twocolor image of the map (256x128), the last byte is the number of hogs that
    64  * twocolor image of the map (256x128), the last byte is the number of hogs that
    67  * fit on the map.
    65  * fit on the map.
    68  */
    66  */
    69 int flib_ipcconn_recv_map(flib_ipcconn ipc, void *data);
    67 int flib_ipcconn_recv_map(flib_ipcconn *ipc, void *data);
    70 
    68 
    71 int flib_ipcconn_send_raw(flib_ipcconn ipc, const void *data, size_t len);
    69 int flib_ipcconn_send_raw(flib_ipcconn *ipc, const void *data, size_t len);
    72 
    70 
    73 /**
    71 /**
    74  * Write a single message (up to 255 bytes) to the engine. This call blocks until the
    72  * Write a single message (up to 255 bytes) to the engine. This call blocks until the
    75  * message is completely written or the connection is closed or an error occurs.
    73  * message is completely written or the connection is closed or an error occurs.
    76  *
    74  *
    77  * Calling this function in a state other than IPC_CONNECTED will fail immediately.
    75  * Calling this function in a state other than IPC_CONNECTED will fail immediately.
    78  * Returns a negative value on failure.
    76  * Returns a negative value on failure.
    79  */
    77  */
    80 int flib_ipcconn_send_message(flib_ipcconn ipc, void *data, size_t len);
    78 int flib_ipcconn_send_message(flib_ipcconn *ipc, void *data, size_t len);
    81 
    79 
    82 /**
    80 /**
    83  * Convenience function for sending a 0-delimited string.
    81  * Convenience function for sending a 0-delimited string.
    84  */
    82  */
    85 int flib_ipcconn_send_messagestr(flib_ipcconn ipc, char *data);
    83 int flib_ipcconn_send_messagestr(flib_ipcconn *ipc, char *data);
    86 
    84 
    87 /**
    85 /**
    88  * Call regularly to allow background work to proceed
    86  * Call regularly to allow background work to proceed
    89  */
    87  */
    90 void flib_ipcconn_accept(flib_ipcconn ipc);
    88 void flib_ipcconn_accept(flib_ipcconn *ipc);
    91 
       
    92 /**
       
    93  * Get a record of the connection. This should be called after
       
    94  * the connection is closed and all messages have been received.
       
    95  *
       
    96  * If demo recording was not enabled, or if the recording failed for some reason,
       
    97  * the buffer will be empty.
       
    98  *
       
    99  * If save=true is passed, the result will be a savegame, otherwise it will be a
       
   100  * demo.
       
   101  *
       
   102  * The buffer is only valid until flib_ipcconn_getsave is called again or the ipcconn
       
   103  * is destroyed.
       
   104  */
       
   105 flib_constbuffer flib_ipcconn_getrecord(flib_ipcconn ipc, bool save);
       
   106 
    89 
   107 #endif /* IPCCONN_H_ */
    90 #endif /* IPCCONN_H_ */
   108 
    91