28 #ifndef WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
31 #include <websocketpp/transport/iostream/base.hpp>
33 #include <websocketpp/transport/base/connection.hpp>
35 #include <websocketpp/logger/levels.hpp>
37 #include <websocketpp/common/connection_hdl.hpp>
38 #include <websocketpp/common/memory.hpp>
39 #include <websocketpp/common/platforms.hpp>
57 template <
typename config>
58 class connection :
public lib::enable_shared_from_this< connection<config> > {
63 typedef lib::shared_ptr<type>
ptr;
73 typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
74 typedef typename concurrency_type::mutex_type mutex_type;
76 typedef lib::shared_ptr<timer> timer_ptr;
78 explicit connection(
bool is_server, alog_type & alog, elog_type & elog)
79 : m_output_stream(NULL)
81 , m_is_server(is_server)
85 , m_remote_endpoint(
"iostream transport")
92 return type::shared_from_this();
104 scoped_lock_type lock(m_read_mutex);
127 friend std::istream &
operator>> (std::istream & in, type & t) {
129 scoped_lock_type lock(t.m_read_mutex);
152 scoped_lock_type lock(m_read_mutex);
154 return this->read_some_impl(buf,len);
175 scoped_lock_type lock(m_read_mutex);
177 size_t total_read = 0;
178 size_t temp_read = 0;
181 temp_read = this->read_some_impl(buf+total_read,len-total_read);
182 total_read += temp_read;
183 }
while (temp_read != 0 && total_read < len);
206 scoped_lock_type lock(m_read_mutex);
222 scoped_lock_type lock(m_read_mutex);
275 m_remote_endpoint =
value;
291 return m_remote_endpoint;
299 return m_connection_hdl;
351 m_shutdown_handler = h;
362 handler(lib::error_code());
393 s <<
"iostream_con async_read_at_least: " << num_bytes;
396 if (num_bytes > len) {
401 if (m_reading ==
true) {
406 if (num_bytes == 0 || len == 0) {
407 handler(lib::error_code(),
size_t(0));
413 m_bytes_needed = num_bytes;
414 m_read_handler = handler;
445 if (m_output_stream) {
446 m_output_stream->write(buf,len);
448 if (m_output_stream->bad()) {
451 }
else if (m_write_handler) {
452 ec = m_write_handler(m_connection_hdl, buf, len);
485 if (m_output_stream) {
486 std::vector<buffer>::const_iterator it;
487 for (it = bufs.begin(); it != bufs.end(); it++) {
488 m_output_stream->write((*it).buf,(*it).len);
490 if (m_output_stream->bad()) {
495 }
else if (m_write_handler) {
496 std::vector<buffer>::const_iterator it;
497 for (it = bufs.begin(); it != bufs.end(); it++) {
498 ec = m_write_handler(m_connection_hdl, (*it).buf, (*it).len);
514 m_connection_hdl = hdl;
530 return lib::error_code();
544 if (m_shutdown_handler) {
545 ec = m_shutdown_handler(m_connection_hdl);
551 void read(std::istream &in) {
560 in.read(m_buf+m_cursor,static_cast<std::streamsize>(m_len-m_cursor));
562 if (in.gcount() == 0) {
567 m_cursor +=
static_cast<size_t>(in.gcount());
575 if (m_cursor >= m_bytes_needed) {
577 complete_read(lib::error_code());
582 size_t read_some_impl(
char const * buf,
size_t len) {
590 size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
592 std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
594 m_cursor += bytes_to_copy;
596 if (m_cursor >= m_bytes_needed) {
597 complete_read(lib::error_code());
600 return bytes_to_copy;
619 void complete_read(lib::error_code
const & ec) {
625 handler(ec,m_cursor);
631 size_t m_bytes_needed;
636 std::ostream * m_output_stream;
642 bool const m_is_server;
646 std::string m_remote_endpoint;
653 mutex_type m_read_mutex;
661 #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
uint16_t value
The type of a close code value.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
void set_handle(connection_hdl hdl)
Set Connection Handle.
friend std::istream & operator>>(std::istream &in, type &t)
Overloaded stream input operator.
void async_write(std::vector< buffer > const &bufs, transport::write_handler handler)
Asyncronous Transport Write (scatter-gather)
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
void set_remote_endpoint(std::string value)
Set human readable remote endpoint address.
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
static level const devel
Low level debugging information (warning: very chatty)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
bool is_secure() const
Tests whether or not the underlying transport is secure.
void async_shutdown(transport::shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
size_t readsome(char const *buf, size_t len)
Manual input supply (DEPRECATED)
async_read called while another async_read was in progress
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
underlying transport pass through
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
static level const devel
Development messages (warning: very chatty)
lib::function< lib::error_code(connection_hdl)> shutdown_handler
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
size_t read_some(char const *buf, size_t len)
Manual input supply (read some)
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
async_read_at_least call requested more bytes than buffer can store
config::alog_type alog_type
Type of this transport's access logging policy.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Namespace for the WebSocket++ project.
config::elog_type elog_type
Type of this transport's error logging policy.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
connection< config > type
Type of this connection transport component.
ptr get_shared()
Get a shared pointer to this component.
void set_write_handler(write_handler h)
Sets the write handler.
void init(init_handler handler)
Initialize the connection transport.
connection_hdl get_handle() const
Get the connection handle.
void set_secure(bool value)
Set whether or not this connection is secure.
timer_ptr set_timer(long, timer_handler)
Call back a function after a period of time.
void fatal_error()
Signal transport error.
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
Initiate an async_read for at least num_bytes bytes into buf.
config::concurrency_type concurrency_type
transport concurrency policy
lib::function< lib::error_code(connection_hdl, char const *, size_t)> write_handler
The type and signature of the callback used by iostream transport to write.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.