diff --git a/peer/msg.go b/peer/msg.go index 3ee4cf380..df02a88b9 100644 --- a/peer/msg.go +++ b/peer/msg.go @@ -1,12 +1,9 @@ package peer import ( - "io" - . "github.com/tendermint/tendermint/binary" ) type Message interface { Binary - Type() string } diff --git a/peer/peer.go b/peer/peer.go index 5fbf47686..7f8e41373 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -201,8 +201,8 @@ func NewPacket(chName String, bytes ByteSlice) Packet { } func (p Packet) WriteTo(w io.Writer) (n int64, err error) { - n, err = WriteOnto(&p.Channel, w, n, err) - n, err = WriteOnto(&p.Bytes, w, n, err) + n, err = WriteOnto(p.Channel, w, n, err) + n, err = WriteOnto(p.Bytes, w, n, err) return } diff --git a/peer/pex.go b/peer/pex.go new file mode 100644 index 000000000..dc0a0319d --- /dev/null +++ b/peer/pex.go @@ -0,0 +1,67 @@ +package peer + +import ( + . "github.com/tendermint/tendermint/binary" + "io" +) + +const pexCh = "PEX" + +func peerExchangeHandler(c *Client) { + + for { + // inPkt := c.Receive(pexCh) // {Peer, Time, Packet} + + // decode message + + // if message is a peer request + + // if message is + } + + // cleanup + +} + +/* Messages */ + +const ( + pexTypeRequest = Byte(0x00) + pexTypeResponse = Byte(0x01) +) + +func decodeMsg(bytes ByteSlice) (t Byte, msg Message) { + //return pexTypeRequest, nil + return pexTypeResponse, nil +} + +/* +A response with peer addresses +*/ +type pexResponseMsg struct { + Addrs []*NetAddress +} + +func readPexResponseMsg(r io.Reader) *pexResponseMsg { + numAddrs := int(ReadUInt32(r)) + addrs := []*NetAddress{} + for i := 0; i < numAddrs; i++ { + addr := ReadNetAddress(r) + addrs = append(addrs, addr) + } + return &pexResponseMsg{ + Addrs: addrs, + } +} + +func (m *pexResponseMsg) WriteTo(w io.Writer) (n int64, err error) { + n, err = WriteOnto(UInt32(len(m.Addrs)), w, n, err) + for _, addr := range m.Addrs { + n, err = WriteOnto(addr, w, n, err) + } + return +} + +func (m *pexResponseMsg) Type() string { + return "pexTypeResponse" +}