-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_bsd.go
93 lines (84 loc) · 1.71 KB
/
loop_bsd.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
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
88
89
90
91
92
93
// +build freebsd dragonfly darwin
package main
import (
"fmt"
"io"
"golang.org/x/sys/unix"
)
func startMainLoop(poller *Poller, idx int) error {
err := poller.Polling(func(fd int, filter int16) error {
err := acceptNewConnection(fd, idx)
if err != nil {
fmt.Println(err.Error())
}
return err
})
return err
}
func (robbin *Robbin) Run(handleEvent func(fd int, filter int16) error) {
for i := 0; i < robbin.total; i++ {
go func(idx int) {
err := robbin.pollers[idx].Polling(handleEvent)
if err != nil {
panic(err)
}
}(i)
}
}
func handleEvent(fd int, filter int16) error {
fdToConn:=fdToConns.Get(fd)
fdToConn.mux.RLock()
conn := fdToConn.fdToConn[fd]
if conn == nil {
fmt.Println("conn is nil")
fdToConn.mux.RUnlock()
return nil
}
fdToConn.mux.RUnlock()
if filter == EVFilterSock {
// fmt.Println("filter close")
_ = loopCloseConn(conn, nil)
return nil
}
if filter == EVFilterWrite {
_, err := conn.write()
if err != nil {
if err == io.EOF {
_ = conn.poller.ModRead(conn.fd)
if !conn.outboundBuffer.IsEmpty() {
_ = conn.poller.ModReadWrite(conn.fd)
}
} else {
fmt.Println(err.Error())
return err
}
}
}
if filter == EVFilterRead {
//fmt.Println("filter read")
_, err := conn.read()
if err != nil {
if err!=unix.EAGAIN{
return err
}
}
if conn.workerBind == false && !conn.inboundBuffer.IsEmpty() {
err = gPool.Submit(func() {
worker := &Worker{conn: conn}
conn.workerBind = true
worker.Start()
})
if err != nil {
panic(err)
}
}
}
return nil
}
func loopRead(conn *Conn, data []byte) (n int, err error) {
rd, err := unix.Read(conn.fd, data)
if err != nil {
return rd, err
}
return rd, nil
}