forked from chunyi1994/cppevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpclient.h
56 lines (46 loc) · 1.37 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
#ifndef TcpCONNECTOR_H
#define TcpCONNECTOR_H
#include <string>
#include <memory>
#include "connection.h"
using std::string;
namespace cppevent{
class EventLoop;
class TcpClient
{
public:
TcpClient(EventLoop *loop);
int connect(const string& ip, int port);
void setConnectionCallback(const ConnectionCallback& cb);
void setMessageCallback(const MessageCallback& cb);
void shutdown();
private:
void handleClose(const ConnectionPtr& conn);
EventLoop* loop_;
std::shared_ptr<Connection> connPtr_;
ConnectionCallback connectionCallback_;
MessageCallback messageCallback_;
};
}
//---------tcp client demo-----------------
//int main(){
// EventLoop loop;
// TcpClient client(&loop);
// client.setConnectionCallback([](const ConnectionPtr& conn){
// HttpMessage msg;
// msg.setRequestLine("GET", "/", "HTTP/1.1");
// msg["Host"] = "cn.bing.com"; //msg.addHeaders("Host","cn.bing.com");
// msg["Connection"] = "keep-alive"; //msg.addHeaders("Connection","keep-alive");
// conn->send(msg.toString());
// });
// client.setMessageCallback([](const ConnectionPtr& conn){
// string msg = conn->readAll();
// cout<<msg<<endl;
// });
// string ip;
// int ret = getHostByName("cn.bing.com", ip);
// client.connect(ip, 80);
// loop.loop();
// return 0;
//}
#endif // CONNECTOR_H