]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "threadengines/threadengine_win32.h"
23
24 void ThreadEngine::Start(Thread* thread)
25 {
26         thread->state.handle = CreateThread(NULL, 0, ThreadEngine::Entry, thread, 0, NULL);
27
28         if (thread->state.handle == NULL)
29         {
30                 DWORD lasterr = GetLastError();
31                 std::string err = "Unable to create new thread: " + ConvToStr(lasterr);
32                 SetLastError(ERROR_SUCCESS);
33                 throw CoreException(err);
34         }
35 }
36
37 DWORD WINAPI ThreadEngine::Entry(void* parameter)
38 {
39         Thread* pt = static_cast<Thread*>(parameter);
40         pt->Run();
41         return 0;
42 }
43
44 void ThreadEngine::Stop(Thread* thread)
45 {
46         thread->SetExitFlag();
47         HANDLE handle = thread->state.handle;
48         WaitForSingleObject(handle,INFINITE);
49         CloseHandle(handle);
50 }
51
52 class ThreadSignalSocket : public BufferedSocket
53 {
54         SocketThread* parent;
55  public:
56         ThreadSignalSocket(SocketThread* t, int newfd)
57                 : BufferedSocket(newfd), parent(t)
58         {
59         }
60
61         void OnDataReady()
62         {
63                 recvq.clear();
64                 parent->OnNotify();
65         }
66
67         void OnError(BufferedSocketError)
68         {
69                 ServerInstance->GlobalCulls.AddItem(this);
70         }
71 };
72
73 SocketThread::SocketThread()
74 {
75         int listenFD = socket(AF_INET, SOCK_STREAM, 0);
76         if (listenFD == -1)
77                 throw CoreException("Could not create ITC pipe");
78         int connFD = socket(AF_INET, SOCK_STREAM, 0);
79         if (connFD == -1)
80                 throw CoreException("Could not create ITC pipe");
81
82         if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
83                 throw CoreException("Could not create ITC pipe");
84         SocketEngine::NonBlocking(connFD);
85
86         struct sockaddr_in addr;
87         socklen_t sz = sizeof(addr);
88         getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
89         connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
90         SocketEngine::Blocking(listenFD);
91         int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
92         if (nfd < 0)
93                 throw CoreException("Could not create ITC pipe");
94         new ThreadSignalSocket(this, nfd);
95         closesocket(listenFD);
96
97         SocketEngine::Blocking(connFD);
98         this->signal.connFD = connFD;
99 }
100
101 void SocketThread::NotifyParent()
102 {
103         char dummy = '*';
104         send(signal.connFD, &dummy, 1, 0);
105 }
106
107 SocketThread::~SocketThread()
108 {
109         if (signal.connFD >= 0)
110         {
111                 shutdown(signal.connFD, 2);
112                 closesocket(signal.connFD);
113         }
114 }