Beispiel #1
0
// decode from bytes.
// Return bytes decoded or 0 if we could not decode a complete object.
//
func decode(data *C.pn_data_t, bytes []byte) int {
	if len(bytes) == 0 {
		return 0
	}
	n := int(C.pn_data_decode(data, cPtr(bytes), cLen(bytes)))
	if n == int(C.PN_UNDERFLOW) {
		C.pn_error_clear(C.pn_data_error(data))
		return 0
	} else if n <= 0 {
		panic(internal.Errorf("unmarshal %s", internal.PnErrorCode(n)))
	}
	return n
}
Beispiel #2
0
// Message decodes the message containined in a delivery.
// Will return an error if delivery.HasMessage() is false.
func (delivery Delivery) Message() (m amqp.Message, err error) {
	if !delivery.Readable() || delivery.Partial() {
		return nil, internal.Errorf("attempting to get incomplete message")
	}
	data := make([]byte, delivery.Pending())
	result := delivery.Link().Recv(data)
	if result != len(data) {
		return nil, internal.Errorf("cannot receive message: %s", internal.PnErrorCode(result))
	}
	m = amqp.NewMessage()
	err = m.Decode(data)
	return
}
Beispiel #3
0
func (m *message) Encode(buffer []byte) ([]byte, error) {
	encode := func(buf []byte) ([]byte, error) {
		len := cLen(buf)
		result := C.pn_message_encode(m.pn, cPtr(buf), &len)
		switch {
		case result == C.PN_OVERFLOW:
			return buf, overflow
		case result < 0:
			return buf, internal.Errorf("cannot encode message: %s", internal.PnErrorCode(result))
		default:
			return buf[:len], nil
		}
	}
	return encodeGrow(buffer, encode)
}
Beispiel #4
0
// Send sends a amqp.Message over a Link.
// Returns a Delivery that can be use to determine the outcome of the message.
func (link Link) Send(m amqp.Message) (Delivery, error) {
	if !link.IsSender() {
		return Delivery{}, internal.Errorf("attempt to send message on receiving link")
	}
	delivery := link.Delivery(tags.Next())
	bytes, err := m.Encode(nil)
	if err != nil {
		return Delivery{}, internal.Errorf("cannot send mesage %s", err)
	}
	result := link.SendBytes(bytes)
	link.Advance()
	if result != len(bytes) {
		if result < 0 {
			return delivery, internal.Errorf("send failed %v", internal.PnErrorCode(result))
		} else {
			return delivery, internal.Errorf("send incomplete %v of %v", result, len(bytes))
		}
	}
	if link.RemoteSndSettleMode() == SndSettled {
		delivery.Settle()
	}
	return delivery, nil
}