HY335a-Project
A_multithread_chat_Server_and_chat_client
|
The header file of the chatserver. More...
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | connection |
struct | client_thread_params |
struct | broadcast_invitation_tread_params |
Macros | |
#define | TRUE 1 |
#define | FALSE 0 |
#define | BACKLOG_SIZE 1000 |
Typedefs | |
typedef char | boolean |
typedef struct connection | connection_t |
typedef struct client_thread_params | client_thread_params_t |
typedef struct broadcast_invitation_tread_params | broadcast_invitation_tread_params_t |
Enumerations | |
enum | msg_type_t { LOGIN_MSG = 0x1, LOGOUT_MSG = 0x2, TEXT_MSG = 0x3, USERNAME_NOT_EXIST = 0x4, USER_ALREADY_LOGGED = 0x5, USERS_LOGGED_REQUEST = 0x6, USERS_LOGGED_RESPONSE = 0x7, BROADCAST_MSG = 0x8, UKNOWN_MSG_TYPE = 0xFE, GENERAL_ERROR = 0xFF, LOGIN_MSG = 0x1, LOGOUT_MSG = 0x2, TEXT_MSG = 0x3, USERNAME_NOT_EXIST = 0x4, USER_ALREADY_LOGGED = 0x5, USERS_LOGGED_REQUEST = 0x6, USERS_LOGGED_RESPONSE = 0x7, BROADCAST_MSG = 0x8, UKNOWN_MSG_TYPE = 0xFE, GENERAL_ERROR = 0xFF } |
Functions | |
int | create_tcp_server_socket (int port) |
void * | print_stats_counters_thread (void *refresh_rate) |
void | handle_incoming_connections (int server_socket, connection_t *connections_list) |
void * | handle_client_connection_thread (void *thread_parameter) |
msg_type_t | get_message_type (char *buffer, size_t buf_len) |
int | send_to_client_online_users (connection_t *connections_list, connection_t *client_connection) |
boolean | handle_login_message (connection_t *connections_list, connection_t *client_connection, char *buffer, size_t buf_len) |
boolean | handle_logout_message (connection_t *connections_list, connection_t *client_connection) |
int | handle_text_message (connection_t *connections_list, connection_t *client_connection, char *buffer, size_t buf_len) |
int | send_back_error_message (connection_t *client_connection, msg_type_t error_type, char *msg_content, size_t msg_content_len) |
void * | broadcast_invitation_tread (void *broadcast_params) |
The header file of the chatserver.
This header file contains several structures that are vital, in order to implement your server.
Also contains the declarations of several functions. All the functions below MUST be implemented, WITHOUT changing its parameters or their return types.
Although you are welcome to add your own.
Note that in your program you should use, all of them, having in mind that some of them are called inside some other. Eg handle_client_connection_thread() will definitly called from the handle_incoming_connections() function.
For questions, problems, or bugs of this header file just email me.
May the code be with you... :)
#define BACKLOG_SIZE 1000 |
This is your default backlog size. Use it at the right function.
typedef char boolean |
An easy way to support boolean type in C
Pass this struct as parameter to the thread that broadcasts the invitation messages for the automatic server discovery.
typedef struct client_thread_params client_thread_params_t |
At every client thread we pass this struct as parameter that contains all the info that you need.
typedef struct connection connection_t |
In this struct we keep information for every active connection.
The server keeps a list with all the connections. If a new client connects the server adds a node to the list. Until the client performs a loggin, is_logged_in remains FALSE, and username is NULL.
NOTE: Fill free to add your own fields
enum msg_type_t |
The different types of messges. The message type is the first byte of every message from the client to the server and vice versa.
As the message type is only one byte we do not care about endianess problems.
NOTE 1: Each message number is in hexademical form. NOTE 2: Enum in C are at least two bytes, so make sure that you take the right byte when you send it. A simple cast to unsigned char would be enough. (Or not? :P ) NOTE 3: Fill free to add your own message types However, they must be unique, one byte long, and be carefull to be the same at the server and at the client.
void* broadcast_invitation_tread | ( | void * | broadcast_params | ) |
Thead that performs the broadcast for the automatic server discovery.
[in] | broadcast_params | all the necessary parameters for the automatic server discovery. |
int create_tcp_server_socket | ( | int | port | ) |
This function creates a server socket that listens at a specific port and is binded to all available network interfaces of the host.
[in] | port | The port number in which the server should listen. |
msg_type_t get_message_type | ( | char * | buffer, |
size_t | buf_len | ||
) |
Function that finds the type of the received message.
[in] | buffer | The buffer containing the data received from the client. |
[in] | buf_len | The length of the buffer. |
void* handle_client_connection_thread | ( | void * | thread_parameter | ) |
This function runs on a new thread that has been created by they handle_incoming_connections() function, and is responsible for sending and receiving message from the connected client.
If your are curious why the parameter is void *, go to the thread Lab and study it AGAIN carefully.
The thread terminates, when the server is killed by the user.
[in] | thread_parameter | A struct that contains all the appropriate info for the client thread. Use the client_thread_params_t struct that has been casted to void *, as we said at the LAB. |
void handle_incoming_connections | ( | int | server_socket, |
connection_t * | connections_list | ||
) |
This function runs until the user kills the server and waits for incoming connections. When accept() returns a new connection, adds it at the connections_list and pass the node (that contains all the necessary that you need) at a new thread that handles the client. The pthread_create() takes as arguement the function handle_client_thread(void *connection_info)
[in] | server_socket | a valid server socket discriptor |
[in,out] | connections_list | the list with the active connections |
boolean handle_login_message | ( | connection_t * | connections_list, |
connection_t * | client_connection, | ||
char * | buffer, | ||
size_t | buf_len | ||
) |
When a login message received, try to login the user. In any case of error this function sends back to the client the appropriate error message. Also if the user succesfully logged in set the username, at the appropriate field of the client_connection node.
[in] | connections_list | the list with all the active connections. |
[in] | client_connection | the node of the connections_list, that corresponds to the current client. |
[in] | buffer | the data as they received from the client |
[in] | buf_len | the length of the buffer |
boolean handle_logout_message | ( | connection_t * | connections_list, |
connection_t * | client_connection | ||
) |
When a logout message received, logout the client by deleting the node at the connections_list list and closing the established connection.
[in,out] | connections_list | the list with all the active connections. |
[in] | client_connection | the node of the connections_list, that corresponds to the current client. |
int handle_text_message | ( | connection_t * | connections_list, |
connection_t * | client_connection, | ||
char * | buffer, | ||
size_t | buf_len | ||
) |
Function responsible for propagating a message received from a client, to the right receiver that must be connected and logged in.
[in] | connections_list | the list with all the active connections. |
[in] | client_connection | the node of the connections_list, that corresponds to the current client. |
[in] | buffer | the data as they received from the client |
[in] | buf_len | the length of the buffer |
void* print_stats_counters_thread | ( | void * | refresh_rate | ) |
This function runs on a seperate thread and prints every refresh_rate seconds the values of the three counters of the server.
If your are curious why the parameter is void *, go to the thread Lab and study it AGAIN carefully.
[in] | refresh_rate | The interval in seconds between two prints of the counters |
int send_back_error_message | ( | connection_t * | client_connection, |
msg_type_t | error_type, | ||
char * | msg_content, | ||
size_t | msg_content_len | ||
) |
General function that sends back to the client, a specified error message.
[in] | error_type | the message type that describes the desired error. |
[in] | msg_content | the contents of the error message. This can be a discriptive human readable text. |
[in] | msg_content_len | the length in bytes, of the msg_content |
int send_to_client_online_users | ( | connection_t * | connections_list, |
connection_t * | client_connection | ||
) |
Function that sends back to the client a list with all the online users.
[in] | connections_list | the list with all the active connections. |
[in] | client_connection | the node of the connections_list, that corresponds to the current client. |