/
Client.cpp
59 lines (48 loc) · 1.19 KB
/
Client.cpp
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
#include "Client.h"
namespace dyc
{
Client::Client()
: mMesgId(0)
{
mEpoller = NEW Epoller();
mLoop = NEW EventLoop(mEpoller);
mSock = NEW Socket(false);
}
Client::~Client()
{
mLoop->quit();
pthread_join(mTid, NULL);
DELETE(mConnection);
DELETE(mSock);
DELETE(mLoop);
DELETE(mEpoller);
}
int Client::connect(const InetAddress& addr) {
if (mEpoller == NULL || mLoop == NULL || mSock == NULL) {
FATAL_LOG("new obj failed");
return false;
}
mEpoller->createEpoll();
mConnection = NEW Connection(mSock, mLoop);
mEpoller->addRW(mConnection);
return mSock->connect(addr);
}
void Client::start() {
pthread_create(&mTid, NULL, thr_fn, mLoop);
}
void* Client::thr_fn(void* data) {
EventLoop* p = (EventLoop*)(data);
p->loop();
}
int64_t Client::send(const char* data, int64_t size) {
Buffer* buffer = new Buffer(data, (size_t)size);
buffer->setMesgId(mMesgId);
DelayFunctor func = boost::bind(&Connection::addBufferToSendQueue, mConnection, buffer);
++mMesgId;
mLoop->queueInLoop(func);
return mMesgId;
}
int64_t Client::send(const std::string& str) {
return send(str.c_str(), str.size());
}
}