streams_map_incoming_bidi.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file was automatically generated by genny.
  2. // Any changes will be lost if this file is regenerated.
  3. // see https://github.com/cheekybits/genny
  4. package quic
  5. import (
  6. "fmt"
  7. "sync"
  8. "github.com/lucas-clemente/quic-go/internal/protocol"
  9. "github.com/lucas-clemente/quic-go/internal/wire"
  10. )
  11. type incomingBidiStreamsMap struct {
  12. mutex sync.RWMutex
  13. cond sync.Cond
  14. streams map[protocol.StreamID]streamI
  15. // When a stream is deleted before it was accepted, we can't delete it immediately.
  16. // We need to wait until the application accepts it, and delete it immediately then.
  17. streamsToDelete map[protocol.StreamID]struct{} // used as a set
  18. nextStreamToAccept protocol.StreamID // the next stream that will be returned by AcceptStream()
  19. nextStreamToOpen protocol.StreamID // the highest stream that the peer openend
  20. maxStream protocol.StreamID // the highest stream that the peer is allowed to open
  21. maxNumStreams uint64 // maximum number of streams
  22. newStream func(protocol.StreamID) streamI
  23. queueMaxStreamID func(*wire.MaxStreamsFrame)
  24. closeErr error
  25. }
  26. func newIncomingBidiStreamsMap(
  27. nextStreamToAccept protocol.StreamID,
  28. initialMaxStreamID protocol.StreamID,
  29. maxNumStreams uint64,
  30. queueControlFrame func(wire.Frame),
  31. newStream func(protocol.StreamID) streamI,
  32. ) *incomingBidiStreamsMap {
  33. m := &incomingBidiStreamsMap{
  34. streams: make(map[protocol.StreamID]streamI),
  35. streamsToDelete: make(map[protocol.StreamID]struct{}),
  36. nextStreamToAccept: nextStreamToAccept,
  37. nextStreamToOpen: nextStreamToAccept,
  38. maxStream: initialMaxStreamID,
  39. maxNumStreams: maxNumStreams,
  40. newStream: newStream,
  41. queueMaxStreamID: func(f *wire.MaxStreamsFrame) { queueControlFrame(f) },
  42. }
  43. m.cond.L = &m.mutex
  44. return m
  45. }
  46. func (m *incomingBidiStreamsMap) AcceptStream() (streamI, error) {
  47. m.mutex.Lock()
  48. defer m.mutex.Unlock()
  49. var id protocol.StreamID
  50. var str streamI
  51. for {
  52. id = m.nextStreamToAccept
  53. var ok bool
  54. if m.closeErr != nil {
  55. return nil, m.closeErr
  56. }
  57. str, ok = m.streams[id]
  58. if ok {
  59. break
  60. }
  61. m.cond.Wait()
  62. }
  63. m.nextStreamToAccept += 4
  64. // If this stream was completed before being accepted, we can delete it now.
  65. if _, ok := m.streamsToDelete[id]; ok {
  66. delete(m.streamsToDelete, id)
  67. if err := m.deleteStream(id); err != nil {
  68. return nil, err
  69. }
  70. }
  71. return str, nil
  72. }
  73. func (m *incomingBidiStreamsMap) GetOrOpenStream(id protocol.StreamID) (streamI, error) {
  74. m.mutex.RLock()
  75. if id > m.maxStream {
  76. m.mutex.RUnlock()
  77. return nil, fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
  78. }
  79. // if the id is smaller than the highest we accepted
  80. // * this stream exists in the map, and we can return it, or
  81. // * this stream was already closed, then we can return the nil
  82. if id < m.nextStreamToOpen {
  83. var s streamI
  84. // If the stream was already queued for deletion, and is just waiting to be accepted, don't return it.
  85. if _, ok := m.streamsToDelete[id]; !ok {
  86. s = m.streams[id]
  87. }
  88. m.mutex.RUnlock()
  89. return s, nil
  90. }
  91. m.mutex.RUnlock()
  92. m.mutex.Lock()
  93. // no need to check the two error conditions from above again
  94. // * maxStream can only increase, so if the id was valid before, it definitely is valid now
  95. // * highestStream is only modified by this function
  96. for newID := m.nextStreamToOpen; newID <= id; newID += 4 {
  97. m.streams[newID] = m.newStream(newID)
  98. m.cond.Signal()
  99. }
  100. m.nextStreamToOpen = id + 4
  101. s := m.streams[id]
  102. m.mutex.Unlock()
  103. return s, nil
  104. }
  105. func (m *incomingBidiStreamsMap) DeleteStream(id protocol.StreamID) error {
  106. m.mutex.Lock()
  107. defer m.mutex.Unlock()
  108. return m.deleteStream(id)
  109. }
  110. func (m *incomingBidiStreamsMap) deleteStream(id protocol.StreamID) error {
  111. if _, ok := m.streams[id]; !ok {
  112. return fmt.Errorf("Tried to delete unknown stream %d", id)
  113. }
  114. // Don't delete this stream yet, if it was not yet accepted.
  115. // Just save it to streamsToDelete map, to make sure it is deleted as soon as it gets accepted.
  116. if id >= m.nextStreamToAccept {
  117. if _, ok := m.streamsToDelete[id]; ok {
  118. return fmt.Errorf("Tried to delete stream %d multiple times", id)
  119. }
  120. m.streamsToDelete[id] = struct{}{}
  121. return nil
  122. }
  123. delete(m.streams, id)
  124. // queue a MAX_STREAM_ID frame, giving the peer the option to open a new stream
  125. if m.maxNumStreams > uint64(len(m.streams)) {
  126. numNewStreams := m.maxNumStreams - uint64(len(m.streams))
  127. m.maxStream = m.nextStreamToOpen + protocol.StreamID((numNewStreams-1)*4)
  128. m.queueMaxStreamID(&wire.MaxStreamsFrame{
  129. Type: protocol.StreamTypeBidi,
  130. MaxStreams: m.maxStream.StreamNum(),
  131. })
  132. }
  133. return nil
  134. }
  135. func (m *incomingBidiStreamsMap) CloseWithError(err error) {
  136. m.mutex.Lock()
  137. m.closeErr = err
  138. for _, str := range m.streams {
  139. str.closeForShutdown(err)
  140. }
  141. m.mutex.Unlock()
  142. m.cond.Broadcast()
  143. }