package quic
import (
"net"
"github.com/quic-go/quic-go/internal/protocol"
)
type sender interface {
Send(p *packetBuffer , gsoSize uint16 , ecn protocol .ECN )
SendProbe(*packetBuffer , net .Addr )
Run() error
WouldBlock() bool
Available() <-chan struct {}
Close()
}
type queueEntry struct {
buf *packetBuffer
gsoSize uint16
ecn protocol .ECN
}
type sendQueue struct {
queue chan queueEntry
closeCalled chan struct {}
runStopped chan struct {}
available chan struct {}
conn sendConn
}
var _ sender = &sendQueue {}
const sendQueueCapacity = 8
func newSendQueue(conn sendConn ) sender {
return &sendQueue {
conn : conn ,
runStopped : make (chan struct {}),
closeCalled : make (chan struct {}),
available : make (chan struct {}, 1 ),
queue : make (chan queueEntry , sendQueueCapacity ),
}
}
func (h *sendQueue ) Send (p *packetBuffer , gsoSize uint16 , ecn protocol .ECN ) {
select {
case h .queue <- queueEntry {buf : p , gsoSize : gsoSize , ecn : ecn }:
if len (h .queue ) == sendQueueCapacity {
select {
case <- h .available :
default :
}
}
case <- h .runStopped :
default :
panic ("sendQueue.Send would have blocked" )
}
}
func (h *sendQueue ) SendProbe (p *packetBuffer , addr net .Addr ) {
h .conn .WriteTo (p .Data , addr )
}
func (h *sendQueue ) WouldBlock () bool {
return len (h .queue ) == sendQueueCapacity
}
func (h *sendQueue ) Available () <-chan struct {} {
return h .available
}
func (h *sendQueue ) Run () error {
defer close (h .runStopped )
var shouldClose bool
for {
if shouldClose && len (h .queue ) == 0 {
return nil
}
select {
case <- h .closeCalled :
h .closeCalled = nil
shouldClose = true
case e := <- h .queue :
if err := h .conn .Write (e .buf .Data , e .gsoSize , e .ecn ); err != nil {
if !isSendMsgSizeErr (err ) {
return err
}
}
e .buf .Release ()
select {
case h .available <- struct {}{}:
default :
}
}
}
}
func (h *sendQueue ) Close () {
close (h .closeCalled )
<-h .runStopped
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .