forked from scott/threaded_network_chat
refactor pt 2
This commit is contained in:
72
chat.cc
72
chat.cc
@@ -5,9 +5,12 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -91,11 +94,11 @@ await_client(void *a)
|
|||||||
|
|
||||||
delete args;
|
delete args;
|
||||||
|
|
||||||
return client_poll();
|
return poll_client();
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
client_poll(void)
|
poll_client(void)
|
||||||
{
|
{
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -191,15 +194,15 @@ client_stop(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
server_poll(void *args)
|
poll_server(void *args)
|
||||||
{
|
{
|
||||||
wclient_args_t *a = static_cast<wclient_args_t*>(args);
|
wclient_args_t *a = static_cast<wclient_args_t *>(args);
|
||||||
int sock_desc = (int)a->aux;
|
int sock_desc = (int)a->aux;
|
||||||
char msg[1024];
|
char msg[1024];
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
memset(&msg, 0, sizeof(msg));
|
memset(&msg, 0, sizeof(msg));
|
||||||
bytes_read += recv(sock_desc, (char*)&msg, sizeof(msg), 0);
|
bytes_read += recv(sock_desc, (char *)&msg, sizeof(msg), 0);
|
||||||
|
|
||||||
log_append(log_path, msg);
|
log_append(log_path, msg);
|
||||||
|
|
||||||
@@ -209,3 +212,62 @@ server_poll(void *args)
|
|||||||
file_disp(log_path, line_position, LOG_LENGTH);
|
file_disp(log_path, line_position, LOG_LENGTH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
client_start(void)
|
||||||
|
{
|
||||||
|
char msg[1024];
|
||||||
|
int bread;
|
||||||
|
int bwrit;
|
||||||
|
int rc;
|
||||||
|
sockaddr_in send_addr;
|
||||||
|
|
||||||
|
struct timeval time_start;
|
||||||
|
struct timeval time_end;
|
||||||
|
struct hostent *host = gethostbyname(listen_ip_address.c_str());
|
||||||
|
|
||||||
|
auto *a = new wclient_args_t{};
|
||||||
|
|
||||||
|
bzero((char *)&send_addr, sizeof(send_addr));
|
||||||
|
send_addr.sin_family = AF_INET;
|
||||||
|
send_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *)*host->h_addr_list));
|
||||||
|
send_addr.sin_port = htons(listen_port);
|
||||||
|
socket_descriptor_client = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
attempt_client_connection:
|
||||||
|
int stat = connect(socket_descriptor_client, (sockaddr *)&send_addr, sizeof(send_addr));
|
||||||
|
if (stat < 0) {
|
||||||
|
log_append(log_path, "Error connecting to socket, retrying...");
|
||||||
|
goto attempt_client_connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_append(log_path, "Connected to the server!");
|
||||||
|
bread = bwrit = 0;
|
||||||
|
|
||||||
|
gettimeofday(&time_start, NULL);
|
||||||
|
|
||||||
|
a->aux = socket_descriptor_client;
|
||||||
|
rc = pthread_create(&client_wait_thread, nullptr, poll_server, a);
|
||||||
|
pthread_detach(client_wait_thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
log_append(std::string p, std::string ln)
|
||||||
|
{
|
||||||
|
std::ofstream f;
|
||||||
|
|
||||||
|
f.open(p, std::ios_base::app);
|
||||||
|
if (!f.is_open())
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
f << ln << std::endl;
|
||||||
|
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
// "this probably would help but theres too much broken stuff right now to be sure" - scott
|
||||||
|
// bool inclnum should be added to function if we do decide to use this block:
|
||||||
|
//if (inclnum)
|
||||||
|
// line_position++;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
6
chat.h
6
chat.h
@@ -27,8 +27,6 @@ const int CURSOR_DEFAULT_POSITION_Y = 12;
|
|||||||
const int LOG_LENGTH = 10;
|
const int LOG_LENGTH = 10;
|
||||||
int line_position = 0;
|
int line_position = 0;
|
||||||
|
|
||||||
chatmode_t mode = NO_MODE;
|
|
||||||
|
|
||||||
struct timeval time_start;
|
struct timeval time_start;
|
||||||
struct timeval time_end;
|
struct timeval time_end;
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
@@ -50,10 +48,10 @@ int count_lines(std::string p);
|
|||||||
int file_disp(std::string p, int lnstart, int lncount = 10);
|
int file_disp(std::string p, int lnstart, int lncount = 10);
|
||||||
int log_append(std::string p, std::string ln);
|
int log_append(std::string p, std::string ln);
|
||||||
int log_linect(std::string p);
|
int log_linect(std::string p);
|
||||||
void *client_poll(void);
|
|
||||||
void client_start(void);
|
void client_start(void);
|
||||||
void client_stop(void);
|
void client_stop(void);
|
||||||
void *server_poll(void *a);
|
void *poll_client(void);
|
||||||
|
void *poll_server(void *a);
|
||||||
void server_start(int port);
|
void server_start(int port);
|
||||||
void server_stop(void);
|
void server_stop(void);
|
||||||
|
|
||||||
|
|||||||
20
main.cc
20
main.cc
@@ -1,8 +1,28 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <bits/types/struct_timeval.h>
|
#include <bits/types/struct_timeval.h>
|
||||||
|
#include <curses.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "chat.h"
|
#include "chat.h"
|
||||||
|
|
||||||
|
chatmode_t mode = NO_MODE;
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
char *usr_in = new char[1024];
|
||||||
|
bool quit = false;
|
||||||
|
|
||||||
|
if (argc > 1 && (!strcmp(argv[1], "client")))
|
||||||
|
mode = CLIENT_MODE;
|
||||||
|
else
|
||||||
|
mode = SERVER_MODE;
|
||||||
|
|
||||||
|
initscr();
|
||||||
|
use_default_colors();
|
||||||
|
|
||||||
|
line_position = count_lines(log_path) - LOG_LENGTH + 1;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user