| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "engine-internal.h" |
| 21 |
#include <proton/condition.h> |
| 22 |
#include <proton/connection.h> |
| 23 |
#include <proton/connection_driver.h> |
| 24 |
#include <proton/event.h> |
| 25 |
#include <proton/transport.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
static pn_event_t *batch_next(pn_connection_driver_t *d) { |
| 29 |
if (!d->collector) return NULL; |
| 30 |
pn_event_t *handled = pn_collector_prev(d->collector); |
| 31 |
if (handled) { |
| 32 |
switch (pn_event_type(handled)) { |
| 33 |
case PN_CONNECTION_INIT: /* Auto-bind after the INIT event is handled */ |
| 34 |
pn_transport_bind(d->transport, d->connection); |
| 35 |
break; |
| 36 |
case PN_TRANSPORT_CLOSED: /* No more events after TRANSPORT_CLOSED */ |
| 37 |
pn_collector_release(d->collector); |
| 38 |
break; |
| 39 |
default: |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
/* Log the next event that will be processed */ |
| 44 |
pn_event_t *next = pn_collector_next(d->collector); |
| 45 |
if (next && PN_SHOULD_LOG(&d->transport->logger, PN_SUBSYSTEM_EVENT, PN_LEVEL_DEBUG)) { |
| 46 |
pni_logger_log_msg_inspect(&d->transport->logger, PN_SUBSYSTEM_EVENT, PN_LEVEL_DEBUG, next, ""); |
| 47 |
} |
| 48 |
return next; |
| 49 |
} |
| 50 |
|
| 51 |
int pn_connection_driver_init(pn_connection_driver_t* d, pn_connection_t *c, pn_transport_t *t) { |
| 52 |
memset(d, 0, sizeof(*d)); |
| 53 |
d->connection = c ? c : pn_connection(); |
| 54 |
d->transport = t ? t : pn_transport(); |
| 55 |
d->collector = pn_collector(); |
| 56 |
if (!d->connection || !d->transport || !d->collector) { |
| 57 |
pn_connection_driver_destroy(d); |
| 58 |
return PN_OUT_OF_MEMORY; |
| 59 |
} |
| 60 |
pn_connection_collect(d->connection, d->collector); |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
int pn_connection_driver_bind(pn_connection_driver_t *d) { |
| 65 |
return pn_transport_bind(d->transport, d->connection); |
| 66 |
} |
| 67 |
|
| 68 |
pn_connection_t *pn_connection_driver_release_connection(pn_connection_driver_t *d) { |
| 69 |
if (d->transport) { /* Make sure transport is closed and unbound */ |
| 70 |
pn_connection_driver_close(d); |
| 71 |
pn_transport_unbind(d->transport); |
| 72 |
} |
| 73 |
pn_connection_t *c = d->connection; |
| 74 |
if (c) { |
| 75 |
d->connection = NULL; |
| 76 |
pn_connection_reset(c); |
| 77 |
pn_connection_collect(c, NULL); /* Disconnect from the collector */ |
| 78 |
} |
| 79 |
return c; |
| 80 |
} |
| 81 |
|
| 82 |
void pn_connection_driver_destroy(pn_connection_driver_t *d) { |
| 83 |
pn_connection_t *c = pn_connection_driver_release_connection(d); |
| 84 |
if (c) pn_connection_free(c); |
| 85 |
if (d->transport) pn_transport_free(d->transport); |
| 86 |
if (d->collector) pn_collector_free(d->collector); |
| 87 |
memset(d, 0, sizeof(*d)); |
| 88 |
} |
| 89 |
|
| 90 |
pn_rwbytes_t pn_connection_driver_read_buffer_sized(pn_connection_driver_t *d, size_t n) { |
| 91 |
ssize_t cap = pni_transport_grow_capacity(d->transport, n); |
| 92 |
return (cap > 0) ? pn_rwbytes(cap, pn_transport_tail(d->transport)) : pn_rwbytes(0, 0); |
| 93 |
} |
| 94 |
|
| 95 |
pn_rwbytes_t pn_connection_driver_read_buffer(pn_connection_driver_t *d) { |
| 96 |
ssize_t cap = pn_transport_capacity(d->transport); |
| 97 |
return (cap > 0) ? pn_rwbytes(cap, pn_transport_tail(d->transport)) : pn_rwbytes(0, 0); |
| 98 |
} |
| 99 |
|
| 100 |
void pn_connection_driver_read_done(pn_connection_driver_t *d, size_t n) { |
| 101 |
if (n > 0) pn_transport_process(d->transport, n); |
| 102 |
} |
| 103 |
|
| 104 |
bool pn_connection_driver_read_closed(pn_connection_driver_t *d) { |
| 105 |
return pn_transport_tail_closed(d->transport); |
| 106 |
} |
| 107 |
|
| 108 |
void pn_connection_driver_read_close(pn_connection_driver_t *d) { |
| 109 |
if (!pn_connection_driver_read_closed(d)) { |
| 110 |
pn_transport_close_tail(d->transport); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
pn_bytes_t pn_connection_driver_write_buffer(pn_connection_driver_t *d) { |
| 115 |
ssize_t pending = pn_transport_pending(d->transport); |
| 116 |
return (pending > 0) ? |
| 117 |
pn_bytes(pending, pn_transport_head(d->transport)) : pn_bytes_null; |
| 118 |
} |
| 119 |
|
| 120 |
pn_bytes_t pn_connection_driver_write_done(pn_connection_driver_t *d, size_t n) { |
| 121 |
pn_transport_pop(d->transport, n); |
| 122 |
ssize_t pending = d->transport->output_pending; |
| 123 |
return (pending > 0) ? |
| 124 |
pn_bytes(pending, pn_transport_head(d->transport)) : pn_bytes_null; |
| 125 |
} |
| 126 |
|
| 127 |
bool pn_connection_driver_write_closed(pn_connection_driver_t *d) { |
| 128 |
return pn_transport_head_closed(d->transport); |
| 129 |
} |
| 130 |
|
| 131 |
void pn_connection_driver_write_close(pn_connection_driver_t *d) { |
| 132 |
if (!pn_connection_driver_write_closed(d)) { |
| 133 |
pn_transport_close_head(d->transport); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void pn_connection_driver_close(pn_connection_driver_t *d) { |
| 138 |
pn_connection_driver_read_close(d); |
| 139 |
pn_connection_driver_write_close(d); |
| 140 |
} |
| 141 |
|
| 142 |
pn_event_t* pn_connection_driver_next_event(pn_connection_driver_t *d) { |
| 143 |
return batch_next(d); |
| 144 |
} |
| 145 |
|
| 146 |
bool pn_connection_driver_has_event(pn_connection_driver_t *d) { |
| 147 |
return d->connection && pn_collector_peek(pn_connection_collector(d->connection)); |
| 148 |
} |
| 149 |
|
| 150 |
bool pn_connection_driver_finished(pn_connection_driver_t *d) { |
| 151 |
return pn_transport_closed(d->transport) && !pn_connection_driver_has_event(d); |
| 152 |
} |
| 153 |
|
| 154 |
void pn_connection_driver_verrorf(pn_connection_driver_t *d, const char *name, const char *fmt, va_list ap) { |
| 155 |
pn_transport_t *t = d->transport; |
| 156 |
pn_condition_t *cond = pn_transport_condition(t); |
| 157 |
pn_condition_vformat(cond, name, fmt, ap); |
| 158 |
} |
| 159 |
|
| 160 |
void pn_connection_driver_errorf(pn_connection_driver_t *d, const char *name, const char *fmt, ...) { |
| 161 |
va_list ap; |
| 162 |
va_start(ap, fmt); |
| 163 |
pn_connection_driver_verrorf(d, name, fmt, ap); |
| 164 |
va_end(ap); |
| 165 |
} |
| 166 |
|
| 167 |
void pn_connection_driver_log(pn_connection_driver_t *d, const char *msg) { |
| 168 |
pni_logger_log(&d->transport->logger, PN_SUBSYSTEM_IO, PN_LEVEL_TRACE, msg); |
| 169 |
} |
| 170 |
|
| 171 |
void pn_connection_driver_logf(pn_connection_driver_t *d, const char *fmt, ...) { |
| 172 |
va_list ap; |
| 173 |
va_start(ap, fmt); |
| 174 |
pni_logger_vlogf(&d->transport->logger, PN_SUBSYSTEM_IO, PN_LEVEL_TRACE, fmt, ap); |
| 175 |
va_end(ap); |
| 176 |
} |
| 177 |
|
| 178 |
void pn_connection_driver_vlogf(pn_connection_driver_t *d, const char *fmt, va_list ap) { |
| 179 |
pni_logger_vlogf(&d->transport->logger, PN_SUBSYSTEM_IO, PN_LEVEL_TRACE, fmt, ap); |
| 180 |
} |
| 181 |
|
| 182 |
pn_connection_driver_t** pn_connection_driver_ptr(pn_connection_t *c) { return &c->driver; } |
| 183 |
|
| 184 |
/* Backwards ABI compatability hack - this has been removed because it can't be used sanely */ |
| 185 |
PN_EXTERN pn_connection_driver_t *pn_event_batch_connection_driver(pn_event_batch_t *b) { return NULL; } |