-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
151 lines (125 loc) · 2.65 KB
/
peer.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
140
141
142
143
144
145
146
147
148
149
150
151
/**
Copyright 2014 JARST, LLC
This file is part of Quibit.
Quibit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
LICENSE file for details.
**/
package quibit
import (
"fmt"
"net"
"strconv"
"time"
)
type Peer struct {
IP net.IP // Standard 16-byte IP Address
Port uint16 // Standard 2-byte TCP Port
conn *net.Conn
external bool
}
func peerFromConn(conn *net.Conn, external bool) Peer {
var p Peer
if conn == nil {
return p
}
addr := (*conn).RemoteAddr()
if addr.Network() != "tcp" {
return p
}
ip, portStr, err := net.SplitHostPort(addr.String())
port, _ := strconv.Atoi(portStr)
if err != nil {
return p
}
// Create New Peer
p.IP = net.ParseIP(ip)
p.Port = uint16(port)
p.conn = conn
p.external = external
return p
}
// String returns the <IP>:<Host> Identifier of Peer
func (p *Peer) String() string {
if p == nil {
return ""
}
return net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
}
// Returns the connection status of Peer
func (p *Peer) IsConnected() bool {
if p == nil {
return false
}
return (p.conn != nil)
}
func (p *Peer) connect() error {
// Check for sane peer object
if p == nil {
return QuibitError(eNILOBJ)
}
if p.conn != nil {
return nil
}
var err error
lConn, err := net.DialTimeout("tcp", p.String(), 10 * time.Second)
p.conn = &lConn
if err != nil {
p.conn = nil
return err
}
// Set Keep-Alives
(*p.conn).(*net.TCPConn).SetKeepAlive(true)
(*p.conn).(*net.TCPConn).SetKeepAlivePeriod(time.Second)
if p.external {
incomingConnections++
}
return nil
}
// Forces Disconnect from Peer and closes all incoming connections.
func (p *Peer) Disconnect() {
if p.conn == nil {
return
}
(*p.conn).Close()
p.conn = nil
if p.external {
incomingConnections--
}
}
func (p *Peer) sendFrame(frame Frame) error {
if p == nil {
return QuibitError(eNILOBJ)
}
if p.conn == nil {
return QuibitError(eNILOBJ)
}
var err error
headerBytes, err := frame.Header.ToBytes()
if err != nil {
return QuibitError(eHEADER)
}
_, err = (*p.conn).Write(append(headerBytes, frame.Payload...))
if err != nil {
return err
}
return nil
}
func (p *Peer) receive(recvChan chan Frame, log chan string) {
if p.conn == nil {
return
}
for {
// So now we have a connection. Let's start receiving
frame, err := recvAll(*p.conn, log)
if err != nil {
log <- fmt.Sprintln("Error receiving from Peer: ", err)
KillPeer(p.String())
break
} else {
frame.Peer = p.String()
recvChan <- frame
}
} // End for
} // End receive()