implement a send queue to send packet asynchronously

This commit is contained in:
Marten Seemann 2019-08-20 12:00:05 +07:00
parent f4621e280e
commit 00c19f7241
4 changed files with 351 additions and 221 deletions

39
send_queue.go Normal file
View file

@ -0,0 +1,39 @@
package quic
type sendQueue struct {
queue chan *packedPacket
closeChan chan struct{}
conn connection
}
func newSendQueue(conn connection) *sendQueue {
s := &sendQueue{
conn: conn,
closeChan: make(chan struct{}),
queue: make(chan *packedPacket, 1),
}
return s
}
func (h *sendQueue) Send(p *packedPacket) {
h.queue <- p
}
func (h *sendQueue) Run() error {
var p *packedPacket
for {
select {
case <-h.closeChan:
return nil
case p = <-h.queue:
}
if err := h.conn.Write(p.raw); err != nil {
return err
}
p.buffer.Release()
}
}
func (h *sendQueue) Close() {
close(h.closeChan)
}