载入中...
搜索中...
未找到
NetWorker.h
浏览该文件的文档.
1#pragma once
2
3#include "NetTypes.h"
4
5#include <atomic>
6#include <condition_variable>
7#include <functional>
8#include <mutex>
9#include <thread>
10#include <vector>
11
12namespace eve::network {
13
14class Network;
15
20class NetWorker {
21public:
23 explicit NetWorker(Network* owner);
24 ~NetWorker();
25
27 void start();
29 void stop();
30
32 void post(NetCompletion c);
34 void drain(std::vector<NetCompletion>& out);
35
37 void submit(std::function<void()> job);
38
39private:
40 void threadMain();
41
42 Network* owner_ = nullptr;
43 std::mutex mu_;
44 std::condition_variable cv_;
45 std::vector<NetCompletion> completions_;
46 std::vector<std::function<void()>> jobs_;
47 std::thread thread_;
48 std::atomic<bool> running_{false};
49};
50
51} // namespace eve::network
uint32_t c
Background thread that runs blocking socket I/O and HTTP jobs. Completions are queued and drained by ...
Definition NetWorker.h:20
void drain(std::vector< NetCompletion > &out)
Test helper: moves all pending completions into out.
Definition NetWorker.cpp:35
void post(NetCompletion c)
Queues a completion for the main thread (thread-safe).
Definition NetWorker.cpp:30
void stop()
Stops the worker thread and joins it.
Definition NetWorker.cpp:20
void start()
Starts the worker thread.
Definition NetWorker.cpp:14
void submit(std::function< void()> job)
Queues an arbitrary blocking job to run on the worker thread.
Definition NetWorker.cpp:40
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33