]> git.tdb.fi Git - libs/core.git/commitdiff
Use EventReader in pipe
authorMikko Rasa <tdb@tdb.fi>
Sun, 12 Jun 2011 15:03:33 +0000 (18:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 12 Jun 2011 15:03:33 +0000 (18:03 +0300)
source/io/pipe.cpp
source/io/pipe.h

index a8487867a137d406699361a3c5d0fe2cfaa35b08..f52801cd834e550199a808ab99020adcac8d6a27 100644 (file)
@@ -12,21 +12,8 @@ using namespace std;
 namespace Msp {
 namespace IO {
 
-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)
+       reader(handle[0], 1024)
 {
 #ifdef WIN32
        string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
@@ -41,14 +28,6 @@ Pipe::Pipe():
                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->buf_size = 1024;
-       priv->buffer = new char[priv->buf_size];
-       priv->buf_avail = 0;
-       priv->buf_next = priv->buffer;
 #else
        int pipe_fd[2];
        if(pipe(pipe_fd)==-1)
@@ -64,11 +43,6 @@ Pipe::Pipe():
 Pipe::~Pipe()
 {
        close();
-#ifdef WIN32
-       CloseHandle(*priv->event);
-       delete priv->buffer;
-#endif
-       delete priv;
 }
 
 void Pipe::close()
@@ -108,33 +82,7 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        if(size==0)
                return 0;
 
-#ifdef WIN32
-       // Initiate overlapped read if needed
-       get_event_handle();
-
-       if(priv->overlapped)
-       {
-               DWORD ret;
-               if(!GetOverlappedResult(*handle[0], priv->overlapped, &ret, !priv->buf_avail))
-                       throw system_error("GetOverlappedResult");
-               else
-               {
-                       priv->buf_avail += ret;
-                       delete priv->overlapped;
-                       priv->overlapped = 0;
-               }
-       }
-
-       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();
-#else
-       unsigned ret = sys_read(handle[0], buf, size);
-#endif
+       unsigned ret = reader.read(buf, size);
 
        if(ret==0)
        {
@@ -145,37 +93,5 @@ unsigned Pipe::do_read(char *buf, unsigned size)
        return ret;
 }
 
-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;
-
-               DWORD ret;
-               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)
-                               throw system_error("ReadFile");
-               }
-               else
-               {
-                       priv->buf_avail = ret;
-                       delete priv->overlapped;
-                       priv->overlapped = 0;
-                       SetEvent(*priv->event);
-               }
-       }
-
-       return priv->event;
-#else
-       return handle[0];
-#endif
-}
-
 } // namespace IO
 } // namespace Msp
index eb77e6ab830d0d50cfa5bbedc16a29b7bbfbe4d2..813c4d8e6c634047c07b70830b8a196e5252df43 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_IO_PIPE_H_
 
 #include "eventobject.h"
+#include "eventreader.h"
 #include "handle.h"
 
 namespace Msp {
@@ -10,10 +11,8 @@ namespace IO {
 class Pipe: public EventObject
 {
 private:
-       struct Private;
-
        Handle handle[2];
-       Private *priv;
+       EventReader reader;
 
 public:
        Pipe();
@@ -28,7 +27,7 @@ protected:
        virtual unsigned do_read(char *, unsigned);
 
 public:
-       virtual const Handle &get_event_handle();
+       virtual const Handle &get_event_handle() { return reader.get_event(); }
 };
 
 } // namespace IO