diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-02-02 20:45:25 +0100 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-02-02 20:45:25 +0100 |
commit | 5871ec04deb8f4715cab37146940baa35c08cbee (patch) | |
tree | 8d643e991a3525c69a317c85cde6708bae2925aa /src/peer.go | |
parent | 4f97b52ea60ce4f2448d8617853aa44759727197 (diff) |
Fixed tests
Diffstat (limited to 'src/peer.go')
-rw-r--r-- | src/peer.go | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/src/peer.go b/src/peer.go index 7776b71..dc04811 100644 --- a/src/peer.go +++ b/src/peer.go @@ -154,6 +154,10 @@ func (peer *Peer) SendBuffer(buffer []byte) error { peer.device.net.mutex.RLock() defer peer.device.net.mutex.RUnlock() + if peer.device.net.bind == nil { + return errors.New("No bind") + } + peer.mutex.RLock() defer peer.mutex.RUnlock() @@ -161,10 +165,6 @@ func (peer *Peer) SendBuffer(buffer []byte) error { return errors.New("No known endpoint for peer") } - if peer.device.net.bind == nil { - return errors.New("No bind") - } - return peer.device.net.bind.Send(buffer, peer.endpoint) } @@ -196,16 +196,19 @@ func (peer *Peer) Start() { peer.routines.mutex.Lock() defer peer.routines.mutex.Unlock() + + if peer.isRunning.Get() { + return + } + peer.device.log.Debug.Println("Starting:", peer.String()) - // stop & wait for ongoing routines (if any) + // sanity check : these should be 0 - peer.isRunning.Set(false) - peer.routines.stop.Broadcast() peer.routines.starting.Wait() peer.routines.stopping.Wait() - // prepare queues + // prepare queues and signals peer.signal.newKeyPair = NewSignal() peer.signal.handshakeBegin = NewSignal() @@ -216,9 +219,11 @@ func (peer *Peer) Start() { peer.queue.outbound = make(chan *QueueOutboundElement, QueueOutboundSize) peer.queue.inbound = make(chan *QueueInboundElement, QueueInboundSize) - // reset signal and start (new) routines - peer.routines.stop = NewSignal() + peer.isRunning.Set(true) + + // wait for routines to start + peer.routines.starting.Add(PeerRoutineNumber) peer.routines.stopping.Add(PeerRoutineNumber) @@ -238,15 +243,28 @@ func (peer *Peer) Stop() { peer.routines.mutex.Lock() defer peer.routines.mutex.Unlock() + if !peer.isRunning.Swap(false) { + return + } + device := peer.device device.log.Debug.Println("Stopping:", peer.String()) - // stop & wait for ongoing peer routines (if any) + // stop & wait for ongoing peer routines peer.routines.stop.Broadcast() peer.routines.starting.Wait() peer.routines.stopping.Wait() + // stop timers + + peer.timer.keepalivePersistent.Stop() + peer.timer.keepalivePassive.Stop() + peer.timer.zeroAllKeys.Stop() + peer.timer.handshakeNew.Stop() + peer.timer.handshakeDeadline.Stop() + peer.timer.handshakeTimeout.Stop() + // close queues close(peer.queue.nonce) @@ -274,9 +292,4 @@ func (peer *Peer) Stop() { device.indices.Delete(hs.localIndex) hs.Clear() hs.mutex.Unlock() - - // reset signal (to handle repeated stopping) - - peer.routines.stop = NewSignal() - peer.isRunning.Set(false) } |