-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheventloopthread.h
51 lines (41 loc) · 1.19 KB
/
eventloopthread.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
#pragma once
#ifndef EVENTLOOPTHREAD_H
#define EVENTLOOPTHREAD_H
#include <vector>
#include <functional>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include "base/mutex.h"
#include "channel.h"
#include "eventloop.h"
class EventLoopThread
{
public:
typedef std::shared_ptr<Channel> ChannelPtr;
typedef Channel::CallbackType CallbackType;
EventLoopThread();
~EventLoopThread();
EventLoopThread(const EventLoopThread &) = delete;
EventLoopThread & operator=(const EventLoopThread &) = delete;
void SetReadCallback(CallbackType readCallback);
void SetWriteCallback(CallbackType writeCallback);
void SetErrorCallback(CallbackType errorCallback);
void SetThreadId(int threadId);
void PushFd(int fd);
void Loop();
void Quit();
private:
void OnReadHandler(EventLoop & eventLoop, std::shared_ptr<Channel> ptChannel);
private:
int m_pipeFd[2]; // to arouse the sub-thread to add socket fd in m_vecQue to m_eventloop with EPOLLIN event
ChannelPtr m_listenChannel;
Mutex m_mutex; // protected the m_vecQue
std::vector<int> m_vecQue;
EventLoop m_eventLoop;
CallbackType m_readCallback;
CallbackType m_writeCallback;
CallbackType m_errorCallback;
int m_threadId;
};
#endif