Skip to content

Commit

Permalink
Allow errors to be captured
Browse files Browse the repository at this point in the history
Added Server.SetErrChannel which will be used to send errors

fixes mcuadros#78
  • Loading branch information
munkyboy committed Aug 12, 2021
1 parent 307370c commit 67a805f
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Server struct {
readTimeoutMilliseconds int64
tlsPeerNameFunc TlsPeerNameFunc
datagramPool sync.Pool
errChannel chan error
}

//NewServer returns a new Server
Expand All @@ -61,6 +62,11 @@ func (s *Server) SetHandler(handler Handler) {
s.handler = handler
}

//Sets a channel for errors
func (s *Server) SetErrChannel(c chan error) {
s.errChannel = c
}

//Sets the connection timeout for TCP connections, in milliseconds
func (s *Server) SetTimeout(millseconds int64) {
s.readTimeoutMilliseconds = millseconds
Expand Down Expand Up @@ -181,6 +187,9 @@ func (s *Server) goAcceptConnection(listener net.Listener) {
}
connection, err := listener.Accept()
if err != nil {
if s.errChannel != nil {
s.errChannel <- &ListenerError{err}
}
continue
}

Expand All @@ -207,6 +216,9 @@ func (s *Server) goScanConnection(connection net.Conn) {
if tlsConn, ok := connection.(*tls.Conn); ok {
// Handshake now so we get the TLS peer information
if err := tlsConn.Handshake(); err != nil {
if s.errChannel != nil {
s.errChannel <- &HandshakeError{err, remoteAddr, tlsConn.ConnectionState()}
}
connection.Close()
return
}
Expand Down Expand Up @@ -241,6 +253,9 @@ loop:
if scanCloser.Scan() {
s.parser([]byte(scanCloser.Text()), client, tlsPeer)
} else {
if err := scanCloser.Err(); err != nil && s.errChannel != nil {
s.errChannel <- &ScannerError{err, client, tlsPeer}
}
break loop
}
}
Expand All @@ -254,6 +269,9 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) {
err := parser.Parse()
if err != nil {
s.lastError = err
if s.errChannel != nil {
s.errChannel <- &ParserError{err}
}
}

logParts := parser.Dump()
Expand Down Expand Up @@ -376,3 +394,56 @@ func (s *Server) goParseDatagrams() {
}
}()
}

// Error types
type ListenerError struct {
wrappedError error
}

func (l *ListenerError) Error() string {
return l.wrappedError.Error()
}

func (l *ListenerError) Unwrap() error {
return l.wrappedError
}

type HandshakeError struct {
wrappedError error
RemoteAddr net.Addr
ConnectionState tls.ConnectionState
}

func (l *HandshakeError) Error() string {
return l.wrappedError.Error()
}

func (l *HandshakeError) Unwrap() error {
return l.wrappedError
}

type ScannerError struct {
wrappedError error
Client string
TLSPeer string
}

func (l *ScannerError) Error() string {
return l.wrappedError.Error()
}

func (l *ScannerError) Unwrap() error {
return l.wrappedError
}

type ParserError struct {
wrappedError error
}

func (l *ParserError) Error() string {
return l.wrappedError.Error()
}

func (l *ParserError) Unwrap() error {
return l.wrappedError
}

0 comments on commit 67a805f

Please sign in to comment.