-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll_nomock.go
54 lines (44 loc) · 1.33 KB
/
poll_nomock.go
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
//go:build linux && slimcap_nomock
// +build linux,slimcap_nomock
package event
import (
"github.com/fako1024/slimcap/capture/afpacket/socket"
"golang.org/x/sys/unix"
)
// Handler wraps a socket file descriptor and an event file descriptor in a single
// instance
type Handler struct {
// Efd denotes the event file descriptor of this handler
Efd EvtFileDescriptor
// Fd denotes the socket file descriptor of this handler
Fd socket.FileDescriptor
}
// Poll polls (blocking, hence no timeout) for events on the file descriptor and the event
// file descriptor (waiting for a POLLIN event).
func (p *Handler) Poll(events int16) (hasEvent bool, errno unix.Errno) {
pollEvents := [...]unix.PollFd{
{
Fd: int32(p.Efd),
Events: unix.POLLIN,
},
{
Fd: int32(p.Fd),
Events: events,
},
}
// Perform blocking PPOLL
errno = pollBlock(&pollEvents[0])
if errno == 0 && pollEvents[1].Revents&eventConnReset != 0 {
errno = unix.ECONNRESET
}
hasEvent = pollEvents[0].Revents&eventPollIn != 0
return
}
// Recvfrom retrieves data directly from the socket
func (p *Handler) Recvfrom(buf []byte, flags int) (int, uint8, error) {
return p.recvfrom(buf, flags)
}
// GetSocketStats returns (and resets) socket / traffic statistics
func (p *Handler) GetSocketStats() (socket.TPacketStats, error) {
return p.Fd.GetSocketStats()
}