summaryrefslogtreecommitdiff
path: root/src/socketengines/socketengine_win32.cpp
blob: 5d2c2d7cbdef4859c215951534f092ce820459df (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
#include "services.h"

static Socket *newsocket = NULL;

class LSocket : public ListenSocket
{
 public:
	LSocket(const Anope::string &host, int port) : ListenSocket(host, port) { }

	bool OnAccept(int fd, const sockaddrs &)
	{
		newsocket = new Socket(fd, this->IPv6);
		return true;
	}
};

int Pipe::RecvInternal(char *buf, size_t sz) const
{
	static char dummy[512];
	return read(this->Sock, &dummy, 512);
}

int Pipe::SendInternal(const Anope::string &) const
{
	static const char dummy = '*';
	return write(this->WritePipe, &dummy, 1);
}

Pipe::Pipe() : BufferedSocket()
{
	LSocket lfs("127.0.0.1", 0);

	int cfd = socket(AF_INET, SOCK_STREAM, 0);
	if (cfd == -1)
		throw CoreException("Error accepting new socket for Pipe");

	sockaddr_in addr;
	socklen_t sz = sizeof(addr);
	getsockname(lfs.GetSock(), reinterpret_cast<sockaddr *>(&addr), &sz);

	if (connect(cfd, reinterpret_cast<sockaddr *>(&addr), sz))
		throw CoreException("Error accepting new socket for Pipe");
	lfs.ProcessRead();
	if (!newsocket)
		throw CoreException("Error accepting new socket for Pipe");
	
	this->Sock = cfd;
	this->WritePipe = newsocket->GetSock();
	this->IPv6 = false;

	SocketEngine->AddSocket(this);
	newsocket = NULL;
}

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

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

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

void Pipe::OnNotify()
{
}