From 777f5f1ded722155ab250ffc46d3f7be2c2c61ec Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 17 Feb 2017 17:04:04 +0700 Subject: [PATCH] open implicitly opened streams in order --- streams_map.go | 29 +++++++++++++++-------------- streams_map_test.go | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/streams_map.go b/streams_map.go index fac38c3a..b894fc4b 100644 --- a/streams_map.go +++ b/streams_map.go @@ -87,18 +87,25 @@ func (m *streamsMap) GetOrOpenStream(id protocol.StreamID) (*stream, error) { return nil, nil } - highestOpened := m.highestStreamOpenedByPeer - sid := id - // sid is always odd - for sid > highestOpened { + if m.perspective == protocol.PerspectiveServer && id%2 == 0 { + return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d from client-side", id)) + } + if m.perspective == protocol.PerspectiveClient && id%2 == 1 { + return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d from server-side", id)) + } + + // sid is the next stream that will be opened + sid := m.highestStreamOpenedByPeer + 2 + // if there is no stream opened yet, and this is the server, stream 1 should be openend + if sid == 2 && m.perspective == protocol.PerspectiveServer { + sid = 1 + } + + for ; sid <= id; sid += 2 { _, err := m.openRemoteStream(sid) if err != nil { return nil, err } - if sid == 1 { - break - } - sid -= 2 } m.nextStreamOrErrCond.Broadcast() @@ -109,12 +116,6 @@ func (m *streamsMap) openRemoteStream(id protocol.StreamID) (*stream, error) { if m.numIncomingStreams >= m.connectionParameters.GetMaxIncomingStreams() { return nil, qerr.TooManyOpenStreams } - if m.perspective == protocol.PerspectiveServer && id%2 == 0 { - return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d from client-side", id)) - } - if m.perspective == protocol.PerspectiveClient && id%2 == 1 { - return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d from server-side", id)) - } if id+protocol.MaxNewStreamIDDelta < m.highestStreamOpenedByPeer { return nil, qerr.Error(qerr.InvalidStreamID, fmt.Sprintf("attempted to open stream %d, which is a lot smaller than the highest opened stream, %d", id, m.highestStreamOpenedByPeer)) } diff --git a/streams_map_test.go b/streams_map_test.go index d2e4ddac..025b7b11 100644 --- a/streams_map_test.go +++ b/streams_map_test.go @@ -146,7 +146,7 @@ var _ = Describe("Streams Map", func() { _, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1)) Expect(err).NotTo(HaveOccurred()) } - _, err := m.GetOrOpenStream(protocol.StreamID(2*maxNumStreams + 2)) + _, err := m.GetOrOpenStream(protocol.StreamID(2*maxNumStreams + 3)) Expect(err).To(MatchError(qerr.TooManyOpenStreams)) })