github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/srpc/frame-data.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package srpc
     4  
     5  import (
     6  	"../protocol"
     7  	"../syllab"
     8  )
     9  
    10  /*
    11  type dataFrame struct {
    12  	Length   [2]byte // including the header fields
    13  	StreamID [4]byte // uint32
    14  	Offset   [4]byte // uint32 also can act as offset
    15  	Payload  []byte
    16  }
    17  */
    18  type dataFrame []byte
    19  
    20  func (f dataFrame) Length() uint16    { return syllab.GetUInt16(f, 0) }
    21  func (f dataFrame) StreamID() uint32  { return syllab.GetUInt32(f, 2) }
    22  func (f dataFrame) Offset() uint32    { return syllab.GetUInt32(f, 6) }
    23  func (f dataFrame) Payload() []byte   { return f[10:f.Length()] }
    24  func (f dataFrame) NextFrame() []byte { return f[f.Length():] }
    25  
    26  // appendData add data to the requested offset of the stream
    27  func appendData(conn protocol.Connection, frame dataFrame) (err protocol.Error) {
    28  	var streamID uint32 = frame.StreamID()
    29  	var stream protocol.Stream
    30  	stream, err = conn.Stream(streamID)
    31  	if err != nil {
    32  		conn.StreamFailed()
    33  		// Send response or just ignore stream
    34  		// TODO::: DDOS!!??
    35  		return
    36  	}
    37  
    38  	// TODO:::
    39  	// add payload to Stream payload
    40  	// var offset uint32 = frame.Offset()
    41  	// err = addNewGPPacket(stream, GetPayload(packet), packetID)
    42  
    43  	if stream.Status() == protocol.ConnectionStateReady {
    44  		// decide by stream odd or even
    45  		// TODO::: check better performance as streamID&1 to check odd id
    46  		if streamID%2 == 0 {
    47  			err = stream.Protocol().HandleIncomeRequest(stream)
    48  			if err == nil {
    49  				conn.StreamSucceed()
    50  			} else {
    51  				conn.StreamFailed()
    52  			}
    53  		} else {
    54  			// income response
    55  			stream.SetState(protocol.ConnectionStateReady)
    56  		}
    57  	}
    58  	return
    59  }