EventPipe.cxx 3.06 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "EventPipe.hxx"
21
#include "FileDescriptor.hxx"
22
#include "system/Error.hxx"
23
#include "util/ScopeExit.hxx"
24
#include "util/Compiler.h"
25

26
#include <assert.h>
27 28
#include <unistd.h>

29
#ifdef _WIN32
30
#include "net/IPv4Address.hxx"
31 32
#include "net/StaticSocketAddress.hxx"
#include "net/UniqueSocketDescriptor.hxx"
33
#include "net/SocketError.hxx"
34 35
#endif

36
#ifdef _WIN32
37
static void PoorSocketPair(int fd[2]);
38 39
#endif

40
EventPipe::EventPipe()
41
{
42
#ifdef _WIN32
43
	PoorSocketPair(fds);
44
#else
45 46
	FileDescriptor r, w;
	if (!FileDescriptor::CreatePipeNonBlock(r, w))
47
		throw MakeErrno("pipe() has failed");
48 49

	fds[0] = r.Steal();
50
	fds[1] = w.Steal();
51
#endif
52 53
}

54
EventPipe::~EventPipe() noexcept
55
{
56
#ifdef _WIN32
57 58 59
	closesocket(fds[0]);
	closesocket(fds[1]);
#else
60
	close(fds[0]);
61 62
	close(fds[1]);
#endif
63 64 65
}

bool
66
EventPipe::Read() noexcept
67 68
{
	assert(fds[0] >= 0);
69
	assert(fds[1] >= 0);
70

71
	char buffer[256];
72
#ifdef _WIN32
73 74
	return recv(fds[0], buffer, sizeof(buffer), 0) > 0;
#else
75
	return read(fds[0], buffer, sizeof(buffer)) > 0;
76
#endif
77 78 79
}

void
80
EventPipe::Write() noexcept
81 82
{
	assert(fds[0] >= 0);
83 84
	assert(fds[1] >= 0);

85
#ifdef _WIN32
86 87
	send(fds[1], "", 1, 0);
#else
88
	gcc_unused ssize_t nbytes = write(fds[1], "", 1);
89 90 91
#endif
}

92
#ifdef _WIN32
93 94

/* Our poor man's socketpair() implementation
95
 * Due to limited protocol/address family support
96
 * it's better to keep this as a private implementation detail of EventPipe
97 98
 * rather than wide-available API.
 */
99 100
static void
PoorSocketPair(int fd[2])
101 102 103
{
	assert (fd != nullptr);

104
	UniqueSocketDescriptor listen_socket;
HyShai's avatar
HyShai committed
105
	if (!listen_socket.Create(AF_INET, SOCK_STREAM, IPPROTO_TCP))
106
		throw MakeSocketError("Failed to create socket");
107

108
	if (!listen_socket.Bind(IPv4Address(IPv4Address::Loopback(), 0)))
109
		throw MakeSocketError("Failed to create socket");
110

111
	if (!listen_socket.Listen(1))
112
		throw MakeSocketError("Failed to listen on socket");
113

114
	UniqueSocketDescriptor socket0;
115
	if (!socket0.Create(AF_INET, SOCK_STREAM, IPPROTO_TCP))
116
		throw MakeSocketError("Failed to create socket");
117

118
	if (!socket0.Connect(listen_socket.GetLocalAddress()))
119
		throw MakeSocketError("Failed to connect socket");
120

121 122
	socket0.SetNonBlocking();

123 124
	auto socket1 = listen_socket.AcceptNonBlock();
	if (!socket1.IsDefined())
125
		throw MakeSocketError("Failed to accept connection");
126 127 128

	fd[0] = socket0.Steal();
	fd[1] = socket1.Steal();
129
}
130 131

#endif