Repository navigation
Expand file tree
/
Copy pathsocket_client.cpp
More file actions
108 lines (82 loc) · 1.93 KB
/
Copy pathsocket_client.cpp
File metadata and controls
108 lines (82 loc) · 1.93 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "net/socket_client.h"
#include "net/net_factory.h"
#include <iostream>
using namespace std;
namespace zz {
void socket_client_t::on_open(io_demultiplexer_i* poll_, sock_connection_info& addr_)
{
net_factory_t::global_data.socket_mgr.add_client(this);
poll_->register_fd(this);
m_addr = addr_;
m_io_channel.post_read_req(this);
m_sock_controller->handle_open(this);
/*string hello = "Welcome to echo server !\r\n";
async_send(hello);*/
}
int socket_client_t::do_cleanup()
{
int ret = 0;
net_factory_t::global_data.socket_mgr.remove_client(this);
m_sock_controller->handle_broken(this);
delete this;
return ret;
}
int socket_client_t::handle_read()
{
int ret = 0;
m_tq->produce(task_binder::bind(&socket_client_t::read_impl,this));
return ret;
}
int socket_client_t::handle_write()
{
int ret = 0;
m_tq->produce(task_binder::bind(&socket_client_t::write_impl,this));
return ret;
}
int socket_client_t::handle_error()
{
int ret = 0;
m_tq->produce(task_binder::bind(&socket_client_t::error_impl,this));
return ret;
}
void socket_client_t::close()
{
net_factory_t::global_data.socket_mgr.remove_client(this);
m_tq->produce(task_binder::bind(&socket_client_t::close_impl,this));
}
void socket_client_t::close_impl()
{
if (m_flag_sock_closed == false)
{
closesocket(m_fd_sock);
m_flag_sock_closed = true ;
}
if (m_io_channel.has_pending_req() == false)
{
do_cleanup();
}
}
void socket_client_t::error_impl()
{
m_io_channel.dec_pending_req_num();
close_impl();
}
void socket_client_t::read_impl()
{
m_io_channel.dec_pending_req_num();
string recv_data;
int ret = m_io_channel.read(this,recv_data);
if (ret > 0)
{
/*
std::cout<<"recv data len:"<<ret;
std::cout<<"{recv data}"<<recv_data.c_str();*/
m_sock_controller->handle_read(recv_data.c_str(), recv_data.length(), this);
}
m_io_channel.post_read_req(this);
}
void socket_client_t::write_impl()
{
m_io_channel.write(this, m_send_buff);
}
}