lichao
2021-05-27 026bbfaf2b5d73a26b8e2fa49158883ef64c211b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * =====================================================================================
 *
 *       Filename:  tcp_connection.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年05月25日 15时34分12秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#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, ShmSocket &shm_socket)
    {
        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, ShmSocket &shm_socket) :
        socket_(io), shm_socket_(shm_socket), remote_(addr), request_(std::move(request)) {}
    void OnError(bserror_t ec);
 
private:
    void Start();
    void Close();
    void OnRead(size_t size);
 
    tcp::socket socket_;
    ShmSocket &shm_socket_; // send reply
    tcp::endpoint remote_;
    std::string request_;
    std::vector<char> recv_buffer_;
    size_t recv_len_ = 0;
};
 
class NodeCenter;
class TcpReply1 : public std::enable_shared_from_this<TcpReply1>
{
public:
    typedef std::shared_ptr<Synced<NodeCenter>> CenterPtr;
    static void Create(tcp::socket sock, CenterPtr pscenter)
    {
        std::make_shared<TcpReply1>(std::move(sock), pscenter)->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_;
};
 
#endif // end of include guard: TCP_CONNECTION_H373GIL5