github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/tcp/stream-codec.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package tcp
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/protocol"
     7  )
     8  
     9  /*
    10  ********** protocol.Codec interface **********
    11   */
    12  
    13  func (s *Socket) MediaType() protocol.MediaType       { return nil }
    14  func (s *Socket) CompressType() protocol.CompressType { return nil }
    15  
    16  func (s *Socket) Decode(reader protocol.Reader) (err protocol.Error) { return }
    17  func (s *Socket) Encode(writer protocol.Writer) (err protocol.Error) {
    18  	var _, goErr = s.WriteTo(writer)
    19  	if goErr != nil {
    20  		// err =
    21  	}
    22  	return
    23  }
    24  func (s *Socket) Marshal() (data []byte, err protocol.Error) {
    25  	err = s.checkSocket()
    26  	if err != nil {
    27  		return
    28  	}
    29  
    30  	if !s.recv.buf.Full() {
    31  		err = s.blockInSelect()
    32  	}
    33  	return s.recv.buf.Marshal()
    34  }
    35  func (s *Socket) MarshalTo(data []byte) (added []byte, err protocol.Error) {
    36  	err = s.checkSocket()
    37  	if err != nil {
    38  		return
    39  	}
    40  
    41  	if !s.recv.buf.Full() {
    42  		err = s.blockInSelect()
    43  	}
    44  	return s.recv.buf.MarshalTo(data)
    45  }
    46  func (s *Socket) Unmarshal(data []byte) (n int, err protocol.Error) {
    47  	err = s.checkSocket()
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	for len(data) > 0 {
    53  		select {
    54  		case <-s.writeTimer.Signal():
    55  			// err =
    56  			return
    57  		default:
    58  			var sendNumber int
    59  			sendNumber, err = s.sendPayload(data)
    60  			if err != nil {
    61  				return
    62  			}
    63  			n += sendNumber
    64  			data = data[sendNumber:]
    65  		}
    66  	}
    67  	return
    68  }
    69  func (s *Socket) UnmarshalFrom(data []byte) (remaining []byte, err protocol.Error) {
    70  	return
    71  }
    72  func (s *Socket) Len() (ln int) { return s.recv.buf.Len() }