@@ -53,6 +53,7 @@ type Server struct {
5353 listenTo string
5454 driverFactory DriverFactory
5555 logger * Logger
56+ listener net.Listener
5657}
5758
5859// serverOptsWithDefaults copies an ServerOpts struct into a new struct,
@@ -186,23 +187,34 @@ func (Server *Server) ListenAndServe() error {
186187
187188 Server .logger .Printf ("%s listening on %d" , Server .Name , Server .Port )
188189
190+ Server .listener = listener
189191 for {
190- tcpConn , err := listener .Accept ()
192+ tcpConn , err := Server . listener .Accept ()
191193 if err != nil {
192- Server .logger .Print ("listening error" )
194+ Server .logger .Printf ("listening error: %v" , err )
193195 break
194196 }
195197 driver , err := Server .driverFactory .NewDriver ()
196198 if err != nil {
197- Server .logger .Print ("Error creating driver, aborting client connection" )
198- break
199+ Server .logger .Printf ("Error creating driver, aborting client connection: %v" , err )
200+ tcpConn .Close ()
201+ } else {
202+ ftpConn := Server .newConn (tcpConn , driver , Server .Auth )
203+ go ftpConn .Serve ()
199204 }
200- ftpConn := Server .newConn (tcpConn , driver , Server .Auth )
201- go ftpConn .Serve ()
202205 }
203206 return nil
204207}
205208
209+ // Gracefully stops a server. Already connected clients will retain their connections
210+ func (Server * Server ) Shutdown () error {
211+ if Server .listener != nil {
212+ return Server .listener .Close ()
213+ }
214+ // server wasnt even started
215+ return nil
216+ }
217+
206218func buildTcpString (hostname string , port int ) (result string ) {
207219 if strings .Contains (hostname , ":" ) {
208220 // ipv6
0 commit comments