123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package yamux
- import (
- "encoding/binary"
- "fmt"
- )
- type NetError struct {
- err error
- timeout bool
- temporary bool
- }
- func (e *NetError) Error() string {
- return e.err.Error()
- }
- func (e *NetError) Timeout() bool {
- return e.timeout
- }
- func (e *NetError) Temporary() bool {
- return e.temporary
- }
- var (
-
-
- ErrInvalidVersion = fmt.Errorf("invalid protocol version")
-
-
- ErrInvalidMsgType = fmt.Errorf("invalid msg type")
-
-
- ErrSessionShutdown = fmt.Errorf("session shutdown")
-
-
- ErrStreamsExhausted = fmt.Errorf("streams exhausted")
-
-
- ErrDuplicateStream = fmt.Errorf("duplicate stream initiated")
-
- ErrRecvWindowExceeded = fmt.Errorf("recv window exceeded")
-
- ErrTimeout = &NetError{
- err: fmt.Errorf("i/o deadline reached"),
-
-
- timeout: true,
- }
-
- ErrStreamClosed = fmt.Errorf("stream closed")
-
- ErrUnexpectedFlag = fmt.Errorf("unexpected flag")
-
- ErrRemoteGoAway = fmt.Errorf("remote end is not accepting connections")
-
-
- ErrConnectionReset = fmt.Errorf("connection reset")
-
-
- ErrConnectionWriteTimeout = fmt.Errorf("connection write timeout")
-
- ErrKeepAliveTimeout = fmt.Errorf("keepalive timeout")
- )
- const (
-
- protoVersion uint8 = 0
- )
- const (
-
-
- typeData uint8 = iota
-
-
-
- typeWindowUpdate
-
-
-
- typePing
-
-
- typeGoAway
- )
- const (
-
-
- flagSYN uint16 = 1 << iota
-
-
- flagACK
-
-
- flagFIN
-
- flagRST
- )
- const (
-
- initialStreamWindow uint32 = 256 * 1024
- )
- const (
-
- goAwayNormal uint32 = iota
-
- goAwayProtoErr
-
- goAwayInternalErr
- )
- const (
- sizeOfVersion = 1
- sizeOfType = 1
- sizeOfFlags = 2
- sizeOfStreamID = 4
- sizeOfLength = 4
- headerSize = sizeOfVersion + sizeOfType + sizeOfFlags +
- sizeOfStreamID + sizeOfLength
- )
- type header []byte
- func (h header) Version() uint8 {
- return h[0]
- }
- func (h header) MsgType() uint8 {
- return h[1]
- }
- func (h header) Flags() uint16 {
- return binary.BigEndian.Uint16(h[2:4])
- }
- func (h header) StreamID() uint32 {
- return binary.BigEndian.Uint32(h[4:8])
- }
- func (h header) Length() uint32 {
- return binary.BigEndian.Uint32(h[8:12])
- }
- func (h header) String() string {
- return fmt.Sprintf("Vsn:%d Type:%d Flags:%d StreamID:%d Length:%d",
- h.Version(), h.MsgType(), h.Flags(), h.StreamID(), h.Length())
- }
- func (h header) encode(msgType uint8, flags uint16, streamID uint32, length uint32) {
- h[0] = protoVersion
- h[1] = msgType
- binary.BigEndian.PutUint16(h[2:4], flags)
- binary.BigEndian.PutUint32(h[4:8], streamID)
- binary.BigEndian.PutUint32(h[8:12], length)
- }
|