1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "threadengines/threadengine_win32.h"
17 ThreadEngine::ThreadEngine(InspIRCd* Instance)
21 void ThreadEngine::Start(Thread* thread)
23 ThreadData* data = new ThreadData;
27 data->handle = CreateThread(NULL,0,ThreadEngine::Entry,thread,0,&ThreadId);
29 if (data->handle == NULL)
33 throw CoreException(std::string("Unable to create new thread: ") + dlerror());
37 ThreadEngine::~ThreadEngine()
41 DWORD WINAPI ThreadEngine::Entry(void* parameter)
43 Thread* pt = reinterpret_cast<Thread*>(parameter);
48 void ThreadData::FreeThread(Thread* thread)
50 thread->SetExitFlag();
51 WaitForSingleObject(handle,INFINITE);
54 class ThreadSignalSocket : public BufferedSocket
58 ThreadSignalSocket(SocketThread* t, InspIRCd* SI, int newfd, char* ip)
59 : BufferedSocket(SI, newfd, ip), parent(t)
63 virtual bool OnDataReady()
66 if (ServerInstance->SE->Recv(this, &data, 1, 0) > 0)
75 class ThreadSignalListener : public ListenSocketBase
78 irc::sockets::insp_sockaddr sock_us;
80 ThreadSignalListener(SocketThread* t, InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr), parent(t)
82 socklen_t uslen = sizeof(sock_us);
83 if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
85 throw ModuleException("Could not getsockname() to find out port number for ITC port");
89 virtual void OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
91 new ThreadSignalSocket(parent, ServerInstance, nfd, const_cast<char*>(ipconnectedto.c_str()));
92 ServerInstance->SE->DelFd(this);
93 // XXX unsafe casts suck
95 /* Using getsockname and ntohs, we can determine which port number we were allocated */
99 return ntohs(sock_us.sin6_port);
101 return ntohs(sock_us.sin_port);
106 SocketThread::SocketThread(InspIRCd* SI)
108 ThreadSignalListener* listener = new ThreadSignalListener(this, SI, 0, "127.0.0.1");
109 if (listener->GetFd() == -1)
110 throw CoreException("Could not create ITC pipe");
111 int connFD = socket(AF_INET, SOCK_STREAM, 0);
113 throw CoreException("Could not create ITC pipe");
115 struct sockaddr_in addr;
116 irc::sockets::insp_aton("127.0.0.1", &addr.sin_addr);
117 addr.sin_family = AF_INET;
118 addr.sin_port = htons(listener->GetPort());
120 if (connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
122 SI->SE->DelFd(listener);
124 throw CoreException("Could not connet to ITC pipe");
126 this->signal.connFD = connFD;
129 void SocketThread::NotifyParent()
132 send(signal.connFD, &dummy, 1, 0);
135 SocketThread::~SocketThread()
137 if (signal.connFD >= 0)
139 shutdown(signal.connFD, 2);
140 closesocket(signal.connFD);