project_files/frontlib/socket.h
changeset 7271 5608ac657362
parent 7234 613998625a3c
child 7314 6171f0bad318
equal deleted inserted replaced
7269:5b0aeef8ba2a 7271:5608ac657362
    13 #define SOCKET_H_
    13 #define SOCKET_H_
    14 
    14 
    15 #include <stdbool.h>
    15 #include <stdbool.h>
    16 #include <stdint.h>
    16 #include <stdint.h>
    17 
    17 
    18 struct _flib_tcpsocket;
       
    19 typedef struct _flib_tcpsocket flib_tcpsocket;
    18 typedef struct _flib_tcpsocket flib_tcpsocket;
    20 
       
    21 struct _flib_acceptor;
       
    22 typedef struct _flib_acceptor flib_acceptor;
    19 typedef struct _flib_acceptor flib_acceptor;
    23 
    20 
    24 /**
    21 /**
    25  * Create a new acceptor which will listen for incoming TCP connections
    22  * Create a new acceptor which will listen for incoming TCP connections
    26  * on the given port. If port is 0, this will listen on a random
    23  * on the given port. If port is 0, this will listen on a random
    27  * unused port which can then be queried with flib_acceptor_listenport.
    24  * unused port which can then be queried with flib_acceptor_listenport.
    28  *
    25  *
    29  * Can return NULL on error.
    26  * Returns NULL on error.
    30  */
    27  */
    31 flib_acceptor *flib_acceptor_create(uint16_t port);
    28 flib_acceptor *flib_acceptor_create(uint16_t port);
    32 
    29 
    33 /**
    30 /**
    34  * Return the port on which the acceptor is listening.
    31  * Return the port on which the acceptor is listening.
    35  */
    32  */
    36 uint16_t flib_acceptor_listenport(flib_acceptor *acceptor);
    33 uint16_t flib_acceptor_listenport(flib_acceptor *acceptor);
    37 
    34 
    38 /**
    35 /**
    39  * Close the acceptor and free its memory.
    36  * Close the acceptor and free its memory. NULL-safe.
    40  * If the acceptor is already NULL, nothing happens.
       
    41  */
    37  */
    42 void flib_acceptor_close(flib_acceptor *acceptor);
    38 void flib_acceptor_close(flib_acceptor *acceptor);
    43 
    39 
    44 /**
    40 /**
    45  * Try to accept a connection from an acceptor (listening socket).
    41  * Try to accept a connection from an acceptor (listening socket).
    52  * Try to connect to the server at the given address.
    48  * Try to connect to the server at the given address.
    53  */
    49  */
    54 flib_tcpsocket *flib_socket_connect(const char *host, uint16_t port);
    50 flib_tcpsocket *flib_socket_connect(const char *host, uint16_t port);
    55 
    51 
    56 /**
    52 /**
    57  * Close the socket and free its memory.
    53  * Close the socket and free its memory. NULL-safe.
    58  * If the socket is already NULL, nothing happens.
       
    59  */
    54  */
    60 void flib_socket_close(flib_tcpsocket *socket);
    55 void flib_socket_close(flib_tcpsocket *socket);
    61 
    56 
    62 /**
    57 /**
    63  * Attempt to receive up to maxlen bytes from the socket, but does not
    58  * Attempt to receive up to maxlen bytes from the socket, but does not
    65  * Returns the ammount of data received, 0 if there was nothing to receive,
    60  * Returns the ammount of data received, 0 if there was nothing to receive,
    66  * or a negative number if the connection was closed or an error occurred.
    61  * or a negative number if the connection was closed or an error occurred.
    67  */
    62  */
    68 int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen);
    63 int flib_socket_nbrecv(flib_tcpsocket *sock, void *data, int maxlen);
    69 
    64 
       
    65 /**
       
    66  * Blocking send all the data in the data buffer. Returns the actual ammount
       
    67  * of data sent, or a negative value on error. If the value returned here
       
    68  * is less than len, either the connection closed or an error occurred.
       
    69  */
    70 int flib_socket_send(flib_tcpsocket *sock, const void *data, int len);
    70 int flib_socket_send(flib_tcpsocket *sock, const void *data, int len);
    71 
    71 
    72 #endif /* SOCKET_H_ */
    72 #endif /* SOCKET_H_ */