]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/pipe.cpp
Move most platform-specific code into overlay directories
[libs/core.git] / source / io / pipe.cpp
index 12f8fbd3886ea7f269b125691a19b2074ee16d11..831076f0086b2ffd756ec9e86a3b1b1364e9b1cc 100644 (file)
@@ -1,11 +1,3 @@
-#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;
@@ -16,27 +8,9 @@ namespace IO {
 Pipe::Pipe():
        reader(read_handle, 1024)
 {
-#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
+       mode = M_RDWR;
+
+       platform_init();
 
        set_events(P_INPUT);
 }
@@ -56,12 +30,8 @@ void Pipe::set_block(bool b)
        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
+       sys_set_blocking(read_handle, b);
+       sys_set_blocking(write_handle, b);
 }
 
 unsigned Pipe::do_write(const char *buf, unsigned size)
@@ -84,5 +54,15 @@ unsigned Pipe::do_read(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