You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Implement peer String() method
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/veggiedefender/torrent-client/bitfield"
|
"github.com/veggiedefender/torrent-client/bitfield"
|
||||||
@@ -64,8 +63,7 @@ func recvBitfield(conn net.Conn) (bitfield.Bitfield, error) {
|
|||||||
// New connects with a peer, completes a handshake, and receives a handshake
|
// New connects with a peer, completes a handshake, and receives a handshake
|
||||||
// returns an err if any of those fail.
|
// returns an err if any of those fail.
|
||||||
func New(peer peers.Peer, peerID, infoHash [20]byte) (*Client, error) {
|
func New(peer peers.Peer, peerID, infoHash [20]byte) (*Client, error) {
|
||||||
hostPort := net.JoinHostPort(peer.IP.String(), strconv.Itoa(int(peer.Port)))
|
conn, err := net.DialTimeout("tcp", peer.String(), 3*time.Second)
|
||||||
conn, err := net.DialTimeout("tcp", hostPort, 3*time.Second)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Peer encodes connection information for a peer
|
// Peer encodes connection information for a peer
|
||||||
@@ -28,3 +29,7 @@ func Unmarshal(peersBin []byte) ([]Peer, error) {
|
|||||||
}
|
}
|
||||||
return peers, nil
|
return peers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Peer) String() string {
|
||||||
|
return net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,3 +37,19 @@ func TestUnmarshal(t *testing.T) {
|
|||||||
assert.Equal(t, test.output, peers)
|
assert.Equal(t, test.output, peers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input Peer
|
||||||
|
output string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: Peer{IP: net.IP{127, 0, 0, 1}, Port: 8080},
|
||||||
|
output: "127.0.0.1:8080",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
s := test.input.String()
|
||||||
|
assert.Equal(t, test.output, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user