Skip to content

Commit 4f21f25

Browse files
committed
Merge branch 'feature_tcp_client' of git.tigerbrokers.net:server/openapi/openapi-cpp-sdk into feature_tcp_client
2 parents 6625b35 + 3e39719 commit 4f21f25

File tree

6 files changed

+81
-100
lines changed

6 files changed

+81
-100
lines changed

include/tigerapi/push_socket/push_client_impl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ namespace TIGER_API
3838
private:
3939
std::function<void(const tigeropen::push::pb::AssetData&)> asset_changed_;
4040
private:
41-
ClientConfig client_config_;
4241
boost::asio::io_service io_service_;
4342
std::shared_ptr<TIGER_API::PushSocket> socket_;
4443
std::shared_ptr<std::thread> worker_thread_;

include/tigerapi/push_socket/push_frame_serialize.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ namespace TIGER_API
1818
~PushFrameDecoder() = default;
1919

2020
bool push_byte(unsigned char value);
21-
uint64_t get_frame_size() const;
21+
uint32_t get_frame_size() const;
2222

2323
private:
24-
uint64_t decode_varint();
24+
uint32_t decode_varint();
2525
std::vector<unsigned char> frame_header_buffer_;
26-
uint64_t frame_len_ = 0;
26+
uint32_t frame_len_ = 0;
2727
};
2828

2929
}

include/tigerapi/push_socket/push_socket.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
#ifndef PUSH_SOCKET_H
22
#define PUSH_SOCKET_H
33

4-
#ifdef _WIN32
5-
#define NOMINMAX // 防止 Windows 定义的 min/max 宏干扰
6-
#endif
7-
8-
#include <memory>
9-
#include <string>
104
#include "boost/asio.hpp"
11-
#include "boost/asio/io_service.hpp"
12-
#include "boost/asio/ip/tcp.hpp"
135
#include "boost/asio/ssl.hpp"
146
#include "boost/bind.hpp"
157
#include "boost/pool/pool.hpp"
16-
#include <boost/optional.hpp>
17-
#include "cpprest/details/basic_types.h"
8+
#include "boost/optional.hpp"
9+
1810
#include "tigerapi/client_config.h"
1911
#include "push_frame_serialize.h"
20-
2112
#include "openapi_pb/pb_source/Request.pb.h"
2213
#include "openapi_pb/pb_source/Response.pb.h"
2314

@@ -27,7 +18,6 @@ namespace TIGER_API
2718
{
2819
CONNECTING, //正在连接
2920
CONNECTED, //已连接
30-
DISCONNECTING, //正在断开
3121
DISCONNECTED //已断开
3222
};
3323

@@ -54,7 +44,7 @@ namespace TIGER_API
5444
void connect();
5545
void disconnect();
5646
bool send_message(const std::string& msg);
57-
uint32_t get_next_id();
47+
unsigned int get_next_id();
5848
private:
5949
void init_socket();
6050
bool verify_certificate(bool preverified,
@@ -64,20 +54,21 @@ namespace TIGER_API
6454
void start_keep_alive();
6555
void send_heart_beat();
6656
void auto_reconnect();
57+
void cancel_reconnect_timer();
6758

6859
void handle_connect(const boost::system::error_code& error,
6960
boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
7061
void handle_handshake(const boost::system::error_code& error);
7162
void handle_write(const boost::system::error_code& error,
7263
size_t bytes_transferred,
73-
unsigned int frame_len);
64+
size_t frame_len);
7465
void handle_read_head(const boost::system::error_code& error,
7566
size_t bytes_transferred);
7667
void handle_read_body(const boost::system::error_code& error,
7768
size_t bytes_transferred,
7869
char* recv_buff,
7970
int page_num,
80-
unsigned int frame_len);
71+
size_t frame_len);
8172
void handle_timer(const boost::system::error_code& error);
8273

8374
void read_head();
@@ -102,17 +93,17 @@ namespace TIGER_API
10293
std::shared_ptr<boost::asio::deadline_timer> reconnect_timer_;
10394
std::atomic<SocketState> socket_state_ = SocketState::DISCONNECTED;
10495

105-
std::atomic<uint32_t> id_counter_ = 0;
96+
std::atomic<unsigned int> id_counter_ = 0;
10697

10798
char head_buff_[1024];
10899
boost::shared_ptr<boost::pool<>> recv_buff_pool_;
109100
TIGER_API::PushFrameDecoder frame_decoder_;
110101
private:
111102
std::time_t last_send_heart_beat_time_ = 0;
112103
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
104+
int reconnect_interval_ = 10 * 1000; //单位:ms
105+
int send_interval_ = 10 * 1000; //单位:ms
106+
int recv_interval_ = 10 * 1000; //单位:ms
116107
};
117108
}
118109
#endif // PUSH_SOCKET_H

src/push_socket/push_client_impl.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "tigerapi/client_config.h"
33
#include "tigerapi/push_socket/push_socket.h"
44
#include "openapi_pb\pb_source\PushData.pb.h"
5-
#include <iostream>
65
#include "google/protobuf/util/json_util.h"
76

87
std::shared_ptr<TIGER_API::PushClientImpl> TIGER_API::PushClientImpl::create_push_client_impl(const TIGER_API::ClientConfig& client_config)
@@ -21,9 +20,8 @@ TIGER_API::PushClientImpl::~PushClientImpl()
2120
}
2221

2322
TIGER_API::PushClientImpl::PushClientImpl(const TIGER_API::ClientConfig& client_config)
24-
:client_config_(client_config)
2523
{
26-
24+
socket_ = PushSocket::create_push_socket(&io_service_, client_config);
2725
}
2826

2927
void TIGER_API::PushClientImpl::connect()
@@ -32,7 +30,7 @@ void TIGER_API::PushClientImpl::connect()
3230
//启动工作线程
3331
worker_thread_ = std::shared_ptr<std::thread>(new std::thread([this]
3432
{
35-
socket_ = PushSocket::create_push_socket(&io_service_, client_config_);
33+
3634
socket_->connect();
3735

3836
LOG(INFO) << "io_service run on work thread";
@@ -48,26 +46,17 @@ void TIGER_API::PushClientImpl::disconnect()
4846

4947
void TIGER_API::PushClientImpl::set_connected_callback(const std::function<void()>& cb)
5048
{
51-
if (socket_)
52-
{
53-
socket_->set_connected_callback(cb);
54-
}
49+
socket_->set_connected_callback(cb);
5550
}
5651

5752
void TIGER_API::PushClientImpl::set_disconnected_callback(const std::function<void()>& cb)
5853
{
59-
if (socket_)
60-
{
61-
socket_->set_disconnected_callback(cb);
62-
}
54+
socket_->set_disconnected_callback(cb);
6355
}
6456

6557
void TIGER_API::PushClientImpl::set_inner_error_callback(const std::function<void(std::string)>& cb)
6658
{
67-
if (socket_)
68-
{
69-
socket_->set_inner_error_callback(cb);
70-
}
59+
socket_->set_inner_error_callback(cb);
7160
}
7261

7362
void TIGER_API::PushClientImpl::set_asset_changed_callback(const std::function<void(const tigeropen::push::pb::AssetData&)>& cb)
@@ -77,7 +66,7 @@ void TIGER_API::PushClientImpl::set_asset_changed_callback(const std::function<v
7766

7867
bool TIGER_API::PushClientImpl::subscribe_asset(const std::string& account)
7968
{
80-
if (!socket_)
69+
if (account.empty())
8170
{
8271
return false;
8372
}
@@ -100,7 +89,7 @@ bool TIGER_API::PushClientImpl::subscribe_asset(const std::string& account)
10089

10190
bool TIGER_API::PushClientImpl::unsubscribe_asset(const std::string& account)
10291
{
103-
if (!socket_)
92+
if (account.empty())
10493
{
10594
return false;
10695
}
@@ -144,18 +133,12 @@ bool TIGER_API::PushClientImpl::send_frame(const tigeropen::push::pb::Request& r
144133

145134
void TIGER_API::PushClientImpl::do_write(const std::string& frame)
146135
{
147-
if (socket_)
148-
{
149-
socket_->send_message(frame);
150-
}
136+
socket_->send_message(frame);
151137
}
152138

153139
void TIGER_API::PushClientImpl::do_disconnect()
154140
{
155-
if (socket_)
156-
{
157-
socket_->disconnect();
158-
}
141+
socket_->disconnect();
159142
}
160143

161144
void TIGER_API::PushClientImpl::on_message(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object)

src/push_socket/push_frame_serialize.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "tigerapi/push_socket/push_frame_serialize.h"
2-
#include <boost/endian/conversion.hpp>
32

43
std::vector<unsigned char> TIGER_API::PushFrameEncoder::encode_frame(const std::string& packed_frame)
54
{
@@ -16,20 +15,20 @@ std::vector<unsigned char> TIGER_API::PushFrameEncoder::encode_frame(const std::
1615
while (pack_size)
1716
{
1817
unsigned char header_byte = 0x80 | bits;
19-
pack_array.push_back(/*boost::endian::native_to_big(header_byte)*/header_byte);
18+
pack_array.push_back(header_byte);
2019
bits = pack_size & 0x7F;
2120
pack_size >>= 7;
2221
}
2322

24-
pack_array.push_back(/*boost::endian::native_to_big(bits)*/bits);
23+
pack_array.push_back(bits);
2524
pack_array.insert(pack_array.end(), packed_frame.begin(), packed_frame.end());
2625
return pack_array;
2726
}
2827

2928
bool TIGER_API::PushFrameDecoder::push_byte(unsigned char value)
3029
{
31-
//逐个字节压入缓冲区(使用大端序列)
32-
frame_header_buffer_.push_back(/*boost::endian::native_to_big(value)*/value);
30+
//逐个字节压入缓冲区
31+
frame_header_buffer_.push_back(value);
3332
if (!(value & 0x80))
3433
{
3534
//完整读取到包长度
@@ -42,12 +41,12 @@ bool TIGER_API::PushFrameDecoder::push_byte(unsigned char value)
4241
return false;
4342
}
4443

45-
uint64_t TIGER_API::PushFrameDecoder::get_frame_size() const
44+
uint32_t TIGER_API::PushFrameDecoder::get_frame_size() const
4645
{
4746
return frame_len_;
4847
}
4948

50-
uint64_t TIGER_API::PushFrameDecoder::decode_varint()
49+
uint32_t TIGER_API::PushFrameDecoder::decode_varint()
5150
{
5251
const uint32_t mask = (1U << 32) - 1; // 32位掩码
5352
uint32_t result = 0;

0 commit comments

Comments
 (0)