]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
5c62b5081b89c5ad8d9fdc4cd3baa461e15b2c68
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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(InspIRCd* Instance)
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, InspIRCd* SI, int newfd, const char* ip)
59                 : BufferedSocket(SI, newfd, ip), parent(t)
60         {
61         }
62
63         virtual bool OnDataReady()
64         {
65                 char data = 0;
66                 if (ServerInstance->SE->Recv(this, &data, 1, 0) > 0)
67                 {
68                         parent->OnNotify();
69                         return true;
70                 }
71                 return false;
72         }
73 };
74
75 SocketThread::SocketThread(InspIRCd* SI)
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 (!SI->BindSocket(listenFD, 0, "127.0.0.1", true))
85                 throw CoreException("Could not create ITC pipe");
86         SI->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         int nfd = accept(listenFD);
93         if (nfd < 0)
94                 throw CoreException("Could not create ITC pipe");
95         new ThreadSignalSocket(parent, ServerInstance, nfd, "127.0.0.1");
96         closesocket(listenFD);
97
98         SI->SE->Blocking(connFD);
99         this->signal.connFD = connFD;
100 }
101
102 void SocketThread::NotifyParent()
103 {
104         char dummy = '*';
105         send(signal.connFD, &dummy, 1, 0);
106 }
107
108 SocketThread::~SocketThread()
109 {
110         if (signal.connFD >= 0)
111         {
112                 shutdown(signal.connFD, 2);
113                 closesocket(signal.connFD);
114         }
115 }