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 <pthread.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <curses.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -91,11 +94,11 @@ await_client(void *a)
|
||||
|
||||
delete args;
|
||||
|
||||
return client_poll();
|
||||
return poll_client();
|
||||
}
|
||||
|
||||
void *
|
||||
client_poll(void)
|
||||
poll_client(void)
|
||||
{
|
||||
char msg[1024];
|
||||
for (;;) {
|
||||
@@ -191,15 +194,15 @@ client_stop(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;
|
||||
char msg[1024];
|
||||
|
||||
for (;;) {
|
||||
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);
|
||||
|
||||
@@ -209,3 +212,62 @@ server_poll(void *args)
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user