Получение пакетов клиентом

This commit is contained in:
2025-02-09 23:52:04 +06:00
parent 871b03632e
commit cea3a0ca28
26 changed files with 927 additions and 269 deletions

View File

@@ -32,7 +32,7 @@ struct Local16 {
using Key = uint16_t;
operator Key() const {
return Key(X) | (Key(Y) << 4) | (Key(Z) << 8);
return Key(uint8_t(X)) | (Key(uint8_t(Y) << 4)) | (Key(uint8_t(Z)) << 8);
};
Local4_u left() const { return Local4_u{uint8_t(uint16_t(X) >> 2), uint8_t(uint16_t(Y) >> 2), uint8_t(uint16_t(Z) >> 2)}; }
@@ -74,18 +74,18 @@ struct GlobalNode {
using Key = uint64_t;
operator Key() const {
return Key(X) | (Key(Y) << 20) | (Key(Z) << 40);
return Key(uint32_t(X)) | (Key(uint32_t(Y) << 20)) | (Key(uint32_t(Z)) << 40);
};
auto operator<=>(const GlobalNode&) const = default;
};
struct GlobalChunk {
int16_t X : 16, Y : 16, Z : 16;
int16_t X, Y, Z;
using Key = uint64_t;
operator Key() const {
return Key(X) | (Key(Y) << 16) | (Key(Z) << 32);
return Key(uint16_t(X)) | (Key(uint16_t(Y)) << 16) | (Key(uint16_t(Z)) << 32);
};
auto operator<=>(const GlobalChunk&) const = default;
@@ -96,7 +96,7 @@ struct GlobalRegion {
using Key = uint64_t;
operator Key() const {
return Key(X) | (Key(Y) << 12) | (Key(Z) << 24);
return Key(uint16_t(X)) | (Key(uint16_t(Y) << 12)) | (Key(uint16_t(Z)) << 24);
};
auto operator<=>(const GlobalRegion&) const = default;
@@ -118,6 +118,20 @@ struct LightPrism {
uint8_t R : 2, G : 2, B : 2;
};
// Идентификаторы на стороне клиента
using TextureId_c = uint16_t;
using SoundId_c = uint16_t;
using ModelId_c = uint16_t;
using DefWorldId_c = uint8_t;
using DefVoxelId_c = uint16_t;
using DefNodeId_c = uint16_t;
using DefPortalId_c = uint8_t;
using WorldId_c = uint8_t;
using PortalId_c = uint8_t;
using DefEntityId_c = uint16_t;
using EntityId_c = uint16_t;
}

View File

@@ -112,7 +112,7 @@ public:
return;
std::atomic_bool flag = false;
Deadline.async_wait([&](boost::system::error_code errc) { flag.store(true); });
Deadline.async_wait([&](boost::system::error_code errc) { flag.store(true); flag.notify_all(); });
lock.unlock();
flag.wait(false);
}
@@ -129,7 +129,7 @@ public:
return;
std::atomic_bool flag = false;
Deadline.async_wait([&](boost::system::error_code errc) { flag.store(true); });
Deadline.async_wait([&](boost::system::error_code errc) { flag.store(true); flag.notify_all(); });
lock.unlock();
flag.wait(false);
}

View File

@@ -110,10 +110,12 @@ public:
: Lock(lock)
{
lock.UseCount++;
lock.UseCount.notify_all();
}
~Guard() {
Lock.UseCount--;
Lock.UseCount.notify_all();
}
private:

View File

@@ -1,6 +1,7 @@
#include "Net.hpp"
#include <TOSLib.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/socket_base.hpp>
namespace LV::Net {
@@ -17,15 +18,14 @@ bool Server::isStopped() {
void Server::stop() {
NeedClose = true;
NeedClose.notify_all();
if(Acceptor.is_open())
Acceptor.close();
}
void Server::wait() {
if(!IsAlive)
return;
Lock.wait();
while(bool val = IsAlive)
IsAlive.wait(val);
}
coro<void> Server::async_wait() {
@@ -45,6 +45,7 @@ coro<void> Server::run() {
}
IsAlive.store(false);
IsAlive.notify_all();
Lock.cancel();
}
@@ -56,6 +57,7 @@ AsyncSocket::~AsyncSocket() {
SendPackets.Context->NeedShutdown = true;
SendPackets.SenderGuard.cancel();
WorkDeadline.cancel();
}
void AsyncSocket::pushPackets(std::vector<Packet> *simplePackets, std::vector<SmartPacket> *smartPackets) {
@@ -135,6 +137,10 @@ coro<> AsyncSocket::read(std::byte *data, uint32_t size) {
}
}
void AsyncSocket::closeRead() {
Socket.shutdown(boost::asio::socket_base::shutdown_receive);
}
coro<> AsyncSocket::waitForSend() {
asio::deadline_timer waiter(IOC);
@@ -249,8 +255,7 @@ coro<tcp::socket> asyncConnectTo(const std::string address, std::function<void(c
if(!re) {
re = Str::match(address, "([-_\\.\\w\\d]+)(?:\\:(\\d+))?");
if(!re) {
addLog("Не удалось разобрать адрес");
co_return nullptr;
MAKE_ERROR("Не удалось разобрать адрес");
}
tcp::resolver resv{ioc};
@@ -288,8 +293,7 @@ coro<tcp::socket> asyncConnectTo(const std::string address, std::function<void(c
}
}
addLog("Не удалось подключится к серверу");
MAKE_ERROR(progress);
MAKE_ERROR("Не удалось подключится к серверу");
}
}

View File

@@ -27,6 +27,7 @@ public:
co_spawn(run());
IsAlive.store(true);
IsAlive.notify_all();
}
~Server();
@@ -78,9 +79,24 @@ protected:
public:
Packet() = default;
Packet(const Packet&) = default;
Packet(Packet&&) = default;
Packet(Packet &&obj)
: Size(obj.Size), Pages(std::move(obj.Pages))
{
obj.Size = 0;
}
Packet& operator=(const Packet&) = default;
Packet& operator=(Packet&&) = default;
Packet& operator=(Packet &&obj) {
if(&obj == this)
return *this;
Size = obj.Size;
Pages = std::move(obj.Pages);
obj.Size = 0;
return *this;
}
inline Packet& write(const std::byte *data, uint16_t size) {
assert(Size+size < MAX_PACKET_SIZE);
@@ -213,17 +229,24 @@ protected:
boost::asio::ip::tcp::no_delay optionNoDelay(true); // Отключает попытки объёденить данные в крупные пакеты
Socket.set_option(optionNoDelay);
asio::co_spawn(ioc, runSender(SendPackets.Context), asio::detached);
co_spawn(runSender(SendPackets.Context));
}
~AsyncSocket();
void pushPackets(std::vector<Packet> *simplePackets, std::vector<SmartPacket> *smartPackets = nullptr);
void pushPacket(Packet &&simplePacket) {
std::vector<Packet> out(1);
out[0] = std::move(simplePacket);
pushPackets(&out);
}
std::string getError() const;
bool isAlive() const;
coro<> read(std::byte *data, uint32_t size);
void closeRead();
template<typename T, std::enable_if_t<std::is_integral_v<T> or std::is_same_v<T, std::string>, int> = 0>
coro<T> read() {

View File

@@ -6,8 +6,14 @@
namespace LV {
namespace ToServer {
enum struct EnumDisconnect {
ByInterface,
CriticalError,
ProtocolError
};
namespace ToServer {
struct PacketQuat {
uint8_t Data[5];
@@ -51,6 +57,17 @@ struct PacketQuat {
*/
// Первый уровень
enum struct L1 : uint8_t {
System,
};
// Второй уровень
enum struct L2System : uint8_t {
InitEnd,
Disconnect
};
}
namespace ToClient {