-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpClient.cpp
More file actions
85 lines (79 loc) · 2.25 KB
/
TcpClient.cpp
File metadata and controls
85 lines (79 loc) · 2.25 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
#include"TcpClient.h"
#include"EventLoop.h"
#include"net/Socket.h"
#include"base/Logging.h"
#include<functional>
TcpClient::TcpClient(EventLoop* loop,
const InetAddress& serverAddr,
std::string name)
: loop_(loop),
name_(name),
retry_(false),
connect_(false),
nextConnId_(1),
mutex_(),
connector_(new Connector(loop_,serverAddr))
{
connector_->setNewConnectionCallback(std::bind(&TcpClient::newConnection,
this,
std::placeholders::_1));
Log<<"TcpClient::TcpClient["<<name_<<"] \n";
}
TcpClient::~TcpClient()
{
Log<<"TcpClient::~TcpClient["<<name_<<"] \n";
}
void TcpClient::connect()
{
Log<<"TcpClient::connect["<<name_<<"] - connecting to "<<connector_->serverAddress().toIpPort();
connect_=true;
connector_->start();
}
void TcpClient::disconnect()
{
connect_=false;
{
MutexLockGuard lock(mutex_);
if(connection_)
{
connection_->shutdown();
}
}
}
void TcpClient::stop()
{
connect_=false;
connector_->stop();
}
void TcpClient::newConnection(int sockfd)
{
loop_->assertInLoopThread();
InetAddress peer(sockets::getPeerAddr(sockfd));
InetAddress local(sockets::getLocalAddr(sockfd));
char buf[64];
snprintf(buf,sizeof buf,"%s%u",peer.toIpPort().c_str(),nextConnId_);
++nextConnId_;
std::string name=name_+buf;
TcpConnectionPtr conn(new TcpConnection(loop_,name,sockfd,local,peer));
conn->setConnectionCallback(connectioncallback_);
conn->setMessageCallback(messagecallback_);
conn->setCloseCallback(std::bind(&TcpClient::removeConnection,
this,
std::placeholders::_1));
{
MutexLockGuard lock(mutex_);
conn_=conn;
}
conn->connectEstablished();
// printf("success TcpClient::newConnection(int sockfd) \n");
}
void TcpClient::removeConnection(const TcpConnectionPtr& conn)
{
loop_->assertInLoopThread();
{
MutexLockGuard lock(mutex_);
assert(conn_==conn);
}
conn_.reset();
loop_->queueInLoop(std::bind(&TcpConnection::connectDestroyed,conn));
}