]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/pipe.cpp
Use the new Handle class to hide platform details from public headers
[libs/core.git] / source / io / pipe.cpp
index 2975a0635074e981aea9dc909cdef5f872d3ca36..1b275e731667f4736929499aaad7e9a17745343b 100644 (file)
@@ -4,6 +4,7 @@
 #endif
 #include <msp/core/systemerror.h>
 #include <msp/strings/formatter.h>
+#include "handle_private.h"
 #include "pipe.h"
 
 using namespace std;
@@ -29,28 +30,32 @@ Pipe::Pipe():
 {
 #ifdef WIN32
        string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
-       handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0);
-       if(handle[0]==INVALID_HANDLE_VALUE)
+       *handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0);
+       if(!handle[0])
                throw system_error("CreateNamedPipe");
 
-       handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
-       if(handle[1]==INVALID_HANDLE_VALUE)
+       *handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
+       if(!handle[1])
        {
                unsigned err = GetLastError();
-               CloseHandle(handle[0]);
+               CloseHandle(*handle[0]);
                throw system_error(format("CreateFile(%s)", name), err);
        }
 
        priv = new Private;
        priv->overlapped = 0;
-       priv->event = CreateEvent(0, true, false, 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)
+       int pipe_fd[2];
+       if(pipe(pipe_fd)==-1)
                throw system_error("pipe");
+
+       *handle[0] = pipe_fd[0];
+       *handle[1] = pipe_fd[1];
 #endif
 
        set_events(P_INPUT);
@@ -60,7 +65,7 @@ Pipe::~Pipe()
 {
        close();
 #ifdef WIN32
-       CloseHandle(priv->event);
+       CloseHandle(*priv->event);
        delete priv->buffer;
 #endif
        delete priv;
@@ -72,11 +77,11 @@ void Pipe::close()
 
        signal_flush_required.emit();
 #ifdef WIN32
-       CloseHandle(handle[0]);
-       CloseHandle(handle[1]);
+       CloseHandle(*handle[0]);
+       CloseHandle(*handle[1]);
 #else
-       ::close(handle[0]);
-       ::close(handle[1]);
+       ::close(*handle[0]);
+       ::close(*handle[1]);
        signal_closed.emit();
 #endif
 }
@@ -88,10 +93,10 @@ void Pipe::set_block(bool b)
                mode = (mode|M_NONBLOCK);
 
 #ifndef WIN32
-       int flags = fcntl(handle[0], F_GETFD);
-       fcntl(handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
-       flags = fcntl(handle[1], F_GETFD);
-       fcntl(handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
+       int flags = fcntl(*handle[0], F_GETFD);
+       fcntl(*handle[0], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
+       flags = fcntl(*handle[1], F_GETFD);
+       fcntl(*handle[1], F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
 #endif
 }
 
@@ -102,10 +107,10 @@ unsigned Pipe::do_write(const char *buf, unsigned size)
 
 #ifdef WIN32
        DWORD ret;
-       if(!WriteFile(handle[1], buf, size, &ret, 0))
+       if(!WriteFile(*handle[1], buf, size, &ret, 0))
                throw system_error("WriteFile");
 #else
-       int ret = ::write(handle[1], buf, size);
+       int ret = ::write(*handle[1], buf, size);
        if(ret==-1)
        {
                if(errno==EAGAIN)
@@ -130,7 +135,7 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        if(priv->overlapped)
        {
                DWORD ret;
-               if(!GetOverlappedResult(handle[0], priv->overlapped, &ret, !priv->buf_avail))
+               if(!GetOverlappedResult(*handle[0], priv->overlapped, &ret, !priv->buf_avail))
                        throw system_error("GetOverlappedResult");
                else
                {
@@ -148,7 +153,7 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        // Initiate another overlapped read in case someone is polling us
        get_event_handle();
 #else
-       int ret = ::read(handle[0], buf, size);
+       int ret = ::read(*handle[0], buf, size);
        if(ret==-1)
        {
                if(errno==EAGAIN)
@@ -167,18 +172,18 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        return ret;
 }
 
-Handle Pipe::get_event_handle()
+const Handle &Pipe::get_event_handle()
 {
 #ifdef WIN32
        if(!priv->overlapped && !priv->buf_avail)
        {
                priv->overlapped = new OVERLAPPED;
                memset(priv->overlapped, 0, sizeof(OVERLAPPED));
-               priv->overlapped->hEvent = priv->event;
+               priv->overlapped->hEvent = *priv->event;
 
                DWORD ret;
                priv->buf_next = priv->buffer;
-               if(!ReadFile(handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped))
+               if(!ReadFile(*handle[0], priv->buffer, priv->buf_size, &ret, priv->overlapped))
                {
                        unsigned err = GetLastError();
                        if(err!=ERROR_IO_PENDING)
@@ -189,7 +194,7 @@ Handle Pipe::get_event_handle()
                        priv->buf_avail = ret;
                        delete priv->overlapped;
                        priv->overlapped = 0;
-                       SetEvent(priv->event);
+                       SetEvent(*priv->event);
                }
        }