X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fpipe.cpp;h=0a72a80068da4d9ac686fdb8cd3216801e316725;hp=12f8fbd3886ea7f269b125691a19b2074ee16d11;hb=9a4c531eb1aa68595cd3ab074b31bfa565aaefe2;hpb=c2003dc69422262a5c90b3bd73b60b459f563c6e diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 12f8fbd..0a72a80 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -1,11 +1,3 @@ -#ifndef WIN32 -#include -#include -#include -#endif -#include -#include -#include "handle_private.h" #include "pipe.h" using namespace std; @@ -16,27 +8,9 @@ namespace IO { Pipe::Pipe(): reader(read_handle, 1024) { -#ifdef WIN32 - string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this); - *read_handle = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0); - if(!read_handle) - throw system_error("CreateNamedPipe"); - - *write_handle = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); - if(!write_handle) - { - 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"); - - *read_handle = pipe_fd[0]; - *write_handle = pipe_fd[1]; -#endif + mode = M_RDWR; + + platform_init(); set_events(P_INPUT); } @@ -50,18 +24,30 @@ Pipe::~Pipe() 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(*read_handle, F_GETFD); - fcntl(*read_handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); - flags = fcntl(*write_handle, F_GETFD); - fcntl(*write_handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); -#endif + sys_set_blocking(read_handle, b); + sys_set_blocking(write_handle, b); } unsigned Pipe::do_write(const char *buf, unsigned size) @@ -84,5 +70,15 @@ unsigned Pipe::do_read(char *buf, unsigned size) 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