70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package node
|
|
|
|
import (
|
|
"time"
|
|
"vppn/m"
|
|
)
|
|
|
|
const (
|
|
dialTimeout = 8 * time.Second
|
|
connectTimeout = 6 * time.Second
|
|
pingInterval = 6 * time.Second
|
|
timeoutInterval = 20 * time.Second
|
|
)
|
|
|
|
func (rp *remotePeer) supervise(conf m.PeerConfig) {
|
|
defer panicHandler()
|
|
|
|
base := &stateBase{
|
|
published: rp.published,
|
|
peers: rp.peers,
|
|
localIP: rp.localIP,
|
|
remoteIP: rp.remoteIP,
|
|
privKey: conf.EncPrivKey,
|
|
localPub: addrIsValid(conf.PublicIP),
|
|
conn: rp.conn,
|
|
counter: &rp.counter,
|
|
pingTimer: time.NewTimer(time.Second),
|
|
timeoutTimer: time.NewTimer(time.Second),
|
|
buf: make([]byte, bufferSize),
|
|
encBuf: make([]byte, bufferSize),
|
|
}
|
|
|
|
var (
|
|
curState peerState = newStateNoPeer(base)
|
|
nextState peerState
|
|
)
|
|
|
|
for {
|
|
nextState = nil
|
|
|
|
select {
|
|
case peer := <-rp.peerUpdates:
|
|
nextState = curState.OnPeerUpdate(peer)
|
|
|
|
case pkt := <-rp.controlPackets:
|
|
switch p := pkt.Payload.(type) {
|
|
case synPacket:
|
|
nextState = curState.OnSyn(pkt.RemoteAddr, p)
|
|
case synAckPacket:
|
|
nextState = curState.OnSynAck(pkt.RemoteAddr, p)
|
|
case ackPacket:
|
|
nextState = curState.OnAck(pkt.RemoteAddr, p)
|
|
default:
|
|
// Unknown packet type.
|
|
}
|
|
|
|
case <-base.pingTimer.C:
|
|
nextState = curState.OnPingTimer()
|
|
|
|
case <-base.timeoutTimer.C:
|
|
nextState = curState.OnTimeoutTimer()
|
|
}
|
|
|
|
if nextState != nil {
|
|
rp.logf("%s --> %s", curState.Name(), nextState.Name())
|
|
curState = nextState
|
|
}
|
|
}
|
|
}
|