Skip to content

Commit

Permalink
Update server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed May 5, 2024
1 parent e35e765 commit f9fbe93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
26 changes: 15 additions & 11 deletions include/TCPServer.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H

#include "Logger.h"
#include "SerialPort.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include "SerialPort.h"
#include "Logger.h"

class TcpServer {
private:
int server_fd_;
struct sockaddr_in address_;
int port_;
bool is_running_;
SerialPort& serial_;

static void handleClient(int client_socket, SerialPort& serial);

public:
TcpServer(int port, SerialPort& serial);
~TcpServer();

void run();
void stop();

private:
void handleClient(int client_socket);

int server_fd_;
int port_;
bool is_running_;
struct sockaddr_in address_;
SerialPort& serial_;
};

#endif // TCPSERVER_H
26 changes: 8 additions & 18 deletions src/TCPServer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#include <sys/socket.h>
#include <unistd.h>
#include "TCPServer.h"
#include <thread>
#include <cstring>
#include <stdexcept>

#include "TCPServer.h"
#include <iostream>

TcpServer::TcpServer(int port, SerialPort& serial) : port_(port), is_running_(false), serial_(serial) {
Logger(Logger::Level::Info) << "Init tcp server on port " << port_;
TcpServer::TcpServer(int port, SerialPort& serial) : port_(port), serial_(serial), is_running_(false) {
if ((server_fd_ = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
throw std::runtime_error("Socket creation failed");
}
Expand All @@ -28,7 +24,6 @@ TcpServer::TcpServer(int port, SerialPort& serial) : port_(port), is_running_(fa
if (listen(server_fd_, 3) < 0) {
throw std::runtime_error("Listen failed");
}
Logger(Logger::Level::Info) << "Init tcp server finished";
}

TcpServer::~TcpServer() {
Expand All @@ -38,41 +33,36 @@ TcpServer::~TcpServer() {
}

void TcpServer::run() {
Logger(Logger::Level::Info) << "Start tcp server";
is_running_ = true;
while (is_running_) {
struct sockaddr_in clientAddr;
socklen_t clientLen = sizeof(clientAddr);
int clientSocket = accept(server_fd_, (struct sockaddr *) &clientAddr, &clientLen);
int clientSocket = accept(server_fd_, (struct sockaddr *)&clientAddr, &clientLen);
if (clientSocket < 0) {
Logger(Logger::Level::Info) << "Cannot accept connection";
continue;
}

std::thread clientThread(handleClient, clientSocket);
std::thread clientThread(&TcpServer::handleClient, this, clientSocket);
clientThread.detach(); // Detach the thread to handle multiple clients
}

}

void TcpServer::stop() {
close(server_fd_);
is_running_ = false;
}

void TcpServer::handleClient(int client_socket, SerialPort& serial) {
void TcpServer::handleClient(int client_socket) {
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
Logger(Logger::Level::Info) << "Reading from client connection";
ssize_t bytesReceived = read(client_socket, buffer, sizeof(buffer));
if (bytesReceived <= 0) {
Logger(Logger::Level::Info) << "Client disconnected or error";
break;
}

serial.writeData(std::string(buffer, bytesReceived));
std::string response = serial.readData();
serial_.writeData(std::string(buffer, bytesReceived));
std::string response = serial_.readData();
write(client_socket, response.c_str(), response.size());
}

Expand Down

0 comments on commit f9fbe93

Please sign in to comment.