2018-11-20 17:51:25 -05:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/wire"
|
|
|
|
)
|
|
|
|
|
2018-11-23 11:04:53 -05:00
|
|
|
//go:generate genny -in $GOFILE -out streams_map_incoming_bidi.go gen "item=streamI Item=BidiStream streamTypeGeneric=protocol.StreamTypeBidi"
|
|
|
|
//go:generate genny -in $GOFILE -out streams_map_incoming_uni.go gen "item=receiveStreamI Item=UniStream streamTypeGeneric=protocol.StreamTypeUni"
|
2018-11-20 17:51:25 -05:00
|
|
|
type incomingItemsMap struct {
|
|
|
|
mutex sync.RWMutex
|
|
|
|
cond sync.Cond
|
|
|
|
|
|
|
|
streams map[protocol.StreamID]item
|
|
|
|
|
2018-11-23 11:04:53 -05:00
|
|
|
nextStreamToAccept protocol.StreamID // the next stream that will be returned by AcceptStream()
|
|
|
|
nextStreamToOpen protocol.StreamID // the highest stream that the peer openend
|
|
|
|
maxStream protocol.StreamID // the highest stream that the peer is allowed to open
|
|
|
|
maxNumStreams uint64 // maximum number of streams
|
2018-11-20 17:51:25 -05:00
|
|
|
|
|
|
|
newStream func(protocol.StreamID) item
|
2018-11-23 11:04:53 -05:00
|
|
|
queueMaxStreamID func(*wire.MaxStreamsFrame)
|
2018-11-20 17:51:25 -05:00
|
|
|
|
|
|
|
closeErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIncomingItemsMap(
|
2018-11-23 11:04:53 -05:00
|
|
|
nextStreamToAccept protocol.StreamID,
|
2018-11-20 17:51:25 -05:00
|
|
|
initialMaxStreamID protocol.StreamID,
|
2018-11-23 11:04:53 -05:00
|
|
|
maxNumStreams uint64,
|
2018-11-20 17:51:25 -05:00
|
|
|
queueControlFrame func(wire.Frame),
|
|
|
|
newStream func(protocol.StreamID) item,
|
|
|
|
) *incomingItemsMap {
|
|
|
|
m := &incomingItemsMap{
|
2018-11-23 11:04:53 -05:00
|
|
|
streams: make(map[protocol.StreamID]item),
|
|
|
|
nextStreamToAccept: nextStreamToAccept,
|
|
|
|
nextStreamToOpen: nextStreamToAccept,
|
|
|
|
maxStream: initialMaxStreamID,
|
|
|
|
maxNumStreams: maxNumStreams,
|
|
|
|
newStream: newStream,
|
|
|
|
queueMaxStreamID: func(f *wire.MaxStreamsFrame) { queueControlFrame(f) },
|
2018-11-20 17:51:25 -05:00
|
|
|
}
|
|
|
|
m.cond.L = &m.mutex
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *incomingItemsMap) AcceptStream() (item, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var str item
|
|
|
|
for {
|
|
|
|
var ok bool
|
|
|
|
if m.closeErr != nil {
|
|
|
|
return nil, m.closeErr
|
|
|
|
}
|
2018-11-23 11:04:53 -05:00
|
|
|
str, ok = m.streams[m.nextStreamToAccept]
|
2018-11-20 17:51:25 -05:00
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m.cond.Wait()
|
|
|
|
}
|
2018-11-23 11:04:53 -05:00
|
|
|
m.nextStreamToAccept += 4
|
2018-11-20 17:51:25 -05:00
|
|
|
return str, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
|
|
|
|
m.mutex.RLock()
|
|
|
|
if id > m.maxStream {
|
|
|
|
m.mutex.RUnlock()
|
|
|
|
return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
|
|
|
|
}
|
|
|
|
// if the id is smaller than the highest we accepted
|
|
|
|
// * this stream exists in the map, and we can return it, or
|
|
|
|
// * this stream was already closed, then we can return the nil
|
2018-11-23 11:04:53 -05:00
|
|
|
if id < m.nextStreamToOpen {
|
2018-11-20 17:51:25 -05:00
|
|
|
s := m.streams[id]
|
|
|
|
m.mutex.RUnlock()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
m.mutex.RUnlock()
|
|
|
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
// no need to check the two error conditions from above again
|
|
|
|
// * maxStream can only increase, so if the id was valid before, it definitely is valid now
|
|
|
|
// * highestStream is only modified by this function
|
2018-11-23 11:04:53 -05:00
|
|
|
for newID := m.nextStreamToOpen; newID <= id; newID += 4 {
|
2018-11-20 17:51:25 -05:00
|
|
|
m.streams[newID] = m.newStream(newID)
|
|
|
|
m.cond.Signal()
|
|
|
|
}
|
2018-11-23 11:04:53 -05:00
|
|
|
m.nextStreamToOpen = id + 4
|
2018-11-20 17:51:25 -05:00
|
|
|
s := m.streams[id]
|
|
|
|
m.mutex.Unlock()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *incomingItemsMap) DeleteStream(id protocol.StreamID) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
if _, ok := m.streams[id]; !ok {
|
|
|
|
return fmt.Errorf("Tried to delete unknown stream %d", id)
|
|
|
|
}
|
|
|
|
delete(m.streams, id)
|
|
|
|
// queue a MAX_STREAM_ID frame, giving the peer the option to open a new stream
|
2018-11-23 11:04:53 -05:00
|
|
|
if m.maxNumStreams > uint64(len(m.streams)) {
|
|
|
|
numNewStreams := m.maxNumStreams - uint64(len(m.streams))
|
|
|
|
m.maxStream = m.nextStreamToOpen + protocol.StreamID((numNewStreams-1)*4)
|
|
|
|
m.queueMaxStreamID(&wire.MaxStreamsFrame{
|
|
|
|
Type: streamTypeGeneric,
|
|
|
|
MaxStreams: m.maxStream.StreamNum(),
|
|
|
|
})
|
2018-11-20 17:51:25 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *incomingItemsMap) CloseWithError(err error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
m.closeErr = err
|
|
|
|
for _, str := range m.streams {
|
|
|
|
str.closeForShutdown(err)
|
|
|
|
}
|
|
|
|
m.mutex.Unlock()
|
|
|
|
m.cond.Broadcast()
|
|
|
|
}
|