const.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package yamux
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. const (
  7. // protoVersion is the only version we support
  8. protoVersion uint8 = 0
  9. )
  10. const (
  11. // Data is used for data frames. They are followed
  12. // by length bytes worth of payload.
  13. typeData uint8 = iota
  14. // WindowUpdate is used to change the window of
  15. // a given stream. The length indicates the delta
  16. // update to the window.
  17. typeWindowUpdate
  18. // Ping is sent as a keep-alive or to measure
  19. // the RTT. The StreamID and Length value are echoed
  20. // back in the response.
  21. typePing
  22. // GoAway is sent to terminate a session. The StreamID
  23. // should be 0 and the length is an error code.
  24. typeGoAway
  25. )
  26. const (
  27. // SYN is sent to signal a new stream. May
  28. // be sent with a data payload
  29. flagSYN uint16 = 1 << iota
  30. // ACK is sent to acknowledge a new stream. May
  31. // be sent with a data payload
  32. flagACK
  33. // FIN is sent to half-close the given stream.
  34. // May be sent with a data payload.
  35. flagFIN
  36. // RST is used to hard close a given stream.
  37. flagRST
  38. // LZW is used to indicate that the payload is
  39. // compressed with the LZW algorithm
  40. flagLZW
  41. )
  42. const (
  43. // initialSessionWindow is the initial session window size
  44. initialSessionWindow uint32 = 2 * 1024 * 1024
  45. // initialStreamWindow is the initial stream window size
  46. initialStreamWindow uint32 = 256 * 1024
  47. )
  48. const (
  49. // goAwayNormal is sent on a normal termination
  50. goAwayNormal uint32 = iota
  51. // goAwayProtoErr sent on a protocol error
  52. goAwayProtoErr
  53. // goAwayInternalErr sent on an internal error
  54. goAwayInternalErr
  55. )
  56. const (
  57. sizeOfVersion = 1
  58. sizeOfType = 1
  59. sizeOfFlags = 2
  60. sizeOfStreamID = 4
  61. sizeOfLength = 4
  62. headerSize = sizeOfVersion + sizeOfType + sizeOfFlags +
  63. sizeOfStreamID + sizeOfLength
  64. )
  65. type header []byte
  66. func (h header) Version() uint8 {
  67. return h[0]
  68. }
  69. func (h header) MsgType() uint8 {
  70. return h[1]
  71. }
  72. func (h header) Flags() uint16 {
  73. return binary.BigEndian.Uint16(h[2:4])
  74. }
  75. func (h header) StreamID() uint32 {
  76. return binary.BigEndian.Uint32(h[4:8])
  77. }
  78. func (h header) Length() uint32 {
  79. return binary.BigEndian.Uint32(h[8:12])
  80. }
  81. func (h header) String() string {
  82. return fmt.Sprintf("Vsn:%d Type:%d Flags:%d StreamID:%d Length:%d",
  83. h.Version(), h.MsgType(), h.Flags(), h.StreamID(), h.Length())
  84. }
  85. func (h header) encode(msgType uint8, flags uint16, streamID uint32, length uint32) {
  86. h[0] = protoVersion
  87. h[1] = msgType
  88. binary.BigEndian.PutUint16(h[2:4], flags)
  89. binary.BigEndian.PutUint32(h[4:8], streamID)
  90. binary.BigEndian.PutUint32(h[8:12], length)
  91. }