]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/pipe.cpp
Rework exceptions for IO
[libs/core.git] / source / io / pipe.cpp
index b3735aa63b6d43a7724aaa13d1109ce96a78b0d2..afa0c259bdc3655195ccca692439774f8f967d8b 100644 (file)
@@ -2,6 +2,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #endif
+#include <msp/core/systemerror.h>
 #include <msp/strings/formatter.h>
 #include "pipe.h"
 
@@ -16,14 +17,14 @@ Pipe::Pipe()
        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)
-               throw SystemError("Unable to create pipe", GetLastError());
+               throw system_error("CreateNamedPipe");
 
        handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
        if(handle[1]==INVALID_HANDLE_VALUE)
        {
                unsigned err = GetLastError();
                CloseHandle(handle[0]);
-               throw SystemError("Unable to create pipe", err);
+               throw system_error(format("CreateFile(%s)", name), err);
        }
 
        overlapped = 0;
@@ -34,7 +35,7 @@ Pipe::Pipe()
        buf_next = buffer;
 #else
        if(pipe(handle)==-1)
-               throw SystemError("Unable to create pipe", errno);
+               throw system_error("pipe");
 #endif
 
        set_events(P_INPUT);
@@ -82,7 +83,7 @@ unsigned Pipe::do_write(const char *buf, unsigned size)
 #ifdef WIN32
        DWORD ret;
        if(!WriteFile(handle[1], buf, size, &ret, 0))
-               throw SystemError("Writing to pipe failed", GetLastError());
+               throw system_error("WriteFile");
 #else
        int ret = ::write(handle[1], buf, size);
        if(ret==-1)
@@ -90,7 +91,7 @@ unsigned Pipe::do_write(const char *buf, unsigned size)
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Writing to pipe failed", errno);
+                       throw system_error("write");
        }
 #endif
 
@@ -110,7 +111,7 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        {
                DWORD ret;
                if(!GetOverlappedResult(handle[0], overlapped, &ret, !buf_avail))
-                       throw SystemError("Reading from pipe failed", GetLastError());
+                       throw system_error("GetOverlappedResult");
                else
                {
                        buf_avail += ret;
@@ -133,7 +134,7 @@ unsigned Pipe::do_read(char *buf, unsigned size)
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Reading from pipe failed", errno);
+                       throw system_error("read");
        }
 #endif
 
@@ -161,7 +162,7 @@ Handle Pipe::get_event_handle()
                {
                        unsigned err = GetLastError();
                        if(err!=ERROR_IO_PENDING)
-                               throw SystemError("Failed to start an overlapped read", err);
+                               throw system_error("ReadFile");
                }
                else
                {