1
0

frame.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package smux
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. const (
  7. version = 1
  8. )
  9. const ( // cmds
  10. cmdSYN byte = iota // stream open
  11. cmdFIN // stream close, a.k.a EOF mark
  12. cmdPSH // data push
  13. cmdNOP // no operation
  14. )
  15. const (
  16. sizeOfVer = 1
  17. sizeOfCmd = 1
  18. sizeOfLength = 2
  19. sizeOfSid = 4
  20. headerSize = sizeOfVer + sizeOfCmd + sizeOfSid + sizeOfLength
  21. )
  22. // Frame defines a packet from or to be multiplexed into a single connection
  23. type Frame struct {
  24. ver byte
  25. cmd byte
  26. sid uint32
  27. data []byte
  28. }
  29. func newFrame(cmd byte, sid uint32) Frame {
  30. return Frame{ver: version, cmd: cmd, sid: sid}
  31. }
  32. type rawHeader []byte
  33. func (h rawHeader) Version() byte {
  34. return h[0]
  35. }
  36. func (h rawHeader) Cmd() byte {
  37. return h[1]
  38. }
  39. func (h rawHeader) Length() uint16 {
  40. return binary.LittleEndian.Uint16(h[2:])
  41. }
  42. func (h rawHeader) StreamID() uint32 {
  43. return binary.LittleEndian.Uint32(h[4:])
  44. }
  45. func (h rawHeader) String() string {
  46. return fmt.Sprintf("Version:%d Cmd:%d StreamID:%d Length:%d",
  47. h.Version(), h.Cmd(), h.StreamID(), h.Length())
  48. }