forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gnet_teamgram.go
90 lines (78 loc) · 1.78 KB
/
gnet_teamgram.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
// Copyright 2024 Teamgram Authors
// All rights reserved.
//
// Author: Benqi ([email protected])
//
package gnet
import (
"github.com/panjf2000/gnet/v2/internal/queue"
"github.com/panjf2000/gnet/v2/pkg/errors"
)
// AsyncWrite - AsyncWrite
func (e Engine) AsyncWrite(connId int64, data []byte) error {
if e.eng == nil {
return errors.ErrEmptyEngine
}
elidx := int(connId >> 48 & 0xffff)
id := uint16(connId >> 32 & 0xffff)
fd := int(connId & 0xffffffff)
e.eng.eventLoops.iterate(func(i int, el *eventloop) bool {
if i == elidx {
_ = el.poller.Trigger(queue.HighPriority, func(_ interface{}) error {
if c := el.connections.getConn(fd); c != nil && c.id == id {
if !c.opened {
return nil
}
c.write(data)
}
return nil
}, nil)
return false
}
return true
})
return nil
}
// Trigger - Trigger
func (e Engine) Trigger(connId int64, cb func(c Conn)) {
if e.eng == nil {
return
}
if cb == nil {
return
}
elidx := int(connId >> 48 & 0xffff)
id := uint16(connId >> 32 & 0xffff)
fd := int(connId & 0xffffffff)
// (queue.LowPriority, func(_ interface{}
e.eng.eventLoops.iterate(func(i int, el *eventloop) bool {
if i == elidx {
_ = el.poller.Trigger(queue.HighPriority, func(_ interface{}) error {
if c := el.connections.getConn(fd); c != nil && c.id == id {
if c.opened {
cb(c)
}
}
return nil
}, nil)
return false
}
return true
})
}
// Iterate - iterate all conns
func (e Engine) Iterate(cb func(c Conn)) {
if e.Validate() != nil {
return
}
e.eng.eventLoops.iterate(func(_ int, el *eventloop) bool {
_ = el.poller.Trigger(queue.LowPriority, func(_ interface{}) error {
el.connections.iterate(func(c *conn) bool {
cb(c)
return true
})
return nil
}, nil)
return true
})
}