Atrinik Client  4.0
Functions | Variables
socket.c File Reference
#include <global.h>
#include <toolkit/packet.h>
#include <network_graph.h>
#include <toolkit/socket_crypto.h>

Go to the source code of this file.

Functions

command_buffercommand_buffer_new (size_t len, uint8_t *data)
 
void command_buffer_free (command_buffer *buf)
 
static void command_buffer_enqueue (command_buffer *buf, command_buffer **queue_start, command_buffer **queue_end)
 
static void command_buffer_enqueue_first (command_buffer *buf, command_buffer **queue_start, command_buffer **queue_end)
 
static command_buffercommand_buffer_dequeue (command_buffer **queue_start, command_buffer **queue_end)
 
void socket_send_packet (struct packet_struct *packet)
 
command_bufferget_next_input_command (void)
 
void add_input_command (command_buffer *buf)
 
static int reader_thread_loop (void *dummy)
 
static int writer_thread_loop (void *dummy)
 
void socket_thread_start (void)
 
void socket_thread_stop (void)
 
int handle_socket_shutdown (void)
 
void client_socket_close (client_socket_t *csock)
 
void client_socket_deinitialize (void)
 
bool client_socket_open (client_socket_t *csock, const char *host, int port, bool secure)
 

Variables

static SDL_Thread * input_thread
 
static SDL_mutex * input_buffer_mutex
 
static SDL_cond * input_buffer_cond
 
static SDL_Thread * output_thread
 
static SDL_mutex * output_buffer_mutex
 
static SDL_cond * output_buffer_cond
 
static SDL_mutex * socket_mutex
 
static int abort_thread = 0
 
static command_bufferinput_queue_start = NULL
 
static command_bufferinput_queue_end = NULL
 
static command_bufferoutput_queue_start = NULL
 
static command_bufferoutput_queue_end = NULL
 

Detailed Description

Client sockets related code.

Definition in file socket.c.

Function Documentation

void client_socket_close ( client_socket_t csock)

Close a client socket.

Parameters
csockSocket to close.

Definition at line 429 of file socket.c.

void client_socket_deinitialize ( void  )

Deinitialize the client sockets.

Definition at line 452 of file socket.c.

bool client_socket_open ( client_socket_t csock,
const char *  host,
int  port,
bool  secure 
)

Open a new socket.

Parameters
csockSocket to open.
hostHost to connect to.
portPort to connect to.
secureWhether the port is the secure port.
Returns
True on success, false on failure.

Definition at line 477 of file socket.c.

static command_buffer* command_buffer_dequeue ( command_buffer **  queue_start,
command_buffer **  queue_end 
)
static

Remove the first command buffer from a queue.

Definition at line 134 of file socket.c.

static void command_buffer_enqueue ( command_buffer buf,
command_buffer **  queue_start,
command_buffer **  queue_end 
)
static

Enqueue a command buffer last in a queue.

Definition at line 96 of file socket.c.

static void command_buffer_enqueue_first ( command_buffer buf,
command_buffer **  queue_start,
command_buffer **  queue_end 
)
static

Enqueue a command buffer first in a queue.

Definition at line 115 of file socket.c.

void command_buffer_free ( command_buffer buf)

Free all memory related to a single command buffer.

Parameters
bufBuffer to free.

Definition at line 88 of file socket.c.

command_buffer* command_buffer_new ( size_t  len,
uint8_t *  data 
)

Create a new command buffer of the given size, copying the data buffer if not NULL. The buffer will always be null-terminated for safety (and one byte larger than requested).

Parameters
lenRequested buffer size in bytes.
dataBuffer data to copy (len bytes), or NULL.
Returns
A new command buffer or NULL in case of an error.

Definition at line 68 of file socket.c.

command_buffer* get_next_input_command ( void  )

Get a command from the queue.

Returns
The command (being removed from queue), NULL if there is no command.

Definition at line 197 of file socket.c.

int handle_socket_shutdown ( void  )

Detect and handle socket system shutdowns. Also reset the socket system for a restart.

The main thread should poll this function which detects connection shutdowns and removes the threads if it happens.

Definition at line 402 of file socket.c.

void socket_thread_start ( void  )

Initialize and start up the worker threads.

Definition at line 353 of file socket.c.

void socket_thread_stop ( void  )

Wait for the socket threads to finish.

Closes the socket first, if it hasn't already been done.

Definition at line 385 of file socket.c.

static int writer_thread_loop ( void *  dummy)
static

Worker for the writer thread. It waits for enqueued outgoing packets and sends them to the server as fast as it can.

If any error is detected, the socket is closed and the thread exits. It is up to them main thread to detect this and join() the worker threads.

Definition at line 312 of file socket.c.

Variable Documentation

int abort_thread = 0
static

All socket threads will exit if they see this flag set.

Definition at line 51 of file socket.c.

SDL_mutex* socket_mutex
static

Mutex to protect socket deinitialization.

Definition at line 46 of file socket.c.