Migrate libasyncio from boost.signal to signals2 (#8756)
[libasyncio] / asyncio / async_pipe.cpp
CommitLineData
8c15b8c7
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on 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 36namespace AsyncIo
5c8a3d40
RP
37{
38
39
40/*
41 * Implementation of SimplePipe
42 */
43
44SimplePipe::SimplePipe()
45{
46 m_signal_read.connect(boost::bind(&SimplePipe::slotReceived,this));
47} // eo SimplePipe::SimplePipe()
48
49
50SimplePipe::~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 */
60bool 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 */
94void 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 */
104void 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