From ca85b346121678c1f5e8c5cbf441578b58ca5e16 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 18 Jun 2016 04:18:05 +0200 Subject: Handshake --- peer/messages.go | 18 ++++++++++++++++++ peer/messages_test.go | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 peer/messages_test.go (limited to 'peer') diff --git a/peer/messages.go b/peer/messages.go index a14de46..a6f2815 100644 --- a/peer/messages.go +++ b/peer/messages.go @@ -1,5 +1,7 @@ package peer +import "io" + type Message int const ( @@ -17,3 +19,19 @@ const ( ) const Proto = `BitTorrent protocol` + +type Handshake struct { + Proto string + Flags [8]byte + InfoHash [20]byte + PeerID [20]byte +} + +func (h Handshake) Encode(w io.Writer) { + l := len(h.Proto) + w.Write([]byte{byte(l)}) + w.Write([]byte(h.Proto)) + w.Write(h.Flags[:]) + w.Write(h.InfoHash[:]) + w.Write(h.PeerID[:]) +} diff --git a/peer/messages_test.go b/peer/messages_test.go new file mode 100644 index 0000000..5640e47 --- /dev/null +++ b/peer/messages_test.go @@ -0,0 +1,13 @@ +package peer + +import ( + "bytes" + "testing" +) + +func TestHandshake(t *testing.T) { + h := Handshake{Proto: Proto} + b := &bytes.Buffer{} + h.Encode(b) + t.Log(b.Bytes()) +} -- cgit v1.2.3