Debian Codegraph @

c/src/handlers/flowcontroller.c

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
22 #include <proton/link.h>
23 #include <proton/handlers.h>
24 #include <assert.h>
25
26 typedef struct {
27 int window;
28 int drained;
29 } pni_flowcontroller_t;
30
31 pni_flowcontroller_t *pni_flowcontroller(pn_handler_t *handler) {
32 return (pni_flowcontroller_t *) pn_handler_mem(handler);
33 }
34
35 static void pni_topup(pn_link_t *link, int window) {
36 int delta = window - pn_link_credit(link);
37 pn_link_flow(link, delta);
38 }
39
40 static void pn_flowcontroller_dispatch(pn_handler_t *handler, pn_event_t *event, pn_event_type_t type) {
41 pni_flowcontroller_t *fc = pni_flowcontroller(handler);
42 int window = fc->window;
43 pn_link_t *link = pn_event_link(event);
44
45 switch (pn_event_type(event)) {
46 case PN_LINK_LOCAL_OPEN:
47 case PN_LINK_REMOTE_OPEN:
48 case PN_LINK_FLOW:
49 case PN_DELIVERY:
50 if (pn_link_is_receiver(link)) {
51 fc->drained += pn_link_drained(link);
52 if (!fc->drained) {
53 pni_topup(link, window);
54 }
55 }
56 break;
57 default:
58 break;
59 }
60 }
61
62 pn_flowcontroller_t *pn_flowcontroller(int window) {
63 // XXX: a window of 1 doesn't work because we won't necessarily get
64 // notified when the one allowed delivery is settled
65 assert(window > 1);
66 pn_flowcontroller_t *handler = pn_handler_new(pn_flowcontroller_dispatch, sizeof(pni_flowcontroller_t), NULL);
67 pni_flowcontroller_t *fc = pni_flowcontroller(handler);
68 fc->window = window;
69 fc->drained = 0;
70 return handler;
71 }