| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "proton/sender.hpp" |
| 23 |
|
| 24 |
#include "proton/link.hpp" |
| 25 |
#include "proton/sender_options.hpp" |
| 26 |
#include "proton/source.hpp" |
| 27 |
#include "proton/target.hpp" |
| 28 |
#include "proton/tracker.hpp" |
| 29 |
|
| 30 |
#include <proton/delivery.h> |
| 31 |
#include <proton/link.h> |
| 32 |
#include <proton/types.h> |
| 33 |
|
| 34 |
#include "proton_bits.hpp" |
| 35 |
#include "contexts.hpp" |
| 36 |
|
| 37 |
#include <assert.h> |
| 38 |
|
| 39 |
namespace proton { |
| 40 |
|
| 41 |
sender::sender(pn_link_t *l): link(make_wrapper(l)) {} |
| 42 |
|
| 43 |
sender::~sender() = default; |
| 44 |
|
| 45 |
void sender::open() { |
| 46 |
attach(); |
| 47 |
} |
| 48 |
|
| 49 |
void sender::open(const sender_options &opts) { |
| 50 |
opts.apply(*this); |
| 51 |
attach(); |
| 52 |
} |
| 53 |
|
| 54 |
class source sender::source() const { |
| 55 |
return proton::source(*this); |
| 56 |
} |
| 57 |
|
| 58 |
class target sender::target() const { |
| 59 |
return proton::target(*this); |
| 60 |
} |
| 61 |
|
| 62 |
tracker sender::send(const message &message) { |
| 63 |
uint64_t id = ++tag_counter; |
| 64 |
const uint8_t *begin = reinterpret_cast<const uint8_t *>(&id); |
| 65 |
const binary tag(begin, begin + sizeof(id)); |
| 66 |
return send(message, tag); |
| 67 |
} |
| 68 |
|
| 69 |
tracker sender::send(const message &message, const binary &tag) { |
| 70 |
pn_delivery_t *dlv = pn_delivery( |
| 71 |
pn_object(), |
| 72 |
pn_dtag((reinterpret_cast<const char *>(&tag[0])), tag.size())); |
| 73 |
std::vector<char> buf; |
| 74 |
message.encode(buf); |
| 75 |
assert(!buf.empty()); |
| 76 |
pn_link_send(pn_object(), &buf[0], buf.size()); |
| 77 |
pn_link_advance(pn_object()); |
| 78 |
if (pn_link_snd_settle_mode(pn_object()) == PN_SND_SETTLED) |
| 79 |
pn_delivery_settle(dlv); |
| 80 |
if (!pn_link_credit(pn_object())) |
| 81 |
link_context::get(pn_object()).draining = false; |
| 82 |
return make_wrapper<tracker>(dlv); |
| 83 |
} |
| 84 |
|
| 85 |
void sender::return_credit() { |
| 86 |
link_context &lctx = link_context::get(pn_object()); |
| 87 |
lctx.draining = false; |
| 88 |
pn_link_drained(pn_object()); |
| 89 |
} |
| 90 |
|
| 91 |
sender_iterator sender_iterator::operator++() { |
| 92 |
if (!!obj_) { |
| 93 |
pn_link_t *lnk = pn_link_next(obj_.pn_object(), 0); |
| 94 |
while (lnk) { |
| 95 |
if (pn_link_is_sender(lnk) && pn_link_session(lnk) == session_) |
| 96 |
break; |
| 97 |
lnk = pn_link_next(lnk, 0); |
| 98 |
} |
| 99 |
obj_ = lnk; |
| 100 |
} |
| 101 |
return *this; |
| 102 |
} |
| 103 |
|
| 104 |
} |