project_files/frontlib/socket.h
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 18:02:45 +0200
changeset 7275 15f722e0b96f
parent 7271 5608ac657362
child 7314 6171f0bad318
permissions -rw-r--r--
frontlib: Getting there :) Added commandline client for testing
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
 *
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents: 7171
diff changeset
     9
 * Further, we support nonblocking reads here.
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
#ifndef SOCKET_H_
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
#define SOCKET_H_
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
#include <stdbool.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
#include <stdint.h>
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    18
typedef struct _flib_tcpsocket flib_tcpsocket;
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    19
typedef struct _flib_acceptor flib_acceptor;
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
 * Create a new acceptor which will listen for incoming TCP connections
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
 * on the given port. If port is 0, this will listen on a random
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
 * unused port which can then be queried with flib_acceptor_listenport.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
 *
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    26
 * Returns NULL on error.
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    28
flib_acceptor *flib_acceptor_create(uint16_t port);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
 * Return the port on which the acceptor is listening.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    33
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
/**
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    36
 * Close the acceptor and free its memory. NULL-safe.
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    38
void flib_acceptor_close(flib_acceptor *acceptor);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
 * Try to accept a connection from an acceptor (listening socket).
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
 * 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
    43
 * Returns NULL if nothing can be accepted.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    45
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
/**
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    48
 * Try to connect to the server at the given address.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    49
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    50
flib_tcpsocket *flib_socket_connect(const char *host, uint16_t port);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    51
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents: 7224
diff changeset
    52
/**
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    53
 * Close the socket and free its memory. NULL-safe.
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
 */
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
void flib_socket_close(flib_tcpsocket *socket);
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
/**
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
 * Attempt to receive up to maxlen bytes from the socket, but does not
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
 * block if nothing is available.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
 * Returns the ammount of data received, 0 if there was nothing to receive,
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
 * or a negative number if the connection was closed or an error occurred.
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    63
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen);
7171
906e72caea7b frontlib refactoring
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
7271
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    65
/**
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    66
 * Blocking send all the data in the data buffer. Returns the actual ammount
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    67
 * of data sent, or a negative value on error. If the value returned here
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    68
 * is less than len, either the connection closed or an error occurred.
5608ac657362 frontlib: Intermittent commit. Things are still in flux but we're getting there :)
Medo <smaxein@googlemail.com>
parents: 7234
diff changeset
    69
 */
7224
5143861c83bd Cleanup, refactoring and generally more development in the frontlib
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
    70
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len);
7171
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_ */