3 #include <msp/core/systemerror.h>
4 #include "eventreader.h"
5 #include "handle_private.h"
12 struct EventReader::Private
14 OVERLAPPED overlapped;
25 EventReader::EventReader(Handle &h, unsigned size):
29 memset(&priv->overlapped, 0, sizeof(OVERLAPPED));
30 *priv->event = CreateEvent(0, true, false, 0);
31 priv->overlapped.hEvent = *priv->event;
32 priv->buf_size = size;
33 priv->buffer = new char[priv->buf_size];
34 priv->buf_next = priv->buffer;
37 EventReader::~EventReader()
39 CloseHandle(*priv->event);
40 delete[] priv->buffer;
44 const Handle &EventReader::get_event()
50 void EventReader::start()
52 if(priv->buf_avail || priv->pending)
56 priv->buf_next = priv->buffer;
57 if(!ReadFile(*handle, priv->buffer, priv->buf_size, &ret, &priv->overlapped))
59 unsigned err = GetLastError();
60 if(err==ERROR_BROKEN_PIPE)
62 else if(err==ERROR_IO_PENDING)
65 throw system_error("ReadFile");
69 priv->buf_avail = ret;
70 SetEvent(*priv->event);
74 void EventReader::wait()
80 if(!GetOverlappedResult(*handle, &priv->overlapped, &ret, true))
82 DWORD err = GetLastError();
83 if(err==ERROR_BROKEN_PIPE)
86 throw system_error("GetOverlappedResult");
90 priv->buf_avail = ret;
91 priv->pending = false;
95 unsigned EventReader::read(char *buf, unsigned len)
102 // No data in buffer, try to get some
107 len = min(len, priv->buf_avail);
108 memcpy(buf, priv->buf_next, len);
109 priv->buf_next += len;
110 priv->buf_avail -= len;
114 ResetEvent(*priv->event);