project_files/frontlib/ipc/ipcconn.h
author Medo <smaxein@googlemail.com>
Sat, 09 Jun 2012 03:28:38 +0200
changeset 7179 f84805e6df03
parent 7177 bf6cf4dd847a
child 7224 5143861c83bd
permissions -rw-r--r--
Implemented game launching API for the frontlib. It is still buggy though, and not all game settings can be conveniently created/modified yet.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
 * Low-level protocol support for the IPC connection to the engine.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 */
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#ifndef IPCCONN_H_
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#define IPCCONN_H_
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
     8
#include "../util/buffer.h"
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
     9
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
#include <stddef.h>
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    11
#include <stdbool.h>
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    13
#define IPCCONN_MAPMSG_BYTES 4097
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    14
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcConnState;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    17
struct _flib_ipcconn;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    18
typedef struct _flib_ipcconn *flib_ipcconn;
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
/**
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    21
 * TODO move demo recording up by one layer?
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    22
 *
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    23
 * Start an engine connection by listening on a random port. The selected port can
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    24
 * be queried with flib_ipcconn_port and has to be passed to the engine.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    25
 *
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    26
 * The parameter "recordDemo" can be used to control whether demo recording should
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    27
 * be enabled for this connection. The localPlayerName is needed for demo
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    28
 * recording purposes.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
 *
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    30
 * Returns NULL on error. Destroy the created object with flib_ipcconn_destroy.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
 *
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    32
 * We stop accepting new connections once a connection has been established, so you
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    33
 * need to create a new ipcconn in order to start a new connection.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    35
flib_ipcconn flib_ipcconn_create(bool recordDemo, const char *localPlayerName);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    36
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    37
uint16_t flib_ipcconn_port(flib_ipcconn ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
/**
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    40
 * Free resources, close sockets, and set the pointer to NULL.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    42
void flib_ipcconn_destroy(flib_ipcconn *ipcptr);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
/**
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
 * Determine the current connection state
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    47
IpcConnState flib_ipcconn_state(flib_ipcconn ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
/**
7173
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
    50
 * Receive a single message (up to 256 bytes) and copy it into the data buffer.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
 * Returns the length of the received message, a negative value if no message could
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
 * be read.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    53
 *
7173
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
    54
 * The first byte of a message is its content length, which is one less than the returned
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
    55
 * value.
7c2eb284f9f1 Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
    56
 *
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    57
 * Note: When a connection is closed, you probably want to call this function until
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    58
 * no further message is returned, to ensure you see all messages that were sent
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    59
 * before the connection closed.
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    61
int flib_ipcconn_recv_message(flib_ipcconn ipc, void *data);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    62
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    63
/**
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    64
 * Try to receive 4097 bytes. This is the size of the reply the engine sends
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    65
 * when successfully queried for map data. The first 4096 bytes are a bit-packed
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    66
 * twocolor image of the map (256x128), the last byte is the number of hogs that
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    67
 * fit on the map.
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    68
 */
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    69
int flib_ipcconn_recv_map(flib_ipcconn ipc, void *data);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    70
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
    71
int flib_ipcconn_send_raw(flib_ipcconn ipc, const void *data, size_t len);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
/**
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
 * Write a single message (up to 255 bytes) to the engine. This call blocks until the
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
 * message is completely written or the connection is closed or an error occurs.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
 *
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
 * Calling this function in a state other than IPC_CONNECTED will fail immediately.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
 * Returns a negative value on failure.
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    80
int flib_ipcconn_send_message(flib_ipcconn ipc, void *data, size_t len);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
/**
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    83
 * Convenience function for sending a 0-delimited string.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    84
 */
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    85
int flib_ipcconn_send_messagestr(flib_ipcconn ipc, char *data);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    86
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    87
/**
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
 * Call regularly to allow background work to proceed
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
 */
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    90
void flib_ipcconn_accept(flib_ipcconn ipc);
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    92
/**
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    93
 * Get a record of the connection. This should be called after
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    94
 * the connection is closed and all messages have been received.
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    95
 *
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    96
 * If demo recording was not enabled, or if the recording failed for some reason,
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents: 7162
diff changeset
    97
 * the buffer will be empty.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    98
 *
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
    99
 * If save=true is passed, the result will be a savegame, otherwise it will be a
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   100
 * demo.
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   101
 *
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   102
 * The buffer is only valid until flib_ipcconn_getsave is called again or the ipcconn
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   103
 * is destroyed.
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   104
 */
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7173
diff changeset
   105
flib_constbuffer flib_ipcconn_getrecord(flib_ipcconn ipc, bool save);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
   106
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
#endif /* IPCCONN_H_ */
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108