Network utility functions and structures for socket communication.
More...
Go to the source code of this file.
|
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 peer * | create_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.
|
|
Network utility functions and structures for socket communication.
◆ MSG_TERMINATOR
#define MSG_TERMINATOR '|' |
◆ 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
-
sock | The file descriptor of the socket to close. |
◆ create_socket_request_listener()
Creates a socket request listener.
- Parameters
-
sock | The file descriptor of the socket to listen on. |
- Returns
- struct socket_request_listener The created socket request listener.
◆ socket_request_listen()
Listens for socket requests.
- Parameters
-
listener | Pointer to the socket request listener. |
server_sock | The file descriptor of the server socket. |
timeout | The 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
-
listener | Pointer to the socket request listener. |
client_sock | The file descriptor of the client socket. |
buffer | Pointer to the buffer to store the received data. |
size | The size of the buffer. |
timeout | The timeout value for receiving, in milliseconds. |
flags | Flags 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
-
socket | The 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
-
peer | Pointer to the peer structure. |
buffer | Pointer to the buffer to store the received message. |
size | The size of the buffer. |
timeout | The timeout value for receiving, in milliseconds. |
flags | Flags 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_received | The number of bytes received. |
msg_buffer | Pointer to the buffer containing the messages. |
p | Pointer 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
-
socket | The file descriptor of the socket. |
message | Pointer to the message to send. |
flags | Flags 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
-
p | Pointer to the data structure containing the message handlers. |
index | The 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
-
p | Pointer to the data structure containing the message handlers. |
msg | Pointer 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
-
peer | Pointer to the peer structure. |
msg | Pointer to the message associated with the handler. |
callback | Pointer to the callback function to handle the message. |
lifespan | The lifespan of the message handler. |
argc | The number of arguments for the callback function. |
data | Pointer to the array of arguments for the callback function. |