This commit is contained in:
crusader 2017-10-27 17:53:26 +09:00
parent cb96187314
commit ab60b6ccda
8 changed files with 32 additions and 8 deletions

View File

@ -42,8 +42,11 @@ func (c *client) Start() error {
} }
c.stopChan = make(chan struct{}) c.stopChan = make(chan struct{})
c.clientHandler.OnStart()
c.stopWg.Add(1) c.stopWg.Add(1)
go runClient(c) go runClient(c)
return nil return nil
} }
@ -54,6 +57,7 @@ func (c *client) Stop() {
close(c.stopChan) close(c.stopChan)
c.stopWg.Wait() c.stopWg.Wait()
c.stopChan = nil c.stopChan = nil
c.clientHandler.OnStop()
} }
func runClient(c *client) { func runClient(c *client) {

View File

@ -6,6 +6,9 @@ import (
) )
type ClientHandler interface { type ClientHandler interface {
OnStart()
OnStop()
Dial() (conn io.ReadWriteCloser, err error) Dial() (conn io.ReadWriteCloser, err error)
OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error
Handle(rwc io.ReadWriteCloser, stopChan chan struct{}) Handle(rwc io.ReadWriteCloser, stopChan chan struct{})

View File

@ -42,6 +42,14 @@ type ClientHandlers struct {
KeepAlivePeriod time.Duration KeepAlivePeriod time.Duration
} }
func (ch *ClientHandlers) OnStart() {
// no op
}
func (ch *ClientHandlers) OnStop() {
// no op
}
func (ch *ClientHandlers) Dial() (conn io.ReadWriteCloser, err error) { func (ch *ClientHandlers) Dial() (conn io.ReadWriteCloser, err error) {
return nil, errors.New("Client: Handler method[Dial] of Client is not implement") return nil, errors.New("Client: Handler method[Dial] of Client is not implement")
} }

View File

@ -15,6 +15,6 @@ func (sh *ServerHandlers) Listen() (l net.Listener, err error) {
return return
} }
func (sh *ServerHandlers) Stopped() { func (sh *ServerHandlers) OnStop() {
os.Remove(sh.Addr) os.Remove(sh.Addr)
} }

View File

@ -11,6 +11,6 @@ func (sh *ServerHandlers) Listen() (net.Listener, error) {
return npipe.Listen(s.path) return npipe.Listen(s.path)
} }
func (sh *ServerHandlers) Stopped() { func (sh *ServerHandlers) OnStop() {
} }

View File

@ -50,8 +50,11 @@ func (s *server) Start() error {
return err return err
} }
s.serverHandler.OnStart()
s.stopWg.Add(1) s.stopWg.Add(1)
go runServer(s) go runServer(s)
return nil return nil
} }
@ -62,7 +65,7 @@ func (s *server) Stop() {
close(s.stopChan) close(s.stopChan)
s.stopWg.Wait() s.stopWg.Wait()
s.stopChan = nil s.stopChan = nil
s.serverHandler.OnStopped() s.serverHandler.OnStop()
} }
func (s *server) Serve() error { func (s *server) Serve() error {

View File

@ -6,10 +6,12 @@ import (
) )
type ServerHandler interface { type ServerHandler interface {
OnStart()
OnStop()
Listen() (net.Listener, error) Listen() (net.Listener, error)
OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error OnHandshake(remoteAddr string, rwc io.ReadWriteCloser) error
Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{}) Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{})
OnStopped()
IsClientDisconnect(err error) bool IsClientDisconnect(err error) bool

View File

@ -23,6 +23,14 @@ type ServerHandlers struct {
KeepAlivePeriod time.Duration KeepAlivePeriod time.Duration
} }
func (sh *ServerHandlers) OnStart() {
// no op
}
func (sh *ServerHandlers) OnStop() {
// no op
}
func (sh *ServerHandlers) Listen() (net.Listener, error) { func (sh *ServerHandlers) Listen() (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement") 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 return err == io.ErrUnexpectedEOF || err == io.EOF
} }
func (sh *ServerHandlers) OnStopped() {
}
func (sh *ServerHandlers) GetAddr() string { func (sh *ServerHandlers) GetAddr() string {
return sh.Addr return sh.Addr
} }