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::Create(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(SignalThread* t, InspIRCd* SI, int newfd, char* ip)
59 : BufferedSocket(SI, newfd, ip), parent(t)
61 parent->results = this;
64 virtual bool OnDataReady()
67 if (ServerInstance->SE->Recv(this, &data, 1, 0) > 0)
76 class ThreadSignalListener : public ListenSocketBase
79 irc::sockets::insp_sockaddr sock_us;
81 ThreadSignalListener(SocketThread* t, InspIRCd* Instance, int port, const std::string &addr) : ListenSocketBase(Instance, port, addr), parent(t)
83 socklen_t uslen = sizeof(sock_us);
84 if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
86 throw ModuleException("Could not getsockname() to find out port number for ITC port");
90 virtual void OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
92 new ThreadSignalSocket(parent, ServerInstance, nfd, const_cast<char*>(ipconnectedto.c_str()));
93 ServerInstance->SE->DelFd(this);
94 // XXX unsafe casts suck
96 /* Using getsockname and ntohs, we can determine which port number we were allocated */
100 return ntohs(sock_us.sin6_port);
102 return ntohs(sock_us.sin_port);
107 SocketThread::SocketThread(InspIRCd* SI)
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);
114 throw CoreException("Could not create ITC pipe");
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());
121 if (connect(connFD, &addr.sa, sizeof(addr.in4)) == -1)
123 ServerInstance->SE->DelFd(listener);
125 throw CoreException("Could not connet to ITC pipe");
127 this->signal.connFD = connFD;
130 void SocketThread::NotifyParent()
133 send(signal.connFD, &dummy, 1, 0);
136 SocketThread::~SocketThread()
138 close(signal.connFD);