-
Notifications
You must be signed in to change notification settings - Fork 5
/
handshake.go
41 lines (38 loc) · 1010 Bytes
/
handshake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package noise
import (
"sync"
"github.com/go-i2p/go-i2p/lib/common/router_info"
)
func (c *NoiseTransport) Handshake(routerInfo router_info.RouterInfo) error {
c.Mutex.Lock()
defer c.Mutex.Unlock()
session, err := c.GetSession(routerInfo)
if err != nil {
return err
}
for {
if session.(*NoiseSession).handshakeComplete {
return nil
}
if session.(*NoiseSession).Cond == nil {
break
}
session.(*NoiseSession).Cond.Wait()
}
// Set handshakeCond to indicate that this goroutine is committing to
// running the handshake.
session.(*NoiseSession).Cond = sync.NewCond(&c.Mutex)
c.Mutex.Unlock()
session.(*NoiseSession).Mutex.Lock()
defer session.(*NoiseSession).Mutex.Unlock()
c.Mutex.Lock()
// if c.config.isClient {
if err := session.(*NoiseSession).RunClientHandshake(); err != nil {
return err
}
// Wake any other goroutines that are waiting for this handshake to
// complete.
session.(*NoiseSession).Cond.Broadcast()
session.(*NoiseSession).Cond = nil
return nil
}