]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
2.0.5 release
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "threadengines/threadengine_win32.h"
16
17 ThreadEngine::ThreadEngine()
18 {
19 }
20
21 void ThreadEngine::Start(Thread* thread)
22 {
23         ThreadData* data = new ThreadData;
24         thread->state = data;
25
26         DWORD ThreadId = 0;
27         data->handle = CreateThread(NULL,0,ThreadEngine::Entry,thread,0,&ThreadId);
28
29         if (data->handle == NULL)
30         {
31                 thread->state = NULL;
32                 delete data;
33                 throw CoreException(std::string("Unable to create new thread: ") + dlerror());
34         }
35 }
36
37 ThreadEngine::~ThreadEngine()
38 {
39 }
40
41 DWORD WINAPI ThreadEngine::Entry(void* parameter)
42 {
43         Thread* pt = static_cast<Thread*>(parameter);
44         pt->Run();
45         return 0;
46 }
47
48 void ThreadData::FreeThread(Thread* thread)
49 {
50         thread->SetExitFlag();
51         WaitForSingleObject(handle,INFINITE);
52 }
53
54 class ThreadSignalSocket : public BufferedSocket
55 {
56         SocketThread* parent;
57  public:
58         ThreadSignalSocket(SocketThread* t, int newfd)
59                 : BufferedSocket(newfd), parent(t)
60         {
61         }
62
63         void OnDataReady()
64         {
65                 recvq.clear();
66                 parent->OnNotify();
67         }
68
69         void OnError(BufferedSocketError)
70         {
71                 ServerInstance->GlobalCulls.AddItem(this);
72         }
73 };
74
75 SocketThread::SocketThread()
76 {
77         int listenFD = socket(AF_INET, SOCK_STREAM, 0);
78         if (listenFD == -1)
79                 throw CoreException("Could not create ITC pipe");
80         int connFD = socket(AF_INET, SOCK_STREAM, 0);
81         if (connFD == -1)
82                 throw CoreException("Could not create ITC pipe");
83
84         if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
85                 throw CoreException("Could not create ITC pipe");
86         ServerInstance->SE->NonBlocking(connFD);
87
88         struct sockaddr_in addr;
89         socklen_t sz = sizeof(addr);
90         getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
91         connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
92         ServerInstance->SE->Blocking(listenFD);
93         int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
94         if (nfd < 0)
95                 throw CoreException("Could not create ITC pipe");
96         new ThreadSignalSocket(this, nfd);
97         closesocket(listenFD);
98
99         ServerInstance->SE->Blocking(connFD);
100         this->signal.connFD = connFD;
101 }
102
103 void SocketThread::NotifyParent()
104 {
105         char dummy = '*';
106         send(signal.connFD, &dummy, 1, 0);
107 }
108
109 SocketThread::~SocketThread()
110 {
111         if (signal.connFD >= 0)
112         {
113                 shutdown(signal.connFD, 2);
114                 closesocket(signal.connFD);
115         }
116 }