-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClient.h
61 lines (46 loc) · 1.28 KB
/
TcpClient.h
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
#ifndef TCPCLIENT_H_
#define TCPCLIENT_H_
#include"Connector.h"
#include"base/noncopyable.h"
#include"TcpConnection.h"
#include"base/mutex.h"
#include<atomic>
class EventLoop;
class TcpClient:noncopyable
{
public:
TcpClient(EventLoop* loop,const InetAddress& serverAddr,std::string name);
~TcpClient();
void connect();
void disconnect();
void stop();
TcpConnectionPtr connection() const
{ MutexLockGuard lock(mutex_); return conn_; }
EventLoop* getLoop()const
{ return loop_; }
bool retry()const
{ return retry_; }
void enableRetry()
{ retry_=true; }
const std::string& name()const
{ return name_; }
void setConnectionCallback(const ConnectionCallback& cb)
{ connectioncallback_=cb; }
void setMessageCallback(const MessageCallback& cb)
{ messagecallback_=cb; }
private:
void newConnection(int sockfd);
void removeConnection(const TcpConnectionPtr& conn);
EventLoop* loop_;
TcpConnectionPtr connection_;
std::string name_;
ConnectionCallback connectioncallback_;
MessageCallback messagecallback_;
std::atomic<bool> retry_;
std::atomic<bool> connect_;
int nextConnId_;
mutable MutexLock mutex_;
TcpConnectionPtr conn_;
std::shared_ptr<Connector> connector_;
};
#endif