]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
a20251348cf6bbb2bc18e147fe74c790f0a35ea3
[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 = reinterpret_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         SignalThread* parent;
57  public:
58         ThreadSignalSocket(SignalThread* t, InspIRCd* SI, int newfd, char* ip)
59                 : BufferedSocket(SI, newfd, ip), parent(t)
60         {
61                 parent->results = this;
62         }
63         
64         virtual bool OnDataReady()
65         {
66                 char data = 0;
67                 if (ServerInstance->SE->Recv(this, &data, 1, 0) > 0)
68                 {
69                         parent->OnNotify();
70                         return true;
71                 }
72                 return false;
73         }
74 };
75
76 class ThreadSignalListener : public ListenSocketBase
77 {
78         SocketThread* parent;
79         irc::sockets::insp_sockaddr sock_us;
80  public:
81         ThreadSignalListener(SocketThread* t, InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr), parent(t)
82         {
83                 socklen_t uslen = sizeof(sock_us);
84                 if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
85                 {
86                         throw ModuleException("Could not getsockname() to find out port number for ITC port");
87                 }
88         }
89
90         virtual void OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
91         {
92                 new ThreadSignalSocket(parent, ServerInstance, nfd, const_cast<char*>(ipconnectedto.c_str()));
93                 ServerInstance->SE->DelFd(this);
94                 // XXX unsafe casts suck
95         }
96 /* Using getsockname and ntohs, we can determine which port number we were allocated */
97         int GetPort()
98         {
99 #ifdef IPV6
100                 return ntohs(sock_us.sin6_port);
101 #else
102                 return ntohs(sock_us.sin_port);
103 #endif
104         }
105 };
106
107 SocketThread::SocketThread(InspIRCd* SI)
108 {
109         ThreadSignalListener* listener = new ThreadSignalListener(this, ServerInstance, 0, "127.0.0.1");
110         if (listener->GetFd() == -1)
111                 throw CoreException("Could not create ITC pipe");
112         int connFD = socket(AF_INET, SOCK_STREAM, 0);
113         if (connFD == -1)
114                 throw CoreException("Could not create ITC pipe");
115         
116         irc::sockets::sockaddrs addr;
117         irc::sockets::insp_aton("127.0.0.1", &addr.in4.sin_addr);
118         addr.in4.sin_family = AF_INET;
119         addr.in4.sin_port = htons(listener->GetPort());
120
121         if (connect(connFD, &addr.sa, sizeof(addr.in4)) == -1)
122         {
123                 ServerInstance->SE->DelFd(listener);
124                 close(connFD);
125                 throw CoreException("Could not connet to ITC pipe");
126         }
127         this->signal.connFD = connFD;
128 }
129
130 void SocketThread::NotifyParent()
131 {
132         char dummy = '*';
133         send(signal.connFD, &dummy, 1, 0);
134 }
135
136 SocketThread::~SocketThread()
137 {
138         close(signal.connFD);
139 }