Wayland example window
This commit is contained in:
parent
160b746182
commit
26f54c4581
30 changed files with 825 additions and 22 deletions
10
src/network/sockets/ISocketMessageHandler.h
Normal file
10
src/network/sockets/ISocketMessageHandler.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class ISocketMessageHandler
|
||||
{
|
||||
public:
|
||||
virtual ~ISocketMessageHandler() = default;
|
||||
virtual std::string onMessage(const std::string& message) = 0;
|
||||
};
|
|
@ -47,3 +47,14 @@ unsigned Socket::GetPort() const
|
|||
{
|
||||
return mPort;
|
||||
}
|
||||
|
||||
std::string Socket::getAddress() const
|
||||
{
|
||||
return mAddress;
|
||||
}
|
||||
|
||||
|
||||
void Socket::setAddress(const std::string& address)
|
||||
{
|
||||
mAddress = address;
|
||||
}
|
||||
|
|
|
@ -26,11 +26,15 @@ public:
|
|||
|
||||
void SetMessage(const std::string& message);
|
||||
|
||||
std::string getAddress() const;
|
||||
void setAddress(const std::string& address);
|
||||
|
||||
private:
|
||||
|
||||
SocketHandle mHandle;
|
||||
unsigned mPort{0};
|
||||
std::string mMessage;
|
||||
std::string mAddress;
|
||||
};
|
||||
|
||||
using SocketPtr = std::unique_ptr<Socket>;
|
||||
|
|
|
@ -13,9 +13,11 @@ public:
|
|||
|
||||
virtual ~ISocketInterface() = default;
|
||||
|
||||
virtual void InitializeSocket(const SocketPtr& socket) = 0;
|
||||
virtual void InitializeSocket(const SocketPtr& socket, const std::string& address = {}) = 0;
|
||||
virtual void Listen(const SocketPtr& socket) = 0;
|
||||
virtual void Run(const SocketPtr& socket) = 0;
|
||||
|
||||
virtual void Write(const SocketPtr& socket, const std::string& message) = 0;
|
||||
};
|
||||
|
||||
using ISocketInterfaceUPtr = std::unique_ptr<ISocketInterface>;
|
||||
|
|
|
@ -2,17 +2,20 @@
|
|||
|
||||
#include "HttpResponse.h"
|
||||
|
||||
#include "HttpMessageHandler.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
UnixSocketInterface::UnixSocketInterface()
|
||||
: mBufferSize(1024)
|
||||
{
|
||||
|
||||
mMessageHandler = std::make_unique<HttpMessageHandler>();
|
||||
}
|
||||
|
||||
std::unique_ptr<UnixSocketInterface> UnixSocketInterface::Create()
|
||||
|
@ -20,10 +23,61 @@ std::unique_ptr<UnixSocketInterface> UnixSocketInterface::Create()
|
|||
return std::make_unique<UnixSocketInterface>();
|
||||
}
|
||||
|
||||
void UnixSocketInterface::InitializeSocket(const SocketPtr& socketPtr)
|
||||
UnixSocketInterface::~UnixSocketInterface()
|
||||
{
|
||||
auto handle = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
}
|
||||
|
||||
void UnixSocketInterface::InitializeSocket(const SocketPtr& socketPtr, const std::string& address)
|
||||
{
|
||||
auto handle = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
socketPtr->SetHandle(handle);
|
||||
|
||||
if (!address.empty())
|
||||
{
|
||||
socketPtr->setAddress(address);
|
||||
}
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Write(const SocketPtr& socket, const std::string& message)
|
||||
{
|
||||
if(socket->GetHandle() < 0)
|
||||
{
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto port = static_cast<int>(socket->GetPort());
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
|
||||
inet_aton(socket->getAddress().c_str(), &serv_addr.sin_addr);
|
||||
serv_addr.sin_port = htons(port);
|
||||
|
||||
int result = connect(socket->GetHandle(), (struct sockaddr *)&serv_addr, sizeof(serv_addr));
|
||||
if(result< 0)
|
||||
{
|
||||
std::cerr << "Error connecting to socket" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto n = write(socket->GetHandle(), message.c_str(), message.length());
|
||||
if (n < 0)
|
||||
{
|
||||
std::cerr << "Error on write" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[mBufferSize] = {0};
|
||||
int res = read(socket->GetHandle(), buffer, mBufferSize);
|
||||
if (res < 0)
|
||||
{
|
||||
std::cerr << "Error on read" << std::endl;
|
||||
return;
|
||||
}
|
||||
socket->SetMessage(buffer);
|
||||
std::cout << "Here is the message: " << buffer << std::endl;
|
||||
}
|
||||
|
||||
void UnixSocketInterface::Listen(const SocketPtr& socket)
|
||||
|
@ -34,7 +88,7 @@ void UnixSocketInterface::Listen(const SocketPtr& socket)
|
|||
return;
|
||||
}
|
||||
|
||||
int port = static_cast<int>(socket->GetPort());
|
||||
const auto port = static_cast<int>(socket->GetPort());
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
|
@ -63,8 +117,7 @@ void UnixSocketInterface::Run(const SocketPtr& socket)
|
|||
socklen_t clilen = sizeof(cli_addr);
|
||||
while(true)
|
||||
{
|
||||
auto new_socket_handle = accept(socket->GetHandle(),
|
||||
(struct sockaddr *) &cli_addr, &clilen);
|
||||
const auto new_socket_handle = accept(socket->GetHandle(), (struct sockaddr *) &cli_addr, &clilen);
|
||||
if (new_socket_handle < 0)
|
||||
{
|
||||
std::cerr << "Error on accept" << std::endl;
|
||||
|
@ -81,11 +134,8 @@ void UnixSocketInterface::Run(const SocketPtr& socket)
|
|||
socket->SetMessage(buffer);
|
||||
std::cout << "Here is the message: " << buffer << std::endl;
|
||||
|
||||
HttpResponse response;
|
||||
response.SetBody("Hello world!");
|
||||
|
||||
auto response_message = response.ToString();
|
||||
n = write(new_socket_handle, response_message.c_str(), response_message.length());
|
||||
const auto response = mMessageHandler->onMessage(buffer);
|
||||
n = write(new_socket_handle, response.c_str(), response.length());
|
||||
if (n < 0)
|
||||
{
|
||||
std::cerr << "Error on write" << std::endl;
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
class ISocketMessageHandler;
|
||||
|
||||
class UnixSocketInterface : public ISocketInterface
|
||||
{
|
||||
|
||||
|
@ -13,19 +15,22 @@ public:
|
|||
|
||||
UnixSocketInterface();
|
||||
|
||||
virtual ~UnixSocketInterface() = default;
|
||||
virtual ~UnixSocketInterface();
|
||||
|
||||
static std::unique_ptr<UnixSocketInterface> Create();
|
||||
|
||||
void InitializeSocket(const SocketPtr& socket) override;
|
||||
void InitializeSocket(const SocketPtr& socket, const std::string& address = {}) override;
|
||||
|
||||
void Listen(const SocketPtr& socket) override;
|
||||
|
||||
void Write(const SocketPtr& socket, const std::string& message) override;
|
||||
|
||||
void Run(const SocketPtr& socket) override;
|
||||
|
||||
private:
|
||||
|
||||
std::size_t mBufferSize { 0 };
|
||||
std::unique_ptr<ISocketMessageHandler> mMessageHandler;
|
||||
};
|
||||
|
||||
using UnixSocketInterfacePtr = std::unique_ptr<UnixSocketInterface>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue