Skip to content

Commit dec6067

Browse files
author
chengxin
committed
【ADD】基于ssl/tls的tcp长连接会话类封装
1 parent ff27d2e commit dec6067

File tree

2 files changed

+698
-0
lines changed

2 files changed

+698
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#ifndef PUSH_SOCKET_H
2+
#define PUSH_SOCKET_H
3+
4+
#ifdef _WIN32
5+
#define NOMINMAX // 防止 Windows 定义的 min/max 宏干扰
6+
#endif
7+
8+
#include <memory>
9+
#include <string>
10+
#include "boost/asio.hpp"
11+
#include "boost/asio/io_service.hpp"
12+
#include "boost/asio/ip/tcp.hpp"
13+
#include "boost/asio/ssl.hpp"
14+
#include "boost/bind.hpp"
15+
#include "boost/pool/pool.hpp"
16+
#include <boost/optional.hpp>
17+
#include "cpprest/details/basic_types.h"
18+
#include "tigerapi/client_config.h"
19+
#include "push_frame_serialize.h"
20+
21+
#include "openapi_pb/pb_source/Request.pb.h"
22+
#include "openapi_pb/pb_source/Response.pb.h"
23+
24+
namespace TIGER_API
25+
{
26+
enum class SocketState
27+
{
28+
CONNECTING, //正在连接
29+
CONNECTED, //已连接
30+
DISCONNECTING, //正在断开
31+
DISCONNECTED //已断开
32+
};
33+
34+
class PushSocket : public std::enable_shared_from_this<PushSocket>
35+
{
36+
public:
37+
static std::shared_ptr<PushSocket> create_push_socket(boost::asio::io_service* io_service,
38+
const TIGER_API::ClientConfig& client_config);
39+
40+
~PushSocket();
41+
PushSocket(const PushSocket&) = delete;
42+
PushSocket& operator=(const PushSocket&) = delete;
43+
44+
private:
45+
PushSocket() = delete;
46+
PushSocket(boost::asio::io_service* io_service,
47+
const TIGER_API::ClientConfig& client_config);
48+
public:
49+
void set_connected_callback(const std::function<void()>& cb);
50+
void set_disconnected_callback(const std::function<void()>& cb);
51+
void set_on_message_callback(const std::function<void(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object)>& cb);
52+
void set_inner_error_callback(const std::function<void(std::string)>& cb);
53+
54+
void connect();
55+
void disconnect();
56+
bool send_message(const std::string& msg);
57+
uint32_t get_next_id();
58+
private:
59+
void init_socket();
60+
bool verify_certificate(bool preverified,
61+
boost::asio::ssl::verify_context& ctx);
62+
void close_session();
63+
void send_authentication();
64+
void start_keep_alive();
65+
void send_heart_beat();
66+
void auto_reconnect();
67+
68+
void handle_connect(const boost::system::error_code& error,
69+
boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
70+
void handle_handshake(const boost::system::error_code& error);
71+
void handle_write(const boost::system::error_code& error,
72+
size_t bytes_transferred,
73+
unsigned int frame_len);
74+
void handle_read_head(const boost::system::error_code& error,
75+
size_t bytes_transferred);
76+
void handle_read_body(const boost::system::error_code& error,
77+
size_t bytes_transferred,
78+
char* recv_buff,
79+
int page_num,
80+
unsigned int frame_len);
81+
void handle_timer(const boost::system::error_code& error);
82+
83+
void read_head();
84+
void read_body(size_t frame_len);
85+
86+
void dispatch_connected_callback();
87+
void dispatch_disconnected_callback();
88+
void dispatch_inner_error_callback(const std::string& error);
89+
90+
void message_filter(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object);
91+
private:
92+
std::function<void()> connected_callback_;
93+
std::function<void()> disconnected_callback_;
94+
std::function<void(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object)> on_message_callback_;
95+
std::function<void(std::string)> on_inner_error_callback_;
96+
private:
97+
TIGER_API::ClientConfig client_config_;
98+
99+
boost::asio::io_service* io_service_ = nullptr;
100+
boost::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> socket_;
101+
std::shared_ptr<boost::asio::deadline_timer> keep_alive_timer_;
102+
std::shared_ptr<boost::asio::deadline_timer> reconnect_timer_;
103+
std::atomic<SocketState> socket_state_ = SocketState::DISCONNECTED;
104+
105+
std::atomic<uint32_t> id_counter_ = 0;
106+
107+
char head_buff_[1024];
108+
boost::shared_ptr<boost::pool<>> recv_buff_pool_;
109+
TIGER_API::PushFrameDecoder frame_decoder_;
110+
private:
111+
std::time_t last_send_heart_beat_time_ = 0;
112+
std::time_t last_io_time_ = 0;
113+
int reconnect_interval_ = 10 * 1000; //单位:ms
114+
int send_interval_ = 10 * 1000; //单位:ms
115+
int recv_interval_ = 10 * 1000; //单位:ms
116+
};
117+
}
118+
#endif // PUSH_SOCKET_H

0 commit comments

Comments
 (0)