Repository navigation
Expand file tree
/
Copy pathconnector.cpp
More file actions
99 lines (75 loc) · 1.88 KB
/
Copy pathconnector.cpp
File metadata and controls
99 lines (75 loc) · 1.88 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
#include "net/io_channel_impl.h"
#include "net/socket_ops.h"
#include "net/socket_client.h"
#include "net/net_factory.h"
#include "base/strtool.h"
#include "net/common_socket_controller.h"
#include "net/connector.h"
#include <iostream>
#include <string>
using namespace std;
namespace zz {
int connector_t::connect(string& host_)
{
int ret = 0;
vector<string> vec_strs;
strtool_t::split(host_,"//",vec_strs);
if ( vec_strs.size() != 2 )
{
return -1;
}
string str_ip_port = vec_strs[1];
strtool_t::split(str_ip_port,":",vec_strs);
if ( vec_strs.size() != 2 )
{
return -1;
}
string addr = vec_strs[0];
unsigned int port = 0;
sscanf_s(vec_strs[1].c_str(), "%d", &port);
m_sock_fd = socket_ops_t::connect(addr.c_str(), port);
if (m_sock_fd == INVALID_SOCKET)
{
ret = -1;
return ret;
}
cout<<"<connector>Á¬½ÓÖ÷»ú:"<<addr.c_str()<<":"<<port<<endl;
on_connect_complete();
return ret;
}
void connector_t::close()
{
closesocket(m_sock_fd);
}
socket_client_t* connector_t::create(int new_fd)
{
return new socket_client_t(m_tqp->random_alloc(new_fd), new_fd, new common_socket_controller_t(m_msg_handler));
}
int connector_t::on_connect_complete()
{
m_socket_client = create(m_sock_fd);
sockaddr_in sa_local = {0};
sockaddr_in sa_remote = {0};
int len = sizeof(sockaddr_in);
int ret = getsockname(m_sock_fd, (sockaddr*)&sa_local, &len);
if (0 != ret)
{
ret = WSAGetLastError();
return ret;
}
ret = getpeername(m_sock_fd, (sockaddr*)&sa_remote, &len);
if (0 != ret)
{
ret = WSAGetLastError();
return ret;
}
sock_connection_info addr_;
addr_.local_addr = inet_ntoa(sa_local.sin_addr);
addr_.local_port = ntohs(sa_local.sin_port);
addr_.remote_addr = inet_ntoa(sa_remote.sin_addr);
addr_.remote_port = ntohs(sa_remote.sin_port);
socket_client_t* client = create(m_sock_fd);
client->on_open(m_iocp_poll,addr_);
return ret;
}
}// namespace zz