libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_IO_DETAIL_HELPER_POSIX_HPP__ 00029 #define MOOST_IO_DETAIL_HELPER_POSIX_HPP__ 00030 00031 #include <boost/asio.hpp> 00032 #include <unistd.h> 00033 00034 namespace moost { namespace io { namespace detail { 00035 00036 class helper 00037 { 00038 public: 00039 typedef int native_io_t; 00040 typedef boost::asio::posix::basic_stream_descriptor<> async_stream_t; 00041 typedef int error_t; 00042 00043 static native_io_t duplicate(native_io_t in) 00044 { 00045 native_io_t duped = ::dup(in); 00046 00047 if (duped == -1) 00048 { 00049 throw std::runtime_error("failed to duplicate handle"); 00050 } 00051 00052 return duped; 00053 } 00054 00055 static bool close(native_io_t in) 00056 { 00057 return ::close(in) != -1; 00058 } 00059 00060 static void create_pipe(native_io_t& read_end, native_io_t& write_end) 00061 { 00062 int pfd[2]; 00063 00064 if (pipe(pfd) != 0) 00065 { 00066 throw std::runtime_error("failed to create pipe"); 00067 } 00068 00069 read_end = pfd[0]; 00070 write_end = pfd[1]; 00071 } 00072 00073 static bool write(native_io_t io, const void *data, size_t length, size_t *written) 00074 { 00075 ssize_t rv = ::write(io, data, length); 00076 00077 if (written) 00078 { 00079 *written = rv >= 0 ? static_cast<size_t>(rv) : 0; 00080 } 00081 00082 return static_cast<size_t>(rv) == length; 00083 } 00084 00085 static error_t error() 00086 { 00087 return errno; 00088 } 00089 }; 00090 00091 } } } 00092 00093 #endif