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

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package protocol
     4  
     5  // Stream is the interface that must implement by any struct to be Stream
     6  type Streams interface {
     7  	// OutcomeStream make the stream and returns it or return error if any problems occur
     8  	OutcomeStream(service Service) (stream Stream, err Error)
     9  	// IncomeStream make the stream and returns it or return error if any problems occur
    10  	IncomeStream(id uint64) (stream Stream, err Error)
    11  	// Stream returns Stream from pool if exists by given ID
    12  	Stream(id uint64) (stream Stream, err Error)
    13  }
    14  
    15  // Stream is the interface that must implement by any struct to be a stream!
    16  type Stream interface {
    17  	ID() uint64
    18  	Connection() Connection
    19  	Protocol() NetworkApplicationHandler // usage is like TCP||UDP ports that indicate payload protocol is GitiURN ID
    20  	Service() Service
    21  	Error() Error                // just indicate peer error that receive by response of the request.
    22  	Status() ConnectionState     // return last stream state
    23  	State() chan ConnectionState // return state channel to listen to new stream state
    24  	Weight() Weight              // sum of connection and service weight.
    25  
    26  	// Authorize request by data in related stream and connection by any data like service, time, ...
    27  	Authorize() (err Error)
    28  
    29  	SendRequest(req Codec) (err Error)  // Listen to stream state to check request successfully send, response ready to serve, ...
    30  	SendResponse(res Codec) (err Error) // Listen to stream state to check response successfully send, ...
    31  
    32  	// Below methods are low level APIs, don't use them in services layer, if you don't know how it can be effect the application.
    33  	Close() (err Error)                        // Just once, must deregister the stream from the connection and send close message to Socket in some types.
    34  	Socket() Codec                             // Chunks manager like sRPC, QUIC, TCP, UDP, ...
    35  	SetSocket(codec Codec)                     // Just once, use SendRequest||SendResponse methods
    36  	SetProtocol(nah NetworkApplicationHandler) // Just once, (But some protocol like http allow to change it after first set in a reusable stream like IP/TCP, Allow them??)
    37  	SetService(ser Service)                    // Just once, (But some protocol like http allow to change it after first set in a reusable stream like IP/TCP, Allow them??)
    38  	SetError(err Error)                        // Just once
    39  	SetState(state ConnectionState)            // change state of stream and send notification on stream StateChannel.
    40  
    41  	// SetDeadline sets the read and write deadlines associated with the connection.
    42  	// It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
    43  	SetDeadline(d Duration) Error
    44  	// SetReadDeadline(d Duration) Error
    45  	// SetWriteDeadline(d Duration) Error
    46  }