moved all messages to receivelist rather than just ones received
This commit is contained in:
@@ -32,25 +32,22 @@ pthread_cond_t sendcv, receivecv;
|
|||||||
char closed[] = "Connection closed.";
|
char closed[] = "Connection closed.";
|
||||||
|
|
||||||
/* networking stuff */
|
/* networking stuff */
|
||||||
int sockfd, confd;
|
int confd, sockfd;
|
||||||
char *remote_machine, *local_port, *remote_port;
|
|
||||||
char s[INET6_ADDRSTRLEN];
|
char s[INET6_ADDRSTRLEN];
|
||||||
|
|
||||||
/* notcurses stuff */
|
/* notcurses stuff */
|
||||||
struct notcurses *nc;
|
struct notcurses *nc;
|
||||||
struct ncplane *chatlog, *prompt, *stdn;
|
struct ncplane *chatlog, *prompt, *stdn;
|
||||||
unsigned int rows, cols;
|
unsigned int rows, cols;
|
||||||
int logsize;
|
|
||||||
|
|
||||||
/* 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 *arg) {
|
void *input() {
|
||||||
char *msg;
|
char *msg;
|
||||||
struct ncinput ni;
|
struct ncinput ni;
|
||||||
uint32_t character;
|
uint32_t character;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
logsize = 0;
|
|
||||||
/* 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);
|
||||||
@@ -61,11 +58,11 @@ void *input(void *arg) {
|
|||||||
notcurses_render(nc);
|
notcurses_render(nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&rendermut);
|
||||||
|
|
||||||
|
/* Loop to get input */
|
||||||
while (1) {
|
while (1) {
|
||||||
character = notcurses_get(nc, NULL, &ni);
|
character = notcurses_get(nc, NULL, &ni);
|
||||||
if (character == NCKEY_ENTER) {
|
if (character == NCKEY_ENTER) {
|
||||||
|
/* Add to sendlist when user presses enter */
|
||||||
/* Add to sendlist */
|
|
||||||
pthread_mutex_lock(&sendmut);
|
pthread_mutex_lock(&sendmut);
|
||||||
ListAppend(sendlist, msg);
|
ListAppend(sendlist, msg);
|
||||||
pthread_mutex_unlock(&sendmut);
|
pthread_mutex_unlock(&sendmut);
|
||||||
@@ -78,7 +75,7 @@ void *input(void *arg) {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* unsure why this doesn't create a scrolling effect */
|
/* Unsure why this doesn't create a scrolling effect */
|
||||||
else if (character == NCKEY_SCROLL_UP) {
|
else if (character == NCKEY_SCROLL_UP) {
|
||||||
ncplane_scrollup(chatlog, -1);
|
ncplane_scrollup(chatlog, -1);
|
||||||
}
|
}
|
||||||
@@ -94,6 +91,7 @@ void *input(void *arg) {
|
|||||||
msg[i] = '\0';
|
msg[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Print the message as the user types it */
|
||||||
ncplane_erase_region(prompt, 0, 0, 1, cols);
|
ncplane_erase_region(prompt, 0, 0, 1, cols);
|
||||||
ncplane_printf_yx(prompt, 0, 0, "Type a message: %s", msg);
|
ncplane_printf_yx(prompt, 0, 0, "Type a message: %s", msg);
|
||||||
|
|
||||||
@@ -102,7 +100,7 @@ void *input(void *arg) {
|
|||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&rendermut);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Wake up thread if it was waiting for a msg to send */
|
/* Wake up thread if it was waiting to have a msg to send */
|
||||||
if (ListCount(sendlist) == 1) {
|
if (ListCount(sendlist) == 1) {
|
||||||
pthread_cond_signal(&sendcv);
|
pthread_cond_signal(&sendcv);
|
||||||
}
|
}
|
||||||
@@ -111,8 +109,8 @@ void *input(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 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 *arg) {
|
void *sender() {
|
||||||
char *msg;
|
char *msg, *header;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -124,9 +122,12 @@ void *sender(void *arg) {
|
|||||||
pthread_mutex_unlock(&sendcvmut);
|
pthread_mutex_unlock(&sendcvmut);
|
||||||
}
|
}
|
||||||
msg = malloc(512);
|
msg = malloc(512);
|
||||||
|
header = malloc(519);
|
||||||
|
memset(msg, 0, 512);
|
||||||
|
memset(header, 0, 519);
|
||||||
/* Obtain mutex, get and then remove message
|
/* Obtain mutex, get and then remove message
|
||||||
* from the list, send it to other party,
|
* from the list, unlock mutex,
|
||||||
* release mutex */
|
* send message. */
|
||||||
pthread_mutex_unlock(&sendmut);
|
pthread_mutex_unlock(&sendmut);
|
||||||
ListFirst(sendlist);
|
ListFirst(sendlist);
|
||||||
strcpy(msg, (char*)ListCurr(sendlist));
|
strcpy(msg, (char*)ListCurr(sendlist));
|
||||||
@@ -135,11 +136,17 @@ void *sender(void *arg) {
|
|||||||
|
|
||||||
bytes = send(confd, msg, 512, 0);
|
bytes = send(confd, msg, 512, 0);
|
||||||
|
|
||||||
ncplane_printf(chatlog, "You: %s\n", msg);
|
/* Append message to header and add to print list */
|
||||||
|
strcpy(header, "You: ");
|
||||||
|
strncat(header, msg, strlen(msg));
|
||||||
|
pthread_mutex_lock(&receivemut);
|
||||||
|
ListAppend(receivelist, header);
|
||||||
|
pthread_mutex_unlock(&receivemut);
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
/* Wake thread waiting for message to print */
|
||||||
notcurses_render(nc);
|
if (ListCount(receivelist) == 1) {
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_cond_signal(&receivecv);
|
||||||
|
}
|
||||||
|
|
||||||
/* Stop execution if the user closes the connection */
|
/* Stop execution if the user closes the connection */
|
||||||
if ((strncmp(msg, "/c", 2)) == 0 || bytes == -1) {
|
if ((strncmp(msg, "/c", 2)) == 0 || bytes == -1) {
|
||||||
@@ -153,26 +160,37 @@ void *sender(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Receive messages, add to print queue */
|
/* Receive messages, add to print queue */
|
||||||
void *receiver(void *arg) {
|
void *receiver() {
|
||||||
char *buf;
|
char *buf, *header;
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
buf = malloc(512);
|
buf = malloc(512);
|
||||||
|
header = malloc(strlen(s) + 514);
|
||||||
memset(buf, 0, 512);
|
memset(buf, 0, 512);
|
||||||
|
memset(header, 0, strlen(s) + 514);
|
||||||
|
strcpy(header, s);
|
||||||
|
strncat(header, ": ", 3);
|
||||||
|
|
||||||
bytes = recv(confd, buf, 512, 0);
|
bytes = recv(confd, buf, 512, 0);
|
||||||
buf[bytes] = '\0';
|
strncat(header, buf, strlen(buf));
|
||||||
|
|
||||||
|
/* Stop execution if the user closes the connection */
|
||||||
if ((strncmp(buf, "/c", 2)) == 0) {
|
if ((strncmp(buf, "/c", 2)) == 0) {
|
||||||
pthread_cancel(send_thread);
|
pthread_cancel(send_thread);
|
||||||
pthread_cancel(input_thread);
|
pthread_cancel(input_thread);
|
||||||
pthread_cancel(output_thread);
|
pthread_cancel(output_thread);
|
||||||
pthread_exit(&closed);
|
pthread_exit(&closed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Add message to list for other thread to print */
|
||||||
if (bytes != -1) {
|
if (bytes != -1) {
|
||||||
pthread_mutex_lock(&receivemut);
|
pthread_mutex_lock(&receivemut);
|
||||||
ListAppend(receivelist, buf);
|
ListAppend(receivelist, header);
|
||||||
pthread_mutex_unlock(&receivemut);
|
pthread_mutex_unlock(&receivemut);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Signal output thread, if it was waiting for something to print */
|
||||||
if (ListCount(receivelist) == 1) {
|
if (ListCount(receivelist) == 1) {
|
||||||
pthread_cond_signal(&receivecv);
|
pthread_cond_signal(&receivecv);
|
||||||
}
|
}
|
||||||
@@ -181,23 +199,27 @@ void *receiver(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Remove a msg from the receivelist and display it */
|
/* Remove a msg from the receivelist and display it */
|
||||||
void *output(void *arg) {
|
void *output() {
|
||||||
char *msg;
|
char *msg;
|
||||||
while (1) {
|
while (1) {
|
||||||
msg = malloc(512);
|
msg = malloc(512 + strlen(s));
|
||||||
|
|
||||||
|
/* Block if there's nothing to output */
|
||||||
if (ListCount(receivelist) == 0) {
|
if (ListCount(receivelist) == 0) {
|
||||||
pthread_mutex_lock(&receivecvmut);
|
pthread_mutex_lock(&receivecvmut);
|
||||||
pthread_cond_wait(&receivecv, &receivecvmut);
|
pthread_cond_wait(&receivecv, &receivecvmut);
|
||||||
pthread_mutex_unlock(&receivecvmut);
|
pthread_mutex_unlock(&receivecvmut);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remove a message from the list and print it to chatlog */
|
||||||
pthread_mutex_lock(&receivemut);
|
pthread_mutex_lock(&receivemut);
|
||||||
ListFirst(receivelist);
|
ListFirst(receivelist);
|
||||||
strcpy(msg, (char*)ListCurr(receivelist));
|
strcpy(msg, (char*)ListCurr(receivelist));
|
||||||
ListRemove(receivelist);
|
ListRemove(receivelist);
|
||||||
pthread_mutex_unlock(&receivemut);
|
pthread_mutex_unlock(&receivemut);
|
||||||
|
|
||||||
ncplane_printf(chatlog, "%s: %s\n", s, msg);
|
ncplane_printf(chatlog, "%s\n", msg);
|
||||||
|
|
||||||
pthread_mutex_lock(&rendermut);
|
pthread_mutex_lock(&rendermut);
|
||||||
notcurses_render(nc);
|
notcurses_render(nc);
|
||||||
pthread_mutex_unlock(&rendermut);
|
pthread_mutex_unlock(&rendermut);
|
||||||
@@ -214,6 +236,7 @@ void *get_in_addr(struct sockaddr *sa) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
char *remote_machine, *local_port, *remote_port;
|
||||||
socklen_t sin_size;
|
socklen_t sin_size;
|
||||||
struct addrinfo *p, *q;
|
struct addrinfo *p, *q;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
@@ -248,8 +271,6 @@ int main(int argc, char* argv[]) {
|
|||||||
remote_machine = argv[2];
|
remote_machine = argv[2];
|
||||||
remote_port = argv[3];
|
remote_port = argv[3];
|
||||||
|
|
||||||
logsize = 0;
|
|
||||||
|
|
||||||
/* Decide who is "host" and who is "client" */
|
/* Decide who is "host" and who is "client" */
|
||||||
if (atoi(local_port) < atoi(remote_port)) {
|
if (atoi(local_port) < atoi(remote_port)) {
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
@@ -423,7 +444,9 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
close(confd);
|
close(confd);
|
||||||
printf("Connection closed.\n");
|
ListFree(receivelist);
|
||||||
|
ListFree(sendlist);
|
||||||
notcurses_stop(nc);
|
notcurses_stop(nc);
|
||||||
|
printf("Connection closed.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user