removed global variables and passed them to threads as arguments instead
This commit is contained in:
@@ -21,67 +21,46 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <list.h>
|
#include <list.h>
|
||||||
#include <ncurses.h>
|
|
||||||
#include <notcurses/notcurses.h>
|
#include <notcurses/notcurses.h>
|
||||||
|
#include "context.h"
|
||||||
/* threading stuff */
|
|
||||||
LIST *receivelist, *sendlist;
|
|
||||||
pthread_t input_thread, send_thread, receive_thread, output_thread;
|
|
||||||
pthread_mutex_t sendmut, receivemut, sendcvmut, receivecvmut, rendermut;
|
|
||||||
pthread_cond_t sendcv, receivecv;
|
|
||||||
char closed[] = "Connection closed.";
|
|
||||||
|
|
||||||
/* networking stuff */
|
|
||||||
int confd, sockfd;
|
|
||||||
char s[INET6_ADDRSTRLEN];
|
|
||||||
|
|
||||||
/* notcurses stuff */
|
|
||||||
struct notcurses *nc;
|
|
||||||
struct ncplane *chatlog, *prompt, *stdn;
|
|
||||||
unsigned int rows, cols;
|
|
||||||
|
|
||||||
/* Accept input from the user and add it to the send list */
|
/* Accept input from the user and add it to the send list */
|
||||||
void *input() {
|
void *input(void *arg) {
|
||||||
char *msg;
|
char *msg;
|
||||||
struct ncinput ni;
|
struct ncinput ni;
|
||||||
uint32_t character;
|
uint32_t character;
|
||||||
int i;
|
int i;
|
||||||
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* Get a message from the user */
|
/* Get a message from the user */
|
||||||
msg = malloc(512);
|
msg = malloc(512);
|
||||||
memset(msg, 0, 512);
|
memset(msg, 0, 512);
|
||||||
i = 0;
|
i = 0;
|
||||||
ncplane_printf_yx(prompt, 0, 0, "Type a message: ");
|
ncplane_printf_yx(context->plane_prompt, 0, 0, "Type a message: ");
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
pthread_mutex_lock(&context->mut_render);
|
||||||
notcurses_render(nc);
|
notcurses_render(context->ctx_nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&context->mut_render);
|
||||||
|
|
||||||
/* Loop to get input */
|
/* Loop to get input */
|
||||||
while (1) {
|
while (1) {
|
||||||
character = notcurses_get(nc, NULL, &ni);
|
character = notcurses_get(context->ctx_nc, NULL, &ni);
|
||||||
if (character == NCKEY_ENTER) {
|
if (character == NCKEY_ENTER) {
|
||||||
/* Add to sendlist when user presses enter */
|
/* Add to sendlist when user presses enter */
|
||||||
pthread_mutex_lock(&sendmut);
|
pthread_mutex_lock(&context->mut_send);
|
||||||
ListAppend(sendlist, msg);
|
ListAppend(context->ctx_sendlist, msg);
|
||||||
pthread_mutex_unlock(&sendmut);
|
pthread_mutex_unlock(&context->mut_send);
|
||||||
|
|
||||||
ncplane_erase_region(prompt, 0, 16, 1, cols);
|
ncplane_erase_region(context->plane_prompt, 0, 16, 1,
|
||||||
|
context->ctx_cols);
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
pthread_mutex_lock(&context->mut_render);
|
||||||
notcurses_render(nc);
|
notcurses_render(context->ctx_nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&context->mut_render);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* Unsure why this doesn't create a scrolling effect */
|
|
||||||
else if (character == NCKEY_SCROLL_UP) {
|
|
||||||
ncplane_scrollup(chatlog, -1);
|
|
||||||
}
|
|
||||||
else if (character == NCKEY_SCROLL_DOWN) {
|
|
||||||
ncplane_scrollup(chatlog, 1);
|
|
||||||
}
|
|
||||||
else if (character == NCKEY_BACKSPACE && i > 0) {
|
else if (character == NCKEY_BACKSPACE && i > 0) {
|
||||||
i--;
|
i--;
|
||||||
msg[i] = '\0';
|
msg[i] = '\0';
|
||||||
@@ -92,137 +71,145 @@ void *input() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Print the message as the user types it */
|
/* Print the message as the user types it */
|
||||||
ncplane_erase_region(prompt, 0, 0, 1, cols);
|
ncplane_erase_region(context->plane_prompt, 0, 0, 1, context->ctx_cols);
|
||||||
ncplane_printf_yx(prompt, 0, 0, "Type a message: %s", msg);
|
ncplane_printf_yx(context->plane_prompt, 0, 0, "Type a message: %s", msg);
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
pthread_mutex_lock(&context->mut_render);
|
||||||
notcurses_render(nc);
|
notcurses_render(context->ctx_nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&context->mut_render);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Wake up thread if it was waiting to have a msg to send */
|
/* Wake up thread if it was waiting to have a msg to send */
|
||||||
if (ListCount(sendlist) == 1) {
|
if (ListCount(context->ctx_sendlist) == 1) {
|
||||||
pthread_cond_signal(&sendcv);
|
pthread_cond_signal(&context->cv_send);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take an item off the send list and send it to the other user */
|
/* Take an item off the send list and send it to the other user */
|
||||||
void *sender() {
|
void *sender(void *arg) {
|
||||||
char *msg, *header;
|
char *msg, *header;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* Check if the list has any content to send
|
/* Check if the list has any content to send
|
||||||
* If not, wait for some to arrive */
|
* If not, wait for some to arrive */
|
||||||
if (ListCount(sendlist) == 0) {
|
if (ListCount(context->ctx_sendlist) == 0) {
|
||||||
pthread_mutex_lock(&sendcvmut);
|
pthread_mutex_lock(&context->mut_sendcv);
|
||||||
pthread_cond_wait(&sendcv, &sendcvmut);
|
pthread_cond_wait(&context->cv_send, &context->mut_sendcv);
|
||||||
pthread_mutex_unlock(&sendcvmut);
|
pthread_mutex_unlock(&context->mut_sendcv);
|
||||||
}
|
}
|
||||||
|
|
||||||
msg = malloc(512);
|
msg = malloc(512);
|
||||||
header = malloc(519);
|
header = malloc(519);
|
||||||
memset(msg, 0, 512);
|
memset(msg, 0, 512);
|
||||||
memset(header, 0, 519);
|
memset(header, 0, 519);
|
||||||
|
|
||||||
/* Obtain mutex, get and then remove message
|
/* Obtain mutex, get and then remove message
|
||||||
* from the list, unlock mutex,
|
* from the list, unlock mutex,
|
||||||
* send message. */
|
* send message. */
|
||||||
pthread_mutex_unlock(&sendmut);
|
pthread_mutex_unlock(&context->mut_send);
|
||||||
ListFirst(sendlist);
|
ListFirst(context->ctx_sendlist);
|
||||||
strcpy(msg, (char*)ListCurr(sendlist));
|
strcpy(msg, (char*)ListCurr(context->ctx_sendlist));
|
||||||
ListRemove(sendlist);
|
ListRemove(context->ctx_sendlist);
|
||||||
pthread_mutex_unlock(&sendmut);
|
pthread_mutex_unlock(&context->mut_send);
|
||||||
|
|
||||||
bytes = send(confd, msg, 512, 0);
|
bytes = send(context->ctx_confd, msg, 512, 0);
|
||||||
|
|
||||||
|
/* Stop execution if the user closes the connection */
|
||||||
|
if ((strncmp(msg, "/c", 2)) == 0 || bytes == -1) {
|
||||||
|
pthread_cancel(*context->thread_in);
|
||||||
|
pthread_cancel(*context->thread_out);
|
||||||
|
pthread_cancel(*context->thread_recv);
|
||||||
|
pthread_exit(&context->ctx_closed);
|
||||||
|
}
|
||||||
|
|
||||||
/* Append message to header and add to print list */
|
/* Append message to header and add to print list */
|
||||||
strcpy(header, "You: ");
|
strcpy(header, "You: ");
|
||||||
strncat(header, msg, strlen(msg));
|
strncat(header, msg, strlen(msg));
|
||||||
pthread_mutex_lock(&receivemut);
|
|
||||||
ListAppend(receivelist, header);
|
pthread_mutex_lock(&context->mut_recv);
|
||||||
pthread_mutex_unlock(&receivemut);
|
ListAppend(context->ctx_recvlist, header);
|
||||||
|
pthread_mutex_unlock(&context->mut_recv);
|
||||||
|
|
||||||
/* Wake thread waiting for message to print */
|
/* Wake thread waiting for message to print */
|
||||||
if (ListCount(receivelist) == 1) {
|
if (ListCount(context->ctx_recvlist) == 1) {
|
||||||
pthread_cond_signal(&receivecv);
|
pthread_cond_signal(&context->cv_recv);
|
||||||
}
|
|
||||||
|
|
||||||
/* Stop execution if the user closes the connection */
|
|
||||||
if ((strncmp(msg, "/c", 2)) == 0 || bytes == -1) {
|
|
||||||
pthread_cancel(receive_thread);
|
|
||||||
pthread_cancel(input_thread);
|
|
||||||
pthread_cancel(output_thread);
|
|
||||||
pthread_exit(&closed);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Receive messages, add to print queue */
|
/* Receive messages, add to print queue */
|
||||||
void *receiver() {
|
void *receiver(void *arg) {
|
||||||
char *buf, *header;
|
char *msg, *header;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
buf = malloc(512);
|
msg = malloc(512);
|
||||||
header = malloc(strlen(s) + 514);
|
header = malloc(strlen(context->ip) + 514);
|
||||||
memset(buf, 0, 512);
|
memset(msg, 0, 512);
|
||||||
memset(header, 0, strlen(s) + 514);
|
memset(header, 0, strlen(context->ip) + 514);
|
||||||
strcpy(header, s);
|
|
||||||
strncat(header, ": ", 3);
|
|
||||||
|
|
||||||
bytes = recv(confd, buf, 512, 0);
|
bytes = recv(context->ctx_confd, msg, 512, 0);
|
||||||
strncat(header, buf, strlen(buf));
|
|
||||||
|
|
||||||
/* Stop execution if the user closes the connection */
|
/* Stop execution if the user closes the connection */
|
||||||
if ((strncmp(buf, "/c", 2)) == 0) {
|
if ((strncmp(msg, "/c", 2)) == 0) {
|
||||||
pthread_cancel(send_thread);
|
pthread_cancel(*context->thread_send);
|
||||||
pthread_cancel(input_thread);
|
pthread_cancel(*context->thread_in);
|
||||||
pthread_cancel(output_thread);
|
pthread_cancel(*context->thread_out);
|
||||||
pthread_exit(&closed);
|
pthread_exit(&context->ctx_closed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strncpy(header, context->ip, strlen(context->ip));
|
||||||
|
strcat(header, ": ");
|
||||||
|
strncat(header, msg, strlen(msg));
|
||||||
|
|
||||||
/* Add message to list for other thread to print */
|
/* Add message to list for other thread to print */
|
||||||
if (bytes != -1) {
|
if (bytes != -1) {
|
||||||
pthread_mutex_lock(&receivemut);
|
pthread_mutex_lock(&context->mut_recv);
|
||||||
ListAppend(receivelist, header);
|
ListAppend(context->ctx_recvlist, header);
|
||||||
pthread_mutex_unlock(&receivemut);
|
pthread_mutex_unlock(&context->mut_recv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Signal output thread, if it was waiting for something to print */
|
/* Signal output thread, if it was waiting for something to print */
|
||||||
if (ListCount(receivelist) == 1) {
|
if (ListCount(context->ctx_recvlist) == 1) {
|
||||||
pthread_cond_signal(&receivecv);
|
pthread_cond_signal(&context->cv_recv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove a msg from the receivelist and display it */
|
/* Remove a msg from the receivelist and display it */
|
||||||
void *output() {
|
void *output(void *arg) {
|
||||||
char *msg;
|
char *msg;
|
||||||
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
msg = malloc(512 + strlen(s));
|
msg = malloc(512 + strlen(context->ip));
|
||||||
|
|
||||||
/* Block if there's nothing to output */
|
/* Block if there's nothing to output */
|
||||||
if (ListCount(receivelist) == 0) {
|
if (ListCount(context->ctx_recvlist) == 0) {
|
||||||
pthread_mutex_lock(&receivecvmut);
|
pthread_mutex_lock(&context->mut_recvcv);
|
||||||
pthread_cond_wait(&receivecv, &receivecvmut);
|
pthread_cond_wait(&context->cv_recv, &context->mut_recvcv);
|
||||||
pthread_mutex_unlock(&receivecvmut);
|
pthread_mutex_unlock(&context->mut_recvcv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove a message from the list and print it to chatlog */
|
/* Remove a message from the list and print it to chatlog */
|
||||||
pthread_mutex_lock(&receivemut);
|
pthread_mutex_lock(&context->mut_recv);
|
||||||
ListFirst(receivelist);
|
ListFirst(context->ctx_recvlist);
|
||||||
strcpy(msg, (char*)ListCurr(receivelist));
|
strcpy(msg, (char*)ListCurr(context->ctx_recvlist));
|
||||||
ListRemove(receivelist);
|
ListRemove(context->ctx_recvlist);
|
||||||
pthread_mutex_unlock(&receivemut);
|
pthread_mutex_unlock(&context->mut_recv);
|
||||||
|
|
||||||
ncplane_printf(chatlog, "%s\n", msg);
|
ncplane_printf(context->plane_log, "%s\n", msg);
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
pthread_mutex_lock(&context->mut_render);
|
||||||
notcurses_render(nc);
|
notcurses_render(context->ctx_nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&context->mut_render);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -236,6 +223,9 @@ void *get_in_addr(struct sockaddr *sa) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
/* networking stuff */
|
||||||
|
int confd, sockfd;
|
||||||
|
char s[INET6_ADDRSTRLEN];
|
||||||
char *remote_machine, *local_port, *remote_port;
|
char *remote_machine, *local_port, *remote_port;
|
||||||
socklen_t sin_size;
|
socklen_t sin_size;
|
||||||
struct addrinfo *p, *q;
|
struct addrinfo *p, *q;
|
||||||
@@ -245,10 +235,23 @@ int main(int argc, char* argv[]) {
|
|||||||
int status;
|
int status;
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
|
|
||||||
|
/* threading stuff */
|
||||||
|
LIST *receivelist, *sendlist;
|
||||||
|
pthread_t input_thread, send_thread, receive_thread, output_thread;
|
||||||
|
pthread_mutex_t sendmut, receivemut, sendcvmut, receivecvmut, rendermut;
|
||||||
|
pthread_cond_t sendcv, receivecv;
|
||||||
|
char closed[] = "Connection closed.";
|
||||||
|
|
||||||
|
/* notcurses stuff */
|
||||||
|
struct notcurses *nc;
|
||||||
|
struct ncplane *chatlog, *prompt, *stdn;
|
||||||
|
unsigned int rows, cols;
|
||||||
|
|
||||||
|
|
||||||
struct notcurses_options opts = {
|
struct notcurses_options opts = {
|
||||||
.flags = NCOPTION_SUPPRESS_BANNERS
|
.flags = NCOPTION_SUPPRESS_BANNERS
|
||||||
};
|
};
|
||||||
struct ncplane_options nopts = {
|
struct ncplane_options clopts = {
|
||||||
.y = 0,
|
.y = 0,
|
||||||
.x = 0,
|
.x = 0,
|
||||||
.rows = 0,
|
.rows = 0,
|
||||||
@@ -352,7 +355,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
if (connect(confd, q->ai_addr, q->ai_addrlen) == -1) {
|
if (connect(confd, q->ai_addr, q->ai_addrlen) == -1) {
|
||||||
perror("Client: connect");
|
perror("Client: connect");
|
||||||
close(sockfd);
|
close(confd);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -411,28 +414,58 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
notcurses_stddim_yx(nc, &rows, &cols);
|
notcurses_stddim_yx(nc, &rows, &cols);
|
||||||
nopts.rows = rows;
|
clopts.rows = rows;
|
||||||
nopts.cols = cols;
|
clopts.cols = cols;
|
||||||
popts.cols = cols;
|
popts.cols = cols;
|
||||||
popts.y = rows - 1;
|
popts.y = rows - 1;
|
||||||
notcurses_cursor_disable(nc);
|
notcurses_cursor_disable(nc);
|
||||||
|
|
||||||
if ((chatlog = ncplane_create(stdn, &nopts)) == NULL) {
|
if ((chatlog = ncplane_create(stdn, &clopts)) == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((prompt = ncplane_create(stdn, &popts)) == NULL) {
|
if ((prompt = ncplane_create(stdn, &popts)) == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
ncplane_set_scrolling(chatlog, true);
|
ncplane_set_scrolling(chatlog, true);
|
||||||
pthread_create(&input_thread, NULL, input, NULL);
|
|
||||||
pthread_create(&send_thread, NULL, sender, NULL);
|
|
||||||
pthread_create(&output_thread, NULL, output, NULL);
|
|
||||||
pthread_create(&receive_thread, NULL, receiver, NULL);
|
|
||||||
|
|
||||||
pthread_join(send_thread, NULL);
|
CONTEXT context = {
|
||||||
pthread_join(receive_thread, NULL);
|
.ctx_recvlist = receivelist,
|
||||||
|
.ctx_sendlist = sendlist,
|
||||||
|
|
||||||
|
.thread_in = &input_thread,
|
||||||
|
.thread_out = &output_thread,
|
||||||
|
.thread_recv = &receive_thread,
|
||||||
|
.thread_send = &send_thread,
|
||||||
|
|
||||||
|
.mut_send = sendmut,
|
||||||
|
.mut_recv = receivemut,
|
||||||
|
.mut_sendcv = sendcvmut,
|
||||||
|
.mut_recvcv = receivecvmut,
|
||||||
|
.mut_render = rendermut,
|
||||||
|
|
||||||
|
.cv_send = sendcv,
|
||||||
|
.cv_recv = receivecv,
|
||||||
|
|
||||||
|
.ip = s,
|
||||||
|
.ctx_confd = confd,
|
||||||
|
|
||||||
|
.ctx_nc = nc,
|
||||||
|
.plane_log = chatlog,
|
||||||
|
.plane_prompt = prompt,
|
||||||
|
.ctx_rows = rows,
|
||||||
|
.ctx_cols = cols,
|
||||||
|
.ctx_closed = closed,
|
||||||
|
};
|
||||||
|
|
||||||
|
pthread_create(&input_thread, NULL, input, &context);
|
||||||
|
pthread_create(&send_thread, NULL, sender, &context);
|
||||||
|
pthread_create(&output_thread, NULL, output, &context);
|
||||||
|
pthread_create(&receive_thread, NULL, receiver, &context);
|
||||||
|
|
||||||
pthread_join(input_thread, NULL);
|
pthread_join(input_thread, NULL);
|
||||||
pthread_join(output_thread, NULL);
|
pthread_join(output_thread, NULL);
|
||||||
|
pthread_join(send_thread, NULL);
|
||||||
|
pthread_join(receive_thread, NULL);
|
||||||
|
|
||||||
pthread_cond_destroy(&sendcv);
|
pthread_cond_destroy(&sendcv);
|
||||||
pthread_cond_destroy(&receivecv);
|
pthread_cond_destroy(&receivecv);
|
||||||
@@ -447,6 +480,6 @@ int main(int argc, char* argv[]) {
|
|||||||
ListFree(receivelist);
|
ListFree(receivelist);
|
||||||
ListFree(sendlist);
|
ListFree(sendlist);
|
||||||
notcurses_stop(nc);
|
notcurses_stop(nc);
|
||||||
printf("Connection closed.\n");
|
printf("%s\n", closed);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user