1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#ifndef CONNECTION_H
#define CONNECTION_H
#include <boost/asio.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
struct ModeBase
{
enum {
TEXT_MODE = 0,
BINARY_MODE = 1,
};
};
template<int>
struct Archive
{
typedef boost::archive::text_iarchive i_archive;
typedef boost::archive::text_oarchive o_archive;
};
template<>
struct Archive<ModeBase::BINARY_MODE>
{
typedef boost::archive::binary_iarchive i_archive;
typedef boost::archive::binary_oarchive o_archive;
};
template <int Mode = ModeBase::TEXT_MODE >
class tcp_connection
{
public:
typedef boost::shared_ptr<tcp_connection<Mode> > pointer;
typedef boost::weak_ptr<tcp_connection<Mode> > w_pointer;
static pointer create (boost::asio::io_service& io_service)
{
return pointer(new tcp_connection<Mode>(io_service));
}
template <typename T, typename Handler>
void async_read(T& t, Handler handler)
{
void (tcp_connection<Mode>::*f)(
const boost::system::error_code&,
T&, boost::tuple<Handler>) // ERREUR ICI
= &tcp_connection<Mode>::handle_read_header<T, Handler>;
boost::asio::async_read(m_socket, boost::asio::buffer(m_inbound_header),
boost::bind(f,
this, boost::asio::placeholders::error, boost::ref(t),
boost::make_tuple(handler)));
}
/// Handle a completed read of a message header. The handler is passed using
/// a tuple since boost::bind seems to have trouble binding a function object
/// created using boost::bind as a parameter.
template <typename T, typename Handler>
void handle_read_header(const boost::system::error_code& e,
T& t, boost::tuple<Handler> handler)
{
// ...
}
private:
/// Constructor.
tcp_connection(boost::asio::io_service& io_service)
: m_socket(io_service)
{
}
/// The underlying socket.
boost::asio::ip::tcp::socket m_socket;
/// The size of a fixed length header.
enum { header_length = 8 };
/// Holds an outbound header.
std::string m_outbound_header;
/// Holds the outbound data.
std::string m_outbound_data;
/// Holds an inbound header.
char m_inbound_header[header_length];
/// Holds the inbound data.
std::vector<char> m_inbound_data;
};
#endif |
Partager