1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 11:52:40 +01:00

Merge remote-tracking branch 'origin/rwng-debugger' into rwng-saves

This commit is contained in:
Daniel Evans 2015-07-02 00:13:32 +01:00
commit 5344b8bfa1
5 changed files with 104 additions and 4 deletions

View File

@ -12,6 +12,8 @@ add_executable(rwgame
debugstate.cpp
DrawUI.cpp
debug/HttpServer.cpp
)
include_directories("${CMAKE_SOURCE_DIR}/rwengine/include" ${BULLET_INCLUDE_DIR})
@ -20,6 +22,7 @@ target_link_libraries( rwgame
rwengine
sfml-graphics
sfml-window
sfml-network
sfml-system
${OPENGL_LIBRARIES}
GLEW

View File

@ -4,6 +4,7 @@
#include "DrawUI.hpp"
#include "ingamestate.hpp"
#include "menustate.hpp"
#include "debug/HttpServer.hpp"
#include <objects/GameObject.hpp>
#include <engine/GameState.hpp>
@ -198,9 +199,13 @@ void RWGame::startScript(const std::string& name)
{
SCMFile* f = world->data->loadSCM(name);
if( f ) {
if( script )
{
delete script;
if( script ) delete script;
if ( ! httpserver) {
httpserver_thread = new std::thread([&](){
httpserver = new HttpServer(this, world);
httpserver->run();
});
}
SCMOpcodes* opcodes = new SCMOpcodes;

View File

@ -11,6 +11,8 @@
#include <SFML/Graphics.hpp>
class PlayerController;
class HttpServer;
class RWGame
{
Logger log;
@ -19,9 +21,11 @@ class RWGame
GameWorld* world;
// must be allocated after Logger setup.
GameRenderer* renderer;
ScriptMachine* script;
ScriptMachine* script;
// Background worker
WorkContext work;
HttpServer* httpserver = nullptr;
std::thread* httpserver_thread = nullptr;
sf::RenderWindow window;
sf::Clock clock;
bool inFocus;

View File

@ -0,0 +1,54 @@
#include "HttpServer.hpp"
#include <SFML/Network.hpp>
#include <iostream>
#include <regex>
HttpServer::HttpServer(RWGame* game, GameWorld* world)
: game(game), world(world)
{}
void HttpServer::run()
{
listener.listen(8091);
listener.reuse();
std::cout << "STARTING HTTP SERVER" << std::endl;
while (true) {
sf::TcpSocket client;
if (listener.accept(client) == sf::Socket::Done) {
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::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);
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.c_str(), response.size());
}
client.disconnect();
}
}
listener.close();
}
std::string HttpServer::dispatch(std::string method, std::string path)
{
return "HTTP/1.1 200 OK\n\n<h1>HELLO FROM OPENRW</h1>";
}

View File

@ -0,0 +1,34 @@
#pragma once
#include "../RWGame.hpp"
#include <engine/GameWorld.hpp>
#include <SFML/Network/TcpListener.hpp>
#ifdef SFML_SYSTEM_WINDOWS
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
class ReuseableListener : public sf::TcpListener
{
public:
void reuse() {
char reuse = 1;
setsockopt(getHandle(), SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
}
};
class HttpServer
{
public:
HttpServer(RWGame* game, GameWorld* world);
void run();
private:
ReuseableListener listener;
RWGame* game;
GameWorld* world;
std::string dispatch(std::string method, std::string path);
};