title: ConnectNamedPipe and asio overlappped ptr
body: <p>I've named pipe server which is written using boost asio. Server creates named pipe and calls ConnectNamedPipe passing asio overlapped ptr to it. The problem is that completion handler passed to asio overlapped is never called, that is calling CreateFile on client side won't trigger completion handler passed to ConnectNamedPipe. What am I doing wrong?</p>
  
  <p>Here is full listing of both client and server:</p>
  
  <pre><code>#define _WIN32_WINNT 0x0501
  #include &lt;string&gt;
  #include &lt;functional&gt;
  #include &lt;thread&gt;
  #include &lt;boost/system/error_code.hpp&gt;
  #include &lt;boost/asio/io_service.hpp&gt;
  #include &lt;boost/asio/windows/overlapped_ptr.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;tchar.h&gt;
  
  #include &lt;Windows.h&gt;
  
  static const uint32_t               PIPE_OUTPUT_BUFFER_RESERVED_SIZE_BYTES = 50 * 1024;
  static const uint32_t               PIPE_INPUT_BUFFER_RESERVED_SIZE_BYTES = 50 * 1024;
  static const std::string            PIPE_NAME = "\\\\.\\pipe\\BC33AFC8-BA51-4DCD-9507-0234785D4F55_native_server_pipe";
  
  
  class Server
      : public std::enable_shared_from_this&lt;Server&gt;
  {
  public:
      Server(){}
      ~Server(){}
  
      void Start()
      {
          mIoService = std::make_shared&lt;boost::asio::io_service&gt;();
          mWork = std::make_shared&lt;boost::asio::io_service::work&gt;(*mIoService);
          mThread = std::make_shared&lt;std::thread&gt;(
              boost::bind(&amp;boost::asio::io_service::run, mIoService));
          mIoService-&gt;post(boost::bind(&amp;Server::Accept, shared_from_this()));
      }
  
  
      void Accept()
      {
          mPipe = CreateNamedPipeA(
              PIPE_NAME.c_str(),
              PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
              PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
              PIPE_UNLIMITED_INSTANCES,
              PIPE_OUTPUT_BUFFER_RESERVED_SIZE_BYTES,
              PIPE_INPUT_BUFFER_RESERVED_SIZE_BYTES,
              0,
              nullptr);
  
          if (mPipe == INVALID_HANDLE_VALUE)
          {
              DWORD err = GetLastError();
              //LOG(Error, "Failed create pipe: " &lt;&lt; mPipeName &lt;&lt; ", error: " &lt;&lt; err);
              return;
          }
  
          //LOG(Trace, "Pipe: " &lt;&lt; mPipeName &lt;&lt; " created successfully");
          boost::asio::windows::overlapped_ptr overlappedPtr(*mIoService,
              std::bind(&amp;Server::OnClientConnected, this, std::placeholders::_1, std::placeholders::_2));
  
          OVERLAPPED* overlapped = overlappedPtr.get();
          BOOL ok = ConnectNamedPipe(mPipe, overlapped);
          DWORD lastError = GetLastError();
  
          if (!ok &amp;&amp; lastError != ERROR_IO_PENDING)
          {
              // The operation completed immediately, so a completion notification needs
              // to be posted. When complete() is called, ownership of the OVERLAPPED-
              // derived object passes to the io_service.
              boost::system::error_code ec(lastError,
                  boost::asio::error::get_system_category());
              overlappedPtr.complete(ec, 0);
          }
          else
          {
              // The operation was successfully initiated, so ownership of the
              // OVERLAPPED-derived object has passed to the io_service.
              overlappedPtr.release();
          }
      }
  
      void OnClientConnected(
          const boost::system::error_code&amp;    ec,
          size_t                              bytesTransferred)
      {
          int a = 0;
          a++;
      }
  
  private:
      HANDLE                      mPipe;
      std::shared_ptr&lt;boost::asio::io_service&gt;        mIoService;
      std::shared_ptr&lt;boost::asio::io_service::work&gt;  mWork;
      std::shared_ptr&lt;std::thread&gt;                    mThread;
  };
  
  class Client
  {
  public:
      void Connect()
      {
          HANDLE hPipe = CreateFileA(
              PIPE_NAME.c_str(),
              GENERIC_READ | GENERIC_WRITE,
              0,
              nullptr,
              OPEN_EXISTING,
              FILE_FLAG_OVERLAPPED,
              nullptr
              );
  
          if (hPipe == INVALID_HANDLE_VALUE)
          {
              DWORD err = GetLastError();
  
              if (err != ERROR_PIPE_BUSY)
              {
                  /*LOG(Error, "Failed create pipe: " &lt;&lt; mPipeName &lt;&lt; ", error: " &lt;&lt; err);
                  mOnConnected(nullptr);*/
                  return;
              }
  
              return;
          }
      }
  };
  
  std::shared_ptr&lt;Server&gt; s = std::make_shared&lt;Server&gt;();
  Client c;
  
  int CALLBACK WinMain(
      _In_  HINSTANCE hInstance,
      _In_  HINSTANCE hPrevInstance,
      _In_  LPSTR lpCmdLine,
      _In_  int nCmdShow
      )
  {
      s-&gt;Start();
      Sleep(10000);
      c.Connect();
  
      Sleep(10000);
  }
  </code></pre>