5 #include <msp/strings/formatter.h>
16 string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
17 handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0);
18 if(handle[0]==INVALID_HANDLE_VALUE)
19 throw SystemError("Unable to create pipe", GetLastError());
21 handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
22 if(handle[1]==INVALID_HANDLE_VALUE)
24 unsigned err = GetLastError();
25 CloseHandle(handle[0]);
26 throw SystemError("Unable to create pipe", err);
30 event = CreateEvent(0, true, false, 0);
32 buffer = new char[buf_size];
37 throw SystemError("Unable to create pipe", errno);
52 signal_flush_required.emit();
54 CloseHandle(handle[0]);
55 CloseHandle(handle[1]);
63 void Pipe::set_block(bool b)
65 mode = (mode&~M_NONBLOCK);
67 mode = (mode|M_NONBLOCK);
70 int flags = fcntl(handle[0], F_GETFD);
71 fcntl(handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
72 flags = fcntl(handle[1], F_GETFD);
73 fcntl(handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
77 unsigned Pipe::do_write(const char *buf, unsigned size)
84 if(!WriteFile(handle[1], buf, size, &ret, 0))
85 throw SystemError("Writing to pipe failed", GetLastError());
87 int ret = ::write(handle[1], buf, size);
93 throw SystemError("Writing to pipe failed", errno);
100 unsigned Pipe::do_read(char *buf, unsigned size)
106 // Initiate overlapped read if needed
112 if(!GetOverlappedResult(handle[0], overlapped, &ret, !buf_avail))
113 throw SystemError("Reading from pipe failed", GetLastError());
122 unsigned ret = min(buf_avail, size);
123 memcpy(buf, buf_next, ret);
127 // Initiate another overlapped read in case someone is polling us
130 int ret = ::read(handle[0], buf, size);
136 throw SystemError("Reading from pipe failed", errno);
143 signal_end_of_file.emit();
149 Handle Pipe::get_event_handle()
152 if(!overlapped && !buf_avail)
154 overlapped = new OVERLAPPED;
155 memset(overlapped, 0, sizeof(OVERLAPPED));
156 overlapped->hEvent = event;
160 if(!ReadFile(handle[0], buffer, buf_size, &ret, overlapped))
162 unsigned err = GetLastError();
163 if(err!=ERROR_IO_PENDING)
164 throw SystemError("Failed to start an overlapped read", err);