network/parser_stream.go

48 lines
1.5 KiB
Go

package network
import (
"fmt"
coder "git.hpds.cc/Component/mq_coder"
"git.hpds.cc/Component/network/frame"
"io"
)
// ParseFrame parses the frame from QUIC stream.
func ParseFrame(stream io.Reader) (frame.Frame, error) {
buf, err := coder.ReadPacket(stream)
if err != nil {
return nil, err
}
frameType := buf[0]
// determine the frame type
switch frameType {
case 0x80 | byte(frame.TagOfHandshakeFrame):
handshakeFrame, err := readHandshakeFrame(buf)
// logger.Debugf("%sHandshakeFrame: name=%s, type=%s", ParseFrameLogPrefix, handshakeFrame.Name, handshakeFrame.Type())
return handshakeFrame, err
case 0x80 | byte(frame.TagOfDataFrame):
data, err := readDataFrame(buf)
// logger.Debugf("%sDataFrame: tid=%s, tag=%#x, len(carriage)=%d", ParseFrameLogPrefix, data.TransactionID(), data.GetDataTag(), len(data.GetCarriage()))
return data, err
case 0x80 | byte(frame.TagOfAcceptedFrame):
return frame.DecodeToAcceptedFrame(buf)
case 0x80 | byte(frame.TagOfRejectedFrame):
return frame.DecodeToRejectedFrame(buf)
case 0x80 | byte(frame.TagOfGoawayFrame):
return frame.DecodeToGoawayFrame(buf)
case 0x80 | byte(frame.TagOfBackFlowFrame):
return frame.DecodeToBackFlowFrame(buf)
default:
return nil, fmt.Errorf("unknown frame type, buf[0]=%#x", buf[0])
}
}
func readHandshakeFrame(buf []byte) (*frame.HandshakeFrame, error) {
return frame.DecodeToHandshakeFrame(buf)
}
func readDataFrame(buf []byte) (*frame.DataFrame, error) {
return frame.DecodeToDataFrame(buf)
}