]> git.tdb.fi Git - libs/core.git/blob - source/io/pipe.cpp
Check errors from CreateSemaphore
[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         adjust_mode(mode, M_NONBLOCK, !b);
46         if(read_handle)
47                 sys_set_blocking(read_handle, b);
48         if(write_handle)
49                 sys_set_blocking(write_handle, b);
50 }
51
52 void Pipe::set_inherit(bool i)
53 {
54         adjust_mode(mode, M_INHERIT, i);
55         if(read_handle)
56                 sys_set_inherit(read_handle, i);
57         if(write_handle)
58                 sys_set_inherit(write_handle, i);
59 }
60
61 size_t Pipe::do_write(const char *buf, size_t size)
62 {
63         if(size==0)
64                 return 0;
65
66         return sys_write(write_handle, buf, size);
67 }
68
69 size_t Pipe::do_read(char *buf, size_t size)
70 {
71         if(size==0)
72                 return 0;
73
74         size_t ret = reader.read(buf, size);
75         if(ret==0)
76                 set_eof();
77
78         return ret;
79 }
80
81 const Handle &Pipe::get_handle(Mode m)
82 {
83         if(m==M_READ)
84                 return read_handle;
85         else if(m==M_WRITE)
86                 return write_handle;
87         else
88                 throw invalid_argument("Pipe::get_handle");
89 }
90
91 } // namespace IO
92 } // namespace Msp