-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsg.go
139 lines (131 loc) · 3.03 KB
/
msg.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dht
import (
"crypto/rand"
"errors"
"net"
"time"
"github.com/esote/dht/core"
"github.com/esote/dht/session"
)
type rpcSession struct {
ch chan *session.MessageCloser
done chan struct{}
rpcid []byte
dht *DHT
}
func (dht *DHT) newSession() (*rpcSession, error) {
rpcid := make([]byte, core.RPCIDSize)
if _, err := rand.Read(rpcid); err != nil {
return nil, err
}
ch := make(chan *session.MessageCloser, 1)
done := make(chan struct{}, 1)
handler := &session.Handler{
Ch: ch,
Done: done,
}
dht.handlers.Add(1)
exp := time.Now().Add(dht.fixedTimeout)
if err := dht.sman.Register(rpcid, exp, handler); err != nil {
dht.handlers.Done()
close(done)
close(ch)
return nil, err
}
return &rpcSession{
ch: ch,
done: done,
rpcid: rpcid,
dht: dht,
}, nil
}
func (s *rpcSession) Close() error {
defer s.dht.handlers.Done() // TODO: order after Remove?
err := s.dht.sman.Remove(s.rpcid)
close(s.done)
close(s.ch)
return err
}
func (s *rpcSession) recv() (*session.MessageCloser, error) {
timer := time.NewTimer(s.dht.fixedTimeout)
defer timer.Stop()
select {
case msg := <-s.ch:
if msg == nil {
return nil, errors.New("received nil message")
}
s.dht.logf(LogInfo, "recv %s from %s %d",
msgTypeString(msg.Hdr.MsgType), msg.Hdr.IP,
msg.Hdr.Port)
return msg, nil
case <-s.done:
return nil, errors.New("session expired")
case <-timer.C:
return nil, errors.New("receive timeout expired")
}
}
func (s *rpcSession) send(payload core.MessagePayload, target *core.NodeTriple) (err error) {
exp := time.Now().Add(s.dht.fixedTimeout)
msg := &core.Message{
Version: core.Version,
BodyKind: payload.BodyKind(),
Hdr: &core.Header{
NetworkID: s.dht.networkId,
MsgType: payload.MsgType(),
ID: s.dht.self.ID,
PuzDynX: s.dht.x,
IP: s.dht.self.IP,
Port: s.dht.self.Port,
RPCID: s.rpcid,
Time: uint64(exp.Unix()),
},
Payload: payload,
}
s.dht.logf(LogInfo, "send %s to %s %d", msgTypeString(msg.Hdr.MsgType),
target.IP, target.Port)
switch msg.BodyKind {
case core.KindFixed:
addr := &net.UDPAddr{
IP: target.IP,
Port: int(target.Port),
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return err
}
defer func() {
if err2 := conn.Close(); err == nil {
err = err2
}
}()
if err = conn.SetDeadline(time.Now().Add(s.dht.fixedTimeout)); err != nil {
return err
}
packet, err := s.dht.codec.EncodeFixed(msg, s.dht.priv, target.ID)
if err != nil {
return err
}
_, err = conn.Write(packet)
return err
case core.KindStream:
addr := &net.TCPAddr{
IP: target.IP,
Port: int(target.Port),
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return err
}
defer func() {
if err2 := conn.Close(); err == nil {
err = err2
}
}()
if err = conn.SetDeadline(time.Now().Add(s.dht.streamTimeout)); err != nil {
return err
}
return s.dht.codec.EncodeStream(msg, conn, s.dht.priv, target.ID)
default:
return errors.New("message body kind unsupported")
}
}