6 #include <msp/core/systemerror.h>
7 #include <msp/strings/format.h>
8 #include "handle_private.h"
17 reader(read_handle, 1024)
22 string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
23 *read_handle = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0);
25 throw system_error("CreateNamedPipe");
27 *write_handle = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
30 unsigned err = GetLastError();
31 CloseHandle(*read_handle);
32 throw system_error(format("CreateFile(%s)", name), err);
37 throw system_error("pipe");
39 *read_handle = pipe_fd[0];
40 *write_handle = pipe_fd[1];
50 signal_flush_required.emit();
51 sys_close(read_handle);
52 sys_close(write_handle);
55 void Pipe::set_block(bool b)
57 mode = (mode&~M_NONBLOCK);
59 mode = (mode|M_NONBLOCK);
62 int flags = fcntl(*read_handle, F_GETFD);
63 fcntl(*read_handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
64 flags = fcntl(*write_handle, F_GETFD);
65 fcntl(*write_handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
69 unsigned Pipe::do_write(const char *buf, unsigned size)
74 return sys_write(write_handle, buf, size);
77 unsigned Pipe::do_read(char *buf, unsigned size)
82 unsigned ret = reader.read(buf, size);
89 const Handle &Pipe::get_handle(Mode m)
96 throw invalid_argument("Pipe::get_handle");