086f579a6166e550eddc33e705b12a340c783b67
[libasyncio] / asyncio / async_pipe.cpp
1 /** @file
2  *
3  * (c) Copyright 2007 by Intra2net AG
4  * 
5  * info@intra2net.com
6  */
7
8 #include "async_pipe.hpp"
9
10 #include <functional>
11 #include <boost/bind.hpp>
12
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <errno.h>
17
18
19 namespace AsyncIo
20 {
21
22
23 /*
24  * Implementation of SimplePipe
25  */
26
27 SimplePipe::SimplePipe()
28 {
29    m_signal_read.connect(boost::bind(&SimplePipe::slotReceived,this));
30 } // eo SimplePipe::SimplePipe()
31
32
33 SimplePipe::~SimplePipe()
34 {
35 } // eo SimplePipe::~SimplePipe()
36
37
38 /**
39  * makes a pipe.
40  * This method connects itself with a newly created peer object with a bidirectional pipe.
41  * @return the peer pipe object.
42  */
43 bool SimplePipe::makePipe(SimplePipe& peer)
44 {
45    close(); // just in case...
46
47    int fds[2];
48
49    int res= ::socketpair( AF_UNIX, SOCK_STREAM, 0, fds);
50
51    if (res)
52    {
53       m_errno= errno;
54       return false;
55    }
56    else
57    {
58       m_errno= 0;
59    }
60
61    peer.close(); // just in case
62
63    setWriteFd(fds[0]);
64    setReadFd(fds[0]);
65
66    peer.setWriteFd(fds[1]);
67    peer.setReadFd(fds[1]);
68
69    return true;
70 } // eo SimplePipe.:makePipe()
71
72
73 /**
74  * sends a string through the pipe.
75  * @param data the data which should be sent to the other side.
76  */
77 void SimplePipe::sendString(const std::string& data)
78 {
79    lowSend(data);
80 } // eo SimplePipe::sendString(const std::string&)
81
82
83 /**
84  * emits the signal signalReceived with the received data.
85  * This slot is connected to IOImplementation::m_signal_read.
86  */
87 void SimplePipe::slotReceived()
88 {
89    std::string data;
90    data.swap(m_input_buffer);
91    signal_received_string(data);
92 } // eo SimplePipe::slotReceived()
93
94
95 } // eo namespace AsyncIo