mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-09 20:24:16 +00:00
Alllll the groundwork
This commit is contained in:
@@ -2,6 +2,7 @@ set(DCHATSERVER_SOURCES
|
||||
"ChatIgnoreList.cpp"
|
||||
"ChatPacketHandler.cpp"
|
||||
"PlayerContainer.cpp"
|
||||
"ChatWebAPI.cpp"
|
||||
)
|
||||
|
||||
add_executable(ChatServer "ChatServer.cpp")
|
||||
@@ -12,5 +13,5 @@ add_library(dChatServer ${DCHATSERVER_SOURCES})
|
||||
target_include_directories(dChatServer PRIVATE "${PROJECT_SOURCE_DIR}/dServer")
|
||||
|
||||
target_link_libraries(dChatServer ${COMMON_LIBRARIES} dChatFilter)
|
||||
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer dServer)
|
||||
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer dServer picohttpparser)
|
||||
|
||||
|
@@ -28,6 +28,8 @@
|
||||
#include "RakNetDefines.h"
|
||||
#include "MessageIdentifiers.h"
|
||||
|
||||
#include "ChatWebAPI.h"
|
||||
|
||||
namespace Game {
|
||||
Logger* logger = nullptr;
|
||||
dServer* server = nullptr;
|
||||
@@ -122,6 +124,14 @@ int main(int argc, char** argv) {
|
||||
uint32_t framesSinceMasterDisconnect = 0;
|
||||
uint32_t framesSinceLastSQLPing = 0;
|
||||
|
||||
|
||||
bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1";
|
||||
ChatWebAPI chatwebapi(Game::config->GetValue("web_server_listen_ip"), GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("web_server_listen_port")).value_or(8080));
|
||||
|
||||
if (web_server_enabled) {
|
||||
LOG("Web server enabled, will process http requests.");
|
||||
}
|
||||
|
||||
Game::logger->Flush(); // once immediately before main loop
|
||||
while (!Game::ShouldShutdown()) {
|
||||
//Check if we're still connected to master:
|
||||
@@ -142,6 +152,10 @@ int main(int argc, char** argv) {
|
||||
Game::server->DeallocatePacket(packet);
|
||||
packet = nullptr;
|
||||
}
|
||||
//Check and handle web requests:
|
||||
if (web_server_enabled) {
|
||||
chatwebapi.HandleRequest();
|
||||
}
|
||||
|
||||
//Push our log every 30s:
|
||||
if (framesSinceLastFlush >= logFlushTime) {
|
||||
|
146
dChatServer/ChatWebAPI.cpp
Normal file
146
dChatServer/ChatWebAPI.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#include "ChatWebAPI.h"
|
||||
#include "Logger.h"
|
||||
#include "Game.h"
|
||||
#include "json.hpp"
|
||||
#include "picohttpparser.h"
|
||||
|
||||
namespace {
|
||||
const int BUFFER_SIZE = 30720;
|
||||
}
|
||||
|
||||
ChatWebAPI::ChatWebAPI(std::string ip_address, int port) :
|
||||
m_ip_address(ip_address),
|
||||
m_port(port),
|
||||
m_socket(),
|
||||
m_new_socket(),
|
||||
m_incomingMessage(),
|
||||
m_socketAddress(),
|
||||
m_socketAddress_len(sizeof(m_socketAddress)) {
|
||||
|
||||
m_socketAddress.sin_family = AF_INET; // IPv4
|
||||
m_socketAddress.sin_port = htons(m_port);
|
||||
m_socketAddress.sin_addr.s_addr = inet_addr(m_ip_address.c_str());
|
||||
|
||||
m_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (m_socket < 0) {
|
||||
LOG_DEBUG("Cannot create socket");
|
||||
return;
|
||||
}
|
||||
if (bind(m_socket, reinterpret_cast<sockaddr*> (&m_socketAddress), m_socketAddress_len) < 0) {
|
||||
LOG_DEBUG("Cannot connect socket to address");
|
||||
return;
|
||||
}
|
||||
if (listen(m_socket, 20) < 0) {
|
||||
LOG_DEBUG("Socket listen failed");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ChatWebAPI::~ChatWebAPI() {
|
||||
close(m_socket);
|
||||
close(m_new_socket);
|
||||
}
|
||||
|
||||
void ChatWebAPI::HandleRequest() {
|
||||
m_tv.tv_sec = 0;
|
||||
m_tv.tv_usec = 33;
|
||||
|
||||
FD_ZERO(&fdread);
|
||||
FD_SET(m_socket, &fdread);
|
||||
int32_t selectStatus = select(m_socket + 1, &fdread, NULL, NULL, &m_tv);
|
||||
|
||||
switch (selectStatus) {
|
||||
case -1:
|
||||
// error
|
||||
LOG_DEBUG("Error in select");
|
||||
break;
|
||||
case 0:
|
||||
// timeout, I.E. nothing to read
|
||||
// LOG_DEBUG("Timeout in select");
|
||||
break;
|
||||
|
||||
default: { // available to read
|
||||
|
||||
m_new_socket = accept(m_socket, reinterpret_cast<sockaddr*> (&m_socketAddress), &m_socketAddress_len);
|
||||
if (m_new_socket < 0) {
|
||||
LOG_DEBUG("Failed to accept connection");
|
||||
}
|
||||
|
||||
int bytesReceived;
|
||||
|
||||
char buffer[BUFFER_SIZE] = { 0 };
|
||||
bytesReceived = read(m_new_socket, buffer, BUFFER_SIZE);
|
||||
if (bytesReceived < 0) {
|
||||
LOG_DEBUG("Failed to read bytes from client socket connection");
|
||||
} else {
|
||||
LOG_DEBUG("------ Received message from client ------");
|
||||
}
|
||||
int status = 404;
|
||||
std::string response = "{\"error\": \"not found\"}";
|
||||
|
||||
const char *method_c {};
|
||||
const char *path_c {};
|
||||
size_t buflen = 0, method_len, path_len, num_headers;
|
||||
std::array<struct phr_header, 100> headers {};
|
||||
int pret, minor_version;
|
||||
|
||||
num_headers = sizeof(headers) / sizeof(headers[0]);
|
||||
|
||||
pret = phr_parse_request(buffer, strlen(buffer), &method_c, &method_len, &path_c, &path_len, &minor_version, headers.data(), &num_headers, 0);
|
||||
|
||||
// cleanup
|
||||
std::string method(method_c, method_len);
|
||||
std::string path(path_c, path_len);
|
||||
std::map<std::string, std::string> headerMap;
|
||||
for (size_t i = 0; i != num_headers; ++i) {
|
||||
std::string headerName(headers[i].name, headers[i].name_len);
|
||||
std::string headerValue(headers[i].value, headers[i].value_len);
|
||||
headerMap[headerName] = headerValue;
|
||||
}
|
||||
// log headers
|
||||
for (auto& header : headerMap) {
|
||||
LOG_DEBUG("header: %s: %s", header.first.c_str(), header.second.c_str());
|
||||
}
|
||||
|
||||
std::string content (buffer + pret);
|
||||
LOG_DEBUG("content: %s", content.c_str());
|
||||
|
||||
if (headerMap["Content-Type"] == "application/json") {
|
||||
auto data = json::parse(content);
|
||||
HandleAction(data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(m_new_socket);
|
||||
}
|
||||
|
||||
std::string ChatWebAPI::buildResponse(int status, std::string input) {
|
||||
std::ostringstream ss;
|
||||
ss << "HTTP/1.1 " << status << " OK\nContent-Type: application/json\nContent-Length: " << input.size() << "\n\n" << input;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void ChatWebAPI::sendResponse(int status, std::string input) {
|
||||
long bytesSent;
|
||||
std::string response = buildResponse(status, input);
|
||||
bytesSent = write(m_new_socket, response.c_str(), response.size());
|
||||
if (bytesSent == response.size()) {
|
||||
LOG_DEBUG("------ Server Response sent to client ------");
|
||||
} else {
|
||||
LOG_DEBUG("Error sending response to client");
|
||||
}
|
||||
}
|
||||
|
||||
void ChatWebAPI::HandleAction(json data) {
|
||||
|
||||
}
|
||||
|
36
dChatServer/ChatWebAPI.h
Normal file
36
dChatServer/ChatWebAPI.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef INCLUDED_CHATWEBAPI_LINUX
|
||||
#define INCLUDED_CHATWEBAPI_LINUX
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include "json_fwd.hpp"
|
||||
using json = nlohmann::json;
|
||||
|
||||
class ChatWebAPI {
|
||||
public:
|
||||
ChatWebAPI(std::string ip_address, int port);
|
||||
~ChatWebAPI();
|
||||
void HandleRequest();
|
||||
|
||||
private:
|
||||
std::string m_ip_address;
|
||||
int m_port;
|
||||
int m_socket;
|
||||
int m_new_socket;
|
||||
long m_incomingMessage;
|
||||
struct sockaddr_in m_socketAddress;
|
||||
unsigned int m_socketAddress_len;
|
||||
std::string m_serverMessage;
|
||||
struct timeval m_tv;
|
||||
fd_set fdread;
|
||||
|
||||
std::string buildResponse(int status, std::string input);
|
||||
void sendResponse(int status, std::string input);
|
||||
void HandleAction(json data);
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user