Wayland example window
This commit is contained in:
parent
160b746182
commit
26f54c4581
30 changed files with 825 additions and 22 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue