aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--peer/messages.go18
-rw-r--r--peer/messages_test.go13
2 files changed, 31 insertions, 0 deletions
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())
+}