Claude Chappe' Curse - A C Game
Logo Institut d'Informatique Claude Chappe Logo Université de Le Mans Logo Raeptor Production
 
Loading...
Searching...
No Matches
network_utils.h File Reference

Network utility functions and structures for socket communication. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MSG_TERMINATOR   '|'
 

Functions

int initiate_socket ()
 Initiates a socket.
 
void close_socket (int sock)
 Closes a socket.
 
struct socket_request_listener create_socket_request_listener (int sock)
 Creates a socket request listener.
 
int socket_request_listen (struct socket_request_listener *listener, int server_sock, int timeout)
 Listens for socket requests.
 
int socket_request_receive (struct socket_request_listener *listener, int client_sock, char *buffer, int size, int timeout, int flags)
 Receives a socket request.
 
struct peercreate_peer (int socket)
 Creates a peer structure.
 
int receive_message (void *peer, char **buffer, int size, int timeout, int flags)
 Receives a message from a peer.
 
int read_messages (int bytes_received, char *msg_buffer, void *p)
 Reads messages from a buffer.
 
int send_message (int socket, const char *message, int flags)
 Sends a message through a socket.
 
void remove_message_handler_by_index (void *p, int index)
 Removes a message handler by its index.
 
void remove_message_handling (void *p, char *msg)
 Removes a message handler by its message.
 
void add_message_handler (void *peer, char *msg, int(*callback)(int, char *, void *, void **), int lifespan, int argc, void **data)
 Adds a message handler.
 

Detailed Description

Network utility functions and structures for socket communication.

Macro Definition Documentation

◆ MSG_TERMINATOR

#define MSG_TERMINATOR   '|'

Function Documentation

◆ initiate_socket()

int initiate_socket ( )

Initiates a socket.

Returns
int The file descriptor of the created socket, or -1 on error.

◆ close_socket()

void close_socket ( int  sock)

Closes a socket.

Parameters
sockThe file descriptor of the socket to close.

◆ create_socket_request_listener()

struct socket_request_listener create_socket_request_listener ( int  sock)

Creates a socket request listener.

Parameters
sockThe file descriptor of the socket to listen on.
Returns
struct socket_request_listener The created socket request listener.

◆ socket_request_listen()

int socket_request_listen ( struct socket_request_listener listener,
int  server_sock,
int  timeout 
)

Listens for socket requests.

Parameters
listenerPointer to the socket request listener.
server_sockThe file descriptor of the server socket.
timeoutThe timeout value for listening, in milliseconds.
Returns
int 0 on success, or -1 on error.

◆ socket_request_receive()

int socket_request_receive ( struct socket_request_listener listener,
int  client_sock,
char *  buffer,
int  size,
int  timeout,
int  flags 
)

Receives a socket request.

Parameters
listenerPointer to the socket request listener.
client_sockThe file descriptor of the client socket.
bufferPointer to the buffer to store the received data.
sizeThe size of the buffer.
timeoutThe timeout value for receiving, in milliseconds.
flagsFlags for receiving data.
Returns
int The number of bytes received, or -1 on error.

◆ create_peer()

struct peer * create_peer ( int  socket)

Creates a peer structure.

Parameters
socketThe file descriptor of the socket associated with the peer.
Returns
struct peer* Pointer to the created peer structure.

◆ receive_message()

int receive_message ( void *  peer,
char **  buffer,
int  size,
int  timeout,
int  flags 
)

Receives a message from a peer.

Parameters
peerPointer to the peer structure.
bufferPointer to the buffer to store the received message.
sizeThe size of the buffer.
timeoutThe timeout value for receiving, in milliseconds.
flagsFlags for receiving data.
Returns
int The number of bytes received, or -1 on error.

◆ read_messages()

int read_messages ( int  bytes_received,
char *  msg_buffer,
void *  p 
)

Reads messages from a buffer.

Parameters
bytes_receivedThe number of bytes received.
msg_bufferPointer to the buffer containing the messages.
pPointer to additional data.
Returns
int 0 on success, or -1 on error.

◆ send_message()

int send_message ( int  socket,
const char *  message,
int  flags 
)

Sends a message through a socket.

Parameters
socketThe file descriptor of the socket.
messagePointer to the message to send.
flagsFlags for sending data.
Returns
int The number of bytes sent, or -1 on error.

◆ remove_message_handler_by_index()

void remove_message_handler_by_index ( void *  p,
int  index 
)

Removes a message handler by its index.

Parameters
pPointer to the data structure containing the message handlers.
indexThe index of the message handler to remove.

◆ remove_message_handling()

void remove_message_handling ( void *  p,
char *  msg 
)

Removes a message handler by its message.

Parameters
pPointer to the data structure containing the message handlers.
msgPointer to the message associated with the handler to remove.

◆ add_message_handler()

void add_message_handler ( void *  peer,
char *  msg,
int(*)(int, char *, void *, void **)  callback,
int  lifespan,
int  argc,
void **  data 
)

Adds a message handler.

Parameters
peerPointer to the peer structure.
msgPointer to the message associated with the handler.
callbackPointer to the callback function to handle the message.
lifespanThe lifespan of the message handler.
argcThe number of arguments for the callback function.
dataPointer to the array of arguments for the callback function.