if(!(mode&M_WRITE))
throw invalid_access(M_WRITE);
-#ifdef WIN32
- DWORD ret;
- if(!WriteFile(*handle, buf, len, &ret, 0))
- throw system_error("WriteFile");
-#else
- int ret = ::write(*handle, buf, len);
- if(ret==-1)
- throw system_error("write");
-#endif
-
- return ret;
+ return sys_write(handle, buf, len);
}
unsigned Console::do_read(char *buf, unsigned len)
if(!(mode&M_READ))
throw invalid_access(M_READ);
-#ifdef WIN32
- DWORD ret;
- if(!ReadFile(*handle, buf, len, &ret, 0))
- throw system_error("ReadFile");
-#else
- int ret = ::read(*handle, buf, len);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("read");
- }
- else if(ret==0)
+ unsigned ret = sys_read(handle, buf, len);
+ if(ret==0)
eof_flag = true;
-#endif
return ret;
}
signal_flush_required.emit();
-#ifdef WIN32
- CloseHandle(*handle);
-#else
- ::close(*handle);
-#endif
+ sys_close(handle);
handle = Handle();
signal_closed.emit();
#ifdef WIN32
if(mode&M_APPEND)
seek(0, S_END);
- DWORD ret;
- if(WriteFile(*handle, buf, size, &ret, 0)==0)
- throw system_error("WriteFile");
-#else
- int ret = ::write(*handle, buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("write");
- }
#endif
- return ret;
+ return sys_write(handle, buf, size);
}
unsigned File::do_read(char *buf, unsigned size)
if(size==0)
return 0;
-#ifdef WIN32
- DWORD ret;
- if(ReadFile(*handle, buf, size, &ret, 0)==0)
- throw system_error("ReadFile");
-#else
- int ret = ::read(*handle, buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("read");
- }
-#endif
-
+ unsigned ret = sys_read(handle, buf, size);
if(ret==0)
{
eof_flag = true;
+#include <cerrno>
+#include <unistd.h>
+#include <msp/core/systemerror.h>
#include "handle.h"
#include "handle_private.h"
return *this;
}
+
+unsigned sys_read(Handle &handle, char *buf, unsigned size)
+{
+#ifdef WIN32
+ DWORD ret;
+ if(ReadFile(*handle, buf, size, &ret, 0)==0)
+ throw system_error("ReadFile");
+#else
+ int ret = read(*handle, buf, size);
+ if(ret==-1)
+ {
+ if(errno==EAGAIN)
+ return 0;
+ else
+ throw system_error("read");
+ }
+#endif
+
+ return ret;
+}
+
+unsigned sys_write(Handle &handle, const char *buf, unsigned size)
+{
+#ifdef WIN32
+ DWORD ret;
+ if(WriteFile(*handle, buf, size, &ret, 0)==0)
+ throw system_error("WriteFile");
+#else
+ int ret = write(*handle, buf, size);
+ if(ret==-1)
+ {
+ if(errno==EAGAIN)
+ return 0;
+ else
+ throw system_error("write");
+ }
+#endif
+
+ return ret;
+}
+
+void sys_close(Handle &handle)
+{
+#ifdef WIN32
+ CloseHandle(*handle);
+#else
+ close(*handle);
+#endif
+}
+
} // namespace IO
} // namespace Msp
operator const void *() const;
};
+
+unsigned sys_read(Handle &, char *, unsigned);
+unsigned sys_write(Handle &, const char *, unsigned);
+void sys_close(Handle &);
+
} // namespace IO
} // namespace Msp
set_events(P_NONE);
signal_flush_required.emit();
-#ifdef WIN32
- CloseHandle(*handle[0]);
- CloseHandle(*handle[1]);
-#else
- ::close(*handle[0]);
- ::close(*handle[1]);
+ sys_close(handle[0]);
+ sys_close(handle[1]);
signal_closed.emit();
-#endif
}
void Pipe::set_block(bool b)
if(size==0)
return 0;
-#ifdef WIN32
- DWORD ret;
- if(!WriteFile(*handle[1], buf, size, &ret, 0))
- throw system_error("WriteFile");
-#else
- int ret = ::write(*handle[1], buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("write");
- }
-#endif
-
- return ret;
+ return sys_write(handle[1], buf, size);
}
unsigned Pipe::do_read(char *buf, unsigned size)
// Initiate another overlapped read in case someone is polling us
get_event_handle();
#else
- int ret = ::read(*handle[0], buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("read");
- }
+ unsigned ret = sys_read(handle[0], buf, size);
#endif
if(ret==0)
void Serial::close()
{
-#ifdef WIN32
- CloseHandle(*handle);
-#else
- ::close(*handle);
-#endif
+ sys_close(handle);
}
void Serial::set_block(bool b)
if(size==0)
return 0;
-#ifdef WIN32
- DWORD ret;
- if(WriteFile(*handle, buf, size, &ret, 0)==0)
- throw system_error("WriteFile");
-#else
- int ret = ::write(*handle, buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("write");
- }
-#endif
-
- return ret;
+ return sys_write(handle, buf, size);
}
unsigned Serial::do_read(char *buf, unsigned size)
if(size==0)
return 0;
-#ifdef WIN32
- DWORD ret;
- if(ReadFile(*handle, buf, size, &ret, 0)==0)
- throw system_error("ReadFile");
-#else
- int ret = ::read(*handle, buf, size);
- if(ret==-1)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw system_error("read");
- }
-#endif
-
- return ret;
+ return sys_read(handle, buf, size);
}
const Handle &Serial::get_event_handle()