예제 #1
0
// HandleMessage marshals and unmarshals net messages, forwarding them to the
// BitSwapMessage receiver
func (bsnet *impl) HandleMessage(
	ctx context.Context, incoming netmsg.NetMessage) netmsg.NetMessage {

	if bsnet.receiver == nil {
		return nil
	}

	received, err := bsmsg.FromNet(incoming)
	if err != nil {
		go bsnet.receiver.ReceiveError(err)
		return nil
	}

	p, bsmsg := bsnet.receiver.ReceiveMessage(ctx, incoming.Peer(), received)

	// TODO(brian): put this in a helper function
	if bsmsg == nil || p == nil {
		bsnet.receiver.ReceiveError(errors.New("ReceiveMessage returned nil peer or message"))
		return nil
	}

	outgoing, err := bsmsg.ToNet(p)
	if err != nil {
		go bsnet.receiver.ReceiveError(err)
		return nil
	}

	log.Debugf("Message size: %d", len(outgoing.Data()))
	return outgoing
}
예제 #2
0
func (bsnet *impl) SendRequest(
	ctx context.Context,
	p peer.Peer,
	outgoing bsmsg.BitSwapMessage) (bsmsg.BitSwapMessage, error) {

	outgoingMsg, err := outgoing.ToNet(p)
	if err != nil {
		return nil, err
	}
	incomingMsg, err := bsnet.service.SendRequest(ctx, outgoingMsg)
	if err != nil {
		return nil, err
	}
	return bsmsg.FromNet(incomingMsg)
}