Reorganize socket classes

This commit is contained in:
2019-06-16 11:42:26 +02:00
parent 9ba225cbde
commit e9509fb117
8 changed files with 32 additions and 33 deletions

View File

@@ -6,22 +6,22 @@
#include <cstdio>
#include <sstream>
void ConnectionOperator::SendResponse(Connection const & connection, Http::Response const & response) const
void ConnectionOperator::SendResponse(ClientSocket const & clientSocket, Http::Response const & response) const
{
auto bytesToSend = response.Serialize();
try
{
connection.WriteBytes(bytesToSend);
clientSocket.WriteBytes(bytesToSend);
}
catch(std::runtime_error & e)
{
logger.Error("Error writing data to connection");
logger.Error("Error writing data to clientSocket");
}
}
void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
void ConnectionOperator::HandleNewConnection(ClientSocket const & newClient)
{
auto requestBytes = newConnection.ReadBytes();
auto requestBytes = newClient.ReadBytes();
Http::Request request;
Http::Response response;
try
@@ -37,7 +37,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
logger.Error(ss.str());
response.code = HttpResponse::Code::BAD_REQUEST;
SendResponse(newConnection, response);
SendResponse(newClient, response);
return;
}
@@ -57,12 +57,12 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
logger.Error(ss.str());
response.code = HttpResponse::Code::NOT_IMPLEMENTED;
SendResponse(newConnection, response);
SendResponse(newClient, response);
return;
}
auto bytesToSend = response.Serialize();
newConnection.WriteBytes(bytesToSend);
newClient.WriteBytes(bytesToSend);
}
ConnectionOperator::ConnectionOperator(Logger & _logger)

View File

@@ -1,8 +1,8 @@
#pragma once
#include "../logger.hpp"
#include "../middleware/base.hpp"
#include "connection.hpp"
#include <memory>
#include "socket/clientsocket.hpp"
#include <string>
#include <vector>
@@ -12,10 +12,10 @@ private:
Logger & logger;
std::vector<std::unique_ptr<Middleware::BaseMiddleware>> middlewares;
void SendResponse(Connection const & connection, Http::Response const & response) const;
void SendResponse(ClientSocket const & clientSocket, Http::Response const & response) const;
public:
void HandleNewConnection(Connection const & newConnection);
void HandleNewConnection(ClientSocket const & newClient);
ConnectionOperator(Logger & logger);
};

View File

@@ -10,12 +10,12 @@ void HttpServer::Execute()
{
try
{
Connection newConnection = listeningSocket.AcceptNextConnection();
connectionOperator.HandleNewConnection(newConnection);
ClientSocket newClient = listeningSocket.AcceptNextConnection();
connectionOperator.HandleNewConnection(newClient);
}
catch (std::runtime_error & e)
{
logger.Info("Connection dropped on accept");
logger.Info("ClientSocket dropped on accept");
}
}
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include "../logger.hpp"
#include "connectionoperator.hpp"
#include "listeningsocket.hpp"
#include "socket/listeningsocket.hpp"
class HttpServer
{

View File

@@ -1,8 +1,8 @@
#include "connection.hpp"
#include "clientsocket.hpp"
#include <stdexcept>
#include <unistd.h>
std::vector<char> Connection::ReadBytes(size_t limit) const
std::vector<char> ClientSocket::ReadBytes(size_t limit) const
{
size_t const readChunkSize = 128;
@@ -25,7 +25,7 @@ std::vector<char> Connection::ReadBytes(size_t limit) const
return buffer;
}
size_t Connection::WriteBytes(std::vector<char> const & bytes) const
size_t ClientSocket::WriteBytes(std::vector<char> const & bytes) const
{
ssize_t totalBytesWritten = 0;
size_t const sizeToWrite = bytes.size();
@@ -43,16 +43,16 @@ size_t Connection::WriteBytes(std::vector<char> const & bytes) const
return totalBytesWritten;
}
Connection::Connection(int _fileDescriptor)
ClientSocket::ClientSocket(int _fileDescriptor)
: fileDescriptor(_fileDescriptor)
{
if (_fileDescriptor < 0)
{
throw std::runtime_error("connection created with invalid file descriptor");
throw std::runtime_error("clientSocket created with invalid file descriptor");
}
}
Connection::~Connection()
ClientSocket::~ClientSocket()
{
if (fileDescriptor >= 0)
{
@@ -60,7 +60,7 @@ Connection::~Connection()
}
}
Connection::Connection(Connection && other)
ClientSocket::ClientSocket(ClientSocket && other)
{
fileDescriptor = other.fileDescriptor;

View File

@@ -2,7 +2,7 @@
#include <cstdint>
#include <vector>
class Connection
class ClientSocket
{
private:
int fileDescriptor;
@@ -13,11 +13,11 @@ public:
size_t WriteBytes(std::vector<char> const & bytes) const;
Connection(int _fileDescriptor);
~Connection();
ClientSocket(int _fileDescriptor);
~ClientSocket();
Connection(Connection && other);
ClientSocket(ClientSocket && other);
Connection(Connection & other) = delete;
Connection & operator=(Connection & other) = delete;
ClientSocket(ClientSocket & other) = delete;
ClientSocket & operator=(ClientSocket & other) = delete;
};

View File

@@ -4,7 +4,7 @@
int const connectionLimit = 10;
Connection ListeningSocket::AcceptNextConnection()
ClientSocket ListeningSocket::AcceptNextConnection()
{
unsigned sockaddrSize = sizeof(sockaddr_in);
int connectionFileDescriptor = accept(
@@ -12,7 +12,7 @@ Connection ListeningSocket::AcceptNextConnection()
reinterpret_cast<sockaddr *>(&socketAddress),
&sockaddrSize);
return Connection(connectionFileDescriptor);
return ClientSocket(connectionFileDescriptor);
}
ListeningSocket::ListeningSocket(int const port)

View File

@@ -1,6 +1,5 @@
#pragma once
#include "../logger.hpp"
#include "connection.hpp"
#include "clientsocket.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
@@ -11,7 +10,7 @@ private:
sockaddr_in socketAddress;
public:
Connection AcceptNextConnection();
ClientSocket AcceptNextConnection();
ListeningSocket(int const port);
~ListeningSocket();