package peer import "io" type Message int const ( Choke Message = iota // choke: Unchoke // unchoke: Interested // interested: NotInterested // not interested: Have // have: BitField // bitfield: Request // request: Piece // piece: Cancel // cancel: Port // port: _ // keep-alive: ) 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[:]) }