namespace Msp {
namespace IO {
-Pipe::Pipe()
+struct Pipe::Private
+{
+#ifdef WIN32
+ OVERLAPPED *overlapped;
+ Handle event;
+ unsigned buf_size;
+ char *buffer;
+ unsigned buf_avail;
+ char *buf_next;
+#endif
+};
+
+
+Pipe::Pipe():
+ priv(0)
{
#ifdef WIN32
string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
throw system_error(format("CreateFile(%s)", name), err);
}
- overlapped = 0;
- event = CreateEvent(0, true, false, 0);
- buf_size = 1024;
- buffer = new char[buf_size];
- buf_avail = 0;
- buf_next = buffer;
+ priv = new Private;
+ priv->overlapped = 0;
+ priv->event = CreateEvent(0, true, false, 0);
+ priv->buf_size = 1024;
+ priv->buffer = new char[priv->buf_size];
+ priv->buf_avail = 0;
+ priv->buf_next = priv->buffer;
#else
if(pipe(handle)==-1)
throw system_error("pipe");
Pipe::~Pipe()
{
close();
+#ifdef WIN32
+ CloseHandle(priv->event);
+ delete priv->buffer;
+#endif
+ delete priv;
}
void Pipe::close()
// Initiate overlapped read if needed
get_event_handle();
- if(overlapped)
+ if(priv->overlapped)
{
DWORD ret;
- if(!GetOverlappedResult(handle[0], overlapped, &ret, !buf_avail))
+ if(!GetOverlappedResult(handle[0], priv->overlapped, &ret, !priv->buf_avail))
throw system_error("GetOverlappedResult");
else
{
- buf_avail += ret;
- delete overlapped;
- overlapped = 0;
+ priv->buf_avail += ret;
+ delete priv->overlapped;
+ priv->overlapped = 0;
}
}
- unsigned ret = min(buf_avail, size);
- memcpy(buf, buf_next, ret);
- buf_next += ret;
- buf_avail -= ret;
+ unsigned ret = min(priv->buf_avail, size);
+ memcpy(buf, priv->buf_next, ret);
+ priv->buf_next += ret;
+ priv->buf_avail -= ret;
// Initiate another overlapped read in case someone is polling us
get_event_handle();
Handle Pipe::get_event_handle()
{
#ifdef WIN32
- if(!overlapped && !buf_avail)
+ if(!priv->overlapped && !priv->buf_avail)
{
- overlapped = new OVERLAPPED;
- memset(overlapped, 0, sizeof(OVERLAPPED));
- overlapped->hEvent = event;
+ priv->overlapped = new OVERLAPPED;
+ memset(priv->overlapped, 0, sizeof(OVERLAPPED));
+ priv->overlapped->hEvent = priv->event;
DWORD ret;
- buf_next = buffer;
- if(!ReadFile(handle[0], buffer, buf_size, &ret, overlapped))
+ priv->buf_next = priv->buffer;
+ if(!ReadFile(handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped))
{
unsigned err = GetLastError();
if(err!=ERROR_IO_PENDING)
}
else
{
- buf_avail = ret;
- delete overlapped;
- overlapped = 0;
- SetEvent(event);
+ priv->buf_avail = ret;
+ delete priv->overlapped;
+ priv->overlapped = 0;
+ SetEvent(priv->event);
}
}
- return event;
+ return priv->event;
#else
return handle[0];
#endif