Add PreHandlerClientFunc and PostHandlerClientFunc to Server struct

This commit is contained in:
qowevisa 2024-10-22 17:43:30 +03:00
parent 028dc99fc2
commit 7338bc69fa

View File

@ -59,9 +59,13 @@ func CreateHandleClientFuncFromCommands(bundle *tcpcommand.CommandBundle, conf S
} }
type Server struct { type Server struct {
addr string addr string
HandleClientFunc func(client net.Conn) PreHandlerClientFunc func(client net.Conn)
Exit chan bool HandleClientFunc func(client net.Conn)
// Use PostHandlerClientFunc in your HandleClientFunc if you
// use custom HandleClientFunc
PostHandlerClientFunc func(client net.Conn)
Exit chan bool
// //
MessageEndRune rune MessageEndRune rune
MessageSplitRune rune MessageSplitRune rune
@ -74,6 +78,9 @@ type Server struct {
func defaultHandleClientFunc(server *Server) func(net.Conn) { func defaultHandleClientFunc(server *Server) func(net.Conn) {
return func(client net.Conn) { return func(client net.Conn) {
if server.PostHandlerClientFunc != nil {
defer server.PostHandlerClientFunc(client)
}
defer client.Close() defer client.Close()
connReader := bufio.NewReader(client) connReader := bufio.NewReader(client)
for { for {
@ -184,6 +191,9 @@ loop:
if s.LogLevel&LogLevel_Connection > 0 { if s.LogLevel&LogLevel_Connection > 0 {
fmt.Printf("New Connection from %s is accepted\n", newClient.RemoteAddr()) fmt.Printf("New Connection from %s is accepted\n", newClient.RemoteAddr())
} }
if s.PreHandlerClientFunc != nil {
s.PreHandlerClientFunc(newClient)
}
go s.HandleClientFunc(newClient) go s.HandleClientFunc(newClient)
} }
} }