github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/gp/streams.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package gp
     4  
     5  import (
     6  	"sync"
     7  
     8  	"../protocol"
     9  )
    10  
    11  // StreamPool set & get streams in a pool by ID!
    12  type StreamPool struct {
    13  	mutex               sync.Mutex                 // TODO::: it is not efficient way and need more work
    14  	p                   map[uint32]protocol.Stream // key is Stream.ID
    15  	freeIncomeStreamID  uint32
    16  	freeOutcomeStreamID uint32
    17  	totalOpenedStreams  uint32 // Manifest.TechnicalInfo.MaxStreamConnectionDaily
    18  }
    19  
    20  // Init initialize the pool
    21  func (sp *StreamPool) Init() {
    22  	sp.p = make(map[uint32]protocol.Stream)
    23  }
    24  
    25  // OutcomeStream make the stream and returns it!
    26  func (sp *StreamPool) OutcomeStream(service protocol.Service) (stream protocol.Stream, err protocol.Error) {
    27  	// TODO::: Check stream isn't closed!!
    28  	return
    29  }
    30  
    31  // Stream returns Stream from pool if exists by given ID!
    32  func (sp *StreamPool) Stream(id uint32) protocol.Stream {
    33  	// TODO::: Check stream isn't closed!!
    34  	return sp.p[id]
    35  }
    36  
    37  // RegisterStream save given Stream to pool
    38  func (sp *StreamPool) RegisterStream(st protocol.Stream) {
    39  	sp.mutex.Lock()
    40  	sp.p[st.GetID()] = st
    41  	sp.mutex.Unlock()
    42  }