summaryrefslogtreecommitdiff
path: root/src/socketengines/socketengine_eventfd.cpp
blob: 4e401632619bfd7f670d7623c2e8d7df8d22db5d (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
#include "services.h"
#include <sys/eventfd.h>

class PipeIO : public SocketIO
{
 public:
	/** Receive something from the buffer
 	 * @param s The socket
	 * @param buf The buf to read to
	 * @param sz How much to read
	 * @return Number of bytes received
	 */
	int Recv(Socket *s, char *buf, size_t sz) const
	{
		static eventfd_t dummy;
		return !eventfd_read(s->GetFD(), &dummy);
	}

	/** Write something to the socket
 	 * @param s The socket
	 * @param buf What to write
	 * @return Number of bytes written
	 */
	int Send(Socket *s, const Anope::string &buf) const
	{
		return !eventfd_write(s->GetFD(), 1);
	}
} pipeSocketIO;

Pipe::Pipe() : BufferedSocket()
{
	this->IO = &pipeSocketIO;
	this->Sock = eventfd(0, EFD_NONBLOCK);
	if (this->Sock < 0)
		throw CoreException(Anope::string("Could not create pipe: ") + Anope::LastError());

	this->IPv6 = false;

	SocketEngine->AddSocket(this);
}

bool Pipe::ProcessRead()
{
	this->IO->Recv(this, NULL, 0);
	return this->Read("");
}

bool Pipe::Read(const Anope::string &)
{
	this->OnNotify();
	return true;
}

void Pipe::Notify()
{
	this->Write("*");
}

void Pipe::OnNotify()
{
}