messy client<->server checkpoint

This commit is contained in:
2026-07-17 13:58:53 -05:00
parent 3a3f67c4ef
commit 235530c03d
16 changed files with 357 additions and 5 deletions

83
client/src/TcpClient.cpp Normal file
View File

@@ -0,0 +1,83 @@
#include "TcpClient.hpp"
TcpClient::TcpClient(ConfigService* config, LoggerService* logger) : config_(config), logger_(logger) {
if(!(config->getConfig<ClientConfig>("TcpClient", "main", &configuration_))) {
logger_->log("TcpClient", LogFlag::Error, "Failed to get configuration");
return;
}
if(init() == ErrorCode::Success) {
logger_->log("TcpClient", LogFlag::Info, "TcpClient initialized.");
} else {
logger_->log("TcpClient", LogFlag::Error, "TcpClient failed to initialize.");
}
}
TcpClient::~TcpClient() {
// close port
}
ErrorCode TcpClient::init() {
logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient...");
// keeping everything in here for now
int socketFd;
int sendBytes;
int receiveBytes;
char buffer[1024];
struct hostent* host;
struct sockaddr_in serverAddress;
struct timeval timestamp;
struct timeval timestampEnd;
host = gethostbyname(configuration_.hostname.c_str());
if(host == NULL) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to parse hostname.");
return ErrorCode::Error;
}
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to open client socket.");
return ErrorCode::Error;
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(configuration_.port);
serverAddress.sin_addr = *((struct in_addr*)host->h_addr);
memset(&serverAddress.sin_zero, 0, sizeof(serverAddress.sin_zero));
if(connect(socketFd, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to connect to server.");
return ErrorCode::Error;
}
memset(buffer, 0x67, sizeof(buffer));
gettimeofday(&timestamp, NULL);
sendBytes = send(socketFd, buffer, sizeof(buffer), 0);
if(sendBytes < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to send to server.");
return ErrorCode::Error;
}
gettimeofday(&timestampEnd, NULL);
memset(buffer, 0x00, sizeof(buffer));
receiveBytes = recv(socketFd, buffer, sizeof(buffer), 0);
if(receiveBytes < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to receive from server.");
return ErrorCode::Error;
}
std::string msg = "Received " + std::to_string(receiveBytes) + " bytes back from server.";
logger_->log("TcpClient", LogFlag::Debug, msg);
close(socketFd);
return ErrorCode::Success;
}