title: boost::asio async_read/async_send are bypassing it's handler
body: <p>I made a static-lib. And I created this three classes in</p>
  
  <p>Connection Class</p>
  
  <pre><code>#ifndef _CONNECTION_H_
  #define _CONNECTION_H_
  
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;memory&gt;
  #include "ByteBuffer.h"
  
  class Connection: public boost::enable_shared_from_this&lt;Connection&gt;
  {
  public:
    typedef boost::shared_ptr&lt;Connection&gt; pointer;
  
    explicit Connection(boost::asio::io_service&amp; io_service);
    virtual ~Connection();
    boost::asio::ip::tcp::socket&amp; socket();
  
    virtual void OnConnected()=0;
    virtual void Send(std::shared_ptr&lt;uint8_t&gt; buffer, int length);
    void Receive();
    void Disconnect();
    bool connected;
  protected:
    virtual void OnReceived(ByteBuffer &amp;b) = 0;
  
  private:
    void handle_Receive(const boost::system::error_code&amp; error, std::size_t bytes_transferred );
    void handle_Send(const boost::system::error_code&amp; error, std::size_t bytes_transferred);
  
    boost::asio::ip::tcp::socket socket_;
    bool disconnecting;
    boost::array&lt;uint8_t, 1000&gt; read_buffer_;
  };
  
  #endif
  
  
  #include "Connection.h"
  
  
  Connection::Connection(boost::asio::io_service&amp; io_service)
      :socket_(io_service),disconnecting(false),connected(false){}
  
  Connection::~Connection(){}
  
  
  boost::asio::ip::tcp::socket&amp; Connection::socket(){
      return socket_;
  }
  
  void Connection::Send(std::shared_ptr&lt;uint8_t&gt; buf, int length){
      boost::asio::async_write(socket_,boost::asio::buffer(buf.get(),length),
              boost::bind(&amp;Connection::handle_Send, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  }
  
  void Connection::handle_Send(const boost::system::error_code&amp; error, std::size_t bytes_transferred){
  
  }
  
  void Connection::Receive(){
       boost::asio::async_read(socket_,boost::asio::buffer(this-&gt;read_buffer_),
          boost::bind(&amp;Connection::handle_Receive, shared_from_this(),boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  }
  
  void Connection::handle_Receive(const boost::system::error_code&amp; error, std::size_t bytes_transferred)
  {
    if(!error)
    {
        if(bytes_transferred &lt;=0){
              this-&gt;Disconnect();
        }else{
  </code></pre>
  
  <p>ByteBuffer b((std::shared_ptr)this->read_buffer_.data(), this->read_buffer_.size());
                  this->OnReceived(b);
                  this->Receive();}
        }<br>
      }</p>
  
  <pre><code>void Connection::Disconnect()
  {
      if (!disconnecting) {
          boost::system::error_code ec;
          socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send,ec);
          socket_.close(ec);
          disconnecting = true;
          std::cout&lt;&lt;"disconnected"&lt;&lt;std::endl;
      }
  }
  </code></pre>
  
  <p>ConnectionFactory class</p>
  
  <pre><code>#pragma once
  #include "Connection.h"
  class ConnectionFactory
  {
  public:
      ConnectionFactory(void);
      virtual ~ConnectionFactory(void);
  
      virtual Connection::pointer create(boost::asio::io_service&amp; io_service) = 0;
  };
  
  
  #include "ConnectionFactory.h"
  
  
  ConnectionFactory::ConnectionFactory(void)
  {
  }
  
  
  ConnectionFactory::~ConnectionFactory(void)
  {
  }
  </code></pre>
  
  <p>Server Class</p>
  
  <pre><code>#ifndef _SERVER_H_
  #define _SERVER_H_
  
  #include "Connection.h"
  #include "ConnectionFactory.h"
  
  class Server
  {
  public:
    Server(boost::asio::io_service&amp; io_service , std::string ip,short port,boost::shared_ptr&lt;ConnectionFactory&gt; factory);
    ~Server();    
  private:
    void start_accept();
    void handle_accept(boost::shared_ptr&lt;Connection&gt; conn,const boost::system::error_code&amp; error);
  
    boost::shared_ptr&lt;ConnectionFactory&gt; m_factory;
    boost::asio::io_service &amp;io_service;
    boost::asio::ip::tcp::acceptor acceptor_;
  };
  
  #endif
  
  #include "Server.h"
  
  
  Server::Server(boost::asio::io_service&amp; io_service,std::string ip,short port,boost::shared_ptr&lt;ConnectionFactory&gt; factory)
      :io_service(io_service), acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){
  
          m_factory = factory;
          start_accept();
  
          std::cout&lt;&lt;"Socket accepting connections..."&lt;&lt;std::endl;
  }
  
  Server::~Server()
  {
  }
  
  
  void Server::start_accept(){
  
      boost::shared_ptr&lt;Connection&gt; conn = m_factory-&gt;create(this-&gt;io_service);
      acceptor_.async_accept(conn-&gt;socket(),
          boost::bind(&amp;Server::handle_accept, this,conn,boost::asio::placeholders::error));
  
  }
  
  
  void Server::handle_accept(boost::shared_ptr&lt;Connection&gt; conn,const boost::system::error_code&amp; error){
      if (!error){
          std::cout&lt;&lt;"on connected"&lt;&lt;std::endl;
          conn-&gt;OnConnected();
          conn-&gt;Receive();
          start_accept();
      }
      //conn-&gt;Disconnect();
  }
  </code></pre>
  
  <p>and I drevid from the static-lib and used this classes and it's working perfect</p>
  
  <p>in my main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "auth_proto.h"
  #include &lt;Server.h&gt;
  #include &lt;ConnectionFactory.h&gt;
  #include "AuthConnectionFactory.h"
  
  using namespace std;
  int main()
  {
      Auth_Setup();
    try
    {
        boost::asio::io_service io_service;
        boost::shared_ptr&lt;ConnectionFactory&gt; fact (new AuthConnectionFactory(io_service));
        Server s(io_service,"5.113.195.156",9959,fact);
        io_service.run();
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
  
    return 0;
  }
  </code></pre>
  
  <p>I really don't understand what is wrong here , when it comes to the Receive() function it's receive the data from the client but it does not invoking the handle_Receive() method so I can use this data, the behavior that am waiting that it should call the handle_Receive() so I can pass the data to ByteBuffer and use it but that's not happening ......</p>