-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_eventlisten.cpp
77 lines (67 loc) · 1.95 KB
/
t_eventlisten.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "t_eventlisten.hpp"
#include "t_tcpsrv.hpp"
namespace T_TCP
{
ListenConn::ListenConn(int ifd, void* pData):ConnBase(ifd), m_pData((TcpSrv*)pData)
{
}
ListenConn::~ListenConn()
{
}
bool ListenConn::AddEvent(struct event_base *pEvBase, int iEvent, void *arg)
{
if (pEvBase == NULL)
{
return false;
}
if (EV_READ & iEvent)
{
if ( 0 != event_assign(&m_stREvent, pEvBase, m_iFd, iEvent, ReadCallBack, this))
{
std::cout << "event_assign() fail for read in listenconn" << std::endl;
return false;
}
if ( 0 != event_add(&m_stREvent, NULL) )
{
std::cout << "event_add() fail for read in listenconn" << std::endl;
return false;
}
std::cout << "add event succ in listenconn (2)" << std::endl;
return true;
}
else if (iEvent & EV_WRITE)
{
event_assign(&m_stWEvent, pEvBase, m_iFd, iEvent, WriteCallBack, this);
event_add(&m_stWEvent, NULL);
return true;
}
else
{
}
return false;
}
void ListenConn::WriteCallBack(int iFd, short sEvent, void *pData)
{
//listen conn need not to write event.
}
void ListenConn::ReadCallBack(int iFd, short sEvent, void *pData)
{
ListenConn* pConn = (ListenConn*) pData;
if (pConn == NULL)
{
return;
}
std::cout << "listen conn recv read event, client new conn" << std::endl;
pConn->DoRead();
}
bool ListenConn::DoRead()
{
TcpSrv* tcpSrv = (TcpSrv*) m_pData;
return tcpSrv->Accept();
}
bool ListenConn::DelEvent(struct event_base *pEvBase, int iEvent, void *arg)
{
std::cout << "call default op in DelEvent" << std::endl;
return true;
}
}