]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_namedpipe.cpp
9a960e4c3b3f93528292bc7aa7c1ae8dcec8bc16
[user/henk/code/inspircd.git] / win / inspircd_namedpipe.cpp
1 #include "inspircd.h"\r
2 #include "threadengine.h"\r
3 #include "inspircd_namedpipe.h"\r
4 #include <windows.h>\r
5 \r
6 void IPCThread::Run()\r
7 {\r
8 \r
9         printf("*** IPCThread::Run() *** \n");\r
10         LPTSTR Pipename = "\\\\.\\pipe\\InspIRCdStatus";\r
11 \r
12         while (GetExitFlag() == false)
13         {\r
14                 Pipe = CreateNamedPipe (Pipename,\r
15                                           PIPE_ACCESS_DUPLEX, // read/write access\r
16                                           PIPE_TYPE_MESSAGE | // message type pipe\r
17                                           PIPE_READMODE_MESSAGE | // message-read mode\r
18                                           PIPE_WAIT, // blocking mode\r
19                                           PIPE_UNLIMITED_INSTANCES, // max. instances\r
20                                           MAXBUF, // output buffer size\r
21                                           MAXBUF, // input buffer size\r
22                                           1000, // client time-out\r
23                                           NULL); // no security attribute\r
24 \r
25                 printf("*** After CreateNamedPipe *** \n");\r
26 \r
27                 if (Pipe == INVALID_HANDLE_VALUE)\r
28                 {\r
29                         printf("*** IPC failure creating named pipe: %s\n", dlerror());\r
30                         return;\r
31                 }\r
32 \r
33                 printf("*** After check, exit flag=%d *** \n", GetExitFlag());\r
34 \r
35                 printf("*** Loop *** \n");\r
36                 Connected = ConnectNamedPipe(Pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);\r
37 \r
38                 printf("*** After ConnectNamedPipe *** \n");\r
39 \r
40                 if (Connected)\r
41                 {\r
42                         ServerInstance->Logs->Log("IPC", DEBUG, "About to ReadFile from pipe");\r
43 \r
44                         Success = ReadFile (Pipe, // handle to pipe\r
45                                 Request, // buffer to receive data\r
46                                 MAXBUF, // size of buffer\r
47                                 &BytesRead, // number of bytes read\r
48                                 NULL); // not overlapped I/O\r
49 \r
50                         Request[BytesRead] = '\0';\r
51                         ServerInstance->Logs->Log("IPC", DEBUG, "Received from IPC: %s", Request);\r
52                         //printf("Data Received: %s\n",chRequest);\r
53 \r
54                         if (!Success || !BytesRead)\r
55                         {\r
56                                 printf("*** IPC failure reading client named pipe: %s\n", dlerror());\r
57                                 continue;\r
58                         }\r
59 \r
60                         std::stringstream status;\r
61                         DWORD Written = 0;\r
62                         ServerInstance->Threads->Mutex(true);\r
63 \r
64                         status << "name " << ServerInstance->Config->ServerName << std::endl;\r
65                         status << "END" << std::endl;\r
66 \r
67                         ServerInstance->Threads->Mutex(false);\r
68 \r
69                         /* This is a blocking call and will succeed, so long as the client doesnt disconnect */\r
70                         Success = WriteFile(Pipe, status.str().data(), status.str().length(), &Written, NULL);\r
71 \r
72                         FlushFileBuffers(Pipe);\r
73                         DisconnectNamedPipe(Pipe);\r
74                 }\r
75                 else\r
76                 {\r
77                         // The client could not connect.\r
78                         printf("*** IPC failure connecting named pipe: %s\n", dlerror());\r
79                 }\r
80 \r
81                 printf("*** sleep for next client ***\n");\r
82                 printf("*** Closing pipe handle\n");\r
83                 CloseHandle(Pipe);\r
84         }\r
85 }\r
86 \r
87 IPC::IPC(InspIRCd* Srv) : ServerInstance(Srv)
88 {
89         /* The IPC pipe is threaded */
90         thread = new IPCThread(Srv);
91         Srv->Threads->Create(thread);
92         printf("*** CREATE IPC THREAD ***\n");
93 }
94
95 void IPC::Check()
96 {
97         ServerInstance->Threads->Mutex(true);
98
99         /* Check the state of the thread, safe in here */
100
101
102
103         ServerInstance->Threads->Mutex(false);
104 }
105
106 IPC::~IPC()
107 {
108         thread->SetExitFlag();
109         delete thread;
110 }