Working on local and websocket client
This commit is contained in:
1 parent
483757f8ca
commit
ea26d6fb23
8 files changed
+178
-37
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
};
|
||||
}
|
||||
@@ -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) {});
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
Reference in new issue
Block a user