fix application bug

This commit is contained in:
Jeff
2019-05-13 11:19:18 +08:00
committed by zryfish
parent 996d6fe4c5
commit 5462f51e65
717 changed files with 87703 additions and 53426 deletions

View File

@@ -1,22 +1,26 @@
package quic
import (
"errors"
"fmt"
"net"
"github.com/lucas-clemente/quic-go/internal/flowcontrol"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
"github.com/lucas-clemente/quic-go/internal/wire"
)
type streamType int
type streamOpenErr struct{ error }
const (
streamTypeOutgoingBidi streamType = iota
streamTypeIncomingBidi
streamTypeOutgoingUni
streamTypeIncomingUni
)
var _ net.Error = &streamOpenErr{}
func (e streamOpenErr) Temporary() bool { return e.error == errTooManyOpenStreams }
func (streamOpenErr) Timeout() bool { return false }
// errTooManyOpenStreams is used internally by the outgoing streams maps.
var errTooManyOpenStreams = errors.New("too many open streams")
type streamsMap struct {
perspective protocol.Perspective
@@ -35,8 +39,8 @@ var _ streamManager = &streamsMap{}
func newStreamsMap(
sender streamSender,
newFlowController func(protocol.StreamID) flowcontrol.StreamFlowController,
maxIncomingStreams int,
maxIncomingUniStreams int,
maxIncomingStreams uint64,
maxIncomingUniStreams uint64,
perspective protocol.Perspective,
version protocol.VersionNumber,
) streamManager {
@@ -45,18 +49,6 @@ func newStreamsMap(
newFlowController: newFlowController,
sender: sender,
}
var firstOutgoingBidiStream, firstOutgoingUniStream, firstIncomingBidiStream, firstIncomingUniStream protocol.StreamID
if perspective == protocol.PerspectiveServer {
firstOutgoingBidiStream = 1
firstIncomingBidiStream = 4 // the crypto stream is handled separately
firstOutgoingUniStream = 3
firstIncomingUniStream = 2
} else {
firstOutgoingBidiStream = 4 // the crypto stream is handled separately
firstIncomingBidiStream = 1
firstOutgoingUniStream = 2
firstIncomingUniStream = 3
}
newBidiStream := func(id protocol.StreamID) streamI {
return newStream(id, m.sender, m.newFlowController(id), version)
}
@@ -67,25 +59,25 @@ func newStreamsMap(
return newReceiveStream(id, m.sender, m.newFlowController(id), version)
}
m.outgoingBidiStreams = newOutgoingBidiStreamsMap(
firstOutgoingBidiStream,
protocol.FirstStream(protocol.StreamTypeBidi, perspective),
newBidiStream,
sender.queueControlFrame,
)
m.incomingBidiStreams = newIncomingBidiStreamsMap(
firstIncomingBidiStream,
protocol.MaxBidiStreamID(maxIncomingStreams, perspective),
protocol.FirstStream(protocol.StreamTypeBidi, perspective.Opposite()),
protocol.MaxStreamID(protocol.StreamTypeBidi, maxIncomingStreams, perspective.Opposite()),
maxIncomingStreams,
sender.queueControlFrame,
newBidiStream,
)
m.outgoingUniStreams = newOutgoingUniStreamsMap(
firstOutgoingUniStream,
protocol.FirstStream(protocol.StreamTypeUni, perspective),
newUniSendStream,
sender.queueControlFrame,
)
m.incomingUniStreams = newIncomingUniStreamsMap(
firstIncomingUniStream,
protocol.MaxUniStreamID(maxIncomingUniStreams, perspective),
protocol.FirstStream(protocol.StreamTypeUni, perspective.Opposite()),
protocol.MaxStreamID(protocol.StreamTypeUni, maxIncomingUniStreams, perspective.Opposite()),
maxIncomingUniStreams,
sender.queueControlFrame,
newUniReceiveStream,
@@ -93,33 +85,6 @@ func newStreamsMap(
return m
}
func (m *streamsMap) getStreamType(id protocol.StreamID) streamType {
if m.perspective == protocol.PerspectiveServer {
switch id % 4 {
case 0:
return streamTypeIncomingBidi
case 1:
return streamTypeOutgoingBidi
case 2:
return streamTypeIncomingUni
case 3:
return streamTypeOutgoingUni
}
} else {
switch id % 4 {
case 0:
return streamTypeOutgoingBidi
case 1:
return streamTypeIncomingBidi
case 2:
return streamTypeOutgoingUni
case 3:
return streamTypeIncomingUni
}
}
panic("")
}
func (m *streamsMap) OpenStream() (Stream, error) {
return m.outgoingBidiStreams.OpenStream()
}
@@ -145,75 +110,78 @@ func (m *streamsMap) AcceptUniStream() (ReceiveStream, error) {
}
func (m *streamsMap) DeleteStream(id protocol.StreamID) error {
switch m.getStreamType(id) {
case streamTypeIncomingBidi:
return m.incomingBidiStreams.DeleteStream(id)
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.DeleteStream(id)
case streamTypeIncomingUni:
switch id.Type() {
case protocol.StreamTypeUni:
if id.InitiatedBy() == m.perspective {
return m.outgoingUniStreams.DeleteStream(id)
}
return m.incomingUniStreams.DeleteStream(id)
case streamTypeOutgoingUni:
return m.outgoingUniStreams.DeleteStream(id)
default:
panic("invalid stream type")
case protocol.StreamTypeBidi:
if id.InitiatedBy() == m.perspective {
return m.outgoingBidiStreams.DeleteStream(id)
}
return m.incomingBidiStreams.DeleteStream(id)
}
panic("")
}
func (m *streamsMap) GetOrOpenReceiveStream(id protocol.StreamID) (receiveStreamI, error) {
switch m.getStreamType(id) {
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.GetStream(id)
case streamTypeIncomingBidi:
return m.incomingBidiStreams.GetOrOpenStream(id)
case streamTypeIncomingUni:
switch id.Type() {
case protocol.StreamTypeUni:
if id.InitiatedBy() == m.perspective {
// an outgoing unidirectional stream is a send stream, not a receive stream
return nil, fmt.Errorf("peer attempted to open receive stream %d", id)
}
return m.incomingUniStreams.GetOrOpenStream(id)
case streamTypeOutgoingUni:
// an outgoing unidirectional stream is a send stream, not a receive stream
return nil, fmt.Errorf("peer attempted to open receive stream %d", id)
default:
panic("invalid stream type")
case protocol.StreamTypeBidi:
if id.InitiatedBy() == m.perspective {
return m.outgoingBidiStreams.GetStream(id)
}
return m.incomingBidiStreams.GetOrOpenStream(id)
}
panic("")
}
func (m *streamsMap) GetOrOpenSendStream(id protocol.StreamID) (sendStreamI, error) {
switch m.getStreamType(id) {
case streamTypeOutgoingBidi:
return m.outgoingBidiStreams.GetStream(id)
case streamTypeIncomingBidi:
return m.incomingBidiStreams.GetOrOpenStream(id)
case streamTypeOutgoingUni:
return m.outgoingUniStreams.GetStream(id)
case streamTypeIncomingUni:
switch id.Type() {
case protocol.StreamTypeUni:
if id.InitiatedBy() == m.perspective {
return m.outgoingUniStreams.GetStream(id)
}
// an incoming unidirectional stream is a receive stream, not a send stream
return nil, fmt.Errorf("peer attempted to open send stream %d", id)
default:
panic("invalid stream type")
case protocol.StreamTypeBidi:
if id.InitiatedBy() == m.perspective {
return m.outgoingBidiStreams.GetStream(id)
}
return m.incomingBidiStreams.GetOrOpenStream(id)
}
panic("")
}
func (m *streamsMap) HandleMaxStreamIDFrame(f *wire.MaxStreamIDFrame) error {
id := f.StreamID
switch m.getStreamType(id) {
case streamTypeOutgoingBidi:
m.outgoingBidiStreams.SetMaxStream(id)
return nil
case streamTypeOutgoingUni:
func (m *streamsMap) HandleMaxStreamsFrame(f *wire.MaxStreamsFrame) error {
if f.MaxStreams > protocol.MaxStreamCount {
return qerr.StreamLimitError
}
id := protocol.MaxStreamID(f.Type, f.MaxStreams, m.perspective)
switch id.Type() {
case protocol.StreamTypeUni:
m.outgoingUniStreams.SetMaxStream(id)
return nil
default:
return fmt.Errorf("received MAX_STREAM_DATA frame for incoming stream %d", id)
case protocol.StreamTypeBidi:
fmt.Printf("")
m.outgoingBidiStreams.SetMaxStream(id)
}
return nil
}
func (m *streamsMap) UpdateLimits(p *handshake.TransportParameters) {
// Max{Uni,Bidi}StreamID returns the highest stream ID that the peer is allowed to open.
// Invert the perspective to determine the value that we are allowed to open.
peerPers := protocol.PerspectiveServer
if m.perspective == protocol.PerspectiveServer {
peerPers = protocol.PerspectiveClient
func (m *streamsMap) UpdateLimits(p *handshake.TransportParameters) error {
if p.MaxBidiStreams > protocol.MaxStreamCount || p.MaxUniStreams > protocol.MaxStreamCount {
return qerr.StreamLimitError
}
m.outgoingBidiStreams.SetMaxStream(protocol.MaxBidiStreamID(int(p.MaxBidiStreams), peerPers))
m.outgoingUniStreams.SetMaxStream(protocol.MaxUniStreamID(int(p.MaxUniStreams), peerPers))
// Max{Uni,Bidi}StreamID returns the highest stream ID that the peer is allowed to open.
m.outgoingBidiStreams.SetMaxStream(protocol.MaxStreamID(protocol.StreamTypeBidi, p.MaxBidiStreams, m.perspective))
m.outgoingUniStreams.SetMaxStream(protocol.MaxStreamID(protocol.StreamTypeUni, p.MaxUniStreams, m.perspective))
return nil
}
func (m *streamsMap) CloseWithError(err error) {