载入中...
搜索中...
未找到
TcpSocket.h
浏览该文件的文档.
1#pragma once
2
3#include "NetTypes.h"
4
5#include <cstdint>
6#include <memory>
7#include <mutex>
8#include <string>
9#include <vector>
10
11namespace eve::data {
12class ByteData;
13}
14
15namespace Poco::Net {
16class StreamSocket;
17class ServerSocket;
18}
19
20namespace eve::network {
21
22class Network;
23
28class TcpSocket {
29public:
31 explicit TcpSocket(Network* net);
32 ~TcpSocket();
33
35 bool connect(std::string host, uint16_t port);
37 bool listen(uint16_t port);
43 bool sendString(std::string s);
45 void close();
47 bool isConnected() const;
49 std::string getPeer() const;
51 uint16_t getLocalPort() const;
52
54 Network* network() const { return net_; }
55 void setConnectedSocket(std::unique_ptr<Poco::Net::StreamSocket> sock);
56 Poco::Net::StreamSocket* stream();
57 Poco::Net::ServerSocket* server();
59 bool isListening() const { return listening_; }
60 bool takeAccepted(std::unique_ptr<TcpSocket>& out);
61 void pushAccepted(std::unique_ptr<TcpSocket> sock);
63 size_t pendingSendBytes() const;
64
66 bool queueSend(const void* d, size_t n);
68 void flushSend();
69 void clearPendingSend();
70
71private:
72 Network* net_ = nullptr;
73 std::unique_ptr<Poco::Net::StreamSocket> stream_;
74 std::unique_ptr<Poco::Net::ServerSocket> server_;
75 bool listening_ = false;
76 bool connected_ = false;
77 std::mutex acceptMu_;
78 std::vector<std::unique_ptr<TcpSocket>> accepted_;
79 std::string peer_;
80
81 mutable std::mutex sendMu_;
82 std::vector<char> pendingSend_;
83};
84
85} // namespace eve::network
glm::vec3 n
Definition Grass.cpp:64
Light2D::Data * data
int d
uint32_t s
Definition Weather.cpp:28
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:11
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
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 pushAccepted(std::unique_ptr< TcpSocket > sock)
Definition TcpSocket.cpp:43
bool listen(uint16_t port)
Starts listening on the given port; true on success.
bool takeAccepted(std::unique_ptr< TcpSocket > &out)
Definition TcpSocket.cpp:48
Poco::Net::StreamSocket * stream()
Definition TcpSocket.cpp:35
uint16_t getLocalPort() const
Local listening port, or 0.
bool queueSend(const void *d, size_t n)
Append bytes to the pending send queue (worker-thread flush via pollSockets).
bool sendString(std::string s)
Sends a string payload.
void setConnectedSocket(std::unique_ptr< Poco::Net::StreamSocket > sock)
Definition TcpSocket.cpp:23
void close()
Closes the socket.
void flushSend()
Push pending bytes onto the socket; called from Network::pollSockets.
std::string getPeer() const
Remote peer address string, e.g. "1.2.3.4:5678".
Poco::Net::ServerSocket * server()
Definition TcpSocket.cpp:39
size_t pendingSendBytes() const
Internal: queued-outgoing byte counter for back-pressure.
TcpSocket * accept()
Accepts one pending client, or nullptr if none.
bool isConnected() const
True while a stream is connected.
bool connect(std::string host, uint16_t port)
Connects to host:port; true on success.
Definition TcpSocket.cpp:56
bool isListening() const
True after listen().
Definition TcpSocket.h:59
bool send(eve::data::ByteData *data)
Sends a framed byte payload; true when queued/accepted.