Skip to content

Commit 8f37257

Browse files
committed
Merge branch 'feature_tcp_client' into feature_tcp_client_sukai
2 parents 5301bf9 + b3b03a7 commit 8f37257

File tree

3 files changed

+12
-49
lines changed

3 files changed

+12
-49
lines changed

include/tigerapi/push_socket/push_socket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ namespace TIGER_API
2323
{
2424
enum class SocketState
2525
{
26+
CONNECTING,
27+
CONNECTED,
28+
DISCONNECTED
2629
CONNECTING, //
2730
CONNECTED, //
2831
DISCONNECTED //
@@ -89,6 +92,7 @@ namespace TIGER_API
8992
private:
9093
std::function<void()> connected_callback_;
9194
std::function<void()> disconnected_callback_;
95+
std::function<void(std::string)> on_inner_error_callback_;
9296
std::function<void(const std::shared_ptr<tigeropen::push::pb::Response>& response_pb_object)> on_message_callback_;
9397
std::function<void(std::string)> on_inner_error_callback_;
9498

src/push_socket/push_frame_serialize.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ std::vector<unsigned char> TIGER_API::PushFrameEncoder::encode_frame(const std::
2727

2828
bool TIGER_API::PushFrameDecoder::push_byte(unsigned char value)
2929
{
30-
//逐个字节压入缓冲区
3130
frame_header_buffer_.push_back(value);
3231
if (!(value & 0x80))
3332
{
34-
//完整读取到包长度
3533
frame_len_ = decode_varint();
3634
frame_header_buffer_.clear();
37-
//frame头部已经读取完毕
3835
return true;
3936
}
40-
//frame头部还有数据,需要继续独取头部数据
4137
return false;
4238
}
4339

@@ -48,7 +44,7 @@ uint32_t TIGER_API::PushFrameDecoder::get_frame_size() const
4844

4945
uint32_t TIGER_API::PushFrameDecoder::decode_varint()
5046
{
51-
const uint32_t mask = (1U << 32) - 1; // 32位掩码
47+
const uint32_t mask = (1U << 32) - 1;
5248
uint32_t result = 0;
5349
int shift = 0;
5450

@@ -62,6 +58,6 @@ uint32_t TIGER_API::PushFrameDecoder::decode_varint()
6258
}
6359
}
6460

65-
result &= mask; // 应用32位掩码
61+
result &= mask;
6662
return result;
6763
}

src/push_socket/push_socket.cpp

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ TIGER_API::PushSocket::PushSocket(boost::asio::io_service* io_service,
2828
send_interval_ = client_config_.send_interval;
2929
recv_interval_ = client_config_.receive_interval;
3030

31-
//��ʼ���ڴ�أ���ֹ��������Ƶ�����ڴ������ͷ����������������ڴ���Ƭ����
3231
recv_buff_pool_.reset(new boost::pool<>(MEMORY_POOL_PAGE_SIZE, MEMORY_POOL_BLOCK_NUM));
33-
34-
//����һ�����ʱ��
3532
keep_alive_timer_ = std::make_shared<boost::asio::deadline_timer>(*io_service);
36-
37-
//����������ʱ��
3833
reconnect_timer_ = std::make_shared<boost::asio::deadline_timer>(*io_service);
3934
}
4035

@@ -60,9 +55,7 @@ void TIGER_API::PushSocket::set_inner_error_callback(const std::function<void(st
6055

6156
void TIGER_API::PushSocket::connect()
6257
{
63-
//���������ⶨʱ����
6458
start_keep_alive();
65-
6659
try
6760
{
6861
socket_state_ = SocketState::CONNECTING;
@@ -71,8 +64,7 @@ void TIGER_API::PushSocket::connect()
7164
boost::asio::ip::tcp::resolver::query query(utility::conversions::to_utf8string(client_config_.get_socket_url()),
7265
utility::conversions::to_utf8string(client_config_.get_socket_port()));
7366
boost::asio::ip::tcp::resolver::iterator rit = resolver.resolve(query);
74-
75-
//��ӡdns����֮���IP��ַ
67+
7668
std::string str_target_server_ip = rit->endpoint().address().to_string();
7769
LOG(INFO) << "resolved ip: " << str_target_server_ip;
7870

@@ -84,7 +76,6 @@ void TIGER_API::PushSocket::connect()
8476
catch (const boost::system::system_error& e)
8577
{
8678
LOG(ERROR) << e.what();
87-
//dns����ʧ��/�쳣����״̬�޸�Ϊ��DISCONNECTED
8879
socket_state_ = SocketState::DISCONNECTED;
8980
}
9081
}
@@ -110,11 +101,9 @@ bool TIGER_API::PushSocket::send_message(const std::string& msg)
110101
return false;
111102
}
112103

113-
//����
114104
TIGER_API::PushFrameEncoder encoder;
115105
std::vector<unsigned char> data = encoder.encode_frame(msg);
116106

117-
//�첽����
118107
boost::asio::async_write(*socket_,
119108
boost::asio::buffer(data, data.size()),
120109
boost::bind(&PushSocket::handle_write, this,
@@ -125,7 +114,7 @@ bool TIGER_API::PushSocket::send_message(const std::string& msg)
125114

126115
void TIGER_API::PushSocket::init_socket()
127116
{
128-
//����ssl�����ģ�ָ��ssl�汾
117+
//set ssl version
129118
boost::asio::ssl::context ssl_content(boost::asio::ssl::context::sslv23);
130119
#if 1
131120
ssl_content.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::single_dh_use);
@@ -134,12 +123,10 @@ void TIGER_API::PushSocket::init_socket()
134123
#endif
135124
if (client_config_.socket_ca_certs.empty())
136125
{
137-
//����֤����֤
138126
ssl_content.set_verify_mode(boost::asio::ssl::verify_none);
139127
}
140128
else
141129
{
142-
//��֤֤��
143130
ssl_content.set_verify_mode(boost::asio::ssl::verify_peer);
144131
ssl_content.set_verify_mode(boost::asio::ssl::context::verify_peer
145132
| boost::asio::ssl::context::verify_fail_if_no_peer_cert);
@@ -213,15 +200,13 @@ void TIGER_API::PushSocket::close_session()
213200

214201
void TIGER_API::PushSocket::send_authentication()
215202
{
216-
//��tiger_id����private_key����ǩ��
203+
//tiger_id sign by private_key
217204
utility::string_t sign = Utils::get_sign(client_config_.private_key, client_config_.tiger_id);
218-
219-
// ����һ�� Request ����
205+
220206
tigeropen::push::pb::Request request;
221207
request.set_command(tigeropen::push::pb::SocketCommon_Command_CONNECT);
222208
request.set_id(get_next_id());
223209

224-
// ����һ�� Request_Connect ����
225210
tigeropen::push::pb::Request_Connect* connect_request = request.mutable_connect();
226211
connect_request->set_tigerid(utility::conversions::to_utf8string(client_config_.tiger_id));
227212
connect_request->set_sign(utility::conversions::to_utf8string(sign));
@@ -231,7 +216,6 @@ void TIGER_API::PushSocket::send_authentication()
231216
connect_request->set_receiveinterval(client_config_.receive_interval);
232217
connect_request->set_usefulltick(client_config_.user_full_tick);
233218

234-
//���л�pb�����ַ���
235219
std::string packed_frame = request.SerializeAsString();
236220
if (!packed_frame.empty())
237221
{
@@ -302,7 +286,7 @@ void TIGER_API::PushSocket::handle_connect(const boost::system::error_code& erro
302286
if (!error)
303287
{
304288
LOG(INFO) << "connect success";
305-
//ssl���ӳɹ�֮����Ҫasync_handshake()���ɹ�֮����ܷ����첽��д��
289+
//start handshake
306290
socket_->async_handshake(boost::asio::ssl::stream_base::client,
307291
boost::bind(&PushSocket::handle_handshake, this,
308292
boost::asio::placeholders::error));
@@ -319,8 +303,6 @@ void TIGER_API::PushSocket::handle_connect(const boost::system::error_code& erro
319303
{
320304
LOG(ERROR) << "[connect failed]: " << error;
321305
dispatch_inner_error_callback(error.message());
322-
323-
//����ʧ�ܹرջỰ
324306
close_session();
325307
}
326308
}
@@ -339,27 +321,19 @@ void TIGER_API::PushSocket::handle_handshake(const boost::system::error_code& er
339321
if (!error)
340322
{
341323
LOG(INFO) << "handshake success";
342-
343-
//���ֳɹ�֮������״̬ΪCONNECTED
344324
socket_state_ = SocketState::CONNECTED;
345-
346-
//����socketѡ��
347325
socket_->lowest_layer().set_option(boost::asio::ip::tcp::acceptor::linger(true, 0));
348326
socket_->lowest_layer().set_option(boost::asio::socket_base::keep_alive(true));
349-
350-
//�����첽�����񣬱���IOѭ��������
327+
351328
read_head();
352329

353-
//������֤
354330
send_authentication();
355331

356-
//���ӻص���ֻ�н������ӳɹ�֮��Ż�ص�
357332
dispatch_connected_callback();
358333
}
359334
else
360335
{
361336
LOG(ERROR) << "[handshake failed]: " << error;
362-
//����ʧ�ܹرջỰ
363337
dispatch_inner_error_callback(error.message());
364338
close_session();
365339
}
@@ -395,7 +369,6 @@ void TIGER_API::PushSocket::handle_read_head(const boost::system::error_code& er
395369
else
396370
{
397371
#if 1
398-
// ѭ����ӡÿ���ֽڵĶ�����ֵ
399372
for (size_t i = 0; i < bytes_transferred; ++i)
400373
{
401374
std::bitset<8> binary(head_buff_[i]);
@@ -405,13 +378,11 @@ void TIGER_API::PushSocket::handle_read_head(const boost::system::error_code& er
405378
last_io_time_ = time(nullptr);
406379
if (frame_decoder_.push_byte(head_buff_[0]))
407380
{
408-
//ͷ���Ѿ���ȡ��ϣ���ʼ��ȡbody
409381
auto frame_len = frame_decoder_.get_frame_size();
410382
read_body(frame_len);
411383
}
412384
else
413385
{
414-
//������ȡͷ��
415386
read_head();
416387
}
417388
}
@@ -458,17 +429,14 @@ void TIGER_API::PushSocket::handle_read_body(const boost::system::error_code& er
458429

459430
if (on_message_callback_)
460431
{
461-
//�·�����
462432
on_message_callback_(response_pb_object);
463433
}
464434

465-
//���滹���ڴ��
466435
if (recv_buff)
467436
{
468437
recv_buff_pool_->ordered_free(recv_buff, page_num);
469438
}
470439

471-
//������ȡ��һ������
472440
read_head();
473441
}
474442

@@ -479,12 +447,10 @@ void TIGER_API::PushSocket::handle_timer(const boost::system::error_code& error)
479447
{
480448
if (socket_state_ == SocketState::CONNECTED)
481449
{
482-
// ����Ƿ���Ҫ��������
483450
if (now_time - last_send_heart_beat_time_ > send_interval_ / 1000)
484451
{
485452
send_heart_beat();
486453
}
487-
//������״̬�Ƿ�೤ʱ��û���յ����ݣ��Ͽ�����
488454
if (now_time - last_io_time_ > recv_interval_ / 1000)
489455
{
490456
LOG(ERROR) << "heart beat timeout";
@@ -497,8 +463,6 @@ void TIGER_API::PushSocket::handle_timer(const boost::system::error_code& error)
497463
cancel_reconnect_timer();
498464
auto_reconnect();
499465
}
500-
501-
// �ٴ�������ʱ��
502466
start_keep_alive();
503467
}
504468
else
@@ -557,7 +521,6 @@ void TIGER_API::PushSocket::message_filter(const std::shared_ptr<tigeropen::push
557521
{
558522
if (response_pb_object->command() == tigeropen::push::pb::SocketCommon_Command_CONNECTED)
559523
{
560-
//���ӳɹ���Ϣ
561524
const std::string& str_msg = response_pb_object->msg();
562525
if (str_msg.find(CONNECTED_HEART_BEAT_CFG_KEY) != std::wstring::npos)
563526
{

0 commit comments

Comments
 (0)