summaryrefslogtreecommitdiff
path: root/src/system/EventPipe.cxx
blob: 462cc4a1c2828232f16fd83d893915301e7fdc4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2003-2020 The Music Player Daemon Project
 * 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.
 */

#include "EventPipe.hxx"
#include "FileDescriptor.hxx"
#include "system/Error.hxx"
#include "util/Compiler.h"

#include <cassert>

#include <unistd.h>

#ifdef _WIN32
#include "net/IPv4Address.hxx"
#include "net/StaticSocketAddress.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "net/SocketError.hxx"
#endif

#ifdef _WIN32
static void PoorSocketPair(int fd[2]);
#endif

EventPipe::EventPipe()
{
#ifdef _WIN32
	PoorSocketPair(fds);
#else
	FileDescriptor r, w;
	if (!FileDescriptor::CreatePipeNonBlock(r, w))
		throw MakeErrno("pipe() has failed");

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

EventPipe::~EventPipe() noexcept
{
#ifdef _WIN32
	closesocket(fds[0]);
	closesocket(fds[1]);
#else
	close(fds[0]);
	close(fds[1]);
#endif
}

bool
EventPipe::Read() noexcept
{
	assert(fds[0] >= 0);
	assert(fds[1] >= 0);

	char buffer[256];
#ifdef _WIN32
	return recv(fds[0], buffer, sizeof(buffer), 0) > 0;
#else
	return read(fds[0], buffer, sizeof(buffer)) > 0;
#endif
}

void
EventPipe::Write() noexcept
{
	assert(fds[0] >= 0);
	assert(fds[1] >= 0);

#ifdef _WIN32
	send(fds[1], "", 1, 0);
#else
	[[maybe_unused]] ssize_t nbytes = write(fds[1], "", 1);
#endif
}

#ifdef _WIN32

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

	UniqueSocketDescriptor listen_socket;
	if (!listen_socket.Create(AF_INET, SOCK_STREAM, IPPROTO_TCP))
		throw MakeSocketError("Failed to create socket");

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

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

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

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

	socket0.SetNonBlocking();

	auto socket1 = listen_socket.AcceptNonBlock();
	if (!socket1.IsDefined())
		throw MakeSocketError("Failed to accept connection");

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

#endif