Working on local and websocket client

This commit is contained in:
Vladislav Khorev 2026-01-15 13:41:00 +03:00
parent 483757f8ca
commit ea26d6fb23
8 changed files with 178 additions and 37 deletions

View File

@ -51,6 +51,8 @@ add_executable(space-game001
../src/Projectile.h
../src/Projectile.cpp
../src/network/NetworkInterface.h
../src/network/LocalClient.h
../src/network/LocalClient.cpp
../src/network/WebSocketClient.h
../src/network/WebSocketClient.cpp
)

View File

@ -13,6 +13,12 @@
#include <android/log.h>
#endif
#ifdef NETWORK
#include "network/WebSocketClient.h"
#else
#include "network/LocalClient.h"
#endif
namespace ZL
{
#ifdef EMSCRIPTEN
@ -375,6 +381,14 @@ namespace ZL
//rockTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/rock.png", CONST_ZIP_FILE));
std::cout << "Init step 8 " << std::endl;
#ifdef NETWORK
networkClient = std::make_unique<WebSocketClient>(taskManager.getIOContext());
networkClient->Connect("127.0.0.1", 8080);
#else
networkClient = std::make_unique<LocalClient>();
networkClient->Connect("", 0);
#endif
}
void Game::drawCubemap(float skyPercent)
@ -626,6 +640,13 @@ namespace ZL
sparkEmitter.update(static_cast<float>(delta));
planetObject.update(static_cast<float>(delta));
static float pingTimer = 0.0f;
pingTimer += delta;
if (pingTimer >= 1000.0f) {
networkClient->Send("PING");
pingTimer = 0.0f;
}
if (Environment::tapDownHold) {
float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0);
@ -765,7 +786,6 @@ namespace ZL
}
}
uiManager.update(static_cast<float>(delta));
//#endif
lastTickCount = newTickCount;
}
}
@ -844,31 +864,6 @@ namespace ZL
if (event.type == SDL_MOUSEBUTTONDOWN) {
int mx = event.button.x;
int my = event.button.y;
uiManager.onMouseDown(mx, my);
bool uiHandled = false;
for (const auto& button : uiManager.findButton("") ? std::vector<std::shared_ptr<UiButton>>{} : std::vector<std::shared_ptr<UiButton>>{}) {
(void)button;
}
auto pressedSlider = [&]() -> std::shared_ptr<UiSlider> {
for (const auto& slider : uiManager.findSlider("") ? std::vector<std::shared_ptr<UiSlider>>{} : std::vector<std::shared_ptr<UiSlider>>{}) {
(void)slider;
}
return nullptr;
}();
if (!uiManager.isUiInteraction()) {
Environment::tapDownHold = true;
Environment::tapDownStartPos(0) = mx;
Environment::tapDownStartPos(1) = my;
Environment::tapDownCurrentPos(0) = mx;
Environment::tapDownCurrentPos(1) = my;
}
handleDown(mx, my);
}
if (event.type == SDL_MOUSEBUTTONUP) {
@ -884,6 +879,7 @@ namespace ZL
handleMotion(mx, my);
}
/*
if (event.type == SDL_MOUSEWHEEL) {
static const float zoomstep = 2.0f;
if (event.wheel.y > 0) {
@ -916,11 +912,12 @@ namespace ZL
Environment::shipVelocity -= 50.f;
//x = x - 2.0;
}
}
}*/
#endif
}
render();
mainThreadHandler.processMainThreadTasks();
networkClient->Poll();
}
void Game::handleDown(int mx, int my)
@ -953,6 +950,7 @@ namespace ZL
Environment::tapDownCurrentPos(1) = my;
}
}
void Game::handleUp(int mx, int my)
{
int uiX = mx;

View File

@ -8,6 +8,7 @@
#include "UiManager.h"
#include "Projectile.h"
#include "utils/TaskManager.h"
#include "network/NetworkInterface.h"
#include <queue>
namespace ZL {
@ -34,6 +35,8 @@ namespace ZL {
Renderer renderer;
TaskManager taskManager;
MainThreadHandler mainThreadHandler;
std::unique_ptr<INetworkClient> networkClient;
private:
void processTickCount();
void drawScene();

View File

@ -0,0 +1,26 @@
#include "LocalClient.h"
#include <iostream>
namespace ZL {
void LocalClient::Connect(const std::string& host, uint16_t port) {
}
void LocalClient::Poll() {
if (!messageQueue.empty()) {
std::string msg = messageQueue.front();
messageQueue.pop();
std::cout << "LocalClient received message: " << msg << std::endl;
}
}
void LocalClient::Send(const std::string& message) {
if (message == "PING")
{
messageQueue.push("PONG");
}
}
}

21
src/network/LocalClient.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
// WebSocketClient.h
#include "NetworkInterface.h"
#include <queue>
namespace ZL {
class LocalClient : public INetworkClient {
private:
std::queue<std::string> messageQueue;
public:
void Connect(const std::string& host, uint16_t port) override;
void Poll() override;
void Send(const std::string& message) override;
bool IsConnected() const override { return true; }
int GetClientId() const { return 1; }
};
}

View File

@ -0,0 +1,73 @@
#include "WebSocketClient.h"
#include <iostream>
namespace ZL {
void WebSocketClient::Connect(const std::string& host, uint16_t port) {
try {
boost::asio::ip::tcp::resolver resolver(ioc_);
auto const results = resolver.resolve(host, std::to_string(port));
ws_ = std::make_unique<boost::beast::websocket::stream<boost::beast::tcp_stream>>(ioc_);
// Âûïîëíÿåì ñèíõðîííûé êîííåêò è handshake äëÿ ïðîñòîòû ñòàðòà
boost::beast::get_lowest_layer(*ws_).connect(results);
ws_->handshake(host, "/");
connected = true;
// Çàïóñêàåì àñèíõðîííîå ÷òåíèå â ïóëå ïîòîêîâ TaskManager
startAsyncRead();
}
catch (std::exception& e) {
std::cerr << "Network Error: " << e.what() << std::endl;
}
}
void WebSocketClient::startAsyncRead() {
ws_->async_read(buffer_, [this](boost::beast::error_code ec, std::size_t bytes) {
if (!ec) {
std::string msg = boost::beast::buffers_to_string(buffer_.data());
buffer_.consume(bytes);
// Òÿæåëàÿ äåñåðèàëèçàöèÿ ïðîèñõîäèò çäåñü (â ïîòîêå TaskManager)
processIncomingMessage(msg);
startAsyncRead();
}
else {
connected = false;
}
});
}
void WebSocketClient::processIncomingMessage(const std::string& msg) {
// Ëîãèêà ïàðñèíãà...
if (msg.rfind("ID:", 0) == 0) {
clientId = std::stoi(msg.substr(3));
}
// Áåçîïàñíî êëàäåì â î÷åðåäü äëÿ ãëàâíîãî ïîòîêà
std::lock_guard<std::mutex> lock(queueMutex);
messageQueue.push(msg);
}
void WebSocketClient::Poll() {
// Çàáèðàåì ñîîáùåíèÿ èç î÷åðåäè è îáðàáàòûâàåì èõ â ãëàâíîì ïîòîêå
std::lock_guard<std::mutex> lock(queueMutex);
while (!messageQueue.empty()) {
std::string msg = messageQueue.front();
messageQueue.pop();
// Òóò ìîæíî âûçâàòü êîëáýêè èãðû èëè îáíîâèòü ñîñòîÿíèå
}
}
void WebSocketClient::Send(const std::string& message) {
if (!connected) return;
// Îòïðàâêó òîæå ìîæíî ñäåëàòü àñèíõðîííîé, ÷òîáû íå æäàòü â ãëàâíîì ïîòîêå
auto ss = std::make_shared<std::string>(message);
ws_->async_write(boost::asio::buffer(*ss), [ss](boost::beast::error_code, std::size_t) {});
}
}

View File

@ -3,25 +3,38 @@
// WebSocketClient.h
#include "NetworkInterface.h"
#include <queue>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
namespace ZL {
class WebSocketClient : public INetworkClient {
private:
// Ïåðåèñïîëüçóåì io_context èç TaskManager
boost::asio::io_context& ioc_;
// Îáúåêòû ïåðååõàëè â ÷ëåíû êëàññà
std::unique_ptr<boost::beast::websocket::stream<boost::beast::tcp_stream>> ws_;
boost::beast::flat_buffer buffer_;
std::queue<std::string> messageQueue;
std::mutex queueMutex; // Çàùèòà äëÿ messageQueue
bool connected = false;
int clientId = -1;
public:
void Connect(const std::string& host, uint16_t port) override {
// Â Emscripten çäåñü áóäåò emscripten_websocket_new
// Â Desktop - boost::beast::websocket::stream
}
void startAsyncRead();
void processIncomingMessage(const std::string& msg);
void Poll() override {
// ×èòàåì äàííûå èç ñîêåòà.
// Åñëè ïîëó÷èëè ID: clientId = parseId(msg); connected = true;
// Åñëè ïîëó÷èëè "PONG": ëîãèðóåì çàäåðæêó.
}
public:
explicit WebSocketClient(boost::asio::io_context& ioc) : ioc_(ioc) {}
void Connect(const std::string& host, uint16_t port) override;
void Poll() override;
void Send(const std::string& message) override;
bool IsConnected() const override { return connected; }
int GetClientId() const { return clientId; }

View File

@ -24,6 +24,11 @@ namespace ZL {
// Graceful shutdown
~TaskManager();
boost::asio::io_context& getIOContext()
{
return ioContext;
}
};