summaryrefslogtreecommitdiff
path: root/win/inspircd_namedpipe.cpp
blob: 9a960e4c3b3f93528292bc7aa7c1ae8dcec8bc16 (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
#include "inspircd.h"
#include "threadengine.h"
#include "inspircd_namedpipe.h"
#include <windows.h>

void IPCThread::Run()
{

	printf("*** IPCThread::Run() *** \n");
	LPTSTR Pipename = "\\\\.\\pipe\\InspIRCdStatus";

	while (GetExitFlag() == false)
	{
		Pipe = CreateNamedPipe (Pipename,
                                          PIPE_ACCESS_DUPLEX, // read/write access
                                          PIPE_TYPE_MESSAGE | // message type pipe
                                          PIPE_READMODE_MESSAGE | // message-read mode
                                          PIPE_WAIT, // blocking mode
                                          PIPE_UNLIMITED_INSTANCES, // max. instances
                                          MAXBUF, // output buffer size
                                          MAXBUF, // input buffer size
                                          1000, // client time-out
                                          NULL); // no security attribute

		printf("*** After CreateNamedPipe *** \n");

		if (Pipe == INVALID_HANDLE_VALUE)
		{
			printf("*** IPC failure creating named pipe: %s\n", dlerror());
			return;
		}

		printf("*** After check, exit flag=%d *** \n", GetExitFlag());

		printf("*** Loop *** \n");
		Connected = ConnectNamedPipe(Pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

		printf("*** After ConnectNamedPipe *** \n");

		if (Connected)
		{
			ServerInstance->Logs->Log("IPC", DEBUG, "About to ReadFile from pipe");

			Success = ReadFile (Pipe, // handle to pipe
				Request, // buffer to receive data
				MAXBUF, // size of buffer
				&BytesRead, // number of bytes read
				NULL); // not overlapped I/O

			Request[BytesRead] = '\0';
			ServerInstance->Logs->Log("IPC", DEBUG, "Received from IPC: %s", Request);
			//printf("Data Received: %s\n",chRequest);

			if (!Success || !BytesRead)
			{
				printf("*** IPC failure reading client named pipe: %s\n", dlerror());
				continue;
			}

			std::stringstream status;
			DWORD Written = 0;
			ServerInstance->Threads->Mutex(true);

			status << "name " << ServerInstance->Config->ServerName << std::endl;
			status << "END" << std::endl;

			ServerInstance->Threads->Mutex(false);

			/* This is a blocking call and will succeed, so long as the client doesnt disconnect */
			Success = WriteFile(Pipe, status.str().data(), status.str().length(), &Written, NULL);

			FlushFileBuffers(Pipe);
			DisconnectNamedPipe(Pipe);
		}
		else
		{
			// The client could not connect.
			printf("*** IPC failure connecting named pipe: %s\n", dlerror());
		}

		printf("*** sleep for next client ***\n");
		printf("*** Closing pipe handle\n");
		CloseHandle(Pipe);
	}
}

IPC::IPC(InspIRCd* Srv) : ServerInstance(Srv)
{
	/* The IPC pipe is threaded */
	thread = new IPCThread(Srv);
	Srv->Threads->Create(thread);
	printf("*** CREATE IPC THREAD ***\n");
}

void IPC::Check()
{
	ServerInstance->Threads->Mutex(true);

	/* Check the state of the thread, safe in here */



	ServerInstance->Threads->Mutex(false);
}

IPC::~IPC()
{
	thread->SetExitFlag();
	delete thread;
}