- const char* pos = static_cast<const char*>(buf);
- while (size > 0) {
- ssize_t res = m_transport->write(pos, size);
- if (res < 0)
- throw Error();
- if (res == 0)
- return;
- size -= res;
- pos += res;
- }
+ m_impl->bind(address, port, handler);
+}
+
+void Proto::run()
+{
+ m_impl->run();
+}
+
+bool Proto::stop()
+{
+ return m_impl->stop();
+}
+
+class Proto::Impl
+{
+ public:
+ Impl(TransportType transport, const std::string& key);
+ ~Impl();
+
+ Connection& connect(const std::string& address, uint16_t port);
+ void bind(const std::string& address, uint16_t port, AcceptHandler handler);
+
+ void run();
+ bool stop();
+
+ private:
+ ba::io_service m_ios;
+ boost::scoped_ptr<Transport> m_transport;
+ std::vector<ConnectionPtr> m_conns;
+ bool m_running;
+ bool m_stopped;
+};
+
+Proto::Impl::Impl(TransportType transport, const std::string& key)
+ : m_transport(makeTransport(transport, key)),
+ m_running(false),
+ m_stopped(true)
+{
+}
+
+Proto::Impl::~Impl()
+{
+ stop();
+}
+
+ConnectionPtr Proto::Impl::connect(const std::string& address, uint16_t port)
+{
+ return m_transport->connect(address, port);
+}
+
+void Proto::Impl::bind(const std::string& address, uint16_t port, AcceptHandler handler)
+{
+ m_transport->bind(address, port, handler);
+}
+
+void Proto::Impl::run()
+{
+ m_stopped = false;
+ m_running = true;
+ while (m_running)
+ m_ios.run_once();
+ m_stopped = true;