lichao
2021-06-02 94f2a94f38261528d98a8ece4fcdb386cbca6566
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
80
81
/*
 * =====================================================================================
 *
 *       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 NodeCenter;
typedef std::shared_ptr<Synced<NodeCenter>> CenterPtr;
 
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, ReplyCB const &cb)
    {
        std::make_shared<TcpRequest1>(io, addr, std::move(request), cb)->Start();
    }
    TcpRequest1(boost::asio::io_context &io, tcp::endpoint const &addr, std::string request, ReplyCB const &cb) :
        socket_(io), reply_cb_(cb), remote_(addr), request_(std::move(request)) {}
    void OnError(bserror_t ec);
 
private:
    void Start();
    void Close();
    void OnRead(size_t size);
    void SendReply(BHMsgHead &head, std::string body_content);
 
    tcp::socket socket_;
    ReplyCB reply_cb_;
    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:
    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