-#ifndef WIN32
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#endif
-#include <msp/core/systemerror.h>
-#include <msp/strings/format.h>
-#include "handle_private.h"
#include "pipe.h"
using namespace std;
{
mode = M_RDWR;
-#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
+ platform_init();
set_events(P_INPUT);
}
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
+ 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)
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