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

View File

@@ -2,6 +2,7 @@
add_executable(accordion-server
src/main.cpp
# other server source files go here
src/TcpServer.cpp
)
target_include_directories(accordion-server PRIVATE

0
server/Dockerfile Normal file
View File

View File

@@ -19,3 +19,11 @@ Logger = (
FileName = "app.log";
}
);
TcpServer = (
{
Id = "main";
Port = 39500;
}
);

View File

95
server/src/TcpServer.cpp Normal file
View File

@@ -0,0 +1,95 @@
#include "TcpServer.hpp"
TcpServer::TcpServer(ConfigService* config, LoggerService* logger) : config_(config), logger_(logger) {
if(!(config->getConfig<ServerConfig>("TcpServer", "main", &configuration_))) {
logger_->log("TcpServer", LogFlag::Error, "Failed to get configuration");
return;
}
if(init() == ErrorCode::Success) {
logger_->log("TcpServer", LogFlag::Info, "TcpServer initialized.");
} else {
logger_->log("TcpServer", LogFlag::Error, "TcpServer failed to initialize.");
}
}
TcpServer::~TcpServer() {
// close port
}
ErrorCode TcpServer::init() {
logger_->log("TcpServer", LogFlag::Debug, "Initializing TcpServer...");
// keeping everything in here for now
int socketFd;
int clientFd;
char buffer[1024];
int receiveBytes;
int sendBytes;
struct sockaddr_in serverAddress;
struct sockaddr_in clientAddress;
socketFd = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd < 0) {
logger_->log("TcpServer", LogFlag::Error, "Unable to open Server socket.");
return ErrorCode::Error;
}
int on = 1;
setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(configuration_.port);
serverAddress.sin_addr.s_addr = INADDR_ANY;
memset(&serverAddress.sin_zero, 0, sizeof(serverAddress.sin_zero));
if(bind(socketFd, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
logger_->log("TcpServer", LogFlag::Error, "Unable to bind server socket.");
return ErrorCode::Error;
}
memset(buffer, 0x0, sizeof(buffer));
listen(socketFd, 10);
logger_->log("TcpServer", LogFlag::Debug, "Server listening...");
unsigned int sinSize = sizeof(struct sockaddr);
clientFd = accept(socketFd, (struct sockaddr*)&clientAddress, &sinSize);
if(clientFd < 0) {
logger_->log("TcpServer", LogFlag::Error, "Unable to accept client socket.");
return ErrorCode::Error;
}
struct sockaddr_in peerAddress;
unsigned int len = sizeof(peerAddress);
char serverIp[20];
std::string msg = "Accept socket... Client address: " + std::string(inet_ntoa(clientAddress.sin_addr)) + " Port: " + std::to_string(ntohs(clientAddress.sin_port));
logger_->log("TcpServer", LogFlag::Debug, msg);
getsockname(clientFd, (struct sockaddr*)&peerAddress, &len);
inet_ntop(AF_INET, &peerAddress, serverIp, sizeof(serverIp));
msg = "Accept socket... Server address: " + std::string(serverIp);
logger_->log("TcpServer", LogFlag::Debug, msg);
receiveBytes = recv(clientFd, buffer, sizeof(buffer), 0);
if(receiveBytes < 0) {
logger_->log("TcpServer", LogFlag::Error, "Unable to receive from client.");
return ErrorCode::Error;
}
msg = "Received " + std::to_string(receiveBytes) + " bytes from client: " + std::to_string(buffer[2]);
logger_->log("TcpClient", LogFlag::Debug, msg);
// echo back
sendBytes = send(clientFd, buffer, sizeof(buffer), 0);
if(sendBytes < 0) {
logger_->log("TcpServer", LogFlag::Error, "Unable to send to client.");
return ErrorCode::Error;
}
msg = "Echoed " + std::to_string(sendBytes) + " bytes back to client.";
close(socketFd);
return ErrorCode::Success;
}

32
server/src/TcpServer.hpp Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h> // TODO replace with chrono
#include "common/config/ConfigService.hpp"
#include "common/LoggerService.hpp"
#include "common/ErrorCodes.hpp"
#include "config/ServerConfig.hpp"
class TcpServer {
public:
TcpServer(ConfigService* config, LoggerService* logger);
~TcpServer();
private:
ErrorCode init();
LoggerService* logger_;
ConfigService* config_;
ServerParams configuration_;
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include "common/config/IConfig.hpp"
struct ServerParams {
std::string id;
uint32_t port;
};
class ServerConfig : public IConfig<ServerParams> {
public:
using IConfig<ServerParams>::IConfig;
bool parseConfig(const libconfig::Setting& setting) override {
if (!setting.lookupValue("Id", params_->id)) {
return false;
}
setting.lookupValue("Port", params_->port);
return true;
}
};

View File

@@ -2,14 +2,24 @@
#include <iostream>
#include "common/config/ConfigService.hpp"
#include "common/config/LoggerConfig.hpp"
#include "common/LoggerService.hpp"
#include "TcpServer.hpp"
int main(int argc, char* argv[]) {
ConfigService config {"server/config/main.cfg"}; // TODO: main config file should be the foremost cli argument
std::string centralConfigPath = "server/config/main.cfg"; // default path
if(argc > 1) {
centralConfigPath = std::string(argv[1]);
}
ConfigService config { centralConfigPath };
LoggerService logger {&config, "main"};
logger.log("main", LogFlag::Debug, "hello world from the server!");
logger.log("main", LogFlag::Debug, "hello world from the Server!");
TcpServer server {&config, &logger};
return 0;
}