2018-04-12 01:23:40 +00:00
|
|
|
package client
|
2018-04-11 09:27:16 +00:00
|
|
|
|
2018-04-18 12:21:05 +00:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
2018-06-29 10:02:40 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons/server-go/socket"
|
2018-04-18 12:21:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OnDisconnectedFunc func(connector Connector)
|
2018-04-13 07:11:43 +00:00
|
|
|
|
2018-04-12 01:23:40 +00:00
|
|
|
type Connector interface {
|
2018-06-29 10:02:40 +00:00
|
|
|
Connect() (readChan <-chan socket.SocketMessage, writeChan chan<- socket.SocketMessage, err error)
|
2018-04-11 09:27:16 +00:00
|
|
|
Disconnect() error
|
2018-04-12 01:23:40 +00:00
|
|
|
|
2018-04-12 05:55:01 +00:00
|
|
|
GetName() string
|
2018-04-18 12:21:05 +00:00
|
|
|
GetOnDisconnected() OnDisconnectedFunc
|
|
|
|
SetOnDisconnected(fnc OnDisconnectedFunc)
|
2018-04-12 05:55:01 +00:00
|
|
|
|
|
|
|
Clone() Connector
|
2018-04-12 01:23:40 +00:00
|
|
|
Validate() error
|
2018-04-11 09:27:16 +00:00
|
|
|
}
|
2018-04-12 05:55:01 +00:00
|
|
|
|
|
|
|
type Connectors struct {
|
2018-04-18 12:21:05 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
OnDisconnected OnDisconnectedFunc `json:"-"`
|
2018-04-13 07:11:43 +00:00
|
|
|
|
|
|
|
validated atomic.Value
|
2018-04-12 05:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connectors) GetName() string {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:21:05 +00:00
|
|
|
func (c *Connectors) GetOnDisconnected() OnDisconnectedFunc {
|
|
|
|
return c.OnDisconnected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connectors) SetOnDisconnected(fnc OnDisconnectedFunc) {
|
|
|
|
c.OnDisconnected = fnc
|
|
|
|
}
|
|
|
|
|
2018-04-12 05:55:01 +00:00
|
|
|
func (c *Connectors) Clone() *Connectors {
|
|
|
|
return &Connectors{
|
2018-04-13 07:11:43 +00:00
|
|
|
Name: c.Name,
|
|
|
|
validated: c.validated,
|
2018-04-12 05:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connectors) Validate() error {
|
2018-04-13 07:11:43 +00:00
|
|
|
if nil != c.validated.Load() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.validated.Store(true)
|
|
|
|
|
2018-04-12 05:55:01 +00:00
|
|
|
if "" == c.Name {
|
|
|
|
c.Name = "Connector"
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|