diff --git a/server.go b/server.go index 804386a..45d7999 100644 --- a/server.go +++ b/server.go @@ -43,7 +43,7 @@ type Server struct { datagramPool sync.Pool } -//NewServer returns a new Server +// NewServer returns a new Server func NewServer() *Server { return &Server{tlsPeerNameFunc: defaultTlsPeerName, datagramPool: sync.Pool{ New: func() interface{} { @@ -55,17 +55,17 @@ func NewServer() *Server { } } -//Sets the syslog format (RFC3164 or RFC5424 or RFC6587) +// Sets the syslog format (RFC3164 or RFC5424 or RFC6587) func (s *Server) SetFormat(f format.Format) { s.format = f } -//Sets the handler, this handler with receive every syslog entry +// Sets the handler, this handler with receive every syslog entry func (s *Server) SetHandler(handler Handler) { s.handler = handler } -//Sets the connection timeout for TCP connections, in milliseconds +// Sets the connection timeout for TCP connections, in milliseconds func (s *Server) SetTimeout(millseconds int64) { s.readTimeoutMilliseconds = millseconds } @@ -89,7 +89,7 @@ func defaultTlsPeerName(tlsConn *tls.Conn) (tlsPeer string, ok bool) { return cn, true } -//Configure the server for listen on an UDP addr +// Configure the server for listen on an UDP addr func (s *Server) ListenUDP(addr string) error { udpAddr, err := net.ResolveUDPAddr("udp", addr) if err != nil { @@ -106,7 +106,20 @@ func (s *Server) ListenUDP(addr string) error { return nil } -//Configure the server for listen on an unix socket +func (s *Server) AddListener(l net.Listener) { + if l != nil { + s.listeners = append(s.listeners, l) + } +} + +func (s *Server) AddConnection(conn *net.UDPConn) { + if conn != nil { + conn.SetReadBuffer(datagramReadBufferSize) + s.connections = append(s.connections, conn) + } +} + +// Configure the server for listen on an unix socket func (s *Server) ListenUnixgram(addr string) error { unixAddr, err := net.ResolveUnixAddr("unixgram", addr) if err != nil { @@ -123,7 +136,7 @@ func (s *Server) ListenUnixgram(addr string) error { return nil } -//Configure the server for listen on a TCP addr +// Configure the server for listen on a TCP addr func (s *Server) ListenTCP(addr string) error { tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { @@ -140,7 +153,7 @@ func (s *Server) ListenTCP(addr string) error { return nil } -//Configure the server for listen on a TCP addr for TLS +// Configure the server for listen on a TCP addr for TLS func (s *Server) ListenTCPTLS(addr string, config *tls.Config) error { listener, err := tls.Listen("tcp", addr, config) if err != nil { @@ -152,7 +165,7 @@ func (s *Server) ListenTCPTLS(addr string, config *tls.Config) error { return nil } -//Starts the server, all the go routines goes to live +// Starts the server, all the go routines goes to live func (s *Server) Boot() error { if s.format == nil { return errors.New("please set a valid format") @@ -278,12 +291,12 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) { s.handler.Handle(logParts, int64(len(line)), err) } -//Returns the last error +// Returns the last error func (s *Server) GetLastError() error { return s.lastError } -//Kill the server +// Kill the server func (s *Server) Kill() error { for _, connection := range s.connections { err := connection.Close() @@ -308,7 +321,7 @@ func (s *Server) Kill() error { return nil } -//Waits until the server stops +// Waits until the server stops func (s *Server) Wait() { s.wait.Wait() }