fixed client segfault on ssqc
This commit is contained in:
parent
f71d3954f0
commit
7ffb9b1d9c
@ -22,6 +22,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
//MACROS
|
//MACROS
|
||||||
#define WHY2_CHAT_CODE_ACCEPT_MESSAGES "code_000"
|
#define WHY2_CHAT_CODE_ACCEPT_MESSAGES "code_000"
|
||||||
#define WHY2_CHAT_CODE_PICK_USERNAME "code_001"
|
#define WHY2_CHAT_CODE_PICK_USERNAME "code_001"
|
||||||
|
#define WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION "code_002"
|
||||||
|
|
||||||
|
//SHORTCUTS CAUSE I'M LAZY BITCH
|
||||||
|
#define WHY2_CHAT_CODE_SSQC WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION
|
||||||
|
|
||||||
#define WHY2_CONFIG_DIR "/home/{USER}/.config"
|
#define WHY2_CONFIG_DIR "/home/{USER}/.config"
|
||||||
#define WHY2_CHAT_CONFIG_DIR WHY2_CONFIG_DIR "/WHY2"
|
#define WHY2_CHAT_CONFIG_DIR WHY2_CONFIG_DIR "/WHY2"
|
||||||
|
@ -19,11 +19,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
#ifndef WHY2_CHAT_MISC_H
|
#ifndef WHY2_CHAT_MISC_H
|
||||||
#define WHY2_CHAT_MISC_H
|
#define WHY2_CHAT_MISC_H
|
||||||
|
|
||||||
|
#include <why2/flags.h> //TODO: fuck this
|
||||||
|
|
||||||
void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect
|
void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect
|
||||||
char *why2_read_socket(int socket); //read lol
|
char *why2_read_socket(int socket); //read lol
|
||||||
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
|
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
|
||||||
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
|
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
|
||||||
void why2_clean_threads(void); //CLOSE EVERY RUNNING THREAD
|
void why2_clean_threads(void); //CLOSE EVERY RUNNING THREAD
|
||||||
void *why2_listen_server(void *socket); //LISTEN FOR OTHER's USERS MESSAGES
|
void *why2_listen_server(void *socket); //LISTEN FOR OTHER's USERS MESSAGES
|
||||||
|
void *why2_getline_thread(WHY2_UNUSED void* arg); //START getline IN SEPARATE THREAD
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -37,8 +37,11 @@ int main(void)
|
|||||||
|
|
||||||
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
|
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
void *return_line = NULL;
|
||||||
size_t line_length = 0;
|
size_t line_length = 0;
|
||||||
pthread_t thread_buffer;
|
pthread_t thread_buffer;
|
||||||
|
pthread_t thread_getline;
|
||||||
|
why2_bool ssqc = 0;
|
||||||
|
|
||||||
//DEFINE SERVER ADDRESS
|
//DEFINE SERVER ADDRESS
|
||||||
struct sockaddr_in server_addr;
|
struct sockaddr_in server_addr;
|
||||||
@ -58,6 +61,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
printf("\n\n\n");
|
printf("\n\n\n");
|
||||||
|
|
||||||
|
free(line); //PREVENT FROM MEMORY LEAK
|
||||||
|
|
||||||
int connectStatus = connect(listen_socket, (WHY2_SA *) &server_addr, sizeof(server_addr)); //CONNECT
|
int connectStatus = connect(listen_socket, (WHY2_SA *) &server_addr, sizeof(server_addr)); //CONNECT
|
||||||
|
|
||||||
if (connectStatus < 0) why2_die("Connecting failed.");
|
if (connectStatus < 0) why2_die("Connecting failed.");
|
||||||
@ -66,7 +71,16 @@ int main(void)
|
|||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
if (getline(&line, &line_length, stdin) == -1) why2_die("Reading input failed.");
|
pthread_create(&thread_getline, NULL, why2_getline_thread, NULL);
|
||||||
|
pthread_join(thread_getline, &return_line);
|
||||||
|
|
||||||
|
if (return_line == WHY2_INVALID_POINTER) //SERVER QUIT COMMUNICATION
|
||||||
|
{
|
||||||
|
ssqc = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
line = (char*) return_line;
|
||||||
|
|
||||||
printf(WHY2_CLEAR_AND_GO_UP);
|
printf(WHY2_CLEAR_AND_GO_UP);
|
||||||
|
|
||||||
@ -77,9 +91,16 @@ int main(void)
|
|||||||
printf("Exiting...\n");
|
printf("Exiting...\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(return_line);
|
||||||
|
}
|
||||||
|
|
||||||
|
//DEALLOCATION
|
||||||
|
if (!ssqc)
|
||||||
|
{
|
||||||
|
pthread_cancel(thread_buffer);
|
||||||
|
free(return_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_cancel(thread_buffer);
|
|
||||||
free(line);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -36,6 +36,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
#include <why2/memory.h>
|
#include <why2/memory.h>
|
||||||
#include <why2/misc.h>
|
#include <why2/misc.h>
|
||||||
|
|
||||||
|
pthread_t getline_thread;
|
||||||
|
|
||||||
//LINKED LIST STUFF
|
//LINKED LIST STUFF
|
||||||
typedef struct _connection_node
|
typedef struct _connection_node
|
||||||
{
|
{
|
||||||
@ -397,6 +399,8 @@ void *why2_communicate_thread(void *arg)
|
|||||||
raw_ptr = NULL;
|
raw_ptr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
why2_send_socket(WHY2_CHAT_CODE_SSQC, WHY2_CHAT_SERVER_USERNAME, connection);
|
||||||
|
|
||||||
printf("User disconnected.\t%d\n", connection);
|
printf("User disconnected.\t%d\n", connection);
|
||||||
|
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
@ -404,7 +408,7 @@ void *why2_communicate_thread(void *arg)
|
|||||||
close(connection);
|
close(connection);
|
||||||
why2_list_remove(&connection_list, find_connection(connection));
|
why2_list_remove(&connection_list, find_connection(connection));
|
||||||
|
|
||||||
return NULL; //TODO: Fix client segfault on timeout
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *why2_read_socket(int socket)
|
char *why2_read_socket(int socket)
|
||||||
@ -475,6 +479,7 @@ void why2_clean_threads(void)
|
|||||||
void *why2_listen_server(void *socket)
|
void *why2_listen_server(void *socket)
|
||||||
{
|
{
|
||||||
char *read = NULL;
|
char *read = NULL;
|
||||||
|
why2_bool exiting = 0;
|
||||||
|
|
||||||
printf(">>> ");
|
printf(">>> ");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
@ -485,7 +490,14 @@ void *why2_listen_server(void *socket)
|
|||||||
|
|
||||||
if (strncmp(read, WHY2_CHAT_SERVER_USERNAME ": code", 12) == 0) //CODE WAS SENT
|
if (strncmp(read, WHY2_CHAT_SERVER_USERNAME ": code", 12) == 0) //CODE WAS SENT
|
||||||
{
|
{
|
||||||
if (strcmp(read + 8, WHY2_CHAT_CODE_PICK_USERNAME) == 0) //PICK USERNAME
|
if (strcmp(read + 8, WHY2_CHAT_CODE_SSQC) == 0)
|
||||||
|
{
|
||||||
|
printf("%s\nServer closed the connection.\n", WHY2_CLEAR_AND_GO_UP);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
pthread_cancel(getline_thread); //CANCEL CLIENT getline
|
||||||
|
exiting = 1; //EXIT THIS THREAD
|
||||||
|
} else if (strcmp(read + 8, WHY2_CHAT_CODE_PICK_USERNAME) == 0) //PICK USERNAME
|
||||||
{
|
{
|
||||||
//TODO: Check username
|
//TODO: Check username
|
||||||
}
|
}
|
||||||
@ -497,7 +509,19 @@ void *why2_listen_server(void *socket)
|
|||||||
}
|
}
|
||||||
|
|
||||||
why2_deallocate(read);
|
why2_deallocate(read);
|
||||||
|
if (exiting) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *why2_getline_thread(WHY2_UNUSED void* arg)
|
||||||
|
{
|
||||||
|
getline_thread = pthread_self();
|
||||||
|
|
||||||
|
char *line = NULL;
|
||||||
|
size_t line_length = 0;
|
||||||
|
if (getline(&line, &line_length, stdin) == -1) why2_die("Reading input failed.");
|
||||||
|
|
||||||
|
return line;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user