]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
Remove InspIRCd* parameters and fields
[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()
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
70 SocketThread::SocketThread()
71 {
72         int listenFD = socket(AF_INET, SOCK_STREAM, 0);
73         if (listenFD == -1)
74                 throw CoreException("Could not create ITC pipe");
75         int connFD = socket(AF_INET, SOCK_STREAM, 0);
76         if (connFD == -1)
77                 throw CoreException("Could not create ITC pipe");
78
79         if (!SI->BindSocket(listenFD, 0, "127.0.0.1", true))
80                 throw CoreException("Could not create ITC pipe");
81         SI->SE->NonBlocking(connFD);
82
83         struct sockaddr_in addr;
84         socklen_t sz = sizeof(addr);
85         getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
86         connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
87         int nfd = accept(listenFD);
88         if (nfd < 0)
89                 throw CoreException("Could not create ITC pipe");
90         new ThreadSignalSocket(parent, nfd);
91         closesocket(listenFD);
92
93         SI->SE->Blocking(connFD);
94         this->signal.connFD = connFD;
95 }
96
97 void SocketThread::NotifyParent()
98 {
99         char dummy = '*';
100         send(signal.connFD, &dummy, 1, 0);
101 }
102
103 SocketThread::~SocketThread()
104 {
105         if (signal.connFD >= 0)
106         {
107                 shutdown(signal.connFD, 2);
108                 closesocket(signal.connFD);
109         }
110 }