2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
// ReadWriter is the interface that groups the ReadFrame and WriteFrame methods.
|
|
|
|
type ReadWriter interface {
|
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader reads frame from underlying stream.
|
|
|
|
type Reader interface {
|
|
|
|
// ReadFrame reads frame, if error, the error returned is not empty
|
|
|
|
// and frame returned is nil.
|
|
|
|
ReadFrame() (Frame, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writer is the interface that wraps the WriteFrame method, It writes
|
|
|
|
// frm to the underlying data stream.
|
|
|
|
type Writer interface {
|
|
|
|
WriteFrame(frm Frame) error
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:36:09 +08:00
|
|
|
// debugFrameSize print frame data size on debug mode
|
|
|
|
var debugFrameSize = 16
|
|
|
|
|
|
|
|
// Kinds of frames transferable within HPDS
|
|
|
|
const (
|
|
|
|
// DataFrame
|
|
|
|
TagOfDataFrame Type = 0x3F
|
|
|
|
// MetaFrame of DataFrame
|
|
|
|
TagOfMetaFrame Type = 0x2F
|
|
|
|
TagOfMetadata Type = 0x03
|
|
|
|
TagOfTransactionId Type = 0x01
|
|
|
|
TagOfSourceId Type = 0x02
|
2023-04-05 16:15:59 +08:00
|
|
|
TagOfBroadcast Type = 0x04
|
2022-10-11 17:36:09 +08:00
|
|
|
// PayloadFrame of DataFrame
|
2023-04-05 16:15:59 +08:00
|
|
|
TagOfPayloadFrame Type = 0x2E
|
|
|
|
TagOfPayloadDataTag Type = 0x01
|
|
|
|
TagOfPayloadCarriage Type = 0x02
|
|
|
|
TagOfBackFlowFrame Type = 0x2D
|
|
|
|
TagOfBackFlowDataTag Type = 0x01
|
|
|
|
TagOfBackFlowCarriage Type = 0x02
|
2022-10-11 17:36:09 +08:00
|
|
|
|
|
|
|
TagOfTokenFrame Type = 0x3E
|
2023-04-05 16:15:59 +08:00
|
|
|
|
|
|
|
// AuthenticationFrame
|
|
|
|
TagOfAuthenticationFrame Type = 0x03
|
|
|
|
TagOfAuthenticationName Type = 0x04
|
|
|
|
TagOfAuthenticationPayload Type = 0x05
|
|
|
|
|
|
|
|
// AuthenticationAckFrame
|
|
|
|
TagOfAuthenticationAckFrame Type = 0x11
|
|
|
|
TagOfAuthenticationAckOk Type = 0x12
|
|
|
|
TagOfAuthenticationAckReason Type = 0x13
|
|
|
|
|
|
|
|
// CloseStreamFrame
|
|
|
|
TagOfCloseStreamFrame Type = 0x14
|
|
|
|
TagOfCloseStreamID Type = 0x15
|
|
|
|
TagOfCloseStreamReason Type = 0x16
|
|
|
|
|
2022-10-11 17:36:09 +08:00
|
|
|
// HandshakeFrame
|
|
|
|
TagOfHandshakeFrame Type = 0x3D
|
|
|
|
TagOfHandshakeName Type = 0x01
|
2023-04-05 16:15:59 +08:00
|
|
|
TagOfHandshakeStreamType Type = 0x02
|
2022-10-11 17:36:09 +08:00
|
|
|
TagOfHandshakeId Type = 0x03
|
|
|
|
TagOfHandshakeAuthName Type = 0x04
|
|
|
|
TagOfHandshakeAuthPayload Type = 0x05
|
|
|
|
TagOfHandshakeObserveDataTags Type = 0x06
|
2023-04-05 16:15:59 +08:00
|
|
|
TagOfHandshakeMetadata Type = 0x07
|
|
|
|
|
|
|
|
// TagOfHandshakeAckFrame
|
|
|
|
TagOfHandshakeAckFrame Type = 0x29
|
|
|
|
TagOfHandshakeAckStreamId Type = 0x28
|
2022-10-11 17:36:09 +08:00
|
|
|
|
|
|
|
TagOfPingFrame Type = 0x3C
|
|
|
|
TagOfPongFrame Type = 0x3B
|
|
|
|
TagOfAcceptedFrame Type = 0x3A
|
|
|
|
TagOfRejectedFrame Type = 0x39
|
|
|
|
TagOfRejectedMessage Type = 0x02
|
|
|
|
// GoawayFrame
|
|
|
|
TagOfGoawayFrame Type = 0x30
|
|
|
|
TagOfGoawayCode Type = 0x01
|
|
|
|
TagOfGoawayMessage Type = 0x02
|
|
|
|
)
|
|
|
|
|
|
|
|
// Type represents the type of frame.
|
|
|
|
type Type uint8
|
|
|
|
|
|
|
|
// Frame is the interface for frame.
|
|
|
|
type Frame interface {
|
|
|
|
// Type gets the type of Frame.
|
|
|
|
Type() Type
|
|
|
|
|
|
|
|
// Encode the frame into []byte.
|
|
|
|
Encode() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f Type) String() string {
|
|
|
|
switch f {
|
|
|
|
case TagOfDataFrame:
|
|
|
|
return "DataFrame"
|
|
|
|
case TagOfTokenFrame:
|
|
|
|
return "TokenFrame"
|
2023-04-05 16:15:59 +08:00
|
|
|
case TagOfAuthenticationFrame:
|
|
|
|
return "AuthenticationFrame"
|
|
|
|
case TagOfAuthenticationAckFrame:
|
|
|
|
return "AuthenticationAckFrame"
|
|
|
|
case TagOfHandshakeAckFrame:
|
|
|
|
return "HandshakeAckFrame"
|
|
|
|
case TagOfCloseStreamFrame:
|
|
|
|
return "CloseStreamFrame"
|
2022-10-11 17:36:09 +08:00
|
|
|
case TagOfHandshakeFrame:
|
|
|
|
return "HandshakeFrame"
|
|
|
|
case TagOfPingFrame:
|
|
|
|
return "PingFrame"
|
|
|
|
case TagOfPongFrame:
|
|
|
|
return "PongFrame"
|
|
|
|
case TagOfAcceptedFrame:
|
|
|
|
return "AcceptedFrame"
|
|
|
|
case TagOfRejectedFrame:
|
|
|
|
return "RejectedFrame"
|
|
|
|
case TagOfGoawayFrame:
|
|
|
|
return "GoawayFrame"
|
|
|
|
case TagOfBackFlowFrame:
|
|
|
|
return "BackFlowFrame"
|
|
|
|
case TagOfMetaFrame:
|
|
|
|
return "MetaFrame"
|
|
|
|
case TagOfPayloadFrame:
|
|
|
|
return "PayloadFrame"
|
|
|
|
case TagOfHandshakeName:
|
|
|
|
return "HandshakeName"
|
2023-04-05 16:15:59 +08:00
|
|
|
case TagOfHandshakeStreamType:
|
2022-10-11 17:36:09 +08:00
|
|
|
return "HandshakeType"
|
|
|
|
default:
|
|
|
|
return "UnknownFrame"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shortly reduce data size for easy viewing
|
|
|
|
func Shortly(data []byte) []byte {
|
|
|
|
if len(data) > debugFrameSize {
|
|
|
|
return data[:debugFrameSize]
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if envFrameSize := os.Getenv("DEBUG_FRAME_SIZE"); envFrameSize != "" {
|
|
|
|
if val, err := strconv.Atoi(envFrameSize); err == nil {
|
|
|
|
debugFrameSize = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|