]> git.tdb.fi Git - libs/core.git/blob - source/io/pipe.cpp
Move most platform-specific code into overlay directories
[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_block(bool b)
28 {
29         mode = (mode&~M_NONBLOCK);
30         if(b)
31                 mode = (mode|M_NONBLOCK);
32
33         sys_set_blocking(read_handle, b);
34         sys_set_blocking(write_handle, b);
35 }
36
37 unsigned Pipe::do_write(const char *buf, unsigned size)
38 {
39         if(size==0)
40                 return 0;
41
42         return sys_write(write_handle, buf, size);
43 }
44
45 unsigned Pipe::do_read(char *buf, unsigned size)
46 {
47         if(size==0)
48                 return 0;
49
50         unsigned ret = reader.read(buf, size);
51         if(ret==0)
52                 set_eof();
53
54         return ret;
55 }
56
57 const Handle &Pipe::get_handle(Mode m)
58 {
59         if(m==M_READ)
60                 return read_handle;
61         else if(m==M_WRITE)
62                 return write_handle;
63         else
64                 throw invalid_argument("Pipe::get_handle");
65 }
66
67 } // namespace IO
68 } // namespace Msp