]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
c3a844a74957a3fc9b17b36ee9248d7abe750f0d
[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 ThreadEngine::ThreadEngine()
25 {
26 }
27
28 void ThreadEngine::Start(Thread* thread)
29 {
30         ThreadData* data = new ThreadData;
31         thread->state = data;
32
33         DWORD ThreadId = 0;
34         data->handle = CreateThread(NULL,0,ThreadEngine::Entry,thread,0,&ThreadId);
35
36         if (data->handle == NULL)
37         {
38                 DWORD lasterr = GetLastError();
39                 thread->state = NULL;
40                 delete data;
41                 std::string err = "Unable to create new thread: " + ConvToStr(lasterr);
42                 SetLastError(ERROR_SUCCESS);
43                 throw CoreException(err);
44         }
45 }
46
47 ThreadEngine::~ThreadEngine()
48 {
49 }
50
51 DWORD WINAPI ThreadEngine::Entry(void* parameter)
52 {
53         Thread* pt = static_cast<Thread*>(parameter);
54         pt->Run();
55         return 0;
56 }
57
58 void ThreadData::FreeThread(Thread* thread)
59 {
60         thread->SetExitFlag();
61         WaitForSingleObject(handle,INFINITE);
62         CloseHandle(handle);
63 }
64
65 class ThreadSignalSocket : public BufferedSocket
66 {
67         SocketThread* parent;
68  public:
69         ThreadSignalSocket(SocketThread* t, int newfd)
70                 : BufferedSocket(newfd), parent(t)
71         {
72         }
73
74         void OnDataReady()
75         {
76                 recvq.clear();
77                 parent->OnNotify();
78         }
79
80         void OnError(BufferedSocketError)
81         {
82                 ServerInstance->GlobalCulls.AddItem(this);
83         }
84 };
85
86 SocketThread::SocketThread()
87 {
88         int listenFD = socket(AF_INET, SOCK_STREAM, 0);
89         if (listenFD == -1)
90                 throw CoreException("Could not create ITC pipe");
91         int connFD = socket(AF_INET, SOCK_STREAM, 0);
92         if (connFD == -1)
93                 throw CoreException("Could not create ITC pipe");
94
95         if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
96                 throw CoreException("Could not create ITC pipe");
97         SocketEngine::NonBlocking(connFD);
98
99         struct sockaddr_in addr;
100         socklen_t sz = sizeof(addr);
101         getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
102         connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
103         SocketEngine::Blocking(listenFD);
104         int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
105         if (nfd < 0)
106                 throw CoreException("Could not create ITC pipe");
107         new ThreadSignalSocket(this, nfd);
108         closesocket(listenFD);
109
110         SocketEngine::Blocking(connFD);
111         this->signal.connFD = connFD;
112 }
113
114 void SocketThread::NotifyParent()
115 {
116         char dummy = '*';
117         send(signal.connFD, &dummy, 1, 0);
118 }
119
120 SocketThread::~SocketThread()
121 {
122         if (signal.connFD >= 0)
123         {
124                 shutdown(signal.connFD, 2);
125                 closesocket(signal.connFD);
126         }
127 }