Add poll() socket engine. Nothing will actually use this, yet, and it's untested..

git-svn-id: http://svn.inspircd.org/repository/branches/1_1_stable@10242 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
w00t 2008-08-24 20:44:09 +00:00
parent 12f236bfba
commit ad2910bdd0
3 changed files with 221 additions and 0 deletions

View File

@ -0,0 +1,66 @@
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd: (C) 2002-2008 InspIRCd Development Team
* See: http://www.inspircd.org/wiki/index.php/Credits
*
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
#ifndef __SOCKETENGINE_POLL__
#define __SOCKETENGINE_POLL__
#include <vector>
#include <string>
#include <map>
#include "inspircd_config.h"
#include "globals.h"
#include "inspircd.h"
#include "socketengine.h"
#ifndef __USE_XOPEN
#define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
#endif
#include <poll.h>
class InspIRCd;
/** A specialisation of the SocketEngine class, designed to use poll().
*/
class PollEngine : public SocketEngine
{
private:
/** These are used by poll() to hold socket events
*/
struct pollfd events[MAX_DESCRIPTORS];
public:
/** Create a new PollEngine
* @param Instance The creator of this object
*/
PollEngine(InspIRCd* Instance);
/** Delete a PollEngine
*/
virtual ~PollEngine();
virtual bool AddFd(EventHandler* eh);
virtual int GetMaxFds();
virtual int GetRemainingFds();
virtual bool DelFd(EventHandler* eh, bool force = false);
virtual int DispatchEvents();
virtual std::string GetName();
virtual void WantWrite(EventHandler* eh);
};
/** Creates a SocketEngine
*/
class SocketEngineFactory
{
public:
/** Create a new instance of SocketEngine based on PollEngine
*/
SocketEngine* Create(InspIRCd* Instance) { return new PollEngine(Instance); }
};
#endif

141
src/socketengine_poll.cpp Normal file
View File

@ -0,0 +1,141 @@
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd: (C) 2002-2008 InspIRCd Development Team
* See: http://www.inspircd.org/wiki/index.php/Credits
*
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
#include "inspircd.h"
#include "exitcodes.h"
#include "socketengine_epoll.h"
PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance)
{
// Poll requires no special setup (which is nice).
CurrentSetSize = 0;
memset(&events, 0, sizeof(struct pollfds * MAX_DESCRIPTORS));
}
PollEngine::~PollEngine()
{
// No destruction required, either.
}
bool PollEngine::AddFd(EventHandler* eh)
{
int fd = eh->GetFd();
if ((fd < 0) || (fd > MAX_DESCRIPTORS))
return false;
if (GetRemainingFds() <= 1)
return false;
if (ref[fd])
return false;
ref[fd] = eh;
events[fd].fd = fd;
if (eh->Readable())
{
events[fd].events = POLLRDNORM;
}
else
{
events[fd].events = POLLOUT;
}
ServerInstance->Log(DEBUG,"New file descriptor: %d", fd);
CurrentSetSize++;
return true;
}
void PollEngine::WantWrite(EventHandler* eh)
{
events[eh->GetFd()].events = POLLRDNORM | POLLOUT;
}
bool PollEngine::DelFd(EventHandler* eh, bool force)
{
int fd = eh->GetFd();
if ((fd < 0) || (fd > MAX_DESCRIPTORS))
return false;
events[fd].fd = -1;
events[fd].events = 0;
CurrentSetSize--;
ref[fd] = NULL;
ServerInstance->Log(DEBUG,"Remove file descriptor: %d", fd);
return true;
}
int PollEngine::GetMaxFds()
{
return MAX_DESCRIPTORS;
}
int PollEngine::GetRemainingFds()
{
return MAX_DESCRIPTORS - CurrentSetSize;
}
int PollEngine::DispatchEvents()
{
int i = poll(events, MAX_DESCRIPTORS, 1000);
int fd = 0;
if (i > 0)
{
for (fd = 0; fd < i; fd++)
{
if (events[fd].event & POLLHUP)
{
if (ref[events[j].data.fd])
ref[events[j].data.fd]->HandleEvent(EVENT_ERROR, 0);
continue;
}
if (events[fd].event & POLLERR)
{
// Get error number
if (getsockopt(events[j].data.fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
errcode = errno;
if (ref[events[j].data.fd])
ref[events[j].data.fd]->HandleEvent(EVENT_ERROR, errcode);
continue;
}
if (events[fd].event & POLLOUT)
{
// Switch to wanting read again
// event handlers have to request to write again if they need it
events[fd].event = POLLRDNORM;
if (ref[fd])
ref[fd]->HandleEvent(EVENT_WRITE);
}
if (events[fd].event & POLLIN)
{
if (ref[events[j].data.fd])
ref[events[j].data.fd]->HandleEvent(EVENT_READ);
}
}
}
return i;
}
std::string PollEngine::GetName()
{
return "poll";
}

View File

@ -142,6 +142,20 @@ int SelectEngine::DispatchEvents()
}
if (ev[i])
{
ServerInstance->Log(DEBUG, "checking for undetected errors on %d", ev[i]->GetFd());
// HACK: select() doesn't always use errfdset to tell us about a connect() erroring
// it generates writable, so we just check for error always (ugh ugh)
if (getsockopt(ev[i]->GetFd(), SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
{
errcode = errno;
ev[i]->HandleEvent(EVENT_ERROR, errcode);
ServerInstance->Log(DEBUG, "handled an error event (%d) on the socket", errcode);
continue;
}
ServerInstance->Log(DEBUG, "no error");
if (writeable[ev[i]->GetFd()])
{
writeable[ev[i]->GetFd()] = false;