Debian Codegraph @

cpp/src/receiver.cpp

raw
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 #include "proton/receiver.hpp"
22
23 #include "proton/error.hpp"
24 #include "proton/link.hpp"
25 #include "proton/receiver_options.hpp"
26 #include "proton/source.hpp"
27 #include "proton/target.hpp"
28
29 #include "msg.hpp"
30 #include "proton_bits.hpp"
31 #include "contexts.hpp"
32
33 #include <proton/connection.h>
34 #include <proton/session.h>
35 #include <proton/link.h>
36 #include <proton/event.h>
37
38 namespace proton {
39
40 receiver::receiver(pn_link_t* r): link(make_wrapper(r)) {}
41
42 receiver::~receiver() = default;
43
44 void receiver::open() {
45 attach();
46 }
47
48 void receiver::open(const receiver_options &opts) {
49 opts.apply(*this);
50 attach();
51 }
52
53 class source receiver::source() const {
54 return proton::source(*this);
55 }
56
57 class target receiver::target() const {
58 return proton::target(*this);
59 }
60
61 void receiver::add_credit(uint32_t credit) {
62 link_context &ctx = link_context::get(pn_object());
63 if (ctx.draining)
64 ctx.pending_credit += credit;
65 else
66 pn_link_flow(pn_object(), credit);
67 }
68
69 void receiver::drain() {
70 link_context &ctx = link_context::get(pn_object());
71 if (ctx.draining)
72 throw proton::error("drain already in progress");
73 else {
74 ctx.draining = true;
75 if (credit() > 0)
76 pn_link_set_drain(pn_object(), true);
77 else {
78 // Drain is already complete. No state to communicate over the wire.
79 // Create dummy flow event where "drain finish" can be detected.
80 pn_connection_t *pnc = pn_session_connection(pn_link_session(pn_object()));
81 pn_collector_put(pn_connection_collector(pnc), PN_OBJECT, pn_object(), PN_LINK_FLOW);
82 }
83 }
84 }
85
86 receiver_iterator receiver_iterator::operator++() {
87 if (!!obj_) {
88 pn_link_t *lnk = pn_link_next(obj_.pn_object(), 0);
89 while (lnk) {
90 if (pn_link_is_receiver(lnk) && pn_link_session(lnk) == session_)
91 break;
92 lnk = pn_link_next(lnk, 0);
93 }
94 obj_ = lnk;
95 }
96 return *this;
97 }
98
99 }