Debian Codegraph @

c/src/core/dispatcher.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 "dispatcher.h"
23
24 #include "consumers.h"
25 #include "dispatch_actions.h"
26 #include "engine-internal.h"
27 #include "framing.h"
28 #include "logger_private.h"
29 #include "protocol.h"
30
31
32 int pni_bad_frame(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) {
33 PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "Error dispatching frame: type: %d: Unknown performative", frame_type);
34 return PN_ERR;
35 }
36
37 int pni_bad_frame_type(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload) {
38 PN_LOG(&transport->logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "Error dispatching frame: Unknown frame type: %d", frame_type);
39 return PN_ERR;
40 }
41
42 // We could use a table based approach here if we needed to dynamically
43 // add new performatives
44 static inline int pni_dispatch_action(pn_transport_t* transport, uint64_t lcode, uint8_t frame_type, uint16_t channel, pn_bytes_t frame_payload)
45 {
46 pn_action_t *action;
47 switch (frame_type) {
48 case AMQP_FRAME_TYPE:
49 /* Regular AMQP frames */
50 switch (lcode) {
51 case OPEN: action = pn_do_open; break;
52 case BEGIN: action = pn_do_begin; break;
53 case ATTACH: action = pn_do_attach; break;
54 case FLOW: action = pn_do_flow; break;
55 case TRANSFER: action = pn_do_transfer; break;
56 case DISPOSITION: action = pn_do_disposition; break;
57 case DETACH: action = pn_do_detach; break;
58 case END: action = pn_do_end; break;
59 case CLOSE: action = pn_do_close; break;
60 default: action = pni_bad_frame; break;
61 };
62 break;
63 case SASL_FRAME_TYPE:
64 /* SASL frames */
65 switch (lcode) {
66 case SASL_MECHANISMS: action = pn_do_mechanisms; break;
67 case SASL_INIT: action = pn_do_init; break;
68 case SASL_CHALLENGE: action = pn_do_challenge; break;
69 case SASL_RESPONSE: action = pn_do_response; break;
70 case SASL_OUTCOME: action = pn_do_outcome; break;
71 default: action = pni_bad_frame; break;
72 };
73 break;
74 default: action = pni_bad_frame_type; break;
75 };
76 return action(transport, frame_type, channel, frame_payload);
77 }
78
79 static int pni_dispatch_frame(pn_frame_t frame, pn_logger_t *logger, pn_transport_t * transport)
80 {
81 pn_bytes_t frame_payload = frame.frame_payload0;
82
83 if (frame_payload.size == 0) { // ignore null frames
84 return 0;
85 }
86
87 uint64_t lcode;
88 pni_consumer_t consumer = make_consumer_from_bytes(frame_payload);
89 pni_consumer_t subconsumer;
90 if (!consume_described_ulong_descriptor(&consumer, &subconsumer, &lcode)) {
91 PN_LOG(logger, PN_SUBSYSTEM_AMQP, PN_LEVEL_ERROR, "Error dispatching frame");
92 return PN_ERR;
93 }
94
95 uint8_t frame_type = frame.type;
96 uint16_t channel = frame.channel;
97
98 int err = pni_dispatch_action(transport, lcode, frame_type, channel, frame_payload);
99
100 return err;
101 }
102
103 ssize_t pn_dispatcher_input(pn_transport_t *transport, const char *bytes, size_t available, bool batch, bool *halt)
104 {
105 size_t read = 0;
106
107 while (available && !*halt) {
108 pn_frame_t frame;
109
110 ssize_t n = pn_read_frame(&frame, bytes + read, available, transport->local_max_frame, &transport->logger);
111 if (n > 0) {
112 read += n;
113 available -= n;
114 transport->input_frames_ct += 1;
115 int e = pni_dispatch_frame(frame, &transport->logger, transport);
116 if (e) return e;
117 } else if (n < 0) {
118 pn_do_error(transport, "amqp:connection:framing-error", "malformed frame");
119 return n;
120 } else {
121 break;
122 }
123
124 if (!batch) break;
125 }
126
127 return read;
128 }
129
130 ssize_t pn_dispatcher_output(pn_transport_t *transport, char *bytes, size_t size)
131 {
132 int n = pn_buffer_get(transport->output_buffer, 0, size, bytes);
133 pn_buffer_trim(transport->output_buffer, n, 0);
134 // XXX: need to check for errors
135 return n;
136 }