project_files/frontlib/socket.h
author sheepluva
Fri, 04 Mar 2016 10:37:42 +0100
changeset 11590 8d1cfedfaf1f
parent 10017 de822cd3df3a
permissions -rw-r--r--
fix pas2c breakage caused by recent changes in uLandGraphics
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
 * Sockets for TCP networking.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    22
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    23
 * This layer offers some functionality over what SDL_net offers directly: listening
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    24
 * sockets (called acceptors here) can be bound to port 0, which will make them listen
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    25
 * on a random unused port, if one can be found. To support this feature, you can also
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    26
 * query the local port that an acceptor is listening on.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    27
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    28
 * Further, we support nonblocking reads here.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    29
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    31
#ifndef SOCKET_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    32
#define SOCKET_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    33
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    34
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    35
#include <stdint.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    37
typedef struct _flib_tcpsocket flib_tcpsocket;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    38
typedef struct _flib_acceptor flib_acceptor;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    39
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    40
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    41
 * Create a new acceptor which will listen for incoming TCP connections
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    42
 * on the given port. If port is 0, this will listen on a random
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    43
 * unused port which can then be queried with flib_acceptor_listenport.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    44
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    45
 * Returns NULL on error.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    46
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    47
flib_acceptor *flib_acceptor_create(uint16_t port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    49
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    50
 * Return the port on which the acceptor is listening.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    51
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    52
uint16_t flib_acceptor_listenport(flib_acceptor *acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    53
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    54
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    55
 * Close the acceptor and free its memory. NULL-safe.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    56
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    57
void flib_acceptor_close(flib_acceptor *acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    58
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    59
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    60
 * Try to accept a connection from an acceptor (listening socket).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    61
 * if localOnly is true, this will only accept connections which came from 127.0.0.1
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    62
 * Returns NULL if nothing can be accepted.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    63
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    64
flib_tcpsocket *flib_socket_accept(flib_acceptor *acceptor, bool localOnly);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    65
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    66
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    67
 * Try to connect to the server at the given address.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    68
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    69
flib_tcpsocket *flib_socket_connect(const char *host, uint16_t port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    70
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    71
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    72
 * Close the socket and free its memory. NULL-safe.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    73
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    74
void flib_socket_close(flib_tcpsocket *socket);
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
 * Attempt to receive up to maxlen bytes from the socket, but does not
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    78
 * block if nothing is available.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    79
 * Returns the ammount of data received, 0 if there was nothing to receive,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    80
 * or a negative number if the connection was closed or an error occurred.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    81
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    82
int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen);
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 all the data in the data buffer. Returns the actual ammount
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    86
 * of data sent, or a negative value on error. If the value returned here
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    87
 * is less than len, either the connection closed or an error occurred.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    88
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    89
int flib_socket_send(flib_tcpsocket *sock, const void *data, int len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    91
#endif /* SOCKET_H_ */