project_files/frontlib/ipc/ipcbase.h
author sheepluva
Sun, 23 Nov 2014 03:38:15 +0100
changeset 10532 28651db3f90e
parent 10017 de822cd3df3a
permissions -rw-r--r--
fix view limits in stereo 3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    20
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    21
 * Low-level protocol support for the IPC connection to the engine.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    22
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    23
#ifndef IPCBASE_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    24
#define IPCBASE_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    26
#include <stddef.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    27
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    28
#include <stdint.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    29
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    30
#define IPCBASE_MAPMSG_BYTES 4097
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    31
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    32
typedef enum {IPC_NOT_CONNECTED, IPC_LISTENING, IPC_CONNECTED} IpcState;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    34
typedef struct _flib_ipcbase flib_ipcbase;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    35
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    36
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    37
 * Start an engine connection by listening on a random port. The selected port can
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    38
 * be queried with flib_ipcbase_port and has to be passed to the engine.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    39
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    40
 * Returns NULL on error. Destroy the created object with flib_ipcbase_destroy.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    41
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    42
 * We stop accepting new connections once a connection has been established, so you
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    43
 * need to create a new ipcbase in order to start a new connection.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    44
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    45
flib_ipcbase *flib_ipcbase_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    47
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    48
 * Return the listening port
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    49
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    50
uint16_t flib_ipcbase_port(flib_ipcbase *ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    52
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    53
 * Free resources and close sockets. NULL safe.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    54
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    55
void flib_ipcbase_destroy(flib_ipcbase *ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    57
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    58
 * Determine the current connection state
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    59
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    60
IpcState flib_ipcbase_state(flib_ipcbase *ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    61
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    62
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    63
 * Receive a single message (up to 256 bytes) and copy it into the data buffer.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    64
 * Returns the length of the received message, a negative value if no message could
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    65
 * be read.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    66
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    67
 * The first byte of a message is its content length, which is one less than the returned
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    68
 * value.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    69
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    70
 * Note: When a connection is closed, you probably want to call this function until
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    71
 * no further message is returned, to ensure you see all messages that were sent
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    72
 * before the connection closed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    73
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    74
int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    75
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    76
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    77
 * Try to receive 4097 bytes. This is the size of the reply the engine sends
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    78
 * when successfully queried for map data. The first 4096 bytes are a bit-packed
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    79
 * twocolor image of the map (256x128), the last byte is the number of hogs that
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    80
 * fit on the map.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    81
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    82
int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    84
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    85
 * Blocking send bytes over the socket. No message framing will be added.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    86
 * Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    87
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    88
int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    89
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    90
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    91
 * Write a single message (up to 255 bytes) to the engine. This call blocks until the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    92
 * message is completely written or the connection is closed or an error occurs.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    93
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    94
 * Calling this function in a state other than IPC_CONNECTED will fail immediately.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    95
 * Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    96
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    97
int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    98
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    99
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   100
 * Try to accept a connection. Only has an effect in state IPC_LISTENING.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   101
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   102
void flib_ipcbase_accept(flib_ipcbase *ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   103
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   104
#endif /* IPCBASE_H_ */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   105