]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/pipe.cpp
Implement get_handle in File and Pipe
[libs/core.git] / source / io / pipe.cpp
index d8de5df2745ca60a1ad0f74912d767435b2f5a4a..639743f4ca79cab6ad1733383d69e5ff77e9556b 100644 (file)
@@ -1,9 +1,10 @@
 #ifndef WIN32
 #include <fcntl.h>
 #include <errno.h>
+#include <unistd.h>
 #endif
 #include <msp/core/systemerror.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include "handle_private.h"
 #include "pipe.h"
 
@@ -13,16 +14,18 @@ namespace Msp {
 namespace IO {
 
 Pipe::Pipe():
-       reader(handle[0], 1024)
+       reader(read_handle, 1024)
 {
+       mode = M_RDWR;
+
 #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])
+       *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");
 
-       *handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
-       if(!handle[1])
+       *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]);
@@ -33,8 +36,8 @@ Pipe::Pipe():
        if(pipe(pipe_fd)==-1)
                throw system_error("pipe");
 
-       *handle[0] = pipe_fd[0];
-       *handle[1] = pipe_fd[1];
+       *read_handle = pipe_fd[0];
+       *write_handle = pipe_fd[1];
 #endif
 
        set_events(P_INPUT);
@@ -45,9 +48,8 @@ Pipe::~Pipe()
        set_events(P_NONE);
 
        signal_flush_required.emit();
-       sys_close(handle[0]);
-       sys_close(handle[1]);
-       signal_closed.emit();
+       sys_close(read_handle);
+       sys_close(write_handle);
 }
 
 void Pipe::set_block(bool b)
@@ -57,10 +59,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(*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
 }
 
@@ -69,7 +71,7 @@ unsigned Pipe::do_write(const char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       return sys_write(handle[1], buf, size);
+       return sys_write(write_handle, buf, size);
 }
 
 unsigned Pipe::do_read(char *buf, unsigned size)
@@ -84,5 +86,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