]> git.tdb.fi Git - libs/core.git/blob - source/io/pipe.cpp
Use the correct unit of time for WaitForSingleObject
[libs/core.git] / source / io / pipe.cpp
1 #include "pipe.h"
2
3 using namespace std;
4
5 namespace Msp {
6 namespace IO {
7
8 Pipe::Pipe():
9         reader(read_handle, 1024)
10 {
11         mode = M_RDWR;
12
13         platform_init();
14
15         set_events(P_INPUT);
16 }
17
18 Pipe::~Pipe()
19 {
20         set_events(P_NONE);
21
22         signal_flush_required.emit();
23         sys_close(read_handle);
24         sys_close(write_handle);
25 }
26
27 void Pipe::set_mode(Mode m)
28 {
29         m = m&M_RDWR;
30         if(!m)
31                 throw invalid_argument("Pipe::set_mode");
32
33         check_access(m);
34
35         Mode close = mode&M_RDWR&~m;
36         if(close&M_READ)
37                 sys_close(read_handle);
38         if(close&M_WRITE)
39                 sys_close(write_handle);
40         mode = (mode&~M_RDWR)|m;
41 }
42
43 void Pipe::set_block(bool b)
44 {
45         mode = (mode&~M_NONBLOCK);
46         if(b)
47                 mode = (mode|M_NONBLOCK);
48
49         sys_set_blocking(read_handle, b);
50         sys_set_blocking(write_handle, b);
51 }
52
53 unsigned Pipe::do_write(const char *buf, unsigned size)
54 {
55         if(size==0)
56                 return 0;
57
58         return sys_write(write_handle, buf, size);
59 }
60
61 unsigned Pipe::do_read(char *buf, unsigned size)
62 {
63         if(size==0)
64                 return 0;
65
66         unsigned ret = reader.read(buf, size);
67         if(ret==0)
68                 set_eof();
69
70         return ret;
71 }
72
73 const Handle &Pipe::get_handle(Mode m)
74 {
75         if(m==M_READ)
76                 return read_handle;
77         else if(m==M_WRITE)
78                 return write_handle;
79         else
80                 throw invalid_argument("Pipe::get_handle");
81 }
82
83 } // namespace IO
84 } // namespace Msp