diff --git a/client.go b/client.go index c91709d..0d8d3d6 100644 --- a/client.go +++ b/client.go @@ -42,8 +42,11 @@ func (c *client) Start() error { } c.stopChan = make(chan struct{}) + c.clientHandler.OnStart() + c.stopWg.Add(1) go runClient(c) + return nil } @@ -54,6 +57,7 @@ func (c *client) Stop() { close(c.stopChan) c.stopWg.Wait() c.stopChan = nil + c.clientHandler.OnStop() } func runClient(c *client) { diff --git a/client_handler.go b/client_handler.go index 3198bfc..4ae2525 100644 --- a/client_handler.go +++ b/client_handler.go @@ -6,6 +6,9 @@ import ( ) type ClientHandler interface { + OnStart() + OnStop() + Dial() (conn io.ReadWriteCloser, err error) OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error Handle(rwc io.ReadWriteCloser, stopChan chan struct{}) diff --git a/client_handlers.go b/client_handlers.go index 5f1ee6b..5be8caf 100644 --- a/client_handlers.go +++ b/client_handlers.go @@ -42,6 +42,14 @@ type ClientHandlers struct { KeepAlivePeriod time.Duration } +func (ch *ClientHandlers) OnStart() { + // no op +} + +func (ch *ClientHandlers) OnStop() { + // no op +} + func (ch *ClientHandlers) Dial() (conn io.ReadWriteCloser, err error) { return nil, errors.New("Client: Handler method[Dial] of Client is not implement") } diff --git a/ipc/server_handlers_unix.go b/ipc/server_handlers_unix.go index 0de673c..5d14406 100644 --- a/ipc/server_handlers_unix.go +++ b/ipc/server_handlers_unix.go @@ -15,6 +15,6 @@ func (sh *ServerHandlers) Listen() (l net.Listener, err error) { return } -func (sh *ServerHandlers) Stopped() { +func (sh *ServerHandlers) OnStop() { os.Remove(sh.Addr) } diff --git a/ipc/server_handlers_windows.go b/ipc/server_handlers_windows.go index 0f46d16..769472c 100644 --- a/ipc/server_handlers_windows.go +++ b/ipc/server_handlers_windows.go @@ -11,6 +11,6 @@ func (sh *ServerHandlers) Listen() (net.Listener, error) { return npipe.Listen(s.path) } -func (sh *ServerHandlers) Stopped() { +func (sh *ServerHandlers) OnStop() { } diff --git a/server.go b/server.go index dbec47a..a72329d 100644 --- a/server.go +++ b/server.go @@ -50,8 +50,11 @@ func (s *server) Start() error { return err } + s.serverHandler.OnStart() + s.stopWg.Add(1) go runServer(s) + return nil } @@ -62,7 +65,7 @@ func (s *server) Stop() { close(s.stopChan) s.stopWg.Wait() s.stopChan = nil - s.serverHandler.OnStopped() + s.serverHandler.OnStop() } func (s *server) Serve() error { diff --git a/server_handler.go b/server_handler.go index a1d1e4d..a5ee011 100644 --- a/server_handler.go +++ b/server_handler.go @@ -6,10 +6,12 @@ import ( ) type ServerHandler interface { + OnStart() + OnStop() + Listen() (net.Listener, error) OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{}) - OnStopped() IsClientDisconnect(err error) bool diff --git a/server_handlers.go b/server_handlers.go index 974a945..3fb2610 100644 --- a/server_handlers.go +++ b/server_handlers.go @@ -23,6 +23,14 @@ type ServerHandlers struct { KeepAlivePeriod time.Duration } +func (sh *ServerHandlers) OnStart() { + // no op +} + +func (sh *ServerHandlers) OnStop() { + // no op +} + func (sh *ServerHandlers) Listen() (net.Listener, error) { return nil, errors.New("Server: Handler method[Listen] of Server is not implement") } @@ -39,10 +47,6 @@ func (sh *ServerHandlers) IsClientDisconnect(err error) bool { return err == io.ErrUnexpectedEOF || err == io.EOF } -func (sh *ServerHandlers) OnStopped() { - -} - func (sh *ServerHandlers) GetAddr() string { return sh.Addr }