project_files/frontlib/socket.h
author Medo <smaxein@googlemail.com>
Sun, 03 Jun 2012 01:24:18 +0200
changeset 7171 906e72caea7b
child 7175 038e3415100a
permissions -rw-r--r--
frontlib refactoring socket.h now completely wraps all the lowlevel neworking, so it would be easy to switch away from SDL_net if needed. Also reduced global state by making an IPC connection an object-like thing.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
/*
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
 * Sockets for TCP networking.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
 *
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
 * This layer offers some functionality over what SDL_net offers directly: listening
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
 * sockets (called acceptors here) can be bound to port 0, which will make them listen
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
 * on a random unused port, if one can be found. To support this feature, you can also
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
 * query the local port that an acceptor is listening on.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
 *
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
 * Further, we support nonblocking reads and writes here. The writes are buffered (TODO),
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 * so that all data will be accepted, and you can configure a maximum buffer size
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * where the connection will be terminated.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 *
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
 * In order to ensure buffered data is actually sent out, you should regularly call
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
 * the tick function on your sockets.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
#ifndef SOCKET_H_
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
#define SOCKET_H_
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
#include <stdbool.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
#include <stdint.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
struct _flib_tcpsocket;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
typedef struct _flib_tcpsocket *flib_tcpsocket;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
struct _flib_acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
typedef struct _flib_acceptor *flib_acceptor;
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
 * Create a new acceptor which will listen for incoming TCP connections
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
 * on the given port. If port is 0, this will listen on a random
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
 * unused port which can then be queried with flib_acceptor_listenport.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
 *
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
 * Can return NULL on error.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
flib_acceptor flib_acceptor_create(uint16_t port);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
 * Return the port on which the acceptor is listening.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
uint16_t flib_acceptor_listenport(flib_acceptor acceptor);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
 * Close the acceptor, free its memory and set it to NULL.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
 * If the acceptor is already NULL, nothing happens.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
void flib_acceptor_close(flib_acceptor *acceptorptr);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
 * Try to accept a connection from an acceptor (listening socket).
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
 * if localOnly is true, this will only accept connections which came from 127.0.0.1
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
 * Returns NULL if nothing can be accepted.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
flib_tcpsocket flib_socket_accept(flib_acceptor acceptor, bool localOnly);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
 * Close the socket, free its memory and set it to NULL.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
 * If the socket is already NULL, nothing happens.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
void flib_socket_close(flib_tcpsocket *socket);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
 * Attempt to receive up to maxlen bytes from the socket, but does not
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
 * block if nothing is available.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
 * Returns the ammount of data received, 0 if there was nothing to receive,
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
 * or a negative number if the connection was closed or an error occurred.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
int flib_socket_nbrecv(flib_tcpsocket sock, void *data, int maxlen);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
int flib_socket_send(flib_tcpsocket sock, void *data, int len);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
#endif /* SOCKET_H_ */