Commit | Line | Data |
---|---|---|
8c15b8c7 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
5c8a3d40 RP |
20 | /** @file |
21 | * | |
22 | * (c) Copyright 2007 by Intra2net AG | |
5c8a3d40 RP |
23 | */ |
24 | ||
42b7c46d | 25 | #include "async_pipe.hpp" |
5c8a3d40 RP |
26 | |
27 | #include <functional> | |
28 | #include <boost/bind.hpp> | |
29 | ||
30 | #include <unistd.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/socket.h> | |
33 | #include <errno.h> | |
34 | ||
35 | ||
42b7c46d | 36 | namespace AsyncIo |
5c8a3d40 RP |
37 | { |
38 | ||
39 | ||
40 | /* | |
41 | * Implementation of SimplePipe | |
42 | */ | |
43 | ||
44 | SimplePipe::SimplePipe() | |
45 | { | |
46 | m_signal_read.connect(boost::bind(&SimplePipe::slotReceived,this)); | |
47 | } // eo SimplePipe::SimplePipe() | |
48 | ||
49 | ||
50 | SimplePipe::~SimplePipe() | |
51 | { | |
52 | } // eo SimplePipe::~SimplePipe() | |
53 | ||
54 | ||
55 | /** | |
56 | * makes a pipe. | |
57 | * This method connects itself with a newly created peer object with a bidirectional pipe. | |
58 | * @return the peer pipe object. | |
59 | */ | |
60 | bool SimplePipe::makePipe(SimplePipe& peer) | |
61 | { | |
62 | close(); // just in case... | |
63 | ||
64 | int fds[2]; | |
65 | ||
66 | int res= ::socketpair( AF_UNIX, SOCK_STREAM, 0, fds); | |
67 | ||
68 | if (res) | |
69 | { | |
70 | m_errno= errno; | |
71 | return false; | |
72 | } | |
73 | else | |
74 | { | |
75 | m_errno= 0; | |
76 | } | |
77 | ||
78 | peer.close(); // just in case | |
79 | ||
80 | setWriteFd(fds[0]); | |
81 | setReadFd(fds[0]); | |
82 | ||
83 | peer.setWriteFd(fds[1]); | |
84 | peer.setReadFd(fds[1]); | |
85 | ||
86 | return true; | |
87 | } // eo SimplePipe.:makePipe() | |
88 | ||
89 | ||
90 | /** | |
91 | * sends a string through the pipe. | |
92 | * @param data the data which should be sent to the other side. | |
93 | */ | |
94 | void SimplePipe::sendString(const std::string& data) | |
95 | { | |
96 | lowSend(data); | |
97 | } // eo SimplePipe::sendString(const std::string&) | |
98 | ||
99 | ||
100 | /** | |
101 | * emits the signal signalReceived with the received data. | |
102 | * This slot is connected to IOImplementation::m_signal_read. | |
103 | */ | |
104 | void SimplePipe::slotReceived() | |
105 | { | |
106 | std::string data; | |
107 | data.swap(m_input_buffer); | |
108 | signal_received_string(data); | |
109 | } // eo SimplePipe::slotReceived() | |
110 | ||
111 | ||
42b7c46d | 112 | } // eo namespace AsyncIo |