Reorganize socket classes
This commit is contained in:
@@ -6,22 +6,22 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <sstream>
|
#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();
|
auto bytesToSend = response.Serialize();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connection.WriteBytes(bytesToSend);
|
clientSocket.WriteBytes(bytesToSend);
|
||||||
}
|
}
|
||||||
catch(std::runtime_error & e)
|
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::Request request;
|
||||||
Http::Response response;
|
Http::Response response;
|
||||||
try
|
try
|
||||||
@@ -37,7 +37,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
|
|||||||
logger.Error(ss.str());
|
logger.Error(ss.str());
|
||||||
|
|
||||||
response.code = HttpResponse::Code::BAD_REQUEST;
|
response.code = HttpResponse::Code::BAD_REQUEST;
|
||||||
SendResponse(newConnection, response);
|
SendResponse(newClient, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,12 +57,12 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
|
|||||||
logger.Error(ss.str());
|
logger.Error(ss.str());
|
||||||
|
|
||||||
response.code = HttpResponse::Code::NOT_IMPLEMENTED;
|
response.code = HttpResponse::Code::NOT_IMPLEMENTED;
|
||||||
SendResponse(newConnection, response);
|
SendResponse(newClient, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto bytesToSend = response.Serialize();
|
auto bytesToSend = response.Serialize();
|
||||||
newConnection.WriteBytes(bytesToSend);
|
newClient.WriteBytes(bytesToSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionOperator::ConnectionOperator(Logger & _logger)
|
ConnectionOperator::ConnectionOperator(Logger & _logger)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../logger.hpp"
|
#include "../logger.hpp"
|
||||||
#include "../middleware/base.hpp"
|
#include "../middleware/base.hpp"
|
||||||
#include "connection.hpp"
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "socket/clientsocket.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -12,10 +12,10 @@ private:
|
|||||||
Logger & logger;
|
Logger & logger;
|
||||||
std::vector<std::unique_ptr<Middleware::BaseMiddleware>> middlewares;
|
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:
|
public:
|
||||||
void HandleNewConnection(Connection const & newConnection);
|
void HandleNewConnection(ClientSocket const & newClient);
|
||||||
|
|
||||||
ConnectionOperator(Logger & logger);
|
ConnectionOperator(Logger & logger);
|
||||||
};
|
};
|
||||||
@@ -10,12 +10,12 @@ void HttpServer::Execute()
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Connection newConnection = listeningSocket.AcceptNextConnection();
|
ClientSocket newClient = listeningSocket.AcceptNextConnection();
|
||||||
connectionOperator.HandleNewConnection(newConnection);
|
connectionOperator.HandleNewConnection(newClient);
|
||||||
}
|
}
|
||||||
catch (std::runtime_error & e)
|
catch (std::runtime_error & e)
|
||||||
{
|
{
|
||||||
logger.Info("Connection dropped on accept");
|
logger.Info("ClientSocket dropped on accept");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../logger.hpp"
|
#include "../logger.hpp"
|
||||||
#include "connectionoperator.hpp"
|
#include "connectionoperator.hpp"
|
||||||
#include "listeningsocket.hpp"
|
#include "socket/listeningsocket.hpp"
|
||||||
|
|
||||||
class HttpServer
|
class HttpServer
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "connection.hpp"
|
#include "clientsocket.hpp"
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#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;
|
size_t const readChunkSize = 128;
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ std::vector<char> Connection::ReadBytes(size_t limit) const
|
|||||||
return buffer;
|
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;
|
ssize_t totalBytesWritten = 0;
|
||||||
size_t const sizeToWrite = bytes.size();
|
size_t const sizeToWrite = bytes.size();
|
||||||
@@ -43,16 +43,16 @@ size_t Connection::WriteBytes(std::vector<char> const & bytes) const
|
|||||||
return totalBytesWritten;
|
return totalBytesWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::Connection(int _fileDescriptor)
|
ClientSocket::ClientSocket(int _fileDescriptor)
|
||||||
: fileDescriptor(_fileDescriptor)
|
: fileDescriptor(_fileDescriptor)
|
||||||
{
|
{
|
||||||
if (_fileDescriptor < 0)
|
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)
|
if (fileDescriptor >= 0)
|
||||||
{
|
{
|
||||||
@@ -60,7 +60,7 @@ Connection::~Connection()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::Connection(Connection && other)
|
ClientSocket::ClientSocket(ClientSocket && other)
|
||||||
{
|
{
|
||||||
fileDescriptor = other.fileDescriptor;
|
fileDescriptor = other.fileDescriptor;
|
||||||
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Connection
|
class ClientSocket
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int fileDescriptor;
|
int fileDescriptor;
|
||||||
@@ -13,11 +13,11 @@ public:
|
|||||||
|
|
||||||
size_t WriteBytes(std::vector<char> const & bytes) const;
|
size_t WriteBytes(std::vector<char> const & bytes) const;
|
||||||
|
|
||||||
Connection(int _fileDescriptor);
|
ClientSocket(int _fileDescriptor);
|
||||||
~Connection();
|
~ClientSocket();
|
||||||
|
|
||||||
Connection(Connection && other);
|
ClientSocket(ClientSocket && other);
|
||||||
|
|
||||||
Connection(Connection & other) = delete;
|
ClientSocket(ClientSocket & other) = delete;
|
||||||
Connection & operator=(Connection & other) = delete;
|
ClientSocket & operator=(ClientSocket & other) = delete;
|
||||||
};
|
};
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
int const connectionLimit = 10;
|
int const connectionLimit = 10;
|
||||||
|
|
||||||
Connection ListeningSocket::AcceptNextConnection()
|
ClientSocket ListeningSocket::AcceptNextConnection()
|
||||||
{
|
{
|
||||||
unsigned sockaddrSize = sizeof(sockaddr_in);
|
unsigned sockaddrSize = sizeof(sockaddr_in);
|
||||||
int connectionFileDescriptor = accept(
|
int connectionFileDescriptor = accept(
|
||||||
@@ -12,7 +12,7 @@ Connection ListeningSocket::AcceptNextConnection()
|
|||||||
reinterpret_cast<sockaddr *>(&socketAddress),
|
reinterpret_cast<sockaddr *>(&socketAddress),
|
||||||
&sockaddrSize);
|
&sockaddrSize);
|
||||||
|
|
||||||
return Connection(connectionFileDescriptor);
|
return ClientSocket(connectionFileDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListeningSocket::ListeningSocket(int const port)
|
ListeningSocket::ListeningSocket(int const port)
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../logger.hpp"
|
#include "clientsocket.hpp"
|
||||||
#include "connection.hpp"
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ private:
|
|||||||
sockaddr_in socketAddress;
|
sockaddr_in socketAddress;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Connection AcceptNextConnection();
|
ClientSocket AcceptNextConnection();
|
||||||
|
|
||||||
ListeningSocket(int const port);
|
ListeningSocket(int const port);
|
||||||
~ListeningSocket();
|
~ListeningSocket();
|
||||||
Reference in New Issue
Block a user