Whitespace and pointer cleanup.
This commit is contained in:
parent
6fc0b8dca8
commit
a03eb9599f
32 changed files with 441 additions and 468 deletions
|
@ -1,8 +1,9 @@
|
|||
#include "Socket.h"
|
||||
|
||||
Socket::Socket()
|
||||
: mPort(8888),
|
||||
mMessage()
|
||||
: mPort(8888),
|
||||
mMessage(),
|
||||
mHandle(-1)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -12,27 +13,37 @@ Socket::~Socket()
|
|||
|
||||
}
|
||||
|
||||
std::shared_ptr<Socket> Socket::Create()
|
||||
void Socket::SetHandle(SocketHandle handle)
|
||||
{
|
||||
return std::make_shared<Socket>();
|
||||
mHandle = handle;
|
||||
}
|
||||
|
||||
std::string Socket::GetMessage()
|
||||
Socket::SocketHandle Socket::GetHandle() const
|
||||
{
|
||||
return mMessage;
|
||||
return mHandle;
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> Socket::Create()
|
||||
{
|
||||
return std::make_unique<Socket>();
|
||||
}
|
||||
|
||||
std::string Socket::GetMessage() const
|
||||
{
|
||||
return mMessage;
|
||||
}
|
||||
|
||||
void Socket::SetMessage(const std::string& message)
|
||||
{
|
||||
mMessage = message;
|
||||
mMessage = message;
|
||||
}
|
||||
|
||||
void Socket::SetPort(unsigned port)
|
||||
{
|
||||
mPort = port;
|
||||
mPort = port;
|
||||
}
|
||||
|
||||
unsigned Socket::GetPort()
|
||||
unsigned Socket::GetPort() const
|
||||
{
|
||||
return mPort;
|
||||
return mPort;
|
||||
}
|
||||
|
|
|
@ -5,24 +5,32 @@
|
|||
|
||||
class Socket
|
||||
{
|
||||
unsigned mPort;
|
||||
std::string mMessage;
|
||||
|
||||
using SocketHandle = int;
|
||||
public:
|
||||
|
||||
Socket();
|
||||
Socket();
|
||||
|
||||
~Socket();
|
||||
~Socket();
|
||||
|
||||
static std::shared_ptr<Socket> Create();
|
||||
static std::unique_ptr<Socket> Create();
|
||||
|
||||
void SetPort(unsigned port);
|
||||
void SetPort(unsigned port);
|
||||
|
||||
unsigned GetPort();
|
||||
void SetHandle(SocketHandle handle);
|
||||
|
||||
std::string GetMessage();
|
||||
SocketHandle GetHandle() const;
|
||||
|
||||
void SetMessage(const std::string& message);
|
||||
unsigned GetPort() const;
|
||||
|
||||
std::string GetMessage() const;
|
||||
|
||||
void SetMessage(const std::string& message);
|
||||
|
||||
private:
|
||||
|
||||
SocketHandle mHandle;
|
||||
unsigned mPort;
|
||||
std::string mMessage;
|
||||
};
|
||||
|
||||
using SocketPtr = std::shared_ptr<Socket>;
|
||||
using SocketPtr = std::unique_ptr<Socket>;
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
#include "SocketInterface.h"
|
||||
#include "Socket.h"
|
||||
|
||||
SocketInterface::SocketInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<SocketInterface> SocketInterface::Create()
|
||||
{
|
||||
return std::make_unique<SocketInterface>();
|
||||
}
|
||||
|
||||
void SocketInterface::CreateSocket(SocketPtr socket)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SocketInterface::Listen(SocketPtr socket)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SocketInterface::Run(SocketPtr socket)
|
||||
{
|
||||
|
||||
}
|
|
@ -1,20 +1,21 @@
|
|||
#pragma
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Socket;
|
||||
using SocketPtr = std::shared_ptr<Socket>;
|
||||
using SocketPtr = std::unique_ptr<Socket>;
|
||||
|
||||
class SocketInterface
|
||||
class ISocketInterface
|
||||
{
|
||||
public:
|
||||
|
||||
SocketInterface();
|
||||
ISocketInterface() = default;
|
||||
|
||||
static std::unique_ptr<SocketInterface> Create();
|
||||
void CreateSocket(SocketPtr socket);
|
||||
void Listen(SocketPtr socket);
|
||||
void Run(SocketPtr socket);
|
||||
virtual ~ISocketInterface() = default;
|
||||
|
||||
virtual void InitializeSocket(const SocketPtr& socket) = 0;
|
||||
virtual void Listen(const SocketPtr& socket) = 0;
|
||||
virtual void Run(const SocketPtr& socket) = 0;
|
||||
};
|
||||
|
||||
using SocketInterfaceUPtr = std::unique_ptr<SocketInterface>;
|
||||
using ISocketInterfaceUPtr = std::unique_ptr<ISocketInterface>;
|
||||
|
|
|
@ -1,86 +1,82 @@
|
|||
#include "UnixSocketInterface.h"
|
||||
|
||||
#include "HttpResponse.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include "HttpResponse.h"
|
||||
|
||||
UnixSocketInterface::UnixSocketInterface()
|
||||
: mOpeningHandles(),
|
||||
mBufferSize(1024)
|
||||
: mBufferSize(1024)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
UnixSocketInterface::~UnixSocketInterface()
|
||||
std::unique_ptr<UnixSocketInterface> UnixSocketInterface::Create()
|
||||
{
|
||||
|
||||
return std::make_unique<UnixSocketInterface>();
|
||||
}
|
||||
|
||||
std::shared_ptr<UnixSocketInterface> UnixSocketInterface::Create()
|
||||
void UnixSocketInterface::InitializeSocket(const SocketPtr& socketPtr)
|
||||
{
|
||||
return std::make_shared<UnixSocketInterface>();
|
||||
auto handle = socket(AF_INET, SOCK_STREAM, 0);
|
||||
socketPtr->SetHandle(handle);
|
||||
}
|
||||
|
||||
void UnixSocketInterface::CreateSocket(SocketPtr socketPtr)
|
||||
void UnixSocketInterface::Listen(const SocketPtr& socket)
|
||||
{
|
||||
mOpeningHandles[socketPtr] = socket(AF_INET, SOCK_STREAM, 0);
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Listen(SocketPtr socket)
|
||||
{
|
||||
if(mOpeningHandles[socket] < 0)
|
||||
if(socket->GetHandle() < 0)
|
||||
{
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int port = static_cast<int>(socket->GetPort());
|
||||
int port = static_cast<int>(socket->GetPort());
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(port);
|
||||
|
||||
int result = bind(mOpeningHandles[socket], (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
||||
int result = bind(socket->GetHandle(), (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
||||
if(result< 0)
|
||||
{
|
||||
std::cerr << "Error binding socket" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error binding socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
listen(mOpeningHandles[socket], 5);
|
||||
listen(socket->GetHandle(), 5);
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Run(SocketPtr socket)
|
||||
void UnixSocketInterface::Run(const SocketPtr& socket)
|
||||
{
|
||||
if(mOpeningHandles[socket] < 0)
|
||||
if(socket->GetHandle() < 0)
|
||||
{
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in cli_addr;
|
||||
socklen_t clilen = sizeof(cli_addr);
|
||||
while(true)
|
||||
{
|
||||
SocketHandle new_socket_handle = accept(mOpeningHandles[socket],
|
||||
(struct sockaddr *) &cli_addr, &clilen);
|
||||
auto new_socket_handle = accept(socket->GetHandle(),
|
||||
(struct sockaddr *) &cli_addr, &clilen);
|
||||
if (new_socket_handle < 0)
|
||||
{
|
||||
std::cerr << "Error on accept" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error on accept" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[mBufferSize] = {0};
|
||||
int n = read(new_socket_handle, buffer, mBufferSize);
|
||||
if (n < 0)
|
||||
{
|
||||
std::cerr << "Error on read" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error on read" << std::endl;
|
||||
return;
|
||||
}
|
||||
socket->SetMessage(buffer);
|
||||
std::cout << "Here is the message: " << buffer << std::endl;
|
||||
|
@ -88,12 +84,12 @@ void UnixSocketInterface::Run(SocketPtr socket)
|
|||
HttpResponse response;
|
||||
response.SetBody("Hello world!");
|
||||
|
||||
std::string response_message = response.ToString();
|
||||
auto response_message = response.ToString();
|
||||
n = write(new_socket_handle, response_message.c_str(), response_message.length());
|
||||
if (n < 0)
|
||||
{
|
||||
std::cerr << "Error on write" << std::endl;
|
||||
return;
|
||||
std::cerr << "Error on write" << std::endl;
|
||||
return;
|
||||
}
|
||||
close(new_socket_handle);
|
||||
}
|
||||
|
|
|
@ -1,30 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include "Socket.h"
|
||||
#include "SocketInterface.h"
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <cstddef>
|
||||
|
||||
#include "Socket.h"
|
||||
|
||||
class UnixSocketInterface
|
||||
class UnixSocketInterface : public ISocketInterface
|
||||
{
|
||||
using SocketHandle = int;
|
||||
std::map<SocketPtr, SocketHandle> mOpeningHandles;
|
||||
std::size_t mBufferSize;
|
||||
|
||||
public:
|
||||
|
||||
UnixSocketInterface();
|
||||
UnixSocketInterface();
|
||||
|
||||
~UnixSocketInterface();
|
||||
virtual ~UnixSocketInterface() = default;
|
||||
|
||||
static std::shared_ptr<UnixSocketInterface> Create();
|
||||
static std::unique_ptr<UnixSocketInterface> Create();
|
||||
|
||||
void CreateSocket(SocketPtr socket);
|
||||
void InitializeSocket(const SocketPtr& socket) override;
|
||||
|
||||
void Listen(SocketPtr socket);
|
||||
void Listen(const SocketPtr& socket) override;
|
||||
|
||||
void Run(SocketPtr socket);
|
||||
void Run(const SocketPtr& socket) override;
|
||||
|
||||
private:
|
||||
|
||||
std::size_t mBufferSize { 0 };
|
||||
};
|
||||
|
||||
using UnixSocketInterfacePtr = std::shared_ptr<UnixSocketInterface>;
|
||||
using UnixSocketInterfacePtr = std::unique_ptr<UnixSocketInterface>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue