forked from apache/qpid-proton
/
message.go
79 lines (70 loc) · 2.59 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package proton
// #include <proton/types.h>
// #include <proton/message.h>
// #include <proton/codec.h>
import "C"
import (
"qpid.apache.org/proton/amqp"
"qpid.apache.org/proton/internal"
)
// HasMessage is true if all message data is available.
// Equivalent to !d.isNil && d.Readable() && !d.Partial()
func (d Delivery) HasMessage() bool { return !d.IsNil() && d.Readable() && !d.Partial() }
// 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
}
// TODO aconway 2015-04-08: proper handling of delivery tags. Tag counter per link.
var tags internal.IdCounter
// 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
}