| | |
| | | #ifndef TCP_CONNECTION_H373GIL5 |
| | | #define TCP_CONNECTION_H373GIL5 |
| | | |
| | | #include "bh_util.h" |
| | | #include "node_center.h" |
| | | #include "tcp_common.h" |
| | | #include <functional> |
| | | #include <memory> |
| | | |
| | | class ShmSocket; |
| | | class TcpRequest1 : public std::enable_shared_from_this<TcpRequest1> |
| | | { |
| | | public: |
| | | static void Create(boost::asio::io_context &io, tcp::endpoint const &addr, std::string request) |
| | | static void Create(boost::asio::io_context &io, tcp::endpoint const &addr, std::string request, ShmSocket &shm_socket) |
| | | { |
| | | std::make_shared<TcpRequest1>(io, addr, std::move(request))->Connect(); |
| | | std::make_shared<TcpRequest1>(io, addr, std::move(request), shm_socket)->Start(); |
| | | } |
| | | |
| | | TcpRequest1(boost::asio::io_context &io, tcp::endpoint const &addr, std::string request); |
| | | TcpRequest1(boost::asio::io_context &io, tcp::endpoint const &addr, std::string request, ShmSocket &shm_socket) : |
| | | socket_(io), shm_socket_(shm_socket), remote_(addr), request_(std::move(request)) {} |
| | | void OnError(bserror_t ec); |
| | | |
| | | private: |
| | | void Connect(); |
| | | void Start(); |
| | | void Close(); |
| | | void SendRequest(); |
| | | void ReadReply(); |
| | | void OnRead(size_t size); |
| | | |
| | | tcp::socket socket_; |
| | | ShmSocket &shm_socket_; // send reply |
| | | tcp::endpoint remote_; |
| | | std::string request_; |
| | | std::vector<char> buffer_; |
| | | std::vector<char> recv_buffer_; |
| | | size_t recv_len_ = 0; |
| | | }; |
| | | |
| | | class NodeCenter; |
| | | class TcpReply1 : public std::enable_shared_from_this<TcpReply1> |
| | | { |
| | | public: |
| | | static void Create(tcp::socket sock) |
| | | typedef std::shared_ptr<Synced<NodeCenter>> CenterPtr; |
| | | static void Create(tcp::socket sock, CenterPtr pscenter) |
| | | { |
| | | std::make_shared<TcpReply1>(std::move(sock))->Start(); |
| | | std::make_shared<TcpReply1>(std::move(sock), pscenter)->Start(); |
| | | } |
| | | |
| | | TcpReply1(tcp::socket sock); |
| | | void Start(); |
| | | TcpReply1(tcp::socket sock, CenterPtr pscenter) : |
| | | socket_(std::move(sock)), pscenter_(pscenter) {} |
| | | void OnError(bserror_t ec); |
| | | |
| | | private: |
| | | void Start(); |
| | | void Close(); |
| | | void OnRead(size_t size); |
| | | |
| | | tcp::socket socket_; |
| | | CenterPtr pscenter_; |
| | | std::vector<char> recv_buffer_; |
| | | uint32_t recv_len_ = 0; |
| | | std::string send_buffer_; |
| | | }; |
| | | |