2018-04-05 16:21:13 +00:00
|
|
|
package socket
|
2018-04-04 13:28:35 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-13 07:11:43 +00:00
|
|
|
"sync/atomic"
|
2018-04-04 13:28:35 +00:00
|
|
|
"time"
|
2018-04-05 16:21:13 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons/server-go"
|
2018-04-04 13:28:35 +00:00
|
|
|
)
|
|
|
|
|
2018-04-04 13:50:34 +00:00
|
|
|
type ClientConnHandler interface {
|
2018-04-05 16:21:13 +00:00
|
|
|
server.ConnectionHandler
|
|
|
|
|
2018-04-04 13:28:35 +00:00
|
|
|
GetReconnectInterval() time.Duration
|
|
|
|
GetReconnectTryTime() int
|
|
|
|
}
|
|
|
|
|
2018-04-04 13:50:34 +00:00
|
|
|
type ClientConnHandlers struct {
|
2018-04-05 16:21:13 +00:00
|
|
|
server.ConnectionHandlers
|
2018-04-04 13:28:35 +00:00
|
|
|
|
2018-04-14 08:49:33 +00:00
|
|
|
ReconnectInterval time.Duration `json:"reconnectInterval,omitempty"`
|
|
|
|
ReconnectTryTime int `json:"reconnectTryTime,omitempty"`
|
2018-04-13 07:11:43 +00:00
|
|
|
|
|
|
|
validated atomic.Value
|
2018-04-04 13:28:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 13:50:34 +00:00
|
|
|
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
|
2018-04-04 13:28:35 +00:00
|
|
|
return cch.ReconnectInterval
|
|
|
|
}
|
|
|
|
|
2018-04-04 13:50:34 +00:00
|
|
|
func (cch *ClientConnHandlers) GetReconnectTryTime() int {
|
2018-04-04 13:28:35 +00:00
|
|
|
return cch.ReconnectTryTime
|
|
|
|
}
|
|
|
|
|
2018-04-12 05:55:01 +00:00
|
|
|
func (cch *ClientConnHandlers) Clone() *ClientConnHandlers {
|
|
|
|
return &ClientConnHandlers{
|
|
|
|
ConnectionHandlers: *cch.ConnectionHandlers.Clone(),
|
|
|
|
ReconnectInterval: cch.ReconnectInterval,
|
|
|
|
ReconnectTryTime: cch.ReconnectTryTime,
|
2018-04-13 07:11:43 +00:00
|
|
|
validated: cch.validated,
|
2018-04-12 05:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 13:50:34 +00:00
|
|
|
func (cch *ClientConnHandlers) Validate() error {
|
2018-04-13 07:11:43 +00:00
|
|
|
if nil != cch.validated.Load() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cch.validated.Store(true)
|
|
|
|
|
2018-04-04 13:28:35 +00:00
|
|
|
if err := cch.ConnectionHandlers.Validate(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cch.ReconnectInterval <= 0 {
|
2018-04-05 16:21:13 +00:00
|
|
|
cch.ReconnectInterval = server.DefaultReconnectInterval
|
2018-04-13 06:37:27 +00:00
|
|
|
} else {
|
|
|
|
cch.ReconnectInterval = cch.ReconnectInterval * time.Second
|
2018-04-04 13:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cch.ReconnectTryTime <= 0 {
|
2018-04-05 16:21:13 +00:00
|
|
|
cch.ReconnectTryTime = server.DefaultReconnectTryTime
|
2018-04-04 13:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|