This commit is contained in:
wangjian 2023-03-10 23:49:52 +08:00
parent 2433c68452
commit a750964220
11 changed files with 122 additions and 109 deletions

View File

@ -56,7 +56,7 @@ func NewClient(appName string, connType ClientType, opts ...ClientOption) *Clien
errChan: make(chan error), errChan: make(chan error),
closeChan: make(chan bool), closeChan: make(chan bool),
} }
c.Init(opts...) _ = c.Init(opts...)
once.Do(func() { once.Do(func() {
c.init() c.init()
}) })
@ -145,8 +145,9 @@ func (c *Client) handleFrame() {
// this will block until a frame is received // this will block until a frame is received
f, err := fs.ReadFrame() f, err := fs.ReadFrame()
if err != nil { if err != nil {
defer c.stream.Close() defer func() {
// defer c.conn.CloseWithError(0xD0, err.Error()) _ = c.stream.Close()
}()
c.logger.Debugf("%shandleFrame(): %T | %v", ClientLogPrefix, err, err) c.logger.Debugf("%shandleFrame(): %T | %v", ClientLogPrefix, err, err)
if e, ok := err.(*quic.IdleTimeoutError); ok { if e, ok := err.(*quic.IdleTimeoutError); ok {
@ -185,7 +186,7 @@ func (c *Client) handleFrame() {
// any error occurred, we should close the stream // any error occurred, we should close the stream
// after this, conn.AcceptStream() will raise the error // after this, conn.AcceptStream() will raise the error
c.setState(ConnStateClosed) c.setState(ConnStateClosed)
c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeUnknown), err.Error()) _ = c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeUnknown), err.Error())
c.logger.Errorf("%sunknown error occurred, err=%v, state=%v", ClientLogPrefix, err, c.getState()) c.logger.Errorf("%sunknown error occurred, err=%v, state=%v", ClientLogPrefix, err, c.getState())
break break
} }
@ -210,7 +211,7 @@ func (c *Client) handleFrame() {
c.setState(ConnStateRejected) c.setState(ConnStateRejected)
if v, ok := f.(*frame.RejectedFrame); ok { if v, ok := f.(*frame.RejectedFrame); ok {
c.logger.Errorf("%s receive RejectedFrame, message=%s", ClientLogPrefix, v.Message()) c.logger.Errorf("%s receive RejectedFrame, message=%s", ClientLogPrefix, v.Message())
c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeRejected), v.Message()) _ = c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeRejected), v.Message())
c.errChan <- errors.New(v.Message()) c.errChan <- errors.New(v.Message())
break break
} }
@ -218,7 +219,7 @@ func (c *Client) handleFrame() {
c.setState(ConnStateGoaway) c.setState(ConnStateGoaway)
if v, ok := f.(*frame.GoawayFrame); ok { if v, ok := f.(*frame.GoawayFrame); ok {
c.logger.Errorf("%s receive GoawayFrame, message=%s", ClientLogPrefix, v.Message()) c.logger.Errorf("%s receive GoawayFrame, message=%s", ClientLogPrefix, v.Message())
c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeGoaway), v.Message()) _ = c.conn.CloseWithError(hpds_err.To(hpds_err.ErrorCodeGoaway), v.Message())
c.errChan <- errors.New(v.Message()) c.errChan <- errors.New(v.Message())
break break
} }
@ -229,8 +230,6 @@ func (c *Client) handleFrame() {
if c.processor == nil { if c.processor == nil {
c.logger.Warnf("%sprocessor is nil", ClientLogPrefix) c.logger.Warnf("%sprocessor is nil", ClientLogPrefix)
} else { } else {
// TODO: should c.processor accept a DataFrame as parameter?
// c.processor(v.GetDataTagID(), v.GetCarriage(), v.GetMetaFrame())
c.processor(v) c.processor(v)
} }
} }

View File

@ -54,10 +54,10 @@ func (c *Context) Clean() {
func (c *Context) CloseWithError(code hpds_err.ErrorCode, msg string) { func (c *Context) CloseWithError(code hpds_err.ErrorCode, msg string) {
log.Debugf("%sconn[%s] context close, errCode=%#x, msg=%s", ServerLogPrefix, c.connId, code, msg) log.Debugf("%sconn[%s] context close, errCode=%#x, msg=%s", ServerLogPrefix, c.connId, code, msg)
if c.Stream != nil { if c.Stream != nil {
c.Stream.Close() _ = c.Stream.Close()
} }
if c.Conn != nil { if c.Conn != nil {
c.Conn.CloseWithError(quic.ApplicationErrorCode(code), msg) _ = c.Conn.CloseWithError(quic.ApplicationErrorCode(code), msg)
} }
c.Clean() c.Clean()
} }

View File

@ -71,7 +71,7 @@ func (d *DataFrame) SourceId() string {
// Encode return coder encoded bytes of `DataFrame` // Encode return coder encoded bytes of `DataFrame`
func (d *DataFrame) Encode() []byte { func (d *DataFrame) Encode() []byte {
data := coder.NewNodePacketEncoder(byte(d.Type())) data := coder.NewNodePacketEncoder(d.Type())
// MetaFrame // MetaFrame
data.AddBytes(d.metaFrame.Encode()) data.AddBytes(d.metaFrame.Encode())
// PayloadFrame // PayloadFrame
@ -90,7 +90,7 @@ func DecodeToDataFrame(buf []byte) (*DataFrame, error) {
data := &DataFrame{} data := &DataFrame{}
if metaBlock, ok := packet.NodePackets[byte(TagOfMetaFrame)]; ok { if metaBlock, ok := packet.NodePackets[TagOfMetaFrame]; ok {
meta, err := DecodeToMetaFrame(metaBlock.GetRawBytes()) meta, err := DecodeToMetaFrame(metaBlock.GetRawBytes())
if err != nil { if err != nil {
return nil, err return nil, err
@ -98,7 +98,7 @@ func DecodeToDataFrame(buf []byte) (*DataFrame, error) {
data.metaFrame = meta data.metaFrame = meta
} }
if payloadBlock, ok := packet.NodePackets[byte(TagOfPayloadFrame)]; ok { if payloadBlock, ok := packet.NodePackets[TagOfPayloadFrame]; ok {
payload, err := DecodeToPayloadFrame(payloadBlock.GetRawBytes()) payload, err := DecodeToPayloadFrame(payloadBlock.GetRawBytes())
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -21,9 +21,9 @@ func (f *GoawayFrame) Type() Type {
// Encode to coder encoded bytes // Encode to coder encoded bytes
func (f *GoawayFrame) Encode() []byte { func (f *GoawayFrame) Encode() []byte {
goaway := coder.NewNodePacketEncoder(byte(f.Type())) goaway := coder.NewNodePacketEncoder(f.Type())
// message // message
msgBlock := coder.NewPrimitivePacketEncoder(byte(TagOfGoawayMessage)) msgBlock := coder.NewPrimitivePacketEncoder(TagOfGoawayMessage)
msgBlock.SetStringValue(f.message) msgBlock.SetStringValue(f.message)
goaway.AddPrimitivePacket(msgBlock) goaway.AddPrimitivePacket(msgBlock)
@ -46,7 +46,7 @@ func DecodeToGoawayFrame(buf []byte) (*GoawayFrame, error) {
goaway := &GoawayFrame{} goaway := &GoawayFrame{}
// message // message
if msgBlock, ok := node.PrimitivePackets[byte(TagOfGoawayMessage)]; ok { if msgBlock, ok := node.PrimitivePackets[TagOfGoawayMessage]; ok {
msg, err := msgBlock.ToUTF8String() msg, err := msgBlock.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -39,24 +39,24 @@ func (h *HandshakeFrame) Type() Type {
// Encode to coder encoding. // Encode to coder encoding.
func (h *HandshakeFrame) Encode() []byte { func (h *HandshakeFrame) Encode() []byte {
// name // name
nameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeName)) nameBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeName)
nameBlock.SetStringValue(h.Name) nameBlock.SetStringValue(h.Name)
// client ID // client ID
idBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeId)) idBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeId)
idBlock.SetStringValue(h.ClientId) idBlock.SetStringValue(h.ClientId)
// client type // client type
typeBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeType)) typeBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeType)
typeBlock.SetBytesValue([]byte{h.ClientType}) typeBlock.SetBytesValue([]byte{h.ClientType})
// observe data tags // observe data tags
observeDataTagsBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeObserveDataTags)) observeDataTagsBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeObserveDataTags)
observeDataTagsBlock.SetBytesValue(h.ObserveDataTags) observeDataTagsBlock.SetBytesValue(h.ObserveDataTags)
// auth // auth
authNameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthName)) authNameBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeAuthName)
authNameBlock.SetStringValue(h.authName) authNameBlock.SetStringValue(h.authName)
authPayloadBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthPayload)) authPayloadBlock := coder.NewPrimitivePacketEncoder(TagOfHandshakeAuthPayload)
authPayloadBlock.SetStringValue(h.authPayload) authPayloadBlock.SetStringValue(h.authPayload)
// handshake frame // handshake frame
handshake := coder.NewNodePacketEncoder(byte(h.Type())) handshake := coder.NewNodePacketEncoder(h.Type())
handshake.AddPrimitivePacket(nameBlock) handshake.AddPrimitivePacket(nameBlock)
handshake.AddPrimitivePacket(idBlock) handshake.AddPrimitivePacket(idBlock)
handshake.AddPrimitivePacket(typeBlock) handshake.AddPrimitivePacket(typeBlock)
@ -77,7 +77,7 @@ func DecodeToHandshakeFrame(buf []byte) (*HandshakeFrame, error) {
handshake := &HandshakeFrame{} handshake := &HandshakeFrame{}
// name // name
if nameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeName)]; ok { if nameBlock, ok := node.PrimitivePackets[TagOfHandshakeName]; ok {
name, err := nameBlock.ToUTF8String() name, err := nameBlock.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err
@ -85,7 +85,7 @@ func DecodeToHandshakeFrame(buf []byte) (*HandshakeFrame, error) {
handshake.Name = name handshake.Name = name
} }
// client id // client id
if idBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeId)]; ok { if idBlock, ok := node.PrimitivePackets[TagOfHandshakeId]; ok {
id, err := idBlock.ToUTF8String() id, err := idBlock.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err
@ -93,23 +93,23 @@ func DecodeToHandshakeFrame(buf []byte) (*HandshakeFrame, error) {
handshake.ClientId = id handshake.ClientId = id
} }
// client type // client type
if typeBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeType)]; ok { if typeBlock, ok := node.PrimitivePackets[TagOfHandshakeType]; ok {
clientType := typeBlock.ToBytes() clientType := typeBlock.ToBytes()
handshake.ClientType = clientType[0] handshake.ClientType = clientType[0]
} }
// observe data tag list // observe data tag list
if observeDataTagsBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeObserveDataTags)]; ok { if observeDataTagsBlock, ok := node.PrimitivePackets[TagOfHandshakeObserveDataTags]; ok {
handshake.ObserveDataTags = observeDataTagsBlock.ToBytes() handshake.ObserveDataTags = observeDataTagsBlock.ToBytes()
} }
// auth // auth
if authNameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthName)]; ok { if authNameBlock, ok := node.PrimitivePackets[TagOfHandshakeAuthName]; ok {
authName, err := authNameBlock.ToUTF8String() authName, err := authNameBlock.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err
} }
handshake.authName = authName handshake.authName = authName
} }
if authPayloadBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthPayload)]; ok { if authPayloadBlock, ok := node.PrimitivePackets[TagOfHandshakeAuthPayload]; ok {
authPayload, err := authPayloadBlock.ToUTF8String() authPayload, err := authPayloadBlock.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -57,20 +57,20 @@ func (m *MetaFrame) SourceId() string {
// Encode implements Frame.Encode method. // Encode implements Frame.Encode method.
func (m *MetaFrame) Encode() []byte { func (m *MetaFrame) Encode() []byte {
meta := coder.NewNodePacketEncoder(byte(TagOfMetaFrame)) meta := coder.NewNodePacketEncoder(TagOfMetaFrame)
// transaction ID // transaction ID
transactionId := coder.NewPrimitivePacketEncoder(byte(TagOfTransactionId)) transactionId := coder.NewPrimitivePacketEncoder(TagOfTransactionId)
transactionId.SetStringValue(m.tid) transactionId.SetStringValue(m.tid)
meta.AddPrimitivePacket(transactionId) meta.AddPrimitivePacket(transactionId)
// source ID // source ID
sourceId := coder.NewPrimitivePacketEncoder(byte(TagOfSourceId)) sourceId := coder.NewPrimitivePacketEncoder(TagOfSourceId)
sourceId.SetStringValue(m.sourceId) sourceId.SetStringValue(m.sourceId)
meta.AddPrimitivePacket(sourceId) meta.AddPrimitivePacket(sourceId)
// metadata // metadata
if m.metadata != nil { if m.metadata != nil {
metadata := coder.NewPrimitivePacketEncoder(byte(TagOfMetadata)) metadata := coder.NewPrimitivePacketEncoder(TagOfMetadata)
metadata.SetBytesValue(m.metadata) metadata.SetBytesValue(m.metadata)
meta.AddPrimitivePacket(metadata) meta.AddPrimitivePacket(metadata)
} }
@ -89,17 +89,17 @@ func DecodeToMetaFrame(buf []byte) (*MetaFrame, error) {
meta := &MetaFrame{} meta := &MetaFrame{}
for k, v := range nodeBlock.PrimitivePackets { for k, v := range nodeBlock.PrimitivePackets {
switch k { switch k {
case byte(TagOfTransactionId): case TagOfTransactionId:
val, err := v.ToUTF8String() val, err := v.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err
} }
meta.tid = val meta.tid = val
break break
case byte(TagOfMetadata): case TagOfMetadata:
meta.metadata = v.ToBytes() meta.metadata = v.ToBytes()
break break
case byte(TagOfSourceId): case TagOfSourceId:
sourceId, err := v.ToUTF8String() sourceId, err := v.ToUTF8String()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -30,7 +30,7 @@ func (m *PayloadFrame) Encode() []byte {
carriage := coder.NewPrimitivePacketEncoder(m.Tag) carriage := coder.NewPrimitivePacketEncoder(m.Tag)
carriage.SetBytesValue(m.Carriage) carriage.SetBytesValue(m.Carriage)
payload := coder.NewNodePacketEncoder(byte(TagOfPayloadFrame)) payload := coder.NewNodePacketEncoder(TagOfPayloadFrame)
payload.AddPrimitivePacket(carriage) payload.AddPrimitivePacket(carriage)
return payload.Encode() return payload.Encode()

View File

@ -21,9 +21,9 @@ func (f *RejectedFrame) Type() Type {
// Encode to coder encoded bytes // Encode to coder encoded bytes
func (f *RejectedFrame) Encode() []byte { func (f *RejectedFrame) Encode() []byte {
rejected := coder.NewNodePacketEncoder(byte(f.Type())) rejected := coder.NewNodePacketEncoder(f.Type())
// message // message
msgBlock := coder.NewPrimitivePacketEncoder(byte(TagOfRejectedMessage)) msgBlock := coder.NewPrimitivePacketEncoder(TagOfRejectedMessage)
msgBlock.SetStringValue(f.message) msgBlock.SetStringValue(f.message)
rejected.AddPrimitivePacket(msgBlock) rejected.AddPrimitivePacket(msgBlock)
@ -45,7 +45,7 @@ func DecodeToRejectedFrame(buf []byte) (*RejectedFrame, error) {
} }
rejected := &RejectedFrame{} rejected := &RejectedFrame{}
// message // message
if msgBlock, ok := node.PrimitivePackets[byte(TagOfRejectedMessage)]; ok { if msgBlock, ok := node.PrimitivePackets[TagOfRejectedMessage]; ok {
msg, e := msgBlock.ToUTF8String() msg, e := msgBlock.ToUTF8String()
if e != nil { if e != nil {
return nil, e return nil, e

View File

@ -130,12 +130,12 @@ func (z *zapLogger) Infof(template string, args ...interface{}) {
} }
// Warnf logs a message at WarnLevel // Warnf logs a message at WarnLevel
func (z zapLogger) Warnf(template string, args ...interface{}) { func (z *zapLogger) Warnf(template string, args ...interface{}) {
z.Instance().Warnf(template, args...) z.Instance().Warnf(template, args...)
} }
// Errorf logs a message at ErrorLevel // Errorf logs a message at ErrorLevel
func (z zapLogger) Errorf(template string, args ...interface{}) { func (z *zapLogger) Errorf(template string, args ...interface{}) {
z.Instance().Errorf(template, args...) z.Instance().Errorf(template, args...)
} }
@ -190,7 +190,7 @@ func (z *zapLogger) Instance() *zap.SugaredLogger {
if err != nil { if err != nil {
return err return err
} }
rotatedLogger.Write(msg.Bytes()) _, _ = rotatedLogger.Write(msg.Bytes())
} }
return nil return nil
}) })
@ -204,11 +204,11 @@ func (z *zapLogger) Instance() *zap.SugaredLogger {
return z.instance return z.instance
} }
func errorRotatedLogger(file string, maxSize, maxBacukups, maxAge int) *lumberjack.Logger { func errorRotatedLogger(file string, maxSize, maxBackups, maxAge int) *lumberjack.Logger {
return &lumberjack.Logger{ return &lumberjack.Logger{
Filename: file, Filename: file,
MaxSize: maxSize, MaxSize: maxSize,
MaxBackups: maxBacukups, MaxBackups: maxBackups,
MaxAge: maxAge, MaxAge: maxAge,
Compress: false, Compress: false,
} }
@ -221,7 +221,7 @@ func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
type logWriter struct{} type logWriter struct{}
func (l logWriter) Write(bytes []byte) (int, error) { func (l logWriter) Write(bytes []byte) (int, error) {
os.Stderr.WriteString(time.Now().Format(timeFormat)) _, _ = os.Stderr.WriteString(time.Now().Format(timeFormat))
os.Stderr.Write([]byte("\t")) _, _ = os.Stderr.Write([]byte("\t"))
return os.Stderr.Write(bytes) return os.Stderr.Write(bytes)
} }

131
server.go
View File

@ -52,7 +52,7 @@ func NewServer(name string, opts ...ServerOption) *Server {
connector: newConnector(), connector: newConnector(),
downStreams: make(map[string]*Client), downStreams: make(map[string]*Client),
} }
s.Init(opts...) _ = s.Init(opts...)
return s return s
} }
@ -100,59 +100,74 @@ func (s *Server) Serve(ctx context.Context, conn net.PacketConn) error {
log.Errorf("%slistener.Listen: err=%v", ServerLogPrefix, err) log.Errorf("%slistener.Listen: err=%v", ServerLogPrefix, err)
return err return err
} }
defer listener.Close() defer func() {
_ = listener.Close()
}()
log.Printf("%s [%s][%d] Listening on: %s, MODE: %s, QUIC: %v, AUTH: %s", ServerLogPrefix, s.name, os.Getpid(), listener.Addr(), mode(), listener.Versions(), s.authNames()) log.Printf("%s [%s][%d] Listening on: %s, MODE: %s, QUIC: %v, AUTH: %s", ServerLogPrefix, s.name, os.Getpid(), listener.Addr(), mode(), listener.Versions(), s.authNames())
s.state = ConnStateConnected s.state = ConnStateConnected
for { for {
// create a new connection when new hpds-client connected _ = s.createNewClientConnection(ctx, listener)
sctx, cancel := context.WithCancel(ctx)
defer cancel()
connect, e := listener.Accept(sctx)
if e != nil {
log.Errorf("%screate connection error: %v", ServerLogPrefix, e)
return e
}
connID := GetConnId(connect)
log.Infof("%s1/ new connection: %s", ServerLogPrefix, connID)
go func(ctx context.Context, qconn quic.Connection) {
for {
log.Infof("%s2/ waiting for new stream", ServerLogPrefix)
stream, err := qconn.AcceptStream(ctx)
if err != nil {
// if client close the connection, then we should close the connection
// @CC: when Source close the connection, it won't affect connectors
name := "--"
if conn := s.connector.Get(connID); conn != nil {
conn.Close()
// connector
s.connector.Remove(connID)
route := s.router.Route(conn.Metadata())
if route != nil {
route.Remove(connID)
}
name = conn.Name()
}
log.Printf("%s [%s](%s) close the connection: %v", ServerLogPrefix, name, connID, err)
break
}
defer stream.Close()
log.Infof("%s3/ [stream:%d] created, connId=%s", ServerLogPrefix, stream.StreamID(), connID)
// process frames on stream
// c := newContext(connId, stream)
c := newContext(connect, stream)
defer c.Clean()
s.handleConnection(c)
log.Infof("%s4/ [stream:%d] handleConnection DONE", ServerLogPrefix, stream.StreamID())
}
}(sctx, connect)
} }
} }
// createNewClientConnection create a new connection when new hpds-client connected
func (s *Server) createNewClientConnection(ctx context.Context, listener Listener) error {
sctx, cancel := context.WithCancel(ctx)
defer cancel()
connect, e := listener.Accept(sctx)
if e != nil {
log.Errorf("%screate connection error: %v", ServerLogPrefix, e)
return e
}
connId := GetConnId(connect)
log.Infof("%s1/ new connection: %s", ServerLogPrefix, connId)
go func(ctx context.Context, qConn quic.Connection) {
for {
err := s.handle(ctx, qConn, connect, connId)
if err != nil {
break
}
}
}(sctx, connect)
return nil
}
func (s *Server) handle(ctx context.Context, qConn quic.Connection, conn quic.Connection, connId string) error {
log.Infof("%s2/ waiting for new stream", ServerLogPrefix)
stream, err := qConn.AcceptStream(ctx)
if err != nil {
name := "--"
if conn := s.connector.Get(connId); conn != nil {
_ = conn.Close()
// connector
s.connector.Remove(connId)
route := s.router.Route(conn.Metadata())
if route != nil {
_ = route.Remove(connId)
}
name = conn.Name()
}
log.Printf("%s [%s](%s) close the connection: %v", ServerLogPrefix, name, connId, err)
return err
}
defer func() {
_ = stream.Close()
}()
log.Infof("%s3/ [stream:%d] created, connId=%s", ServerLogPrefix, stream.StreamID(), connId)
// process frames on stream
// c := newContext(connId, stream)
c := newContext(conn, stream)
defer c.Clean()
s.handleConnection(c)
log.Infof("%s4/ [stream:%d] handleConnection DONE", ServerLogPrefix, stream.StreamID())
return nil
}
// Close will shut down the server. // Close will shut down the server.
func (s *Server) Close() error { func (s *Server) Close() error {
if s.router != nil { if s.router != nil {
@ -204,28 +219,28 @@ func (s *Server) handleConnection(c *Context) {
frameType := f.Type() frameType := f.Type()
data := f.Encode() data := f.Encode()
log.Debugf("%stype=%s, frame[%d]=%# x", ServerLogPrefix, frameType, len(data), frame.Shortly(data)) log.Debugf("%stype=%s, frame[%d]=%# x", ServerLogPrefix, frameType, len(data), frame.Shortly(data))
// add frame to context // add frame to contextFrame
context := c.WithFrame(f) contextFrame := c.WithFrame(f)
// before frame handlers // before frame handlers
for _, handler := range s.beforeHandlers { for _, handler := range s.beforeHandlers {
if e := handler(context); e != nil { if e := handler(contextFrame); e != nil {
log.Errorf("%safterFrameHandler e: %s", ServerLogPrefix, e) log.Errorf("%safterFrameHandler e: %s", ServerLogPrefix, e)
context.CloseWithError(hpds_err.ErrorCodeBeforeHandler, e.Error()) contextFrame.CloseWithError(hpds_err.ErrorCodeBeforeHandler, e.Error())
return return
} }
} }
// main handler // main handler
if e := s.mainFrameHandler(context); e != nil { if e := s.mainFrameHandler(contextFrame); e != nil {
log.Errorf("%smainFrameHandler e: %s", ServerLogPrefix, e) log.Errorf("%smainFrameHandler e: %s", ServerLogPrefix, e)
context.CloseWithError(hpds_err.ErrorCodeMainHandler, e.Error()) contextFrame.CloseWithError(hpds_err.ErrorCodeMainHandler, e.Error())
return return
} }
// after frame handler // after frame handler
for _, handler := range s.afterHandlers { for _, handler := range s.afterHandlers {
if e := handler(context); e != nil { if e := handler(contextFrame); e != nil {
log.Errorf("%safterFrameHandler e: %s", ServerLogPrefix, e) log.Errorf("%safterFrameHandler e: %s", ServerLogPrefix, e)
context.CloseWithError(hpds_err.ErrorCodeAfterHandler, e.Error()) contextFrame.CloseWithError(hpds_err.ErrorCodeAfterHandler, e.Error())
return return
} }
} }
@ -262,7 +277,7 @@ func (s *Server) mainFrameHandler(c *Context) error {
s.dispatchToDownStreams(f) s.dispatchToDownStreams(f)
} }
// observe data tags back flow // observe data tags back flow
s.handleBackFlowFrame(c) _ = s.handleBackFlowFrame(c)
} }
default: default:
log.Errorf("%serr=%v, frame=%v", ServerLogPrefix, err, frame.Shortly(c.Frame.Encode())) log.Errorf("%serr=%v, frame=%v", ServerLogPrefix, err, frame.Shortly(c.Frame.Encode()))
@ -334,7 +349,7 @@ func (s *Server) handleHandshakeFrame(c *Context) error {
default: default:
// unknown client type // unknown client type
s.connector.Remove(connId) s.connector.Remove(connId)
err := fmt.Errorf("Illegal ClientType: %#x", f.ClientType) err := fmt.Errorf("Illegal ClientType: %#x ", f.ClientType)
c.CloseWithError(hpds_err.ErrorCodeUnknownClient, err.Error()) c.CloseWithError(hpds_err.ErrorCodeUnknownClient, err.Error())
return err return err
} }
@ -477,7 +492,7 @@ func (s *Server) AddDownstreamServer(addr string, c *Client) {
func (s *Server) dispatchToDownStreams(df *frame.DataFrame) { func (s *Server) dispatchToDownStreams(df *frame.DataFrame) {
for addr, ds := range s.downStreams { for addr, ds := range s.downStreams {
log.Debugf("%sdispatching to [%s]: %# x", ServerLogPrefix, addr, df.Tag()) log.Debugf("%sdispatching to [%s]: %# x", ServerLogPrefix, addr, df.Tag())
ds.WriteFrame(df) _ = ds.WriteFrame(df)
} }
} }

View File

@ -10,7 +10,6 @@ import (
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"errors" "errors"
"io/ioutil"
"math/big" "math/big"
"net" "net"
"os" "os"
@ -88,7 +87,7 @@ func getCACertPool() (*x509.CertPool, error) {
return nil, errors.New("tls: must provide CA certificate on production mode, you can configure this via environment variables: `HPDS_TLS_CACERT_FILE`") return nil, errors.New("tls: must provide CA certificate on production mode, you can configure this via environment variables: `HPDS_TLS_CACERT_FILE`")
} }
caCert, err = ioutil.ReadFile(caCertPath) caCert, err = os.ReadFile(caCertPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -116,12 +115,12 @@ func getCertAndKey() (*tls.Certificate, error) {
} }
// certificate // certificate
cert, err = ioutil.ReadFile(certPath) cert, err = os.ReadFile(certPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// private key // private key
key, err = ioutil.ReadFile(keyPath) key, err = os.ReadFile(keyPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }