Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add server connection handler #359

Merged
merged 1 commit into from Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 26 additions & 11 deletions core/server.go
Expand Up @@ -31,19 +31,23 @@ type ServerOption func(*ServerOptions)
// FrameHandler is the handler for frame.
type FrameHandler func(c *Context) error

// ConnectionHandler is the handler for quic connection
type ConnectionHandler func(conn quic.Connection)

// Server is the underlining server of Zipper
type Server struct {
name string
state string
connector Connector
router Router
metadataBuilder MetadataBuilder
counterOfDataFrame int64
downstreams map[string]*Client
mu sync.Mutex
opts ServerOptions
beforeHandlers []FrameHandler
afterHandlers []FrameHandler
name string
state string
connector Connector
router Router
metadataBuilder MetadataBuilder
counterOfDataFrame int64
downstreams map[string]*Client
mu sync.Mutex
opts ServerOptions
beforeHandlers []FrameHandler
afterHandlers []FrameHandler
connectionCloseHandlers []ConnectionHandler
}

// NewServer create a Server instance.
Expand Down Expand Up @@ -120,6 +124,12 @@ func (s *Server) Serve(ctx context.Context, conn net.PacketConn) error {
logger.Infof("%s❤️1/ new connection: %s", ServerLogPrefix, connID)

go func(ctx context.Context, qconn quic.Connection) {
// connection close handlers
defer func() {
for _, h := range s.connectionCloseHandlers {
h(qconn)
}
}()
for {
logger.Infof("%s❤️2/ waiting for new stream", ServerLogPrefix)
stream, err := qconn.AcceptStream(ctx)
Expand Down Expand Up @@ -532,6 +542,11 @@ func (s *Server) SetAfterHandlers(handlers ...FrameHandler) {
s.afterHandlers = append(s.afterHandlers, handlers...)
}

// SetConnectionCloseHandlers set the connection close handlers of server.
func (s *Server) SetConnectionCloseHandlers(handlers ...ConnectionHandler) {
s.connectionCloseHandlers = append(s.connectionCloseHandlers, handlers...)
}

func (s *Server) authNames() []string {
if len(s.opts.Auths) == 0 {
return []string{"none"}
Expand Down