Initial commit.
This commit is contained in:
commit
59c6161fdb
134 changed files with 4751 additions and 0 deletions
38
src/network/sockets/Socket.cpp
Normal file
38
src/network/sockets/Socket.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include "Socket.h"
|
||||
|
||||
Socket::Socket()
|
||||
: mPort(8888),
|
||||
mMessage()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<Socket> Socket::Create()
|
||||
{
|
||||
return std::make_shared<Socket>();
|
||||
}
|
||||
|
||||
std::string Socket::GetMessage()
|
||||
{
|
||||
return mMessage;
|
||||
}
|
||||
|
||||
void Socket::SetMessage(const std::string& message)
|
||||
{
|
||||
mMessage = message;
|
||||
}
|
||||
|
||||
void Socket::SetPort(unsigned port)
|
||||
{
|
||||
mPort = port;
|
||||
}
|
||||
|
||||
unsigned Socket::GetPort()
|
||||
{
|
||||
return mPort;
|
||||
}
|
28
src/network/sockets/Socket.h
Normal file
28
src/network/sockets/Socket.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Socket
|
||||
{
|
||||
unsigned mPort;
|
||||
std::string mMessage;
|
||||
|
||||
public:
|
||||
|
||||
Socket();
|
||||
|
||||
~Socket();
|
||||
|
||||
static std::shared_ptr<Socket> Create();
|
||||
|
||||
void SetPort(unsigned port);
|
||||
|
||||
unsigned GetPort();
|
||||
|
||||
std::string GetMessage();
|
||||
|
||||
void SetMessage(const std::string& message);
|
||||
};
|
||||
|
||||
using SocketPtr = std::shared_ptr<Socket>;
|
100
src/network/sockets/UnixSocketInterface.cpp
Normal file
100
src/network/sockets/UnixSocketInterface.cpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
#include "UnixSocketInterface.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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
UnixSocketInterface::~UnixSocketInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<UnixSocketInterface> UnixSocketInterface::Create()
|
||||
{
|
||||
return std::make_shared<UnixSocketInterface>();
|
||||
}
|
||||
|
||||
void UnixSocketInterface::CreateSocket(SocketPtr socketPtr)
|
||||
{
|
||||
mOpeningHandles[socketPtr] = socket(AF_INET, SOCK_STREAM, 0);
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Listen(SocketPtr socket)
|
||||
{
|
||||
if(mOpeningHandles[socket] < 0)
|
||||
{
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
if(result< 0)
|
||||
{
|
||||
std::cerr << "Error binding socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
listen(mOpeningHandles[socket], 5);
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Run(SocketPtr socket)
|
||||
{
|
||||
if(mOpeningHandles[socket] < 0)
|
||||
{
|
||||
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);
|
||||
if (new_socket_handle < 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
socket->SetMessage(buffer);
|
||||
std::cout << "Here is the message: " << buffer << std::endl;
|
||||
|
||||
HttpResponse response;
|
||||
response.SetBody("Hello world!");
|
||||
|
||||
std::string 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;
|
||||
}
|
||||
close(new_socket_handle);
|
||||
}
|
||||
}
|
30
src/network/sockets/UnixSocketInterface.h
Normal file
30
src/network/sockets/UnixSocketInterface.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <cstddef>
|
||||
|
||||
#include "Socket.h"
|
||||
|
||||
class UnixSocketInterface
|
||||
{
|
||||
using SocketHandle = int;
|
||||
std::map<SocketPtr, SocketHandle> mOpeningHandles;
|
||||
std::size_t mBufferSize;
|
||||
|
||||
public:
|
||||
|
||||
UnixSocketInterface();
|
||||
|
||||
~UnixSocketInterface();
|
||||
|
||||
static std::shared_ptr<UnixSocketInterface> Create();
|
||||
|
||||
void CreateSocket(SocketPtr socket);
|
||||
|
||||
void Listen(SocketPtr socket);
|
||||
|
||||
void Run(SocketPtr socket);
|
||||
};
|
||||
|
||||
using UnixSocketInterfacePtr = std::shared_ptr<UnixSocketInterface>;
|
Loading…
Add table
Add a link
Reference in a new issue