Exemple #1
0
// Write writes an Envelope to the given writer.
func Write(p protocol.Protocol, w io.Writer, seqID int32, e Enveloper) error {
	body, err := e.ToWire()
	if err != nil {
		return err
	}
	return p.EncodeEnveloped(wire.Envelope{
		SeqID: seqID,
		Name:  e.MethodName(),
		Type:  e.EnvelopeType(),
		Value: body,
	}, w)
}
Exemple #2
0
// ReadReply reads enveloped responses from the given reader.
func ReadReply(p protocol.Protocol, r io.ReaderAt) (_ wire.Value, seqID int32, _ error) {
	envelope, err := p.DecodeEnveloped(r)
	if err != nil {
		return wire.Value{}, 0, err
	}

	switch {
	case envelope.Type == wire.Reply:
		return envelope.Value, envelope.SeqID, nil
	case envelope.Type != wire.Exception:
		return envelope.Value, envelope.SeqID, fmt.Errorf("unknown envelope type for reply, got %v", envelope.Type)
	}

	// Decode the exception payload.
	ex := &exception.TApplicationException{}
	if err := ex.FromWire(envelope.Value); err != nil {
		return envelope.Value, envelope.SeqID, fmt.Errorf("failed to decode exception: %v", err)
	}

	return envelope.Value, envelope.SeqID, ex
}