More de-bastardizing.

This commit is contained in:
JoelKatz
2013-01-25 11:08:49 -08:00
parent 938357b7ae
commit 2fb4df3ba3
3 changed files with 60 additions and 87 deletions

View File

@@ -2,6 +2,7 @@
#define __AUTOSOCKET_H_
#include <vector>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
@@ -9,6 +10,7 @@
#include <boost/make_shared.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/read_until.hpp>
#include "Log.h"
extern LogPartition AutoSocketPartition;
@@ -103,6 +105,24 @@ public:
PlainSocket().async_read_some(buffers, handler);
}
template <typename Seq, typename Condition, typename Handler>
void async_read_until(const Seq& buffers, Condition condition, Handler handler)
{
if (isSecure())
basio::async_read_until(*mSocket, buffers, condition, handler);
else
basio::async_read_until(PlainSocket(), buffers, condition, handler);
}
template <typename Allocator, typename Handler>
void async_read_until(basio::basic_streambuf<Allocator>& buffers, const std::string& delim, Handler handler)
{
if (isSecure())
basio::async_read_until(*mSocket, buffers, delim, handler);
else
basio::async_read_until(PlainSocket(), buffers, delim, handler);
}
template <typename Buf, typename Handler> void async_write(const Buf& buffers, Handler handler)
{
if (isSecure())
@@ -121,6 +141,15 @@ public:
boost::asio::async_read(PlainSocket(), buffers, cond, handler);
}
template <typename Allocator, typename Condition, typename Handler>
void async_read(basio::basic_streambuf<Allocator>& buffers, Condition cond, Handler handler)
{
if (isSecure())
boost::asio::async_read(*mSocket, buffers, cond, handler);
else
boost::asio::async_read(PlainSocket(), buffers, cond, handler);
}
template <typename Buf, typename Handler> void async_read(const Buf& buffers, Handler handler)
{
if (isSecure())

View File

@@ -899,38 +899,18 @@ public:
{
// TODO: read timeout timer?
if (socket_type::get_socket().isSecure())
{
boost::asio::async_read(
socket_type::get_socket().SSLSocket(),
m_buf,
boost::asio::transfer_at_least(std::min(
m_read_threshold,
static_cast<size_t>(m_processor->get_bytes_needed())
)),
m_strand.wrap(boost::bind(
&type::handle_read_frame,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
}
else
{
boost::asio::async_read(
socket_type::get_socket().PlainSocket(),
m_buf,
boost::asio::transfer_at_least(std::min(
m_read_threshold,
static_cast<size_t>(m_processor->get_bytes_needed())
)),
m_strand.wrap(boost::bind(
&type::handle_read_frame,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
}
socket_type::get_socket().async_read(
m_buf,
boost::asio::transfer_at_least(std::min(
m_read_threshold,
static_cast<size_t>(m_processor->get_bytes_needed())
)),
m_strand.wrap(boost::bind(
&type::handle_read_frame,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
}
}
public:
@@ -1228,31 +1208,14 @@ public:
//m_endpoint.alog().at(log::alevel::DEVEL) << "write header: " << zsutil::to_hex(m_write_queue.front()->get_header()) << log::endl;
if (socket_type::get_socket().isSecure())
{
boost::asio::async_write(
socket_type::get_socket().SSLSocket(),
m_write_buf,
m_strand.wrap(boost::bind(
&type::handle_write,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
}
else
{
boost::asio::async_write(
socket_type::get_socket().PlainSocket(),
m_write_buf,
m_strand.wrap(boost::bind(
&type::handle_write,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
}
socket_type::get_socket().async_write(
m_write_buf,
m_strand.wrap(boost::bind(
&type::handle_write,
type::shared_from_this(),
boost::asio::placeholders::error
))
);
} else {
// if we are in an inturrupted state and had nothing else to write
// it is safe to terminate the connection.

View File

@@ -544,36 +544,17 @@ void server<endpoint>::connection<connection_type>::async_init() {
m_connection.register_timeout(5000,fail::status::TIMEOUT_WS,
"Timeout on WebSocket handshake");
if (m_connection.get_socket().isSecure())
{
boost::asio::async_read_until(
m_connection.get_socket().SSLSocket(),
m_connection.buffer(),
// match_header,
"\r\n\r\n",
m_connection.get_strand().wrap(boost::bind(
&type::handle_read_request,
m_connection.shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
))
);
}
else
{
boost::asio::async_read_until(
m_connection.get_socket().PlainSocket(),
m_connection.buffer(),
// match_header,
"\r\n\r\n",
m_connection.get_strand().wrap(boost::bind(
&type::handle_read_request,
m_connection.shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
))
);
}
m_connection.get_socket().async_read_until(
m_connection.buffer(),
// match_header,
std::string("\r\n\r\n"),
m_connection.get_strand().wrap(boost::bind(
&type::handle_read_request,
m_connection.shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
))
);
}
/// processes the response from an async read for an HTTP header