载入中...
搜索中...
未找到
Channel.cpp
浏览该文件的文档.
1#include "network/Channel.h"
2#include "network/TcpSocket.h"
3#include "network/Network.h"
4#include "network/NetTypes.h"
5#include "data/ByteData.h"
6#include "event/Event.h"
7#include "common/Assert.h"
8#include "common/Module.h"
9
10#include <cstring>
11
12namespace eve::network {
13
14namespace {
15
16void writeBe32(uint32_t v, char out[4]) {
17 out[0] = static_cast<char>((v >> 24) & 0xff);
18 out[1] = static_cast<char>((v >> 16) & 0xff);
19 out[2] = static_cast<char>((v >> 8) & 0xff);
20 out[3] = static_cast<char>(v & 0xff);
21}
22
23uint32_t readBe32(const char* p) {
24 return (uint32_t(uint8_t(p[0])) << 24) | (uint32_t(uint8_t(p[1])) << 16) |
25 (uint32_t(uint8_t(p[2])) << 8) | uint32_t(uint8_t(p[3]));
26}
27
28} // namespace
29
30Channel::Channel(TcpSocket* socket) : socket_(socket) {
31 EV_PARAM_CHECK(socket != nullptr, "channel requires a TCP socket");
32}
33
35 if (socket_ && socket_->network()) socket_->network()->unbindChannel(socket_);
36}
37
39 if (!data || !socket_) return false;
40 size_t n = data->getSize();
41 if (n > kMaxFrameSize) {
42 if (socket_->network()) {
45 c.kind = NetKind::Channel;
46 c.handle = this;
47 c.reason = "limit";
48 socket_->network()->post(std::move(c));
49 }
50 return false;
51 }
52 std::vector<char> frame(4 + n);
53 writeBe32(static_cast<uint32_t>(n), frame.data());
54 std::memcpy(frame.data() + 4, data->getData(), n);
55 eve::data::ByteData wrapped(frame.data(), frame.size());
56 return socket_->send(&wrapped);
57}
58
59bool Channel::sendMsgString(std::string s) {
60 if (s.empty()) return false;
61 eve::data::ByteData data(s.data(), s.size());
62 return sendMsg(&data);
63}
64
65void Channel::feed(const std::vector<char>& bytes) {
66 buffer_.insert(buffer_.end(), bytes.begin(), bytes.end());
67 auto* net = socket_ ? socket_->network() : nullptr;
68 while (true) {
69 if (!hasLen_) {
70 if (buffer_.size() < 4) return;
71 pendingLen_ = readBe32(buffer_.data());
72 hasLen_ = true;
73 buffer_.erase(buffer_.begin(), buffer_.begin() + 4);
74 if (pendingLen_ > kMaxFrameSize) {
75 if (net) {
78 c.kind = NetKind::Channel;
79 c.handle = this;
80 c.reason = "limit";
81 net->post(std::move(c));
85 e.handle = this;
86 e.reason = "limit";
87 net->post(std::move(e));
88 }
89 if (socket_) socket_->close();
90 buffer_.clear();
91 hasLen_ = false;
92 return;
93 }
94 }
95 if (buffer_.size() < pendingLen_) return;
96 auto payload = std::make_shared<std::vector<char>>(buffer_.begin(), buffer_.begin() + pendingLen_);
97 buffer_.erase(buffer_.begin(), buffer_.begin() + pendingLen_);
98 hasLen_ = false;
99 pendingLen_ = 0;
100 if (net) {
103 c.kind = NetKind::Channel;
104 c.handle = this;
105 c.bytes = std::move(payload);
106 // Emit directly via event on main thread — feed is called from pump
109 auto* ev = eve::ModuleManager::getInstance<eve::event::Event>("Event");
110 if (ev) {
111 eve::data::ByteData* bd = nullptr;
112 if (c.bytes && !c.bytes->empty())
113 bd = new eve::data::ByteData(c.bytes->data(), c.bytes->size());
114 std::vector<Variant> args;
115 args.push_back(Variant::makePtr(this));
116 args.push_back(Variant::makePtr(bd));
117 ev->push(new Message("chmsg", args));
118 }
119 }
120 }
121}
122
123} // namespace eve::network
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
glm::vec3 n
Definition Grass.cpp:64
uint32_t c
glm::vec4 p[6]
Light2D::Data * data
int v
uint32_t s
Definition Weather.cpp:28
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:11
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
Definition Event.h:56
bool sendMsg(eve::data::ByteData *data)
Sends one framed message.
Definition Channel.cpp:38
Channel(TcpSocket *socket)
Creates a channel over an existing socket (socket must be non-null).
Definition Channel.cpp:30
void feed(const std::vector< char > &bytes)
Feeds received bytes into the frame parser. Called on the main thread from Network::pump / the emitCo...
Definition Channel.cpp:65
bool sendMsgString(std::string s)
Sends a string payload as one framed message (empty strings are rejected).
Definition Channel.cpp:59
void post(NetCompletion c)
Posts a completion to the worker queue (thread-safe).
Definition Network.cpp:167
void unbindChannel(TcpSocket *sock)
Definition Network.cpp:202
TCP socket backed by Poco::Net; supports both client (connect) and server (listen/accept) roles....
Definition TcpSocket.h:28
Network * network() const
Internal: owning module (used by Network::pump / NetWorker).
Definition TcpSocket.h:54
void close()
Closes the socket.
bool send(eve::data::ByteData *data)
Sends a framed byte payload; true when queued/accepted.
constexpr size_t kMaxFrameSize
Maximum framed message size for Channel (1 MiB).
Definition NetTypes.h:13
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33