X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fpipe.cpp;h=01bf20aaea32c056b33e2213dafdd1d0515c3f1e;hp=3882bd5b79b62eb6159cdd372ddaa4ca9a5e4e18;hb=c8bf2d6c15893ccc9dbc4e04611b7229029f4808;hpb=3ba7e8ca4cc6a2c77028d49687d29364f9e1ca26 diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 3882bd5..01bf20a 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -1,10 +1,3 @@ -#ifndef WIN32 -#include -#include -#endif -#include -#include -#include "handle_private.h" #include "pipe.h" using namespace std; @@ -13,29 +6,11 @@ namespace Msp { namespace IO { Pipe::Pipe(): - reader(handle[0], 1024) + reader(read_handle, 1024) { -#ifdef WIN32 - string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this); - *handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0); - if(!handle[0]) - throw system_error("CreateNamedPipe"); - - *handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); - if(!handle[1]) - { - unsigned err = GetLastError(); - CloseHandle(*handle[0]); - throw system_error(format("CreateFile(%s)", name), err); - } -#else - int pipe_fd[2]; - if(pipe(pipe_fd)==-1) - throw system_error("pipe"); - - *handle[0] = pipe_fd[0]; - *handle[1] = pipe_fd[1]; -#endif + mode = M_RDWR; + + platform_init(); set_events(P_INPUT); } @@ -45,43 +20,69 @@ Pipe::~Pipe() set_events(P_NONE); signal_flush_required.emit(); - sys_close(handle[0]); - sys_close(handle[1]); + sys_close(read_handle); + sys_close(write_handle); +} + +void Pipe::set_mode(Mode m) +{ + m = m&M_RDWR; + if(!m) + throw invalid_argument("Pipe::set_mode"); + + check_access(m); + + Mode close = mode&M_RDWR&~m; + if(close&M_READ) + sys_close(read_handle); + if(close&M_WRITE) + sys_close(write_handle); + mode = (mode&~M_RDWR)|m; } void Pipe::set_block(bool b) { - mode = (mode&~M_NONBLOCK); - if(b) - mode = (mode|M_NONBLOCK); - -#ifndef WIN32 - int flags = fcntl(*handle[0], F_GETFD); - fcntl(*handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); - flags = fcntl(*handle[1], F_GETFD); - fcntl(*handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); -#endif + adjust_mode(mode, M_NONBLOCK, !b); + sys_set_blocking(read_handle, b); + sys_set_blocking(write_handle, b); +} + +void Pipe::set_inherit(bool i) +{ + adjust_mode(mode, M_INHERIT, i); + sys_set_inherit(read_handle, i); + sys_set_inherit(write_handle, i); } -unsigned Pipe::do_write(const char *buf, unsigned size) +size_t Pipe::do_write(const char *buf, size_t size) { if(size==0) return 0; - return sys_write(handle[1], buf, size); + return sys_write(write_handle, buf, size); } -unsigned Pipe::do_read(char *buf, unsigned size) +size_t Pipe::do_read(char *buf, size_t size) { if(size==0) return 0; - unsigned ret = reader.read(buf, size); + size_t ret = reader.read(buf, size); if(ret==0) set_eof(); return ret; } +const Handle &Pipe::get_handle(Mode m) +{ + if(m==M_READ) + return read_handle; + else if(m==M_WRITE) + return write_handle; + else + throw invalid_argument("Pipe::get_handle"); +} + } // namespace IO } // namespace Msp