project_files/frontlib/ipcconn.h
changeset 7171 906e72caea7b
parent 7162 fe76d24a25d7
child 7173 7c2eb284f9f1
equal deleted inserted replaced
7169:b66eef8c8092 7171:906e72caea7b
    10 #include <stddef.h>
    10 #include <stddef.h>
    11 #include <stdbool.h>
    11 #include <stdbool.h>
    12 
    12 
    13 typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
    13 typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
    14 
    14 
    15 /**
    15 struct _flib_ipcconn;
    16  * Called by flib_init(). Initialize everything related to ipc.
    16 typedef struct _flib_ipcconn *flib_ipcconn;
    17  */
       
    18 void flib_ipcconn_init();
       
    19 
    17 
    20 /**
    18 /**
    21  * Called by flib_quit(). Free resources and shut down.
    19  * Start an engine connection by listening on a random port. The selected port can
       
    20  * be queried with flib_ipcconn_port and has to be passed to the engine.
       
    21  *
       
    22  * The parameter "recordDemo" can be used to control whether demo recording should
       
    23  * be enabled for this connection. The localPlayerName is needed for demo
       
    24  * recording purposes.
       
    25  *
       
    26  * Returns NULL on error. Destroy the created object with flib_ipcconn_destroy.
       
    27  *
       
    28  * We stop accepting new connections once a connection has been established, so you
       
    29  * need to create a new ipcconn in order to start a new connection.
    22  */
    30  */
    23 void flib_ipcconn_quit();
    31 flib_ipcconn flib_ipcconn_create(bool recordDemo, const char *localPlayerName);
       
    32 
       
    33 uint16_t flib_ipcconn_port(flib_ipcconn ipc);
    24 
    34 
    25 /**
    35 /**
    26  * Start listening for a connection from the engine. The system has to be in state
    36  * Free resources, close sockets, and set the pointer to NULL.
    27  * IPC_NOT_CONNECTED when calling this function, and will be in state IPC_LISTENING
       
    28  * if the function returns successfully.
       
    29  *
       
    30  * The parameter "recordDemo" can be used to control whether demo recording should
       
    31  * be enabled for this connection.
       
    32  *
       
    33  * Returns the port we started listening on, or a negative value if there is an error.
       
    34  *
       
    35  * We stop listening once a connection has been established, so if you want to start
       
    36  * the engine again and talk to it you need to call this function again after the old
       
    37  * connection is closed.
       
    38  */
    37  */
    39 int flib_ipcconn_start(bool recordDemo);
    38 void flib_ipcconn_destroy(flib_ipcconn *ipcptr);
    40 
       
    41 /**
       
    42  * Close the current IPC connection and/or stop listening for an incoming one.
       
    43  * This also discards all unread messages.
       
    44  */
       
    45 void flib_ipcconn_close();
       
    46 
    39 
    47 /**
    40 /**
    48  * Determine the current connection state
    41  * Determine the current connection state
    49  */
    42  */
    50 IpcConnState flib_ipcconn_state();
    43 IpcConnState flib_ipcconn_state(flib_ipcconn ipc);
    51 
    44 
    52 /**
    45 /**
    53  * Receive a single message (up to 255 bytes) and copy it into the data buffer.
    46  * Receive a single message (up to 255 bytes) and copy it into the data buffer.
    54  * Returns the length of the received message, a negative value if no message could
    47  * Returns the length of the received message, a negative value if no message could
    55  * be read.
    48  * be read.
    56  *
    49  *
    57  * Note: When a connection is closed, you probably want to call this function until
    50  * 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
    51  * no further message is returned, to ensure you see all messages that were sent
    59  * before the connection closed.
    52  * before the connection closed.
    60  */
    53  */
    61 int flib_ipcconn_recv_message(void *data);
    54 int flib_ipcconn_recv_message(flib_ipcconn ipc, void *data);
       
    55 
       
    56 int flib_ipcconn_send_raw(flib_ipcconn ipc, void *data, size_t len);
    62 
    57 
    63 /**
    58 /**
    64  * Write a single message (up to 255 bytes) to the engine. This call blocks until the
    59  * Write a single message (up to 255 bytes) to the engine. This call blocks until the
    65  * message is completely written or the connection is closed or an error occurs.
    60  * message is completely written or the connection is closed or an error occurs.
    66  *
    61  *
    67  * Calling this function in a state other than IPC_CONNECTED will fail immediately.
    62  * Calling this function in a state other than IPC_CONNECTED will fail immediately.
    68  * Returns a negative value on failure.
    63  * Returns a negative value on failure.
    69  */
    64  */
    70 int flib_ipcconn_send_message(void *data, size_t len);
    65 int flib_ipcconn_send_message(flib_ipcconn ipc, void *data, size_t len);
    71 
    66 
    72 /**
    67 /**
    73  * Convenience function for sending a 0-delimited string.
    68  * Convenience function for sending a 0-delimited string.
    74  */
    69  */
    75 int flib_ipcconn_send_messagestr(char *data);
    70 int flib_ipcconn_send_messagestr(flib_ipcconn ipc, char *data);
    76 
    71 
    77 /**
    72 /**
    78  * Call regularly to allow background work to proceed
    73  * Call regularly to allow background work to proceed
    79  */
    74  */
    80 void flib_ipcconn_tick();
    75 void flib_ipcconn_tick(flib_ipcconn ipc);
    81 
    76 
    82 /**
    77 /**
    83  * Get a demo record of the last connection. This should be called after
    78  * Get a demo record of the connection. This should be called after
    84  * the connection is closed and all messages have been received.
    79  * the connection is closed and all messages have been received.
    85  *
    80  *
    86  * If demo recording was not enabled in the last call to flib_ipcconn_start(),
    81  * If demo recording was not enabled, or if the recording failed for some reason,
    87  * or if the recording failed for some reason, the buffer will be empty.
    82  * the buffer will be empty.
    88  *
    83  *
    89  * The buffer is only valid until the next call to flib_ipcconn_start() or
    84  * The buffer is only valid until a call to flib_ipcconn_getsave(), since save
    90  * a call to flib_ipcconn_getsave() (save and demo records have some minor
    85  * and demo records have some minor differences, and those are performed directly
    91  * differences, and those are performed directly on the buffer before returning it).
    86  * on the buffer before returning it).
    92  */
    87  */
    93 flib_constbuffer flib_ipcconn_getdemo();
    88 flib_constbuffer flib_ipcconn_getdemo(flib_ipcconn ipc);
    94 
    89 
    95 /**
    90 /**
    96  * Get a savegame record of the last connection. This should be called after
    91  * Get a savegame record of the connection. This should be called after
    97  * the connection is closed and all messages have been received.
    92  * the connection is closed and all messages have been received.
    98  *
    93  *
    99  * If demo recording was not enabled in the last call to flib_ipcconn_start(),
    94  * If demo recording was not enabled, or if the recording failed for some reason,
   100  * or if the recording failed for some reason, the buffer will be empty.
    95  * the buffer will be empty.
   101  *
    96  *
   102  * The buffer is only valid until the next call to flib_ipcconn_start() or
    97  * The buffer is only valid until a call to flib_ipcconn_getdemo(), since save
   103  * a call to flib_ipcconn_getdemo() (save and demo records have some minor
    98  * and demo records have some minor differences, and those are performed directly
   104  * differences, and those are performed directly on the buffer before returning it).
    99  * on the buffer before returning it).
   105  */
   100  */
   106 flib_constbuffer flib_ipcconn_getsave();
   101 flib_constbuffer flib_ipcconn_getsave(flib_ipcconn ipc);
   107 
   102 
   108 #endif /* IPCCONN_H_ */
   103 #endif /* IPCCONN_H_ */
   109 
   104