Treat ERROR_BROKEN_PIPE as end-of-file instead of an error
authorMikko Rasa <tdb@tdb.fi>
Sun, 22 Aug 2021 10:19:29 +0000 (13:19 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 22 Aug 2021 10:19:29 +0000 (13:19 +0300)
source/io/windows/eventreader.cpp

index 8a4c862d3eb7a9d846af590322fffed6d73b1ccb..82d9e8c2a297a48dbe202eeace2edfbafc035328 100644 (file)
@@ -18,6 +18,7 @@ struct EventReader::Private
        unsigned buf_avail;
        char *buf_next;
        bool pending;
+       bool eof;
 };
 
 
@@ -33,6 +34,7 @@ EventReader::EventReader(Handle &h, unsigned size):
        priv->buf_avail = 0;
        priv->buf_next = priv->buffer;
        priv->pending = false;
+       priv->eof = false;
 }
 
 EventReader::~EventReader()
@@ -58,7 +60,9 @@ void EventReader::start()
        if(!ReadFile(*handle, priv->buffer, priv->buf_size, &ret, &priv->overlapped))
        {
                unsigned err = GetLastError();
-               if(err==ERROR_IO_PENDING)
+               if(err==ERROR_BROKEN_PIPE)
+                       priv->eof = true;
+               else if(err==ERROR_IO_PENDING)
                        priv->pending = true;
                else
                        throw system_error("ReadFile");
@@ -77,7 +81,13 @@ void EventReader::wait()
 
        DWORD ret;
        if(!GetOverlappedResult(*handle, &priv->overlapped, &ret, true))
-               throw system_error("GetOverlappedResult");
+       {
+               DWORD err = GetLastError();
+               if(err==ERROR_BROKEN_PIPE)
+                       priv->eof = true;
+               else
+                       throw system_error("GetOverlappedResult");
+       }
        else
        {
                priv->buf_avail = ret;
@@ -89,6 +99,9 @@ unsigned EventReader::read(char *buf, unsigned len)
 {
        if(!priv->buf_avail)
        {
+               if(priv->eof)
+                       return 0;
+
                // No data in buffer, try to get some
                start();
                wait();