// GetData retrieves requests objects with the specified hashes from the // peer node that sent us ver. It returns the number of objects // successfully received and an error if n < len(hashes). func (nd *Node) GetData(ver *payload.Version, hashes [][]byte) (n int, err error) { conn, err := net.DialTimeout("tcp", ver.FromAddr.Addr(), defaultTimeout) if err != nil { return n, err } defer conn.Close() pay, err := payload.GetDataEncode(ver.Protocol(), hashes) if err != nil { panic(err) } m := msg.New(msg.Cgetdata, pay) conn.Write(m.Encode()) for i := 0; i < len(hashes); i++ { m, err := msg.Decode(conn) if err != nil { return n, err } switch m.Cmd() { case msg.Cversion, msg.Cverack, msg.Caddr, msg.Cinv, msg.Cgetdata: return n, fmt.Errorf("getdata resp contains invalid msg type %v", m.Cmd()) } nd.ObjectsIn <- m n++ } return n, nil }
func (h *RecvHandler) Handle(conn net.Conn) { m, err := msg.Decode(conn) if err != nil { return } conn.Write(m.Encode()) }
func (n *Node) handleConn(conn net.Conn) { defer func() { if r := recover(); r != nil { n.Log.Print(r) } }() defer conn.Close() m := msg.Must(msg.Decode(conn)) n.Log.Printf("Received msg type %v", m.Cmd()) switch m.Cmd() { case msg.Cversion: n.versionSequence(m, conn) case msg.Cgetdata: n.respondGetData(m, conn) case msg.CgetpubKey, msg.Cpubkey, msg.Cmsg, msg.Cbroadcast: n.ObjectsIn <- m default: n.Log.Printf("Received unsupported communication %v", m.Cmd()) } }