| 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 |
/* Enable POSIX features beyond c99 for modern pthread and standard strerror_r() */ |
| 23 |
#ifndef _POSIX_C_SOURCE |
| 24 |
#define _POSIX_C_SOURCE 200809L |
| 25 |
#endif |
| 26 |
/* Avoid GNU extensions, in particular the incompatible alternative strerror_r() */ |
| 27 |
#undef _GNU_SOURCE |
| 28 |
|
| 29 |
#include "proton/raw_connection.h" |
| 30 |
|
| 31 |
#include "proton/event.h" |
| 32 |
#include "proton/listener.h" |
| 33 |
#include "proton/object.h" |
| 34 |
#include "proton/proactor.h" |
| 35 |
#include "proton/types.h" |
| 36 |
|
| 37 |
#include "core/util.h" |
| 38 |
#include "proactor-internal.h" |
| 39 |
|
| 40 |
#include <assert.h> |
| 41 |
#include <errno.h> |
| 42 |
#include <stdint.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#include "raw_connection-internal.h" |
| 47 |
|
| 48 |
PN_STRUCT_CLASSDEF(pn_raw_connection) |
| 49 |
|
| 50 |
void pni_raw_initialize(pn_raw_connection_t *conn) { |
| 51 |
// Link together free lists |
| 52 |
for (buff_ptr i = 1; i<=read_buffer_count; i++) { |
| 53 |
conn->rbuffers[i-1].next = i==read_buffer_count ? 0 : i+1; |
| 54 |
conn->rbuffers[i-1].type = buff_rempty; |
| 55 |
conn->wbuffers[i-1].next = i==read_buffer_count ? 0 : i+1; |
| 56 |
conn->wbuffers[i-1].type = buff_wempty; |
| 57 |
} |
| 58 |
|
| 59 |
conn->condition = pn_condition(); |
| 60 |
conn->collector = pn_collector(); |
| 61 |
conn->attachments = pn_record(); |
| 62 |
|
| 63 |
conn->rbuffer_first_empty = 1; |
| 64 |
conn->wbuffer_first_empty = 1; |
| 65 |
|
| 66 |
conn->state = conn_init; |
| 67 |
} |
| 68 |
|
| 69 |
typedef enum { |
| 70 |
conn_connected = 0, |
| 71 |
conn_read_closed = 1, |
| 72 |
conn_write_closed = 2, |
| 73 |
conn_write_drained = 3, |
| 74 |
int_read_shutdown = 4, |
| 75 |
int_write_shutdown = 5, |
| 76 |
int_disconnect = 6, |
| 77 |
api_read_close = 7, |
| 78 |
api_write_close = 8, |
| 79 |
} raw_event; |
| 80 |
|
| 81 |
/* |
| 82 |
* There's a little trick in this state table - both conn_init - the initial state and the self transition are represented by |
| 83 |
* 0. This is because no state ever transitions to the initial state. |
| 84 |
*/ |
| 85 |
const uint8_t state_transitions[][9] = { |
| 86 |
/* State\event cconnected, crclosed, cwclosed, cwdrained, irshutdown, iwshutdown, idsconnect, arclose, awclose */ |
| 87 |
[conn_init] = {conn_ro_wo, conn_wrong, conn_wrong, conn_wrong, conn_wrong, conn_wrong, conn_wrong, conn_rc_wo, conn_ro_wd}, |
| 88 |
[conn_ro_wo] = {conn_wrong, conn_rc_wo, conn_ro_wc, 0, conn_wrong, conn_wrong, conn_wrong, conn_rc_wo, conn_ro_wd}, |
| 89 |
[conn_ro_wd] = {conn_wrong, conn_rc_wd, conn_ro_wc, conn_ro_wc, conn_wrong, conn_wrong, conn_wrong, conn_rc_wd, 0}, |
| 90 |
[conn_ro_wc] = {conn_wrong, conn_rs_ws, 0, conn_wrong, conn_wrong, conn_ro_ws, conn_wrong, conn_rs_ws, 0}, |
| 91 |
[conn_ro_ws] = {conn_wrong, conn_rs_ws, 0, 0, conn_wrong, conn_wrong, conn_wrong, conn_rs_ws, 0}, |
| 92 |
[conn_rc_wo] = {conn_wrong, 0, conn_rs_ws, 0, conn_rs_wo, conn_wrong, conn_wrong, 0, conn_rc_wd}, |
| 93 |
[conn_rc_wd] = {conn_wrong, 0, conn_rs_ws, conn_rs_ws, conn_rs_wd, conn_wrong, conn_wrong, 0, 0}, |
| 94 |
[conn_rs_wo] = {conn_wrong, 0, conn_rs_ws, 0, conn_wrong, conn_wrong, conn_wrong, 0, conn_rs_wd}, |
| 95 |
[conn_rs_wd] = {conn_wrong, 0, conn_rs_ws, conn_rs_ws, conn_wrong, conn_wrong, conn_wrong, 0, 0}, |
| 96 |
[conn_rs_ws] = {conn_wrong, 0, 0, conn_wrong, conn_wrong, conn_wrong, conn_fini, 0, 0}, |
| 97 |
[conn_fini] = {conn_wrong, conn_wrong, conn_wrong, conn_wrong, conn_wrong, conn_wrong, 0, 0, 0}, |
| 98 |
}; |
| 99 |
|
| 100 |
static inline uint8_t pni_raw_new_state(pn_raw_connection_t *conn, raw_event event) { |
| 101 |
uint8_t old_state = conn->state; |
| 102 |
uint8_t new_state = state_transitions[old_state][event]; |
| 103 |
assert(new_state != (uint8_t)conn_wrong); |
| 104 |
return new_state == 0 ? old_state : new_state; |
| 105 |
} |
| 106 |
|
| 107 |
// Open for read by application |
| 108 |
static inline bool pni_raw_ropen(pn_raw_connection_t *conn) { |
| 109 |
switch (conn->state) { |
| 110 |
case conn_ro_wo: |
| 111 |
case conn_ro_wd: |
| 112 |
case conn_ro_wc: |
| 113 |
case conn_ro_ws: |
| 114 |
return true; |
| 115 |
default: |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Closed for reading by application |
| 121 |
static inline bool pni_raw_rclosed(pn_raw_connection_t *conn) { |
| 122 |
switch (conn->state) { |
| 123 |
case conn_rc_wo: |
| 124 |
case conn_rc_wd: |
| 125 |
case conn_rs_wo: |
| 126 |
case conn_rs_wd: |
| 127 |
case conn_rs_ws: |
| 128 |
case conn_fini : |
| 129 |
return true; |
| 130 |
default: |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Not yet sent read closed event |
| 136 |
static inline bool pni_raw_rclosing(pn_raw_connection_t *conn) { |
| 137 |
switch (conn->state) { |
| 138 |
case conn_rc_wo: |
| 139 |
case conn_rc_wd: |
| 140 |
return true; |
| 141 |
default: |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Unused code |
| 147 |
#if 0 |
| 148 |
// Open for write by application |
| 149 |
static inline bool pni_raw_wopen(pn_raw_connection_t *conn) { |
| 150 |
switch (conn->state) { |
| 151 |
case conn_ro_wo: |
| 152 |
case conn_rc_wo: |
| 153 |
case conn_rs_wo: |
| 154 |
return true; |
| 155 |
default: |
| 156 |
return false; |
| 157 |
} |
| 158 |
} |
| 159 |
#endif |
| 160 |
|
| 161 |
// Return whether closed for writing by application (name a little confusing!) |
| 162 |
// NB Initial state is NOT closed for writing - app can write before connecting |
| 163 |
static inline bool pni_raw_wclosed(pn_raw_connection_t *conn) { |
| 164 |
switch (conn->state) { |
| 165 |
case conn_ro_wd: |
| 166 |
case conn_ro_wc: |
| 167 |
case conn_ro_ws: |
| 168 |
case conn_rc_wd: |
| 169 |
case conn_rs_wd: |
| 170 |
case conn_rs_ws: |
| 171 |
case conn_fini : |
| 172 |
return true; |
| 173 |
default: |
| 174 |
return false; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// Return whether closed for application and all writes drained |
| 179 |
static inline bool pni_raw_wdrained(pn_raw_connection_t *conn) { |
| 180 |
switch (conn->state) { |
| 181 |
case conn_ro_wc: |
| 182 |
case conn_ro_ws: |
| 183 |
case conn_rs_ws: |
| 184 |
case conn_fini : |
| 185 |
return true; |
| 186 |
default: |
| 187 |
return false; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Not yet sent write closed event |
| 192 |
static inline bool pni_raw_wclosing(pn_raw_connection_t *conn) { |
| 193 |
switch (conn->state) { |
| 194 |
case conn_ro_wc: |
| 195 |
return true; |
| 196 |
default: |
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Fully closed for read/write - sent both closed events |
| 202 |
static inline bool pni_raw_rwclosed(pn_raw_connection_t *conn) { |
| 203 |
switch (conn->state) { |
| 204 |
case conn_rs_ws: |
| 205 |
case conn_fini : |
| 206 |
return true; |
| 207 |
default: |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
bool pni_raw_validate(pn_raw_connection_t *conn) { |
| 213 |
int rempty_count = 0; |
| 214 |
for (buff_ptr i = conn->rbuffer_first_empty; i; i = conn->rbuffers[i-1].next) { |
| 215 |
if (conn->rbuffers[i-1].type != buff_rempty) return false; |
| 216 |
rempty_count++; |
| 217 |
} |
| 218 |
int runused_count = 0; |
| 219 |
for (buff_ptr i = conn->rbuffer_first_unused; i; i = conn->rbuffers[i-1].next) { |
| 220 |
if (conn->rbuffers[i-1].type != buff_unread) return false; |
| 221 |
runused_count++; |
| 222 |
} |
| 223 |
int rread_count = 0; |
| 224 |
for (buff_ptr i = conn->rbuffer_first_read; i; i = conn->rbuffers[i-1].next) { |
| 225 |
if (conn->rbuffers[i-1].type != buff_read) return false; |
| 226 |
rread_count++; |
| 227 |
} |
| 228 |
if (rempty_count+runused_count+rread_count != read_buffer_count) return false; |
| 229 |
if (!conn->rbuffer_first_unused && conn->rbuffer_last_unused) return false; |
| 230 |
if (conn->rbuffer_last_unused && |
| 231 |
(conn->rbuffers[conn->rbuffer_last_unused-1].type != buff_unread || conn->rbuffers[conn->rbuffer_last_unused-1].next != 0)) return false; |
| 232 |
if (!conn->rbuffer_first_read && conn->rbuffer_last_read) return false; |
| 233 |
if (conn->rbuffer_last_read && |
| 234 |
(conn->rbuffers[conn->rbuffer_last_read-1].type != buff_read || conn->rbuffers[conn->rbuffer_last_read-1].next != 0)) return false; |
| 235 |
|
| 236 |
int wempty_count = 0; |
| 237 |
for (buff_ptr i = conn->wbuffer_first_empty; i; i = conn->wbuffers[i-1].next) { |
| 238 |
if (conn->wbuffers[i-1].type != buff_wempty) return false; |
| 239 |
wempty_count++; |
| 240 |
} |
| 241 |
int wunwritten_count = 0; |
| 242 |
for (buff_ptr i = conn->wbuffer_first_towrite; i; i = conn->wbuffers[i-1].next) { |
| 243 |
if (conn->wbuffers[i-1].type != buff_unwritten) return false; |
| 244 |
wunwritten_count++; |
| 245 |
} |
| 246 |
int wwritten_count = 0; |
| 247 |
for (buff_ptr i = conn->wbuffer_first_written; i; i = conn->wbuffers[i-1].next) { |
| 248 |
if (conn->wbuffers[i-1].type != buff_written) return false; |
| 249 |
wwritten_count++; |
| 250 |
} |
| 251 |
if (wempty_count+wunwritten_count+wwritten_count != write_buffer_count) return false; |
| 252 |
if (!conn->wbuffer_first_towrite && conn->wbuffer_last_towrite) return false; |
| 253 |
if (conn->wbuffer_last_towrite && |
| 254 |
(conn->wbuffers[conn->wbuffer_last_towrite-1].type != buff_unwritten || conn->wbuffers[conn->wbuffer_last_towrite-1].next != 0)) return false; |
| 255 |
if (!conn->wbuffer_first_written && conn->wbuffer_last_written) return false; |
| 256 |
if (conn->wbuffer_last_written && |
| 257 |
(conn->wbuffers[conn->wbuffer_last_written-1].type != buff_written || conn->wbuffers[conn->wbuffer_last_written-1].next != 0)) return false; |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
void pni_raw_finalize(pn_raw_connection_t *conn) { |
| 262 |
pn_condition_free(conn->condition); |
| 263 |
pn_collector_free(conn->collector); |
| 264 |
pn_free(conn->attachments); |
| 265 |
} |
| 266 |
|
| 267 |
size_t pn_raw_connection_read_buffers_capacity(pn_raw_connection_t *conn) { |
| 268 |
assert(conn); |
| 269 |
bool rclosed = pni_raw_rclosed(conn); |
| 270 |
return rclosed ? 0 : (read_buffer_count - conn->rbuffer_count); |
| 271 |
} |
| 272 |
|
| 273 |
size_t pn_raw_connection_write_buffers_capacity(pn_raw_connection_t *conn) { |
| 274 |
assert(conn); |
| 275 |
bool wclosed = pni_raw_wclosed(conn); |
| 276 |
return wclosed ? 0 : (write_buffer_count-conn->wbuffer_count); |
| 277 |
} |
| 278 |
|
| 279 |
size_t pn_raw_connection_give_read_buffers(pn_raw_connection_t *conn, pn_raw_buffer_t const *buffers, size_t num) { |
| 280 |
assert(conn); |
| 281 |
size_t can_take = pn_min(num, pn_raw_connection_read_buffers_capacity(conn)); |
| 282 |
if ( can_take==0 ) return 0; |
| 283 |
|
| 284 |
buff_ptr current = conn->rbuffer_first_empty; |
| 285 |
assert(current); |
| 286 |
|
| 287 |
buff_ptr previous; |
| 288 |
for (size_t i = 0; i < can_take; i++) { |
| 289 |
// Get next free |
| 290 |
assert(conn->rbuffers[current-1].type == buff_rempty); |
| 291 |
conn->rbuffers[current-1].context = buffers[i].context; |
| 292 |
conn->rbuffers[current-1].bytes = buffers[i].bytes; |
| 293 |
conn->rbuffers[current-1].capacity = buffers[i].capacity; |
| 294 |
conn->rbuffers[current-1].size = 0; |
| 295 |
conn->rbuffers[current-1].offset = buffers[i].offset; |
| 296 |
conn->rbuffers[current-1].type = buff_unread; |
| 297 |
|
| 298 |
previous = current; |
| 299 |
current = conn->rbuffers[current-1].next; |
| 300 |
} |
| 301 |
if (!conn->rbuffer_last_unused) { |
| 302 |
conn->rbuffer_last_unused = previous; |
| 303 |
} |
| 304 |
|
| 305 |
conn->rbuffers[previous-1].next = conn->rbuffer_first_unused; |
| 306 |
conn->rbuffer_first_unused = conn->rbuffer_first_empty; |
| 307 |
conn->rbuffer_first_empty = current; |
| 308 |
|
| 309 |
conn->rbuffer_count += can_take; |
| 310 |
conn->rrequestedbuffers = false; |
| 311 |
return can_take; |
| 312 |
} |
| 313 |
|
| 314 |
size_t pn_raw_connection_take_read_buffers(pn_raw_connection_t *conn, pn_raw_buffer_t *buffers, size_t num) { |
| 315 |
assert(conn); |
| 316 |
size_t count = 0; |
| 317 |
|
| 318 |
buff_ptr current = conn->rbuffer_first_read; |
| 319 |
if (!current) return 0; |
| 320 |
|
| 321 |
buff_ptr previous; |
| 322 |
for (; current && count < num; count++) { |
| 323 |
assert(conn->rbuffers[current-1].type == buff_read); |
| 324 |
buffers[count].context = conn->rbuffers[current-1].context; |
| 325 |
buffers[count].bytes = conn->rbuffers[current-1].bytes; |
| 326 |
buffers[count].capacity = conn->rbuffers[current-1].capacity; |
| 327 |
buffers[count].size = conn->rbuffers[current-1].size; |
| 328 |
buffers[count].offset = conn->rbuffers[current-1].offset - conn->rbuffers[current-1].size; |
| 329 |
conn->rbuffers[current-1].type = buff_rempty; |
| 330 |
|
| 331 |
previous = current; |
| 332 |
current = conn->rbuffers[current-1].next; |
| 333 |
} |
| 334 |
if (!count) return 0; |
| 335 |
|
| 336 |
conn->rbuffers[previous-1].next = conn->rbuffer_first_empty; |
| 337 |
conn->rbuffer_first_empty = conn->rbuffer_first_read; |
| 338 |
|
| 339 |
conn->rbuffer_first_read = current; |
| 340 |
if (!current) { |
| 341 |
conn->rbuffer_last_read = 0; |
| 342 |
} |
| 343 |
conn->rbuffer_count -= count; |
| 344 |
return count; |
| 345 |
} |
| 346 |
|
| 347 |
size_t pn_raw_connection_write_buffers(pn_raw_connection_t *conn, pn_raw_buffer_t const *buffers, size_t num) { |
| 348 |
assert(conn); |
| 349 |
size_t can_take = pn_min(num, pn_raw_connection_write_buffers_capacity(conn)); |
| 350 |
if ( can_take==0 ) return 0; |
| 351 |
|
| 352 |
buff_ptr current = conn->wbuffer_first_empty; |
| 353 |
assert(current); |
| 354 |
|
| 355 |
buff_ptr previous; |
| 356 |
for (size_t i = 0; i < can_take; i++) { |
| 357 |
// Get next free |
| 358 |
assert(conn->wbuffers[current-1].type == buff_wempty); |
| 359 |
conn->wbuffers[current-1].context = buffers[i].context; |
| 360 |
conn->wbuffers[current-1].bytes = buffers[i].bytes; |
| 361 |
conn->wbuffers[current-1].capacity = buffers[i].capacity; |
| 362 |
conn->wbuffers[current-1].size = buffers[i].size; |
| 363 |
conn->wbuffers[current-1].offset = buffers[i].offset; |
| 364 |
conn->wbuffers[current-1].type = buff_unwritten; |
| 365 |
|
| 366 |
previous = current; |
| 367 |
current = conn->wbuffers[current-1].next; |
| 368 |
} |
| 369 |
|
| 370 |
if (!conn->wbuffer_first_towrite) { |
| 371 |
conn->wbuffer_first_towrite = conn->wbuffer_first_empty; |
| 372 |
} |
| 373 |
if (conn->wbuffer_last_towrite) { |
| 374 |
conn->wbuffers[conn->wbuffer_last_towrite-1].next = conn->wbuffer_first_empty; |
| 375 |
} |
| 376 |
|
| 377 |
conn->wbuffer_last_towrite = previous; |
| 378 |
conn->wbuffers[previous-1].next = 0; |
| 379 |
conn->wbuffer_first_empty = current; |
| 380 |
|
| 381 |
conn->wbuffer_count += can_take; |
| 382 |
conn->wrequestedbuffers = false; |
| 383 |
return can_take; |
| 384 |
} |
| 385 |
|
| 386 |
size_t pn_raw_connection_take_written_buffers(pn_raw_connection_t *conn, pn_raw_buffer_t *buffers, size_t num) { |
| 387 |
assert(conn); |
| 388 |
size_t count = 0; |
| 389 |
|
| 390 |
buff_ptr current = conn->wbuffer_first_written; |
| 391 |
if (!current) return 0; |
| 392 |
|
| 393 |
buff_ptr previous; |
| 394 |
for (; current && count < num; count++) { |
| 395 |
assert(conn->wbuffers[current-1].type == buff_written); |
| 396 |
buffers[count].context = conn->wbuffers[current-1].context; |
| 397 |
buffers[count].bytes = conn->wbuffers[current-1].bytes; |
| 398 |
buffers[count].capacity = conn->wbuffers[current-1].capacity; |
| 399 |
buffers[count].size = conn->wbuffers[current-1].size; |
| 400 |
buffers[count].offset = conn->wbuffers[current-1].offset; |
| 401 |
conn->wbuffers[current-1].type = buff_wempty; |
| 402 |
|
| 403 |
previous = current; |
| 404 |
current = conn->wbuffers[current-1].next; |
| 405 |
} |
| 406 |
if (!count) return 0; |
| 407 |
|
| 408 |
conn->wbuffers[previous-1].next = conn->wbuffer_first_empty; |
| 409 |
conn->wbuffer_first_empty = conn->wbuffer_first_written; |
| 410 |
|
| 411 |
conn->wbuffer_first_written = current; |
| 412 |
if (!current) { |
| 413 |
conn->wbuffer_last_written = 0; |
| 414 |
} |
| 415 |
conn->wbuffer_count -= count; |
| 416 |
return count; |
| 417 |
} |
| 418 |
|
| 419 |
static inline void pni_raw_put_event(pn_raw_connection_t *conn, pn_event_type_t type) { |
| 420 |
pn_collector_put(conn->collector, PN_CLASSCLASS(pn_raw_connection), (void*)conn, type); |
| 421 |
} |
| 422 |
|
| 423 |
static inline void pni_raw_release_buffers(pn_raw_connection_t *conn) { |
| 424 |
for(;conn->rbuffer_first_unused;) { |
| 425 |
buff_ptr p = conn->rbuffer_first_unused; |
| 426 |
assert(conn->rbuffers[p-1].type == buff_unread); |
| 427 |
conn->rbuffers[p-1].size = 0; |
| 428 |
if (!conn->rbuffer_first_read) { |
| 429 |
conn->rbuffer_first_read = p; |
| 430 |
} |
| 431 |
if (conn->rbuffer_last_read) { |
| 432 |
conn->rbuffers[conn->rbuffer_last_read-1].next = p; |
| 433 |
} |
| 434 |
conn->rbuffer_last_read = p; |
| 435 |
conn->rbuffer_first_unused = conn->rbuffers[p-1].next; |
| 436 |
|
| 437 |
conn->rbuffers[p-1].next = 0; |
| 438 |
conn->rbuffers[p-1].type = buff_read; |
| 439 |
} |
| 440 |
conn->rbuffer_last_unused = 0; |
| 441 |
for(;conn->wbuffer_first_towrite;) { |
| 442 |
buff_ptr p = conn->wbuffer_first_towrite; |
| 443 |
assert(conn->wbuffers[p-1].type == buff_unwritten); |
| 444 |
if (!conn->wbuffer_first_written) { |
| 445 |
conn->wbuffer_first_written = p; |
| 446 |
} |
| 447 |
if (conn->wbuffer_last_written) { |
| 448 |
conn->wbuffers[conn->wbuffer_last_written-1].next = p; |
| 449 |
} |
| 450 |
conn->wbuffer_last_written = p; |
| 451 |
conn->wbuffer_first_towrite = conn->wbuffers[p-1].next; |
| 452 |
|
| 453 |
conn->wbuffers[p-1].next = 0; |
| 454 |
conn->wbuffers[p-1].type = buff_written; |
| 455 |
} |
| 456 |
conn->wbuffer_last_towrite = 0; |
| 457 |
} |
| 458 |
|
| 459 |
static inline void pni_raw_disconnect(pn_raw_connection_t *conn) { |
| 460 |
pni_raw_release_buffers(conn); |
| 461 |
conn->disconnectpending = true; |
| 462 |
conn->disconnect_state = disc_init; |
| 463 |
conn->state = pni_raw_new_state(conn, int_disconnect); |
| 464 |
} |
| 465 |
|
| 466 |
void pni_raw_connected(pn_raw_connection_t *conn) { |
| 467 |
pn_condition_clear(conn->condition); |
| 468 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_CONNECTED); |
| 469 |
conn->state = pni_raw_new_state(conn, conn_connected); |
| 470 |
} |
| 471 |
|
| 472 |
void pni_raw_connect_failed(pn_raw_connection_t *conn) { |
| 473 |
conn->state = conn_fini; |
| 474 |
pni_raw_disconnect(conn); |
| 475 |
} |
| 476 |
|
| 477 |
void pni_raw_wake(pn_raw_connection_t *conn) { |
| 478 |
conn->wakepending = true; |
| 479 |
} |
| 480 |
|
| 481 |
void pni_raw_read(pn_raw_connection_t *conn, int sock, long (*recv)(int, void*, size_t), void(*set_error)(pn_raw_connection_t *, const char *, int)) { |
| 482 |
assert(conn); |
| 483 |
|
| 484 |
if (!pni_raw_ropen(conn)) return; |
| 485 |
|
| 486 |
bool closed = false; |
| 487 |
for(;conn->rbuffer_first_unused;) { |
| 488 |
buff_ptr p = conn->rbuffer_first_unused; |
| 489 |
assert(conn->rbuffers[p-1].type == buff_unread); |
| 490 |
char *bytes = conn->rbuffers[p-1].bytes+conn->rbuffers[p-1].offset; |
| 491 |
size_t s = conn->rbuffers[p-1].capacity-conn->rbuffers[p-1].offset; |
| 492 |
int r = recv(sock, bytes, s); |
| 493 |
if (r < 0) { |
| 494 |
switch (errno) { |
| 495 |
// Interrupted system call try again |
| 496 |
case EINTR: continue; |
| 497 |
|
| 498 |
// Would block |
| 499 |
case EWOULDBLOCK: goto finished_reading; |
| 500 |
|
| 501 |
// Detected an error |
| 502 |
default: |
| 503 |
set_error(conn, "recv error", errno); |
| 504 |
pni_raw_close(conn); |
| 505 |
return; |
| 506 |
} |
| 507 |
} |
| 508 |
conn->rbuffers[p-1].size += r; |
| 509 |
conn->rbuffers[p-1].offset += r; |
| 510 |
|
| 511 |
if (!conn->rbuffer_first_read) { |
| 512 |
conn->rbuffer_first_read = p; |
| 513 |
} |
| 514 |
if (conn->rbuffer_last_read) { |
| 515 |
conn->rbuffers[conn->rbuffer_last_read-1].next = p; |
| 516 |
} |
| 517 |
conn->rbuffer_last_read = p; |
| 518 |
conn->rbuffer_first_unused = conn->rbuffers[p-1].next; |
| 519 |
|
| 520 |
conn->rbuffers[p-1].next = 0; |
| 521 |
conn->rbuffers[p-1].type = buff_read; |
| 522 |
|
| 523 |
// Checking for end of stream here ensures that there is a buffer at the end with nothing in it |
| 524 |
if (r == 0) { |
| 525 |
closed = true; |
| 526 |
break; |
| 527 |
} |
| 528 |
} |
| 529 |
finished_reading: |
| 530 |
if (!conn->rbuffer_first_unused) { |
| 531 |
conn->rbuffer_last_unused = 0; |
| 532 |
} |
| 533 |
// Read something - we are now either out of buffers; end of stream; or blocked for read |
| 534 |
if (conn->rbuffer_first_read && !conn->rpending) { |
| 535 |
conn->rpending = true; |
| 536 |
} |
| 537 |
|
| 538 |
uint8_t old_state = conn->state; |
| 539 |
|
| 540 |
// Socket closed for read |
| 541 |
if (closed) { |
| 542 |
conn->state = pni_raw_new_state(conn, conn_read_closed); |
| 543 |
conn->rclosedpending = true; |
| 544 |
} |
| 545 |
|
| 546 |
if (conn->state != old_state) { |
| 547 |
if (pni_raw_rwclosed(conn)) { |
| 548 |
pni_raw_disconnect(conn); |
| 549 |
} |
| 550 |
} |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
void pni_raw_write(pn_raw_connection_t *conn, int sock, long (*send)(int, const void*, size_t), void(*set_error)(pn_raw_connection_t *, const char *, int)) { |
| 555 |
assert(conn); |
| 556 |
|
| 557 |
if (pni_raw_wdrained(conn)) return; |
| 558 |
|
| 559 |
bool closed = false; |
| 560 |
bool drained = false; |
| 561 |
for(;conn->wbuffer_first_towrite;) { |
| 562 |
buff_ptr p = conn->wbuffer_first_towrite; |
| 563 |
assert(conn->wbuffers[p-1].type == buff_unwritten); |
| 564 |
char *bytes = conn->wbuffers[p-1].bytes+conn->wbuffers[p-1].offset+conn->unwritten_offset; |
| 565 |
size_t s = conn->wbuffers[p-1].size-conn->unwritten_offset; |
| 566 |
int r = send(sock, bytes, s); |
| 567 |
if (r < 0) { |
| 568 |
// Interrupted system call try again |
| 569 |
switch (errno) { |
| 570 |
// Interrupted system call try again |
| 571 |
case EINTR: continue; |
| 572 |
|
| 573 |
case EWOULDBLOCK: |
| 574 |
goto finished_writing; |
| 575 |
|
| 576 |
default: |
| 577 |
set_error(conn, "send error", errno); |
| 578 |
pni_raw_release_buffers(conn); |
| 579 |
pni_raw_close(conn); |
| 580 |
return; |
| 581 |
} |
| 582 |
} |
| 583 |
// return of 0 was never observed in testing and the documentation |
| 584 |
// implies that 0 could only be returned if 0 bytes were sent; however |
| 585 |
// leaving this case here seems safe. |
| 586 |
if (r == 0 && s > 0) { |
| 587 |
closed = true; |
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
// Only wrote a partial buffer - adjust buffer |
| 592 |
if (r != (int)s) { |
| 593 |
conn->unwritten_offset += r; |
| 594 |
break; |
| 595 |
} |
| 596 |
|
| 597 |
conn->unwritten_offset = 0; |
| 598 |
|
| 599 |
if (!conn->wbuffer_first_written) { |
| 600 |
conn->wbuffer_first_written = p; |
| 601 |
} |
| 602 |
if (conn->wbuffer_last_written) { |
| 603 |
conn->wbuffers[conn->wbuffer_last_written-1].next = p; |
| 604 |
} |
| 605 |
conn->wbuffer_last_written = p; |
| 606 |
conn->wbuffer_first_towrite = conn->wbuffers[p-1].next; |
| 607 |
|
| 608 |
conn->wbuffers[p-1].next = 0; |
| 609 |
conn->wbuffers[p-1].type = buff_written; |
| 610 |
} |
| 611 |
finished_writing: |
| 612 |
if (!conn->wbuffer_first_towrite) { |
| 613 |
conn->wbuffer_last_towrite = 0; |
| 614 |
drained = true; |
| 615 |
} |
| 616 |
// Wrote something; end of stream; out of buffers; or blocked for write |
| 617 |
if (conn->wbuffer_first_written && !conn->wpending) { |
| 618 |
conn->wpending = true; |
| 619 |
} |
| 620 |
|
| 621 |
uint8_t old_state = conn->state; |
| 622 |
|
| 623 |
// Drained all writes |
| 624 |
if (drained) { |
| 625 |
conn->state = pni_raw_new_state(conn, conn_write_drained); |
| 626 |
} |
| 627 |
|
| 628 |
// Socket closed for write |
| 629 |
if (closed) { |
| 630 |
conn->state = pni_raw_new_state(conn, conn_write_closed); |
| 631 |
conn->wclosedpending = true; |
| 632 |
} |
| 633 |
|
| 634 |
if (conn->state != old_state) { |
| 635 |
if (pni_raw_rwclosed(conn)) { |
| 636 |
pni_raw_disconnect(conn); |
| 637 |
} |
| 638 |
} |
| 639 |
return; |
| 640 |
} |
| 641 |
|
| 642 |
void pni_raw_process_shutdown(pn_raw_connection_t *conn, int sock, int (*shutdown_rd)(int), int (*shutdown_wr)(int)) { |
| 643 |
assert(conn); |
| 644 |
if (pni_raw_rclosing(conn)) { |
| 645 |
shutdown_rd(sock); |
| 646 |
conn->state = pni_raw_new_state(conn, int_read_shutdown); |
| 647 |
} |
| 648 |
if (pni_raw_wclosing(conn)) { |
| 649 |
shutdown_wr(sock); |
| 650 |
conn->state = pni_raw_new_state(conn, int_write_shutdown); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
bool pni_raw_can_read(pn_raw_connection_t *conn) { |
| 655 |
return pni_raw_ropen(conn) && conn->rbuffer_first_unused; |
| 656 |
} |
| 657 |
|
| 658 |
bool pni_raw_can_write(pn_raw_connection_t *conn) { |
| 659 |
return !pni_raw_wdrained(conn) && conn->wbuffer_first_towrite; |
| 660 |
} |
| 661 |
|
| 662 |
pn_event_t *pni_raw_event_next(pn_raw_connection_t *conn) { |
| 663 |
assert(conn); |
| 664 |
do { |
| 665 |
pn_event_t *event = pn_collector_next(conn->collector); |
| 666 |
if (event) { |
| 667 |
return pni_log_event(conn, event); |
| 668 |
} else if (conn->wakepending) { |
| 669 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_WAKE); |
| 670 |
conn->wakepending = false; |
| 671 |
} else if (conn->rpending) { |
| 672 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_READ); |
| 673 |
conn->rpending = false; |
| 674 |
} else if (conn->wpending) { |
| 675 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_WRITTEN); |
| 676 |
conn->wpending = false; |
| 677 |
} else if (conn->rclosedpending) { |
| 678 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_CLOSED_READ); |
| 679 |
conn->rclosedpending = false; |
| 680 |
} else if (conn->wclosedpending) { |
| 681 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_CLOSED_WRITE); |
| 682 |
conn->wclosedpending = false; |
| 683 |
} else if (conn->disconnectpending) { |
| 684 |
switch (conn->disconnect_state) { |
| 685 |
case disc_init: |
| 686 |
if (conn->rbuffer_first_read || conn->wbuffer_first_written) { |
| 687 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_DRAIN_BUFFERS); |
| 688 |
} |
| 689 |
conn->disconnect_state = disc_drain_msg; |
| 690 |
break; |
| 691 |
// TODO: We'll leave the read/written events in here for the moment for backward compatibility |
| 692 |
// remove them soon (after dispatch uses DRAIN_BUFFER) |
| 693 |
case disc_drain_msg: |
| 694 |
if (conn->rbuffer_first_read) { |
| 695 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_READ); |
| 696 |
} |
| 697 |
conn->disconnect_state = disc_read_msg; |
| 698 |
break; |
| 699 |
case disc_read_msg: |
| 700 |
if (conn->wbuffer_first_written) { |
| 701 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_WRITTEN); |
| 702 |
} |
| 703 |
conn->disconnect_state = disc_written_msg; |
| 704 |
break; |
| 705 |
case disc_written_msg: |
| 706 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_DISCONNECTED); |
| 707 |
conn->disconnectpending = false; |
| 708 |
conn->disconnect_state = disc_fini; |
| 709 |
break; |
| 710 |
} |
| 711 |
} else if (!pni_raw_wdrained(conn) && !conn->wbuffer_first_towrite && !conn->wrequestedbuffers) { |
| 712 |
// Ran out of write buffers |
| 713 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_NEED_WRITE_BUFFERS); |
| 714 |
conn->wrequestedbuffers = true; |
| 715 |
} else if (!pni_raw_rclosed(conn) && !conn->rbuffer_first_unused && !conn->rrequestedbuffers) { |
| 716 |
// Ran out of read buffers |
| 717 |
pni_raw_put_event(conn, PN_RAW_CONNECTION_NEED_READ_BUFFERS); |
| 718 |
conn->rrequestedbuffers = true; |
| 719 |
} else { |
| 720 |
return NULL; |
| 721 |
} |
| 722 |
} while (true); |
| 723 |
} |
| 724 |
|
| 725 |
void pni_raw_read_close(pn_raw_connection_t *conn) { |
| 726 |
// If already fully closed nothing to do |
| 727 |
if (pni_raw_rwclosed(conn)) { |
| 728 |
return; |
| 729 |
} |
| 730 |
if (!pni_raw_rclosed(conn)) { |
| 731 |
conn->rclosedpending = true; |
| 732 |
} |
| 733 |
conn->state = pni_raw_new_state(conn, api_read_close); |
| 734 |
if (pni_raw_rwclosed(conn)) { |
| 735 |
pni_raw_disconnect(conn); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
void pni_raw_write_close(pn_raw_connection_t *conn) { |
| 740 |
// If already fully closed nothing to do |
| 741 |
if (pni_raw_rwclosed(conn)) { |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
if (!pni_raw_wdrained(conn)) { |
| 746 |
if (!pni_raw_wclosed(conn)) { |
| 747 |
conn->wclosedpending = true; |
| 748 |
} |
| 749 |
conn->state = pni_raw_new_state(conn, api_write_close); |
| 750 |
|
| 751 |
if (!conn->wbuffer_first_towrite) { |
| 752 |
conn->state = pni_raw_new_state(conn, conn_write_drained); |
| 753 |
} |
| 754 |
} |
| 755 |
if (pni_raw_rwclosed(conn)) { |
| 756 |
pni_raw_disconnect(conn); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
void pni_raw_close(pn_raw_connection_t *conn) { |
| 761 |
// If already fully closed nothing to do |
| 762 |
if (pni_raw_rwclosed(conn)) { |
| 763 |
return; |
| 764 |
} |
| 765 |
if (!pni_raw_rclosed(conn)) { |
| 766 |
conn->rclosedpending = true; |
| 767 |
} |
| 768 |
conn->state = pni_raw_new_state(conn, api_read_close); |
| 769 |
|
| 770 |
if (!pni_raw_wdrained(conn)) { |
| 771 |
if (!pni_raw_wclosed(conn)) { |
| 772 |
conn->wclosedpending = true; |
| 773 |
} |
| 774 |
conn->state = pni_raw_new_state(conn, api_write_close); |
| 775 |
|
| 776 |
if (!conn->wbuffer_first_towrite) { |
| 777 |
conn->state = pni_raw_new_state(conn, conn_write_drained); |
| 778 |
} |
| 779 |
} |
| 780 |
if (pni_raw_rwclosed(conn)) { |
| 781 |
pni_raw_disconnect(conn); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
bool pn_raw_connection_is_read_closed(pn_raw_connection_t *conn) { |
| 786 |
assert(conn); |
| 787 |
return pni_raw_rclosed(conn); |
| 788 |
} |
| 789 |
|
| 790 |
bool pn_raw_connection_is_write_closed(pn_raw_connection_t *conn) { |
| 791 |
assert(conn); |
| 792 |
return pni_raw_wclosed(conn); |
| 793 |
} |
| 794 |
|
| 795 |
pn_condition_t *pn_raw_connection_condition(pn_raw_connection_t *conn) { |
| 796 |
assert(conn); |
| 797 |
return conn->condition; |
| 798 |
} |
| 799 |
|
| 800 |
void *pn_raw_connection_get_context(pn_raw_connection_t *conn) { |
| 801 |
assert(conn); |
| 802 |
return pn_record_get(conn->attachments, PN_LEGCTX); |
| 803 |
} |
| 804 |
|
| 805 |
void pn_raw_connection_set_context(pn_raw_connection_t *conn, void *context) { |
| 806 |
assert(conn); |
| 807 |
pn_record_set(conn->attachments, PN_LEGCTX, context); |
| 808 |
} |
| 809 |
|
| 810 |
pn_record_t *pn_raw_connection_attachments(pn_raw_connection_t *conn) { |
| 811 |
assert(conn); |
| 812 |
return conn->attachments; |
| 813 |
} |
| 814 |
|
| 815 |
pn_raw_connection_t *pn_event_raw_connection(pn_event_t *event) { |
| 816 |
return (pn_event_class(event) == PN_CLASSCLASS(pn_raw_connection)) ? (pn_raw_connection_t*)pn_event_context(event) : NULL; |
| 817 |
} |