Move annoucement stuff to handshake.go
3 files changed, 112 insertions(+), 109 deletions(-)

A => handshake.go
M kgp.go
M kgp_test.go
A => handshake.go +112 -0
@@ 0,0 1,112 @@ 
+package kgp
+
+import (
+	"bytes"
+	"encoding/binary"
+	"encoding/json"
+	"fmt"
+	"io"
+)
+
+type UUID [16]byte
+
+type Announcement struct {
+	UUID     UUID
+	Metadata io.Reader
+}
+
+type announcementPacket struct {
+	Kgp      [3]byte
+	Major    uint32
+	Minor    uint32
+	Revision uint32
+	UUID     UUID
+	Length   uint32
+}
+
+func (a *Announcement) Write(w io.Writer) (n int, err error) {
+	var meta bytes.Buffer
+	_, err = meta.ReadFrom(a.Metadata)
+	if err != nil {
+		return
+	}
+	var packet = announcementPacket{
+		Kgp:      [...]byte{'k', 'g', 'p'},
+		Major:    0,
+		Minor:    1,
+		Revision: 0,
+		UUID:     a.UUID,
+		Length:   uint32(meta.Len()),
+	}
+
+	if err = binary.Write(w, binary.BigEndian, &packet); err != nil {
+		return
+	}
+	n = binary.Size(&packet)
+
+	var x int64
+	x, err = io.Copy(w, &meta)
+	n += int(x)
+
+	return
+}
+
+func ReadAnnouncement(reader io.Reader) (a Announcement, err error) {
+	var packet announcementPacket
+
+	if err = binary.Read(reader, binary.BigEndian, &packet); err != nil {
+		return
+	}
+	if !bytes.Equal(packet.Kgp[:], []byte{'k', 'g', 'p'}) {
+		err = fmt.Errorf("invalid header: %q", packet.Kgp)
+		return
+	}
+
+	if packet.Major != 0 || packet.Minor != 1 || packet.Revision != 0 {
+		err = fmt.Errorf(
+			"invalid protocol version: %d.%d.%d",
+			packet.Major, packet.Minor, packet.Revision,
+		)
+		return
+	}
+
+	copy(a.UUID[:], packet.UUID[:])
+
+	if packet.Length > 0 {
+		var meta = &bytes.Buffer{}
+		a.Metadata = meta
+
+		if _, err = io.CopyN(meta, reader, int64(packet.Length)); err != nil {
+			return
+		}
+	}
+
+	return
+}
+
+// Return the last seen packet from the last client connected to this tunnel.
+// Use this as the sequence number + 1 with Demux.Run()
+func Handshake(conn io.ReadWriter, meta io.Reader) (lastSeenSeq uint32, err error) {
+	var my = Announcement{
+		Metadata: meta,
+	}
+	if _, err = my.Write(conn); err != nil {
+		return
+	}
+
+	a, err := ReadAnnouncement(conn)
+	if err != nil {
+		return
+	}
+
+	var doc struct {
+		// We have to use float64 otherwise it won't parse
+		Last_seen_seq float64
+	}
+	if err = json.NewDecoder(a.Metadata).Decode(&doc); err != nil {
+		return
+	}
+	lastSeenSeq = uint32(doc.Last_seen_seq)
+
+	return
+}

          
M kgp.go +0 -106
@@ 7,117 7,11 @@ import (
 	"time"
 	"bytes"
 	"context"
-	"encoding/binary"
-	"encoding/json"
-	"fmt"
 	"io"
 	"log"
 	"sync/atomic"
 )
 
-type UUID [16]byte
-
-type Announcement struct {
-	UUID     UUID
-	Metadata io.Reader
-}
-
-type announcementPacket struct {
-	Kgp      [3]byte
-	Major    uint32
-	Minor    uint32
-	Revision uint32
-	UUID     UUID
-	Length   uint32
-}
-
-func (a *Announcement) Write(w io.Writer) (n int, err error) {
-	var meta bytes.Buffer
-	_, err = meta.ReadFrom(a.Metadata)
-	if err != nil {
-		return
-	}
-	var packet = announcementPacket{
-		Kgp:      [...]byte{'k', 'g', 'p'},
-		Major:    0,
-		Minor:    1,
-		Revision: 0,
-		UUID:     a.UUID,
-		Length:   uint32(meta.Len()),
-	}
-
-	if err = binary.Write(w, binary.BigEndian, &packet); err != nil {
-		return
-	}
-	n = binary.Size(&packet)
-
-	var x int64
-	x, err = io.Copy(w, &meta)
-	n += int(x)
-
-	return
-}
-
-func ReadAnnouncement(reader io.Reader) (a Announcement, err error) {
-	var packet announcementPacket
-
-	if err = binary.Read(reader, binary.BigEndian, &packet); err != nil {
-		return
-	}
-	if !bytes.Equal(packet.Kgp[:], []byte{'k', 'g', 'p'}) {
-		err = fmt.Errorf("invalid header: %q", packet.Kgp)
-		return
-	}
-
-	if packet.Major != 0 || packet.Minor != 1 || packet.Revision != 0 {
-		err = fmt.Errorf(
-			"invalid protocol version: %d.%d.%d",
-			packet.Major, packet.Minor, packet.Revision,
-		)
-		return
-	}
-
-	copy(a.UUID[:], packet.UUID[:])
-
-	if packet.Length > 0 {
-		var meta = &bytes.Buffer{}
-		a.Metadata = meta
-
-		if _, err = io.CopyN(meta, reader, int64(packet.Length)); err != nil {
-			return
-		}
-	}
-
-	return
-}
-
-// Return the last seen packet from the last client connected to this tunnel.
-// Use this as the sequence number + 1 with Demux.Run()
-func Handshake(conn io.ReadWriter, meta io.Reader) (lastSeenSeq uint32, err error) {
-	var my = Announcement{
-		Metadata: meta,
-	}
-	if _, err = my.Write(conn); err != nil {
-		return
-	}
-
-	a, err := ReadAnnouncement(conn)
-	if err != nil {
-		return
-	}
-
-	var doc struct {
-		// We have to use float64 otherwise it won't parse
-		Last_seen_seq float64
-	}
-	if err = json.NewDecoder(a.Metadata).Decode(&doc); err != nil {
-		return
-	}
-	lastSeenSeq = uint32(doc.Last_seen_seq)
-
-	return
-}
-
 type ConnectFunc func(context.Context) (io.ReadWriteCloser, error)
 
 //

          
M kgp_test.go +0 -3
@@ 270,9 270,6 @@ func TestRelayRequestResponse(t *testing
 	p.SetSeqAck(1, 0)
 	p.WriteTo(requestWriter)
 
-	// Wait for the channel to open
-	waitChannelCountEqual(1, demux, ctx.Done)
-
 	// Write "hello"
 	p.Payload = []byte("hello")
 	p.SetSeqAck(2, 0)