-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2PPeer_ConnDIsc.go
78 lines (66 loc) · 2.04 KB
/
P2PPeer_ConnDIsc.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
package epsp
import (
"context"
traditionalnet "net"
"strings"
"time"
"github.com/pkg/errors"
net "github.com/toyo/go-net"
)
// NewP2PServer は、ピアからの接続を待ちます
func NewP2PServer(ctx context.Context, l *traditionalnet.TCPListener, myagent []string) (ps *P2PPeer, err error) {
ps = new(P2PPeer)
ps.conn, err = l.AcceptTCP()
if err != nil {
if ne, ok := err.(traditionalnet.Error); ok {
if ne.Temporary() {
err = errors.Wrap(err, "AcceptTCP")
return
}
err = errors.Wrap(err, "AcceptTCP0")
return
}
err = errors.Wrap(err, "AcceptTCP1")
return
}
ps.IPPort = ps.conn.RemoteAddr().String()
logln(`[INFO] ピア` + ps.IPPort + `: TCP接続受理`)
ps.EPSPConn.SetConnTime()
if err = ps.Write(`614`, `1`, strings.Join(myagent, `:`)); err != nil { // バージョン要求
ps.Close()
return
}
logln("[DEBUG] ピア" + ps.IPPort + `: バージョン要求` + ` ` + strings.Join(myagent, `:`))
return
}
// NewP2PClient は、他のピアと接続します。
func NewP2PClient(ctx context.Context, ipportpeerid string, connectedIPPortPeersList func() []string) (pc *P2PPeer, err error) {
ipportpeerids := strings.Split(ipportpeerid, `,`)
for _, connedctedipportpeerid := range connectedIPPortPeersList() {
connedctedipportpeerids := strings.Split(connedctedipportpeerid, `,`)
if ipportpeerids[2] == connedctedipportpeerids[2] {
err = errors.New(`PeerID重複: ` + ipportpeerid + ` == ` + connedctedipportpeerid)
return
}
}
pc = new(P2PPeer)
pc.IPPort = ipportpeerids[0] + `:` + ipportpeerids[1]
pc.PeerID = ipportpeerids[2]
ctxtimeout, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
pc.EPSPConn.conn, err = net.DialContext(ctxtimeout, `tcp`, pc.EPSPConn.IPPort)
if err != nil {
err = errors.Wrap(err, `TCP接続不可エラー`)
pc.Close()
return
}
logln("[INFO] ピア", pc.PeerID, ": TCP接続完了 ", pc.EPSPConn.IPPort)
pc.SetConnTime()
return
}
// Close は、ピア接続を終了します
func (p *P2PPeer) Close() {
if p != nil {
p.EPSPConn.Close()
}
}