2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
22 #include "threadengines/threadengine_win32.h"
24 void ThreadEngine::Start(Thread* thread)
26 thread->state.handle = CreateThread(NULL, 0, ThreadEngine::Entry, thread, 0, NULL);
28 if (thread->state.handle == NULL)
30 DWORD lasterr = GetLastError();
31 std::string err = "Unable to create new thread: " + ConvToStr(lasterr);
32 SetLastError(ERROR_SUCCESS);
33 throw CoreException(err);
37 DWORD WINAPI ThreadEngine::Entry(void* parameter)
39 Thread* pt = static_cast<Thread*>(parameter);
44 void ThreadEngine::Stop(Thread* thread)
46 thread->SetExitFlag();
47 HANDLE handle = thread->state.handle;
48 WaitForSingleObject(handle,INFINITE);
52 class ThreadSignalSocket : public BufferedSocket
56 ThreadSignalSocket(SocketThread* t, int newfd)
57 : BufferedSocket(newfd), parent(t)
67 void OnError(BufferedSocketError)
69 ServerInstance->GlobalCulls.AddItem(this);
73 static bool BindAndListen(int sockfd, int port, const char* addr)
75 irc::sockets::sockaddrs servaddr;
76 if (!irc::sockets::aptosa(addr, port, servaddr))
79 if (SocketEngine::Bind(sockfd, servaddr) != 0)
82 if (SocketEngine::Listen(sockfd, ServerInstance->Config->MaxConn) != 0)
84 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR in listen(): %s", strerror(errno));
91 SocketThread::SocketThread()
93 int listenFD = socket(AF_INET, SOCK_STREAM, 0);
95 throw CoreException("Could not create ITC pipe");
96 int connFD = socket(AF_INET, SOCK_STREAM, 0);
98 throw CoreException("Could not create ITC pipe");
100 if (!BindAndListen(listenFD, 0, "127.0.0.1"))
101 throw CoreException("Could not create ITC pipe");
102 SocketEngine::NonBlocking(connFD);
104 struct sockaddr_in addr;
105 socklen_t sz = sizeof(addr);
106 getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
107 connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
108 SocketEngine::Blocking(listenFD);
109 int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
111 throw CoreException("Could not create ITC pipe");
112 new ThreadSignalSocket(this, nfd);
113 closesocket(listenFD);
115 SocketEngine::Blocking(connFD);
116 this->signal.connFD = connFD;
119 void SocketThread::NotifyParent()
122 send(signal.connFD, &dummy, 1, 0);
125 SocketThread::~SocketThread()
127 if (signal.connFD >= 0)
129 shutdown(signal.connFD, 2);
130 closesocket(signal.connFD);