| 1 |
#ifndef PROTON_IO_CONNECTION_DRIVER_HPP |
| 2 |
#define PROTON_IO_CONNECTION_DRIVER_HPP |
| 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 "../connection_options.hpp" |
| 26 |
#include "../error_condition.hpp" |
| 27 |
#include "../fwd.hpp" |
| 28 |
#include "../types_fwd.hpp" |
| 29 |
|
| 30 |
#include <proton/connection_driver.h> |
| 31 |
|
| 32 |
#include <string> |
| 33 |
|
| 34 |
/// @file |
| 35 |
/// @copybrief proton::io::connection_driver |
| 36 |
|
| 37 |
namespace proton { |
| 38 |
|
| 39 |
class work_queue; |
| 40 |
class proton_handler; |
| 41 |
|
| 42 |
namespace io { |
| 43 |
|
| 44 |
/// **Unsettled API** - A pointer to a mutable memory region with a |
| 45 |
/// size. |
| 46 |
struct mutable_buffer { |
| 47 |
char* data; ///< Beginning of the buffered data. |
| 48 |
size_t size; ///< Number of bytes in the buffer. |
| 49 |
|
| 50 |
/// Construct a buffer starting at data_ with size_ bytes. |
| 51 |
mutable_buffer(char* data_=0, size_t size_=0) : data(data_), size(size_) {} |
| 52 |
}; |
| 53 |
|
| 54 |
/// **Unsettled API** - A pointer to an immutable memory region with a |
| 55 |
/// size. |
| 56 |
struct const_buffer { |
| 57 |
const char* data; ///< Beginning of the buffered data. |
| 58 |
size_t size; ///< Number of bytes in the buffer. |
| 59 |
|
| 60 |
/// Construct a buffer starting at data_ with size_ bytes. |
| 61 |
const_buffer(const char* data_=0, size_t size_=0) : data(data_), size(size_) {} |
| 62 |
}; |
| 63 |
|
| 64 |
/// **Unsettled API** - An AMQP driver for a single connection. |
| 65 |
/// |
| 66 |
/// io::connection_driver manages a single proton::connection and dispatches |
| 67 |
/// events to a proton::messaging_handler. It does no IO of its own, but allows you to |
| 68 |
/// integrate AMQP protocol handling into any IO or concurrency framework. |
| 69 |
/// |
| 70 |
/// The application is coded the same way as for the |
| 71 |
/// proton::container. The application implements a |
| 72 |
/// proton::messaging_handler to respond to transport, connection, |
| 73 |
/// session, link, and message events. With a little care, the same |
| 74 |
/// handler classes can be used for both container and |
| 75 |
/// connection_driver. the @ref broker.cpp example illustrates this. |
| 76 |
/// |
| 77 |
/// You need to write the IO code to read AMQP data to the |
| 78 |
/// read_buffer(). The engine parses the AMQP frames. dispatch() calls |
| 79 |
/// the appropriate functions on the applications proton::messaging_handler. You |
| 80 |
/// write output data from the engine's write_buffer() to your IO. |
| 81 |
/// |
| 82 |
/// The engine is not safe for concurrent use, but you can process |
| 83 |
/// different engines concurrently. A common pattern for |
| 84 |
/// high-performance servers is to serialize read/write activity |
| 85 |
/// per connection and dispatch in a fixed-size thread pool. |
| 86 |
/// |
| 87 |
/// The engine is designed to work with a classic reactor (e.g., |
| 88 |
/// select, poll, epoll) or an async-request driven proactor (e.g., |
| 89 |
/// windows completion ports, boost.asio, libuv). |
| 90 |
/// |
| 91 |
/// The engine never throws exceptions. |
| 92 |
class |
| 93 |
PN_CPP_CLASS_EXTERN connection_driver { |
| 94 |
public: |
| 95 |
/// An engine without a container id. |
| 96 |
PN_CPP_EXTERN connection_driver(); |
| 97 |
|
| 98 |
/// Create a connection driver associated with a container id. |
| 99 |
PN_CPP_EXTERN connection_driver(const std::string&); |
| 100 |
|
| 101 |
PN_CPP_EXTERN ~connection_driver(); |
| 102 |
|
| 103 |
/// Configure a connection by applying exactly the options in opts (including proton::messaging_handler) |
| 104 |
/// Does not apply any default options, to apply container defaults use connect() or accept() |
| 105 |
/// instead. If server==true, configure a server connection. |
| 106 |
void configure(const connection_options& opts=connection_options(), bool server=false); |
| 107 |
|
| 108 |
/// Call configure() with client options and call connection::open() |
| 109 |
/// Options applied: container::id(), container::client_connection_options(), opts. |
| 110 |
PN_CPP_EXTERN void connect(const connection_options& opts); |
| 111 |
|
| 112 |
/// Call configure() with server options. |
| 113 |
/// Options applied: container::id(), container::server_connection_options(), opts. |
| 114 |
/// |
| 115 |
/// Note this does not call connection::open(). If there is a messaging_handler in the |
| 116 |
/// composed options it will receive messaging_handler::on_connection_open() and can |
| 117 |
/// respond with connection::open() or connection::close() |
| 118 |
PN_CPP_EXTERN void accept(const connection_options& opts); |
| 119 |
|
| 120 |
/// The engine's read buffer. Read data into this buffer then call read_done() when complete. |
| 121 |
/// Returns mutable_buffer(0, 0) if the engine cannot currently read data. |
| 122 |
/// Calling dispatch() may open up more buffer space. |
| 123 |
PN_CPP_EXTERN mutable_buffer read_buffer(); |
| 124 |
|
| 125 |
/// Indicate that the first n bytes of read_buffer() have valid data. |
| 126 |
/// This changes the buffer, call read_buffer() to get the updated buffer. |
| 127 |
PN_CPP_EXTERN void read_done(size_t n); |
| 128 |
|
| 129 |
/// Indicate that the read side of the transport is closed and no more data will be read. |
| 130 |
/// Note that there may still be events to dispatch() or data to write. |
| 131 |
PN_CPP_EXTERN void read_close(); |
| 132 |
|
| 133 |
/// The engine's write buffer. Write data from this buffer then call write_done() |
| 134 |
/// Returns const_buffer(0, 0) if the engine has nothing to write. |
| 135 |
/// Calling dispatch() may generate more data in the write buffer. |
| 136 |
PN_CPP_EXTERN const_buffer write_buffer(); |
| 137 |
|
| 138 |
/// Indicate that the first n bytes of write_buffer() have been written successfully. |
| 139 |
/// This changes the buffer, call write_buffer() to get the updated buffer. |
| 140 |
PN_CPP_EXTERN const_buffer write_done(size_t n); |
| 141 |
|
| 142 |
/// Indicate that the write side of the transport has closed and no more data can be written. |
| 143 |
/// Note that there may still be events to dispatch() or data to read. |
| 144 |
PN_CPP_EXTERN void write_close(); |
| 145 |
|
| 146 |
/// Indicate that time has passed |
| 147 |
/// |
| 148 |
/// @return the expiration time of the next unexpired timer. You must arrange to call tick() |
| 149 |
/// no later than this expiration time. In practice this will mean calling tick() every time |
| 150 |
/// there is anything read or written, and if nothing is read or written then as soon as possible |
| 151 |
/// after the returned timestamp (so you will probably need to set a platform specific timeout to |
| 152 |
/// know when this occurs). |
| 153 |
PN_CPP_EXTERN timestamp tick(timestamp now); |
| 154 |
|
| 155 |
/// Inform the engine that the transport been disconnected unexpectedly, |
| 156 |
/// without completing the AMQP connection close sequence. |
| 157 |
/// |
| 158 |
/// This calls read_close(), write_close(), sets the transport().error() and |
| 159 |
/// queues an `on_transport_error` event. You must call dispatch() one more |
| 160 |
/// time to dispatch the messaging_handler::on_transport_error() call and other final |
| 161 |
/// events. |
| 162 |
/// |
| 163 |
/// Note this does not close the connection() so that a proton::messaging_handler can |
| 164 |
/// distinguish between a connection close error sent by the remote peer and |
| 165 |
/// a transport failure. |
| 166 |
/// |
| 167 |
PN_CPP_EXTERN void disconnected(const error_condition& = error_condition()); |
| 168 |
|
| 169 |
/// There are events to be dispatched by dispatch() |
| 170 |
PN_CPP_EXTERN bool has_events() const; |
| 171 |
|
| 172 |
/// Dispatch all available events and call the corresponding \ref messaging_handler methods. |
| 173 |
/// |
| 174 |
/// Returns true if the engine is still active, false if it is finished and |
| 175 |
/// can be destroyed. The engine is finished when all events are dispatched |
| 176 |
/// and one of the following is true: |
| 177 |
/// |
| 178 |
/// - both read_close() and write_close() have been called, no more IO is possible. |
| 179 |
/// - The AMQP connection() is closed AND the write_buffer() is empty. |
| 180 |
/// |
| 181 |
/// May modify the read_buffer() and/or the write_buffer(). |
| 182 |
/// |
| 183 |
PN_CPP_EXTERN bool dispatch(); |
| 184 |
|
| 185 |
/// Get the AMQP connection associated with this connection_driver. |
| 186 |
PN_CPP_EXTERN proton::connection connection() const; |
| 187 |
|
| 188 |
/// Get the transport associated with this connection_driver. |
| 189 |
PN_CPP_EXTERN proton::transport transport() const; |
| 190 |
|
| 191 |
/// Get the container associated with this connection_driver, if there is one. |
| 192 |
PN_CPP_EXTERN proton::container* container() const; |
| 193 |
|
| 194 |
private: |
| 195 |
void init(); |
| 196 |
connection_driver(const connection_driver&); |
| 197 |
connection_driver& operator=(const connection_driver&); |
| 198 |
|
| 199 |
std::string container_id_; |
| 200 |
messaging_handler* handler_; |
| 201 |
pn_connection_driver_t driver_; |
| 202 |
}; |
| 203 |
|
| 204 |
} // io |
| 205 |
} // proton |
| 206 |
|
| 207 |
#endif // PROTON_IO_CONNECTION_DRIVER_HPP |