#ifndef NAMED_PIPE_SERVICE_HPP #define NAMED_PIPE_SERVICE_HPP #include #include #include #include #include class named_pipe_service : public boost::asio::detail::service_base { public: typedef boost::asio::detail::win_iocp_handle_service::native_type native_type; typedef boost::asio::detail::win_iocp_handle_service::implementation_type implementation_type; named_pipe_service(boost::asio::io_service &io_service) : boost::asio::detail::service_base(io_service), m_handle_service(boost::asio::use_service< boost::asio::detail::win_iocp_handle_service>(io_service)) { } void shutdown_service() { } void construct(implementation_type &impl) { m_handle_service.construct(impl); } void destroy(implementation_type &impl) { m_handle_service.destroy(impl); } boost::system::error_code open(implementation_type &impl, const char *name, boost::system::error_code &ec) { if (is_open(impl)) { ec = boost::asio::error::already_open; return ec; } HANDLE handle = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (handle == INVALID_HANDLE_VALUE) { DWORD last_error = GetLastError(); ec = boost::system::error_code(last_error, boost::asio::error::get_system_category()); return ec; } if (m_handle_service.assign(impl, handle, ec)) CloseHandle(handle); return ec; } boost::system::error_code assign(implementation_type &impl, const native_type &native_handle, boost::system::error_code &ec) { return m_handle_service.assign(impl, native_handle, ec); } bool is_open(const implementation_type &impl) const { return m_handle_service.is_open(impl); } boost::system::error_code close(implementation_type &impl, boost::system::error_code &ec) { return m_handle_service.close(impl, ec); } native_type native(implementation_type &impl) { return m_handle_service.native(impl); } boost::system::error_code cancel(implementation_type &impl, boost::system::error_code &ec) { return m_handle_service.cancel(impl, ec); } boost::system::error_code set_mode(implementation_type &impl, DWORD mode, boost::system::error_code &ec) { BOOL res = SetNamedPipeHandleState(m_handle_service.native(impl), &mode, NULL, NULL); if (!res) { DWORD last_error = GetLastError(); ec = boost::system::error_code(last_error, boost::asio::error::get_system_category()); } ec = boost::system::error_code(); return ec; } template size_t write_some(implementation_type &impl, const ConstBufferSequence &buffers, boost::system::error_code &ec) { return m_handle_service.write_some(impl, buffers, ec); } template void async_write_some(implementation_type &impl, const ConstBufferSequence &buffers, Handler handler) { m_handle_service.async_write_some(impl, buffers, handler); } template size_t read_some(implementation_type &impl, const MutableBufferSequence &buffers, boost::system::error_code &ec) { return m_handle_service.read_some(impl, buffers, ec); } template void async_read_some(implementation_type &impl, const MutableBufferSequence &buffers, Handler handler) { m_handle_service.async_read_some(impl, buffers, handler); } private: boost::asio::detail::win_iocp_handle_service &m_handle_service; }; #endif