28 #ifndef WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP 29 #define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP 59 template <
typename config>
60 class connection :
public lib::enable_shared_from_this< connection<config> > {
65 typedef lib::shared_ptr<type>
ptr;
80 explicit connection(
bool is_server,
const lib::shared_ptr<alog_type> & alog,
const lib::shared_ptr<elog_type> & elog)
81 : m_output_stream(NULL)
83 , m_is_server(is_server)
87 , m_remote_endpoint(
"iostream transport")
94 return type::shared_from_this();
106 scoped_lock_type lock(m_read_mutex);
142 friend std::istream &
operator>> (std::istream & in, type & t) {
144 scoped_lock_type lock(t.m_read_mutex);
167 scoped_lock_type lock(m_read_mutex);
169 return this->read_some_impl(buf,len);
190 scoped_lock_type lock(m_read_mutex);
192 size_t total_read = 0;
193 size_t temp_read = 0;
196 temp_read = this->read_some_impl(buf+total_read,len-total_read);
197 total_read += temp_read;
198 }
while (temp_read != 0 && total_read < len);
209 return this->read_some(buf,len);
221 scoped_lock_type lock(m_read_mutex);
237 scoped_lock_type lock(m_read_mutex);
290 m_remote_endpoint =
value;
306 return m_remote_endpoint;
314 return m_connection_hdl;
381 m_vector_write_handler = h;
401 m_shutdown_handler = h;
412 handler(lib::error_code());
443 s <<
"iostream_con async_read_at_least: " << num_bytes;
446 if (num_bytes > len) {
451 if (m_reading ==
true) {
456 if (num_bytes == 0 || len == 0) {
457 handler(lib::error_code(),
size_t(0));
463 m_bytes_needed = num_bytes;
464 m_read_handler = handler;
495 if (m_output_stream) {
496 m_output_stream->write(buf,len);
498 if (m_output_stream->bad()) {
501 }
else if (m_write_handler) {
502 ec = m_write_handler(m_connection_hdl, buf, len);
535 if (m_output_stream) {
536 std::vector<buffer>::const_iterator it;
537 for (it = bufs.begin(); it != bufs.end(); it++) {
538 m_output_stream->write((*it).buf,(*it).len);
540 if (m_output_stream->bad()) {
545 }
else if (m_vector_write_handler) {
546 ec = m_vector_write_handler(m_connection_hdl, bufs);
547 }
else if (m_write_handler) {
548 std::vector<buffer>::const_iterator it;
549 for (it = bufs.begin(); it != bufs.end(); it++) {
550 ec = m_write_handler(m_connection_hdl, (*it).buf, (*it).len);
566 m_connection_hdl = hdl;
582 return lib::error_code();
596 if (m_shutdown_handler) {
597 ec = m_shutdown_handler(m_connection_hdl);
603 void read(std::istream &in) {
612 in.read(m_buf+m_cursor,static_cast<std::streamsize>(m_len-m_cursor));
614 if (in.gcount() == 0) {
619 m_cursor +=
static_cast<size_t>(in.gcount());
627 if (m_cursor >= m_bytes_needed) {
629 complete_read(lib::error_code());
634 size_t read_some_impl(
char const * buf,
size_t len) {
642 size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
644 std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
646 m_cursor += bytes_to_copy;
648 if (m_cursor >= m_bytes_needed) {
649 complete_read(lib::error_code());
652 return bytes_to_copy;
671 void complete_read(lib::error_code
const & ec) {
677 handler(ec,m_cursor);
683 size_t m_bytes_needed;
688 std::ostream * m_output_stream;
695 bool const m_is_server;
697 lib::shared_ptr<alog_type> m_alog;
698 lib::shared_ptr<elog_type> m_elog;
699 std::string m_remote_endpoint;
706 mutex_type m_read_mutex;
714 #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP An operation that requires an output stream was attempted before setting one.
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
The type and signature of the callback used by iostream transport to perform vectored writes...
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
void set_handle(connection_hdl hdl)
Set Connection Handle.
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)
Empty timer class to stub out for timer functionality that iostream transport doesn't support...
void set_uri(uri_ptr)
Set uri hook.
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.
A fake lock guard implementation that does nothing.
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::shared_ptr< timer > timer_ptr
void set_vector_write_handler(vector_write_handler h)
Sets the vectored write handler.
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)
A fake mutex implementation that does nothing.
underlying transport pass through
static level const devel
Development messages (warning: very chatty)
lib::function< lib::error_code(connection_hdl)> shutdown_handler
The type and signature of the callback used by iostream transport to signal a transport shutdown...
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
std::istream & operator>>(std::istream &is, Batches &batch)
Read components from input and add them to components.
concurrency_type::scoped_lock_type scoped_lock_type
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.
bool is_secure() const
Tests whether or not the underlying transport is secure.
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.
concurrency_type::mutex_type mutex_type
Stub concurrency policy that implements the interface using no-ops.
connection< config > type
Type of this connection transport component.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
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.
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.
connection_hdl get_handle() const
Get the connection handle.
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
lib::error_code make_error_code(error::value e)
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
Stub logger that ignores all input.
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.
connection(bool is_server, const lib::shared_ptr< alog_type > &alog, const lib::shared_ptr< elog_type > &elog)