-
Notifications
You must be signed in to change notification settings - Fork 7
/
uringnet.go
674 lines (577 loc) · 19.2 KB
/
uringnet.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//go:build linux
// +build linux
package uringnet
import (
"crypto/tls"
"fmt"
socket "github.com/y001j/uringnet/sockets"
"github.com/y001j/uringnet/uring"
"golang.org/x/sys/unix"
"log"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"
)
type URingNet struct {
Addr string // TCP address to listen on, ":http" if empty
Type socket.NetAddressType //the connection type
SocketFd int //listener socket fd
Handler EventHandler // It is used to handle the network event.
TLSConfig *tls.Config // optional TLS config, to support TLS is under development
ReadTimeout time.Duration // maximum duration before timing out read of the request, it will be used to set the socket option
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
Fd atomic.Uintptr
//TLSNextProto map[string]func(*URingNet, *tls.Conn, Handler)
//ConnState func(net.Conn, ConnState)
ErrorLog *log.Logger
disableKeepAlives int32 // accessed atomically.
inShutdown int32
Count uint32
nextProtoOnce sync.Once
nextProtoErr error
ring uring.Ring
userDataList sync.Map // all the userdata
userDataMap map[uint64]*UserData
ReadBuffer []byte
WriteBuffer []byte
Autobuffer [][bufLength]byte // it is just prepared for auto buffer of io_uring
ringloop *Ringloop
mu sync.Mutex
//listeners map[*net.Listener]struct{}
//activeConn map[*conn]struct{} // 活跃连接
//doneChan chan struct{}
onShutdown []func()
//BufferPool sync.Pool
}
type NetAddressType int
type UserdataState uint32
//var UserDataPool = sync.Pool{
// New: func() interface{} {
// return &UserData{}
// },
//}
const (
accepted UserdataState = iota // 0. the socket is accepted, that means the network socket is established
prepareReader // 1. network read is completed
PrepareWriter // 2. network write is completed
closed // 3. the socket is closed.
provideBuffer // 4. buffer has been created.
)
type UserData struct {
id uint64
//resulter chan<- Result
opcode uint8
//ReadBuf []byte
WriteBuf []byte //bytes.Buffer
state uint32 //userdataState
//ringNet *URingNet
Fd int32
Buffer []byte
BufOffset uint64
BufSize int32
// for accept socket
ClientSock *syscall.RawSockaddrAny
socklen *uint32
//Bytebuffer bytes.Buffer
//r0 interface{}
//R1 interface{}
//client unix.RawSockaddrAny
//holds []interface{}
//request *request
}
//var UserDataList sync.Map
// var Buffers [1024][1024]byte
// SetState change the state of unique userdata
func (data *UserData) SetState(state UserdataState) {
atomic.StoreUint32(&data.state, uint32(state))
}
type request struct {
ringNet URingNet
done chan struct{}
}
var increase uint64 = 1
func makeUserData(state UserdataState) *UserData {
defer func() {
err := recover() // 内置函数,可以捕获异常
if err != nil {
fmt.Println("err:", err)
fmt.Println("发生异常............")
}
}()
userData := new(UserData)
//userData := &UserData{
// //ringNet: ringNet,
// state: uint32(state),
//}
userData.state = uint32(state)
userData.id = increase
increase++
return userData
}
// SetUring creates an IO_Uring instance
func (ringNet *URingNet) SetUring(size uint, params *uring.IOUringParams) (ring *uring.Ring, err error) {
thering, err := uring.Setup(size, params)
ringNet.ring = *thering
return thering, err
}
var paraFlags uint32
// Run2 is the core running cycle of io_uring, this function don't use auto buffer.
// TODO: Still don't have the best formula to get buffer size and SQE size.
func (ringNet *URingNet) Run2(ringing uint16) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ringNet.Handler.OnBoot(ringNet)
//var connect_num uint32 = 0
for {
cqe, err := ringNet.ring.GetCQEntry(1)
//defer ringnet.ring.Close()
// have accepted
//theFd := ringnet.Fd.Load()
//if theFd != 0 {
// sqe := ringnet.ring.GetSQEntry()
// ringnet.read(int32(theFd), sqe, ringindex)
// ringnet.Fd.Store(0)
//}
if err != nil {
if err == unix.EAGAIN {
//log.Println("Completion queue is empty!")
continue
}
//log.Println("uring has fatal error! ", err)
continue
}
data, suc := ringNet.userDataList.Load(cqe.UserData())
//data, suc := ringnet.userDataMap[cqe.UserData()]
if !suc {
//log.Println("Cannot find matched userdata!")
//ringnet.ring.Flush()
continue
}
thedata := (data).(*UserData)
//ioc := unix.Iovec{}
//ioc.SetLen(1)
switch thedata.state {
case uint32(provideBuffer):
ringNet.userDataList.Delete(thedata.id)
continue
case uint32(accepted):
ringNet.Handler.OnOpen(thedata)
ringNet.EchoLoop()
Fd := cqe.Result()
//connect_num++
//log.Printf("URing Number: %d Client Conn %d: \n", ringindex, connect_num)
//log.Println("URing Number: ", ringindex, " Client Conn %d:", connect_num)
sqe := ringNet.ring.GetSQEntry()
//claim buffer for read
//buffer := make([]byte, 1024) //ringnet.BufferPool.Get().(*[]byte)
//temp := ringnet.BufferPool.Get()
//bb := temp.(*[]byte)
//ringNet.read2(Fd, sqe)
ringNet.recv(Fd, sqe, ringing)
//ringnet.read(Fd, sqe, ringindex)
ringNet.userDataList.Delete(thedata.id)
continue
//recycle the buffer
//ringnet.BufferPool.Put(thedata.buffer)
//delete(ringnet.userDataMap, thedata.id)
case uint32(prepareReader):
if cqe.Result() <= 0 {
continue
}
//fmt.Println(BytesToString(thedata.Buffer))
//log.Println("the buffer:", BytesToString(thedata.Buffer))
response(ringNet, thedata, ringing, 0)
continue
case uint32(PrepareWriter):
if cqe.Result() <= 0 {
continue
}
ringNet.Handler.OnWritten(*thedata)
ringNet.userDataList.Delete(thedata.id)
continue
case uint32(closed):
ringNet.Handler.OnClose(*thedata)
//delete(ringnet.userDataMap, thedata.id)
ringNet.userDataList.Delete(thedata.id)
}
}
}
// Run is the core running cycle of io_uring, this function will use auto buffer.
func (ringNet *URingNet) Run(ringing uint16) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ringNet.Handler.OnBoot(ringNet)
//var connect_num uint32 = 0
for {
cqe, err := ringNet.ring.GetCQEntry(1)
//defer ringnet.ring.Close()
// have accepted
//theFd := ringnet.Fd.Load()
//if theFd != 0 {
// sqe := ringnet.ring.GetSQEntry()
// ringnet.read(int32(theFd), sqe, ringindex)
// ringnet.Fd.Store(0)
//}
if err != nil {
if err == unix.EAGAIN {
//log.Println("Completion queue is empty!")
continue
}
//log.Println("uring has fatal error! ", err)
continue
}
data, suc := ringNet.userDataList.Load(cqe.UserData())
//data, suc := ringnet.userDataMap[cqe.UserData()]
if !suc {
//log.Println("Cannot find matched userdata!")
//ringnet.ring.Flush()
continue
}
thedata := (data).(*UserData)
//ioc := unix.Iovec{}
//ioc.SetLen(1)
switch thedata.state {
case uint32(provideBuffer):
ringNet.userDataList.Delete(thedata.id)
continue
case uint32(accepted):
ringNet.Handler.OnOpen(thedata)
ringNet.EchoLoop()
Fd := cqe.Result()
//connect_num++
//log.Printf("URing Number: %d Client Conn %d: \n", ringindex, connect_num)
//log.Println("URing Number: ", ringindex, " Client Conn %d:", connect_num)
sqe := ringNet.ring.GetSQEntry()
//claim buffer for read
//buffer := make([]byte, 1024) //ringnet.BufferPool.Get().(*[]byte)
//temp := ringnet.BufferPool.Get()
//bb := temp.(*[]byte)
ringNet.read2(Fd, sqe)
//ringnet.read(Fd, sqe, ringindex)
ringNet.userDataList.Delete(thedata.id)
continue
//recycle the buffer
//ringnet.BufferPool.Put(thedata.buffer)
//delete(ringnet.userDataMap, thedata.id)
case uint32(prepareReader):
if cqe.Result() <= 0 {
continue
}
//log.Println("the buffer:", BytesToString(thedata.Buffer))
offset := uint64(cqe.Flags() >> uring.IORING_CQE_BUFFER_SHIFT)
thedata.Buffer = ringNet.Autobuffer[offset][:]
thedata.BufSize = cqe.Result()
//fmt.Println(BytesToString(thedata.Buffer))
//log.Println("the buffer:", BytesToString(thedata.Buffer))
responseWithBuffer(ringNet, thedata, ringing, offset)
continue
case uint32(PrepareWriter):
if cqe.Result() <= 0 {
continue
}
ringNet.Handler.OnWritten(*thedata)
ringNet.userDataList.Delete(thedata.id)
continue
case uint32(closed):
ringNet.Handler.OnClose(*thedata)
//delete(ringnet.userDataMap, thedata.id)
ringNet.userDataList.Delete(thedata.id)
}
}
}
func (ringNet *URingNet) ShutDown() {
ringNet.ring.Flush()
ringNet.ring.Close()
ringNet.inShutdown = 1
ringNet.ReadBuffer = nil
ringNet.WriteBuffer = nil
ringNet.userDataMap = nil
ringNet.Handler.OnShutdown(ringNet)
}
func response(ringnet *URingNet, data *UserData, gid uint16, offset uint64) {
action := ringnet.Handler.OnTraffic(data, ringnet)
switch action {
case Echo: // Echo: First write and then add another read event into SQEs.
//sqe2 := ringnet.ring.GetSQEntry()
sqe1 := ringnet.ring.GetSQEntry()
//ringnet.write(data, sqe2)
//ringnet.write(data, sqe1)
ringnet.send(data, sqe1, gid)
sqe := ringnet.ring.GetSQEntry()
ringnet.recv(data.Fd, sqe, gid)
//fmt.Println("read is set for uring ", gid)
case Read:
sqe := ringnet.ring.GetSQEntry()
//ringnet.read2(data.Fd, sqe)
ringnet.recv(data.Fd, sqe, gid)
case Write:
sqe1 := ringnet.ring.GetSQEntry()
//ringnet.write(data, sqe1)
ringnet.send(data, sqe1, gid)
_, err := ringnet.ring.Submit(0, ¶Flags)
if err != nil {
fmt.Println("Error Message: ", err)
}
//EchoAndClose type just send a write event into SQEs and then close the socket connection. the write and close event should be linked together.
case EchoAndClose:
sqe2 := ringnet.ring.GetSQEntry()
// claim buffer for I/O write
//bw := ringnet.BufferPool.Get().(*[]byte)
//bw := make([]byte, 1024)
//sqe2.SetFlags(uring.IOSQE_IO_LINK)
//ringnet.write(data, sqe2)
ringnet.send(data, sqe2, gid)
sqe := ringnet.ring.GetSQEntry()
sqe.SetFlags(uring.IOSQE_IO_DRAIN)
ringnet.close(data, sqe)
_, err := ringnet.ring.Submit(0, ¶Flags)
if err != nil {
fmt.Println("Error Message: ", err)
}
case Close:
sqe := ringnet.ring.GetSQEntry()
ringnet.close(data, sqe)
}
// recover kernel buffer; the buffer should be restored after using.
// remove the userdata in this loop
//data.Buffer = nil
//data.WriteBuf = nil
ringnet.userDataList.Delete(data.id)
//delete(ringnet.userDataMap, data.id)
}
// Run is the core running cycle of io_uring, this function will use auto buffer.
func responseWithBuffer(ringnet *URingNet, data *UserData, gid uint16, offset uint64) {
action := ringnet.Handler.OnTraffic(data, ringnet)
switch action {
case Echo: // Echo: First write and then add another read event into SQEs.
sqe1 := ringnet.ring.GetSQEntry()
ringnet.write(data, sqe1)
sqe := ringnet.ring.GetSQEntry()
ringnet.read(data.Fd, sqe, gid)
//fmt.Println("read is set for uring ", gid)
case Read:
sqe := ringnet.ring.GetSQEntry()
ringnet.read(data.Fd, sqe, gid)
case Write:
sqe1 := ringnet.ring.GetSQEntry()
ringnet.write(data, sqe1)
_, err := ringnet.ring.Submit(0, ¶Flags)
if err != nil {
fmt.Println("Error Message: ", err)
}
//EchoAndClose type just send a write event into SQEs and then close the socket connection. the write and close event should be linked together.
case EchoAndClose:
sqe2 := ringnet.ring.GetSQEntry()
ringnet.write(data, sqe2)
sqe := ringnet.ring.GetSQEntry()
sqe.SetFlags(uring.IOSQE_IO_DRAIN)
ringnet.close(data, sqe)
_, err := ringnet.ring.Submit(0, ¶Flags)
if err != nil {
fmt.Println("Error Message: ", err)
}
case Close:
sqe := ringnet.ring.GetSQEntry()
ringnet.addBuffer(offset, gid)
ringnet.close(data, sqe)
}
// recover kernel buffer; the buffer should be restored after using.
ringnet.addBuffer(offset, gid)
// remove the userdata in this loop
ringnet.userDataList.Delete(data.id)
//delete(ringnet.userDataMap, data.id)
}
func (ringNet *URingNet) close(thedata *UserData, sqe *uring.SQEntry) {
data := makeUserData(closed)
data.Fd = thedata.Fd
ringNet.userDataList.Store(data.id, data)
//ringnet.userDataMap[data.id] = data
sqe.SetUserData(data.id)
sqe.SetLen(1)
uring.Close(sqe, uintptr(thedata.Fd))
//return data
}
func (ringNet *URingNet) write(thedata *UserData, sqe2 *uring.SQEntry) {
data1 := makeUserData(PrepareWriter)
data1.Fd = thedata.Fd
//thebuffer := make([]byte, 1024)
//thedata.buffer = thebuffer
//copy(thebuffer, thedata.buffer)
ringNet.userDataList.Store(data1.id, data1)
//ringnet.userDataMap[data1.id] = data1
//ringnet.mu.Unlock()
sqe2.SetUserData(data1.id)
//sqe2.SetFlags(uring.IOSQE_IO_LINK)
uring.Write(sqe2, uintptr(data1.Fd), thedata.WriteBuf)
//uring.write(sqe2, uintptr(data1.Fd), thedata.Buffer) //data.WriteBuf)
//ringnet.ring.Submit(0, ¶Flags)
}
func (ringNet *URingNet) write2(Fd int32, buffer []byte) {
sqe2 := ringNet.ring.GetSQEntry()
data1 := makeUserData(PrepareWriter)
data1.Fd = Fd
//ringnet.userDataMap[data1.id] = data1
ringNet.userDataList.Store(data1.id, data1)
//ringnet.mu.Unlock()
sqe2.SetUserData(data1.id)
uring.Write(sqe2, uintptr(data1.Fd), buffer)
ringNet.ring.Submit(0, ¶Flags)
}
// read method when using auto buffer
func (ringNet *URingNet) read(Fd int32, sqe *uring.SQEntry, ringIndex uint16) {
data2 := makeUserData(prepareReader)
data2.Fd = Fd
//data2.buffer = make([]byte, 1024)
//data2.bytebuffer = buffer
//data2.client = thedata.client
sqe.SetUserData(data2.id)
//ioc := unix.Iovec{}
//ioc.SetLen(1)
//Add read event
sqe.SetFlags(uring.IOSQE_BUFFER_SELECT)
sqe.SetBufGroup(ringIndex)
//uring.Read(sqe, uintptr(data2.Fd), ringnet.ReadBuffer)
uring.ReadNoBuf(sqe, uintptr(Fd), uint32(bufLength))
//ringnet.userDataList.Store(data2.id, data2)
//co := conn{}
//co.fd = data2.Fd
//co.rawSockAddr = sqe.
//ringnet.ringloop.connections.Store(data2.Fd)
//ringnet.userDataMap[data2.id] = data2
ringNet.userDataList.Store(data2.id, data2)
//paraFlags = uring.IORING_SETUP_SQPOLL
ringNet.ring.Submit(0, ¶Flags)
}
func (ringNet *URingNet) recv(Fd int32, sqe *uring.SQEntry, ringIndex uint16) {
data2 := makeUserData(prepareReader)
data2.Fd = Fd
sqe.SetUserData(data2.id)
uring.Recv(sqe, uintptr(Fd), ringNet.ReadBuffer, 0)
ringNet.userDataList.Store(data2.id, data2)
//paraFlags = uring.IORING_ENTER_SQ_WAKEUP
//}
ringNet.ring.Submit(0, ¶Flags)
}
func (ringNet *URingNet) send(thedata *UserData, sqe *uring.SQEntry, ringIndex uint16) {
data2 := makeUserData(PrepareWriter)
data2.Fd = thedata.Fd
sqe.SetUserData(data2.id)
uring.Send(sqe, uintptr(data2.Fd), thedata.WriteBuf, unix.MSG_DONTWAIT|unix.MSG_ZEROCOPY)
ringNet.userDataList.Store(data2.id, data2)
}
func (ringNet *URingNet) read_multi(Fd int32, sqes []*uring.SQEntry, ringIndex uint16) {
data2 := makeUserData(prepareReader)
data2.Fd = Fd
for _, sqe := range sqes {
sqe.SetUserData(data2.id)
//Add read event
sqe.SetFlags(uring.IOSQE_BUFFER_SELECT)
sqe.SetBufGroup(ringIndex)
uring.ReadNoBuf(sqe, uintptr(Fd), uint32(bufLength))
ringNet.userDataList.Store(data2.id, data2)
}
//sqes的长度如何获取:
ringNet.ring.Submit(uint32(len(sqes)), ¶Flags)
}
// this function is used to read data from the network socket without auto buffer.
func (ringNet *URingNet) read2(Fd int32, sqe *uring.SQEntry) {
data2 := makeUserData(prepareReader)
data2.Fd = Fd
sqe.SetUserData(data2.id)
//data2.Buffer = make([]byte, 1024)
//ringnet.userDataMap[data2.id] = data2
ringNet.userDataList.Store(data2.id, data2)
uring.Read(sqe, uintptr(Fd), ringNet.ReadBuffer)
ringNet.ring.Submit(0, ¶Flags)
}
// New Creates a new uRingnNet which is used to
func New(addr NetAddress, size uint, sqpoll bool, options socket.SocketOptions) (*URingNet, error) {
//1. set the socket
//var ringNet *URingNet
ringNet := &URingNet{}
ringNet.userDataMap = make(map[uint64]*UserData)
ops := socket.SetOptions(string(addr.AddrType), options)
switch addr.AddrType {
case socket.Tcp, socket.Tcp4, socket.Tcp6:
ringNet.SocketFd, _, _ = socket.TCPSocket(string(addr.AddrType), addr.Address, true, ops...) //ListenTCPSocket(addr)
case socket.Udp, socket.Udp4, socket.Udp6:
ringNet.SocketFd, _, _ = socket.UDPSocket(string(addr.AddrType), addr.Address, true, ops...)
case socket.Unix:
ringNet.SocketFd, _, _ = socket.UnixSocket(string(addr.AddrType), addr.Address, true, ops...)
default:
ringNet.SocketFd = -1
}
ringNet.Addr = addr.Address
ringNet.Type = addr.AddrType
//ringNet.userDataList = make(sync.Map, 1024)
//Create the io_uring instance
if sqpoll {
ringNet.SetUring(size, &uring.IOUringParams{Flags: uring.IORING_SETUP_SQPOLL | uring.IORING_SETUP_SQ_AFF, SQThreadCPU: 1})
} else {
ringNet.SetUring(size, nil)
}
return ringNet, nil
}
// NewMany Create multiple uring instances
//
// @Description:
// @param addr
// @param size set SQ size
// @param sqpoll if set sqpoll to true, io_uring submit SQs automatically without enter syscall.
// @param num number of io_uring instances need to be created
// @return *[]URingNet
// @return error
func NewMany(addr NetAddress, size uint, sqpoll bool, num int, options socket.SocketOptions, handler EventHandler) ([]*URingNet, error) {
//1. set the socket
var sockfd int
ops := socket.SetOptions(string(addr.AddrType), options)
switch addr.AddrType {
case socket.Tcp, socket.Tcp4, socket.Tcp6:
sockfd, _, _ = socket.TCPSocket(string(addr.AddrType), addr.Address, true, ops...) //ListenTCPSocket(addr)
case socket.Udp, socket.Udp4, socket.Udp6:
sockfd, _, _ = socket.UDPSocket(string(addr.AddrType), addr.Address, true, ops...)
case socket.Unix:
sockfd, _, _ = socket.UnixSocket(string(addr.AddrType), addr.Address, true, ops...)
default:
sockfd = -1
}
uringArray := make([]*URingNet, num) //*URingNet{}
//ringNet.userDataList = make(sync.Map, 1024)
//Create the io_uring instance
for i := 0; i < num; i++ {
uringArray[i] = &URingNet{}
//uringArray[i].userDataMap = make(map[uint64]*UserData)
uringArray[i].ReadBuffer = make([]byte, 1024)
uringArray[i].WriteBuffer = make([]byte, 1024)
uringArray[i].SocketFd = sockfd
uringArray[i].Addr = addr.Address
uringArray[i].Type = addr.AddrType
uringArray[i].Handler = handler
if sqpoll {
uringArray[i].SetUring(size, &uring.IOUringParams{Flags: uring.IORING_SETUP_SQPOLL, Features: uring.IORING_FEAT_NODROP | uring.IORING_FEAT_FAST_POLL | uring.IORING_FEAT_SQPOLL_NONFIXED}) //Features: uring.IORING_FEAT_FAST_POLL|uring.IORING_FEAT_NODROP})
} else {
uringArray[i].SetUring(size, &uring.IOUringParams{Features: uring.IORING_FEAT_FAST_POLL | uring.IORING_FEAT_NODROP})
}
fmt.Println("Uring instance initiated!")
}
return uringArray, nil
}
type NetAddress struct {
AddrType socket.NetAddressType
Address string
}
// addBuffer kernel buffer should be restored after using
func (ringNet *URingNet) addBuffer(offset uint64, gid uint16) {
sqe := ringNet.ring.GetSQEntry()
uring.ProvideSingleBuf(sqe, &ringNet.Autobuffer[offset], 1, uint32(bufLength), gid, offset)
data := makeUserData(provideBuffer)
sqe.SetUserData(data.id)
ringNet.userDataList.Store(data.id, data)
//ringNet.ringloop.ringNet.userDataMap[data.id] = data
//_, _ = ringNet.ring.Submit(0, nil)
}