fix server broadcasting demo

This commit is contained in:
2026-07-21 21:29:29 -05:00
parent a400b233a2
commit 8a97a32823
4 changed files with 54 additions and 31 deletions

View File

@@ -24,27 +24,25 @@ ErrorCode TcpClient::init() {
logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient...");
// keeping everything in here for now
int socketFd; // file descriptor for the client socket (socket proper is a function)
int sendBytes;
int receiveBytes;
char buffer[1024];
struct hostent* host; // houses host network info
struct sockaddr_in serverAddress; // ip address of the server socket
struct sockaddr_in serverAddress; // ip address of the server socket
// parse the hostname string into uint8s
host = gethostbyname(configuration_.hostname.c_str());
if(host == NULL) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to parse hostname.");
close(socketFd); // TODO: probably on a destructor
close(socketFd_); // TODO: probably on a destructor
return ErrorCode::Error;
}
// create tcp socket
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd < 0) {
socketFd_ = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd_ < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to open client socket.");
close(socketFd);
close(socketFd_);
return ErrorCode::Error;
}
@@ -55,31 +53,25 @@ ErrorCode TcpClient::init() {
serverAddress.sin_addr = *((struct in_addr*)host->h_addr);
// connect to server
if(connect(socketFd, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
if(connect(socketFd_, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to connect to server.");
close(socketFd);
close(socketFd_);
return ErrorCode::Error;
}
std::string message = "placeholder";
while(!message.empty()) {
logger_->log("TcpClient", LogFlag::Debug, "Enter message...");
std::getline(std::cin, message);
// send data buffer to server
sendBytes = tcpSend(socketFd, message.c_str(), message.size());
if(sendBytes < 0) {
logger_->log("TcpClient", LogFlag::Error, "Unable to send to server.");
close(socketFd);
return ErrorCode::Error;
}
logger_->log("TcpClient", LogFlag::Debug, "Sent {} bytes to the server.", sendBytes);
// start sending thread (only needed for cli because std::cin is blocking. a regular message box doesn't need to block for input)
pthread_t messengerThread;
if(pthread_create(&messengerThread, NULL, TcpClient::sendMessage, static_cast<TcpClient*>(this)) != 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to spawn messenger thread.");
close(socketFd_);
return ErrorCode::Error;
}
while(true) {
// listen for receive from the server
memset(buffer, 0x00, sizeof(buffer));
receiveBytes = tcpRead(socketFd, buffer, sizeof(buffer)); // TODO: this currently blocks forever, add timeout
receiveBytes = tcpRead(socketFd_, buffer, sizeof(buffer)); // TODO: this currently blocks forever, add timeout
// TODO: separate thread
// we expect a response based on the server design, something went wrong otherwise
@@ -88,7 +80,7 @@ ErrorCode TcpClient::init() {
return ErrorCode::Error;
} else if(receiveBytes == 0) {
logger_->log("TcpClient", LogFlag::Error, "Server closed the connection during receive.");
close(socketFd);
close(socketFd_);
return ErrorCode::Error;
}
@@ -96,7 +88,30 @@ ErrorCode TcpClient::init() {
logger_->log("TcpClient", LogFlag::Debug, "Received {} bytes back from the server: {}", receiveBytes, receivedMessage);
}
close(socketFd);
close(socketFd_);
pthread_join(messengerThread, NULL);
return ErrorCode::Success;
}
void* TcpClient::sendMessage(void* arg) {
int sendBytes;
std::string message = "placeholder";
TcpClient* obj = static_cast<TcpClient*>(arg);
while(true) {
obj->logger_->log("TcpClient", LogFlag::Debug, "Enter message...");
std::getline(std::cin, message);
// send data buffer to server
sendBytes = obj->tcpSend(obj->socketFd_, message.c_str(), message.size());
if(sendBytes < 0) {
obj->logger_->log("TcpClient", LogFlag::Error, "Unable to send to server.");
close(obj->socketFd_);
return nullptr;
}
obj->logger_->log("TcpClient", LogFlag::Debug, "Sent {} bytes to the server.", sendBytes);
}
}

View File

@@ -7,6 +7,7 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h> // TODO replace with chrono
#include <pthread.h>
#include "common/config/ConfigService.hpp"
#include "common/LoggerService.hpp"
@@ -25,6 +26,10 @@ private:
ErrorCode init();
// handler for sending messages since std::cin is blocking
static void* sendMessage(void* arg);
ClientParams configuration_;
int socketFd_; // file descriptor for the client socket (socket proper is a function)
};