socketpoll.h
1.86 KB
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
78
79
80
81
82
83
84
85
86
87
//
// socketpoll.h
// BBAAPIRequest
//
// Created by cairui01 on 2020/05/11.
// Copyright (c) 2020年 baidu. All rights reserved.
//
#ifndef _SOCKSTPOLL_
#define _SOCKSTPOLL_
#include <poll.h>
#include <vector>
#include <map>
#include "unix_socket.h"
#include "socketbreaker.h"
namespace apirequest {
namespace sdt {
struct PollEvent {
friend class SocketPoll;
public:
PollEvent();
bool Readable() const;
bool Writealbe() const;
bool HangUp() const;
bool Error() const;
bool Invalid() const;
void* UserData();
SOCKET FD() const;
private:
pollfd poll_event_;
void* user_data_;
};
class SocketPoll {
public:
SocketPoll(SocketBreaker& _breaker, bool _autoclear = false);
virtual ~SocketPoll();
bool Consign(SocketPoll& _consignor, bool _recover = false);
void AddEvent(SOCKET _fd, bool _read, bool _write, void* _user_data);
void ReadEvent(SOCKET _fd, bool _active);
void WriteEvent(SOCKET _fd, bool _active);
void NullEvent(SOCKET _fd);
void DelEvent(SOCKET _fd);
void ClearEvent();
virtual int Poll();
virtual int Poll(int _msec);
int Ret() const;
int Errno() const;
bool BreakerIsBreak() const;
bool BreakerIsError() const;
bool ConsignReport(SocketPoll& _consignor, int64_t _timeout) const;
const std::vector<PollEvent>& TriggeredEvents() const;
SocketBreaker& Breaker();
private:
SocketPoll(const SocketPoll&);
SocketPoll& operator=(const SocketPoll&);
protected:
SocketBreaker& breaker_;
const bool autoclear_;
std::vector<pollfd> events_;
std::map<SOCKET, void*> events_user_data_;
std::vector<PollEvent> triggered_events_;
int ret_;
int errno_;
};
}} // namespace apirequest::sdt
#endif