-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtype.go
More file actions
213 lines (188 loc) · 5.49 KB
/
Copy pathdtype.go
File metadata and controls
213 lines (188 loc) · 5.49 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package aionet
import (
"fmt"
"math"
"time"
"github.com/pcbuildpluscoding/aionet/dtype"
tpt "github.com/pcbuildpluscoding/transport"
)
// ==================================================================//
// fdOpen
// ==================================================================
type fdOpen struct {
fd int
err error
pname string
}
// ==================================================================
func (x fdOpen) tell() (int, error, string) {
return x.fd, x.err, x.pname
}
// ==================================================================//
// Listener
// ==================================================================//
type Listener interface {
Accept() (*HdpConn, error)
}
// ==================================================================//
// Dialer
// ==================================================================//
type Dialer interface {
Dial(dtype.HdpEvent) (*HdpConn, error)
}
// ==================================================================//
// Statet
// ==================================================================//
type Statet func(dtype.HdpEvent) (Statet, dtype.HdpEvent)
// ================================================================//
// Timer
// ================================================================//
type funcTimeout func(uint32) func()
type Timer map[uint32]timer
// ---------------------------------------------------------------//
// New
// ---------------------------------------------------------------//
func (tr Timer) New(timeout uint16, req dtype.HdpEvent, f funcTimeout) uint32 {
key := uint32(time.Now().UnixMicro() % math.MaxUint32)
t := timer{}
if timeout > 0 {
dura := time.Duration(timeout) * time.Millisecond
t.t = time.AfterFunc(dura, f(key))
}
t.f = func() dtype.HdpEvent { return req }
tr[key] = t
return key
}
// ---------------------------------------------------------------//
// stopTimer
// ---------------------------------------------------------------//
func (tr Timer) Stop(key uint32) (res dtype.HdpEvent) {
// key is a timestamp
if _, found := tr[key]; !found {
return res.With1(fmt.Errorf("Timer[%s] does not exist", time.UnixMilli(int64(key)).Format(tpt.MilliDatestamp)))
}
t := tr[key]
if t.t != nil {
t.t.Stop()
}
if t.f == nil {
panic(fmt.Errorf("Timer error : resume func is nil"))
}
delete(tr, key)
return t.f()
}
// ================================================================//
// Timer
// ================================================================//
type timer struct {
t *time.Timer
f func() dtype.HdpEvent
}
// ---------------------------------------------------------------//
// New
// ---------------------------------------------------------------//
func (t *timer) new(timeout uint16, req dtype.HdpEvent, f func()) {
if timeout > 0 {
dura := time.Duration(timeout) * time.Millisecond
t.t = time.AfterFunc(dura, f)
}
t.f = func() dtype.HdpEvent { return req }
}
// ---------------------------------------------------------------//
// stopTimer
// ---------------------------------------------------------------//
func (t *timer) stop() dtype.HdpEvent {
if t.t != nil {
t.t.Stop()
}
if t.f == nil {
panic(fmt.Errorf("Timer error : resume func is nil"))
}
return t.f()
}
// ==================================================================//
// EpollEvent
// ==================================================================//
type EpollEvent struct {
cid string
fd uint32
flags ioMode
}
// ==================================================================//
// ioEvent
// ==================================================================//
type ioEvent struct {
ch chan error
dlRef uint16
epoll EpollEvent
err error
pipeName string
timedAt time.Time
}
// ==================================================================//
// interrupt
// ==================================================================//
type interrupt struct {
aux any
code int
err error
}
func (i *interrupt) Error() string {
return i.err.Error()
}
// ==================================================================//
// reqRef
// ==================================================================//
type reqRef struct {
cid string
fd uint32
flags ioMode
deadline time.Time
pipeName string
}
// ==================================================================//
// ioReq
// ==================================================================//
type ioReq struct {
aux []any
ch chan error
op int
ref reqRef
}
// ==================================================================
func (r *ioReq) sendError(flags ioMode, err error) func() {
ch := r.ch
r.ch = nil
return func() {
if ch == nil {
return
}
dura := time.Duration(100) * time.Microsecond
select {
case <-time.After(dura):
logger.Errorf("%s |%s| result was not consumed before deadline, error : %v ...", r.ref.cid, flags.String(), err)
case ch <- err:
}
}
}
type Void struct{}
// ==================================================================//
// pipeAddr
// ==================================================================//
type pipeAddr struct {
emtype string // emulated network type
fd int
name string
}
func (p pipeAddr) Network() string {
return p.emtype
}
func (p pipeAddr) String() string {
return fmt.Sprintf("%d:%s", p.fd, p.name)
}
// ==================================================================
type UniconnReq struct {
cid, pollsubject, serverAddr string
mode int
resultCh chan dtype.HdpEvent
}