package tcp_listen
type Connection interface {
Read() (value interface{}, tag byte, err error)
Write(tag byte, body interface{}) (err error)
Close() error
LocalAddr() string
RemoteAddr() string
}
type isReconn struct {
reconn bool
connFunc func()
continueChan []chan bool
}
func (self *isReconn) success() {
self.reconn = true
self.connFunc()
for _, Chan := range self.continueChan {
Chan <- true
}
self.reconn = false
}
func (self *isReconn) Reconnection() <-chan bool {
Chan := make(chan bool, 1)
if !self.reconn {
self.continueChan = []chan bool{}
self.continueChan = append(self.continueChan, Chan)
go self.success()
} else {
self.continueChan = append(self.continueChan, Chan)
}
return Chan
}
func NewReconnection(f func()) *isReconn {
return &isReconn{connFunc: f, continueChan: []chan bool{}}
}
有疑问加站长微信联系(非本文作者)