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