| 1 |
#ifndef PROTON_SRC_IOCP_H |
| 2 |
#define PROTON_SRC_IOCP_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <proton/import_export.h> |
| 26 |
#include <proton/selectable.h> |
| 27 |
#include <proton/type_compat.h> |
| 28 |
|
| 29 |
typedef struct pni_acceptor_t pni_acceptor_t; |
| 30 |
typedef struct write_result_t write_result_t; |
| 31 |
typedef struct read_result_t read_result_t; |
| 32 |
typedef struct write_pipeline_t write_pipeline_t; |
| 33 |
typedef struct iocp_t iocp_t; |
| 34 |
typedef struct iocpdesc_t iocpdesc_t; |
| 35 |
|
| 36 |
|
| 37 |
// One per pn_io_t. |
| 38 |
|
| 39 |
struct iocp_t { |
| 40 |
HANDLE completion_port; |
| 41 |
pn_hash_t *iocpdesc_map; |
| 42 |
pn_list_t *zombie_list; |
| 43 |
unsigned shared_pool_size; |
| 44 |
char *shared_pool_memory; |
| 45 |
write_result_t **shared_results; |
| 46 |
write_result_t **available_results; |
| 47 |
size_t shared_available_count; |
| 48 |
size_t writer_count; |
| 49 |
int loopback_bufsize; |
| 50 |
bool iocp_trace; |
| 51 |
pn_selector_t *selector; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
// One for each socket. |
| 56 |
// This iocpdesc_t structure is ref counted by the iocpdesc_map, zombie_list, |
| 57 |
// selector->iocp_descriptors list. It should remain ref counted in the |
| 58 |
// zombie_list until ops_in_progress == 0 or the completion port is closed. |
| 59 |
|
| 60 |
struct iocpdesc_t { |
| 61 |
pn_socket_t socket; |
| 62 |
iocp_t *iocp; |
| 63 |
pni_acceptor_t *acceptor; |
| 64 |
pn_error_t *error; |
| 65 |
int ops_in_progress; |
| 66 |
bool read_in_progress; |
| 67 |
write_pipeline_t *pipeline; |
| 68 |
read_result_t *read_result; |
| 69 |
bool external; // true if socket set up outside Proton |
| 70 |
bool bound; // associted with the completion port |
| 71 |
bool closing; // pn_close called by application |
| 72 |
bool read_closed; // EOF or read error |
| 73 |
bool write_closed; // shutdown sent or write error |
| 74 |
bool poll_error; // flag posix-like POLLERR/POLLHUP/POLLNVAL |
| 75 |
bool deadline_desc; // Socket-less deadline descriptor for selectors |
| 76 |
pn_selector_t *selector; |
| 77 |
pn_selectable_t *selectable; |
| 78 |
int events; |
| 79 |
int interests; |
| 80 |
pn_timestamp_t deadline; |
| 81 |
iocpdesc_t *triggered_list_next; |
| 82 |
iocpdesc_t *triggered_list_prev; |
| 83 |
iocpdesc_t *deadlines_next; |
| 84 |
iocpdesc_t *deadlines_prev; |
| 85 |
pn_timestamp_t reap_time; |
| 86 |
}; |
| 87 |
|
| 88 |
typedef enum { IOCP_ACCEPT, IOCP_CONNECT, IOCP_READ, IOCP_WRITE } iocp_type_t; |
| 89 |
|
| 90 |
typedef struct { |
| 91 |
OVERLAPPED overlapped; |
| 92 |
iocp_type_t type; |
| 93 |
iocpdesc_t *iocpd; |
| 94 |
HRESULT status; |
| 95 |
} iocp_result_t; |
| 96 |
|
| 97 |
struct write_result_t { |
| 98 |
iocp_result_t base; |
| 99 |
size_t requested; |
| 100 |
bool in_use; |
| 101 |
pn_bytes_t buffer; |
| 102 |
}; |
| 103 |
|
| 104 |
iocpdesc_t *pni_iocpdesc_create(iocp_t *, pn_socket_t s, bool external); |
| 105 |
iocpdesc_t *pni_iocpdesc_map_get(iocp_t *, pn_socket_t s); |
| 106 |
iocpdesc_t *pni_deadline_desc(iocp_t *); |
| 107 |
void pni_iocpdesc_map_del(iocp_t *, pn_socket_t s); |
| 108 |
void pni_iocpdesc_map_push(iocpdesc_t *iocpd); |
| 109 |
void pni_iocpdesc_start(iocpdesc_t *iocpd); |
| 110 |
void pni_iocp_drain_completions(iocp_t *); |
| 111 |
int pni_iocp_wait_one(iocp_t *, int timeout, pn_error_t *); |
| 112 |
void pni_iocp_start_accepting(iocpdesc_t *iocpd); |
| 113 |
pn_socket_t pni_iocp_end_accept(iocpdesc_t *ld, struct sockaddr *addr, socklen_t *addrlen, bool *would_block, pn_error_t *error); |
| 114 |
pn_socket_t pni_iocp_begin_connect(iocp_t *, pn_socket_t sock, struct addrinfo *addr, pn_error_t *error); |
| 115 |
ssize_t pni_iocp_begin_write(iocpdesc_t *, const void *, size_t, bool *, pn_error_t *); |
| 116 |
ssize_t pni_iocp_recv(iocpdesc_t *iocpd, void *buf, size_t size, bool *would_block, pn_error_t *error); |
| 117 |
void pni_iocp_begin_close(iocpdesc_t *iocpd); |
| 118 |
iocp_t *pni_iocp(); |
| 119 |
|
| 120 |
void pni_events_update(iocpdesc_t *iocpd, int events); |
| 121 |
write_result_t *pni_write_result(iocpdesc_t *iocpd, const char *buf, size_t buflen); |
| 122 |
write_pipeline_t *pni_write_pipeline(iocpdesc_t *iocpd); |
| 123 |
size_t pni_write_pipeline_size(write_pipeline_t *); |
| 124 |
bool pni_write_pipeline_writable(write_pipeline_t *); |
| 125 |
void pni_write_pipeline_return(write_pipeline_t *, write_result_t *); |
| 126 |
size_t pni_write_pipeline_reserve(write_pipeline_t *, size_t); |
| 127 |
write_result_t *pni_write_pipeline_next(write_pipeline_t *); |
| 128 |
void pni_shared_pool_create(iocp_t *); |
| 129 |
void pni_shared_pool_free(iocp_t *); |
| 130 |
void pni_zombie_check(iocp_t *, pn_timestamp_t); |
| 131 |
pn_timestamp_t pni_zombie_deadline(iocp_t *); |
| 132 |
|
| 133 |
pn_selector_t *pni_selector_create(iocp_t *iocp); |
| 134 |
|
| 135 |
int pni_win32_error(pn_error_t *error, const char *msg, HRESULT code); |
| 136 |
|
| 137 |
#endif /* iocp.h */ |