76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
|
package client
|
||
|
|
||
|
import (
|
||
|
"sync/atomic"
|
||
|
|
||
|
"git.loafle.net/overflow/server-go/socket"
|
||
|
)
|
||
|
|
||
|
type OnDisconnectedFunc func(connector Connector)
|
||
|
|
||
|
type Connector interface {
|
||
|
socket.ClientConnHandler
|
||
|
socket.ReadWriteHandler
|
||
|
|
||
|
Connect() (readChan <-chan socket.SocketMessage, writeChan chan<- socket.SocketMessage, err error)
|
||
|
Disconnect() error
|
||
|
|
||
|
GetName() string
|
||
|
GetOnDisconnected() OnDisconnectedFunc
|
||
|
SetOnDisconnected(fnc OnDisconnectedFunc)
|
||
|
|
||
|
Clone() Connector
|
||
|
Validate() error
|
||
|
}
|
||
|
|
||
|
type Connectors struct {
|
||
|
socket.ClientConnHandlers
|
||
|
socket.ReadWriteHandlers
|
||
|
|
||
|
Name string `json:"name,omitempty"`
|
||
|
OnDisconnected OnDisconnectedFunc `json:"-"`
|
||
|
|
||
|
validated atomic.Value
|
||
|
}
|
||
|
|
||
|
func (c *Connectors) GetName() string {
|
||
|
return c.Name
|
||
|
}
|
||
|
|
||
|
func (c *Connectors) GetOnDisconnected() OnDisconnectedFunc {
|
||
|
return c.OnDisconnected
|
||
|
}
|
||
|
|
||
|
func (c *Connectors) SetOnDisconnected(fnc OnDisconnectedFunc) {
|
||
|
c.OnDisconnected = fnc
|
||
|
}
|
||
|
|
||
|
func (c *Connectors) Clone() *Connectors {
|
||
|
return &Connectors{
|
||
|
Name: c.Name,
|
||
|
ClientConnHandlers: *c.ClientConnHandlers.Clone(),
|
||
|
ReadWriteHandlers: *c.ReadWriteHandlers.Clone(),
|
||
|
validated: c.validated,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *Connectors) Validate() error {
|
||
|
if nil != c.validated.Load() {
|
||
|
return nil
|
||
|
}
|
||
|
c.validated.Store(true)
|
||
|
|
||
|
if err := c.ClientConnHandlers.Validate(); nil != err {
|
||
|
return err
|
||
|
}
|
||
|
if err := c.ReadWriteHandlers.Validate(); nil != err {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if "" == c.Name {
|
||
|
c.Name = "Connector"
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|