1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

Replace sf::TcpListener/sf::TcpSocket with a drop-in implementation

This commit is contained in:
Christoph Heiss 2016-06-22 11:13:41 +02:00
parent 572f61e076
commit 9835b0fc61
5 changed files with 139 additions and 40 deletions

View File

@ -18,6 +18,7 @@ add_executable(rwgame
DrawUI.cpp
debug/HttpServer.cpp
debug/TcpSocket.cpp
)
include_directories(SYSTEM

View File

@ -1,8 +1,6 @@
#include "HttpServer.hpp"
#include <engine/GameState.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <regex>
@ -126,47 +124,43 @@ HttpServer::HttpServer(RWGame* game, GameWorld* world)
void HttpServer::run()
{
listener.listen(8091);
if (!socket.bind(8091))
return;
std::cout << "STARTING HTTP SERVER" << std::endl;
while ( game->getWindow().isOpen() ) {
sf::TcpSocket client;
if (listener.accept(client) == sf::Socket::Done) {
std::cout << "New connection from "
<< client.getRemoteAddress() << ":" << client.getRemotePort()
<< std::endl;
TcpSocket client;
while (game->getWindow().isOpen() && socket.listen(client)) {
std::cout << "New connection from "
<< client.getRemoteAddress() << ":" << client.getRemotePort()
<< std::endl;
char buf[1024];
size_t received;
client.receive(buf, 1023, received);
buf[received] = '\0';
std::cout << "Got " << received << " bytes: " << buf << std::endl;
std::string buffer;
client.recv(buffer, 1024);
try
{
std::regex regex_http_first_line("(\\w+)\\s+(/.*?)\\s+HTTP/\\d+.\\d+");
std::cmatch regex_match;
std::regex_search(buf, regex_match, regex_http_first_line);
std::cout << "Got " << buffer.length() << " bytes: " << buffer.c_str() << std::endl;
if (regex_match.size() == 3) {
std::string http_method = regex_match.str(1);
std::string http_path = regex_match.str(2);
try
{
std::regex regex_http_first_line("(\\w+)\\s+(/.*?)\\s+HTTP/\\d+.\\d+");
std::cmatch regex_match;
std::regex_search(buffer.c_str(), regex_match, regex_http_first_line);
std::string response = dispatch(http_method, http_path);
client.send(response.c_str(), response.size());
}
if (regex_match.size() == 3) {
std::string http_method = regex_match.str(1);
std::string http_path = regex_match.str(2);
std::string response = dispatch(http_method, http_path);
client.send(response);
}
catch(std::regex_error er)
{
std::cerr << er.what() << " " << er.code() << std::endl;
}
client.disconnect();
}
}
catch(std::regex_error er)
{
std::cerr << er.what() << " " << er.code() << std::endl;
}
listener.close();
client.disconnect();
}
}
void HttpServer::handleBreakpoint(const SCMBreakpoint &bp)

View File

@ -2,13 +2,8 @@
#include "../RWGame.hpp"
#include <engine/GameWorld.hpp>
#include "TcpSocket.hpp"
#include <SFML/Network/TcpListener.hpp>
#ifdef SFML_SYSTEM_WINDOWS
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
class HttpServer
{
@ -18,7 +13,7 @@ public:
void handleBreakpoint(const SCMBreakpoint& bp);
private:
sf::TcpListener listener;
TcpSocket socket;
RWGame* game;
GameWorld* world;
bool paused;

View File

@ -0,0 +1,83 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <vector>
#include "TcpSocket.hpp"
TcpSocket::TcpSocket() :
sock(-1), addr()
{
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("TcpSocket: Could not create socket");
}
bool TcpSocket::bind(short port)
{
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (::bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) {
perror("TcpSocket: Could not bind address to socket");
return false;
}
if (::listen(sock, 1)) {
perror("TcpSocket: Could not listen for connections");
return false;
}
return true;
}
bool TcpSocket::listen(TcpSocket& client)
{
socklen_t len = sizeof(client.addr);
client.sock = accept(sock, reinterpret_cast<sockaddr*>(&client.addr), &len);
return client.sock != -1;
}
void TcpSocket::recv(std::string& out, size_t len)
{
std::vector<char> buffer(len);
int ret = read(sock, buffer.data(), len);
if (ret != -1)
out = { buffer.begin(), buffer.begin()+ret };
else
out = "";
}
size_t TcpSocket::send(const std::string& str)
{
int ret = write(sock, str.c_str(), str.length());
return ret != -1 ? ret : 0;
}
void TcpSocket::disconnect()
{
close(sock);
sock = -1;
}
std::string TcpSocket::getRemoteAddress() const
{
char buffer[INET_ADDRSTRLEN+1] = { };
return inet_ntop(AF_INET, &addr, buffer, INET_ADDRSTRLEN+1);
}
short TcpSocket::getRemotePort() const
{
return ntohs(addr.sin_port);
}

View File

@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <netinet/in.h>
class TcpSocket
{
public:
TcpSocket();
~TcpSocket() { disconnect(); }
bool bind(short port);
bool listen(TcpSocket& client);
void recv(std::string& out, size_t len);
size_t send(const std::string& str);
void disconnect();
std::string getRemoteAddress() const;
short getRemotePort() const;
private:
int sock;
sockaddr_in addr;
};