Atrinik Client 2.5
Functions | Variables
client/socket.c File Reference
#include <global.h>

Go to the source code of this file.

Functions

static command_buffercommand_buffer_new (size_t len, uint8 *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 command_buffercommand_buffer_dequeue (command_buffer **queue_start, command_buffer **queue_end)
int send_command_binary (uint8 cmd, uint8 *body, unsigned int len)
int send_socklist (SockList msg)
command_bufferget_next_input_command ()
static int reader_thread_loop (void *dummy)
static int writer_thread_loop (void *dummy)
void socket_thread_start ()
void socket_thread_stop ()
int handle_socket_shutdown ()
int socket_get_error ()
int socket_close (struct ClientSocket *csock)
int socket_initialize ()
void socket_deinitialize ()
static int socket_create (int *fd, char *host, int port)
int socket_open (struct ClientSocket *csock, char *host, int port)

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

Socket related code.

Definition in file socket.c.


Function Documentation

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 105 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 85 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 78 of file socket.c.

static command_buffer* command_buffer_new ( size_t  len,
uint8 data 
) [static]

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 59 of file socket.c.

command_buffer* get_next_input_command ( )

Get a command from the queue.

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

Definition at line 193 of file socket.c.

int handle_socket_shutdown ( )

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 446 of file socket.c.

int send_command_binary ( uint8  cmd,
uint8 body,
unsigned int  len 
)

Add a binary command to the output buffer. If body is NULL, a single-byte command is created from cmd. Otherwise body should include the length and cmd header.

Definition at line 130 of file socket.c.

int send_socklist ( SockList  msg)

Move a command buffer to the out buffer so it can be written to the socket.

Definition at line 166 of file socket.c.

int socket_close ( struct ClientSocket csock)

Close a socket.

Parameters:
csockSocket to close.

Definition at line 486 of file socket.c.

static int socket_create ( int *  fd,
char *  host,
int  port 
) [static]

Create a new socket.

Parameters:
[out]fdFile descriptor we'll update.
hostHost to connect to.
portPort to use.
Returns:
1 on success, 0 on failure.

Definition at line 580 of file socket.c.

void socket_deinitialize ( )

Deinitialize the socket.

Definition at line 562 of file socket.c.

int socket_get_error ( )

Get error number.

Returns:
The error number.

Definition at line 474 of file socket.c.

int socket_initialize ( )

Initialize the socket.

Returns:
1 on success, 0 on failure.

Definition at line 527 of file socket.c.

int socket_open ( struct ClientSocket csock,
char *  host,
int  port 
)

Open a new socket.

Parameters:
csockSocket to open.
hostHost to connect to.
portPort to connect to.
Returns:
1 on success, 0 on failure.

Definition at line 792 of file socket.c.

void socket_thread_start ( )

Initialize and start up the worker threads.

Definition at line 395 of file socket.c.

void socket_thread_stop ( )

Wait for the socket threads to finish. Closes the socket first, if it hasn't already been done.

Definition at line 428 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 330 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 46 of file socket.c.

SDL_mutex* socket_mutex [static]

Mutex to protect socket deinitialization.

Definition at line 42 of file socket.c.