InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
SocketEngine Class Referenceabstract

#include <socketengine.h>

Inheritance diagram for SocketEngine:
EPollEngine KQueueEngine PollEngine PortsEngine SelectEngine

Public Member Functions

 SocketEngine ()
 
virtual ~SocketEngine ()
 
virtual bool AddFd (EventHandler *eh, int event_mask)=0
 
void ChangeEventMask (EventHandler *eh, int event_mask)
 
int GetMaxFds () const
 
int GetUsedFds () const
 
virtual void DelFd (EventHandler *eh)=0
 
virtual bool HasFd (int fd)
 
virtual EventHandlerGetRef (int fd)
 
virtual int DispatchEvents ()=0
 
virtual void DispatchTrialWrites ()
 
virtual std::string GetName ()=0
 
virtual bool BoundsCheckFd (EventHandler *eh)
 
int Accept (EventHandler *fd, sockaddr *addr, socklen_t *addrlen)
 
int Close (EventHandler *fd)
 
int Close (int fd)
 
int Send (EventHandler *fd, const void *buf, size_t len, int flags)
 
int Recv (EventHandler *fd, void *buf, size_t len, int flags)
 
int RecvFrom (EventHandler *fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
 
int SendTo (EventHandler *fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
 
int Connect (EventHandler *fd, const sockaddr *serv_addr, socklen_t addrlen)
 
int Blocking (int fd)
 
int NonBlocking (int fd)
 
int Shutdown (EventHandler *fd, int how)
 
int Shutdown (int fd, int how)
 
int Bind (int fd, const irc::sockets::sockaddrs &addr)
 
int Listen (int sockfd, int backlog)
 
void SetReuse (int sockfd)
 
virtual void RecoverFromFork ()
 
void GetStats (float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
 

Static Public Member Functions

static bool IgnoreError ()
 
static std::string LastError ()
 
static std::string GetError (int errnum)
 

Public Attributes

unsigned long TotalEvents
 
unsigned long ReadEvents
 
unsigned long WriteEvents
 
unsigned long ErrorEvents
 

Protected Member Functions

void UpdateStats (size_t len_in, size_t len_out)
 
virtual void OnSetEvent (EventHandler *eh, int old_mask, int new_mask)=0
 
void SetEventMask (EventHandler *eh, int value)
 

Protected Attributes

int CurrentSetSize
 
EventHandler ** ref
 
std::set< int > trials
 
int MAX_DESCRIPTORS
 
size_t indata
 
size_t outdata
 
time_t lastempty
 

Detailed Description

Provides basic file-descriptor-based I/O support. The actual socketengine class presents the same interface on all operating systems, but its private members and internal behaviour should be treated as blackboxed, and vary from system to system and upon the config settings chosen by the server admin. The current version supports select, epoll and kqueue. The configure script will enable a socket engine based upon what OS is detected, and will derive a class from SocketEngine based upon what it finds. The derived classes file will also implement a classfactory, SocketEngineFactory, which will create a derived instance of SocketEngine using polymorphism so that the core and modules do not have to be aware of which SocketEngine derived class they are using.

Constructor & Destructor Documentation

SocketEngine::SocketEngine ( )

Constructor. The constructor transparently initializes the socket engine which the ircd is using. Please note that if there is a catastrophic failure (for example, you try and enable epoll on a 2.4 linux kernel) then this function may bail back to the shell.

SocketEngine::~SocketEngine ( )
virtual

Destructor. The destructor transparently tidies up any resources used by the socket engine.

Member Function Documentation

int SocketEngine::Accept ( EventHandler fd,
sockaddr *  addr,
socklen_t *  addrlen 
)

Abstraction for BSD sockets accept(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
addrThe client IP address and port
addrlenThe size of the sockaddr parameter.
Returns
This method should return exactly the same values as the system call it emulates.
virtual bool SocketEngine::AddFd ( EventHandler eh,
int  event_mask 
)
pure virtual

Add an EventHandler object to the engine. Use AddFd to add a file descriptor to the engine and have the socket engine monitor it. You must provide an object derived from EventHandler which implements HandleEvent().

Parameters
ehAn event handling object to add
event_maskThe initial event mask for the object

Implemented in PollEngine, PortsEngine, KQueueEngine, EPollEngine, and SelectEngine.

int SocketEngine::Bind ( int  fd,
const irc::sockets::sockaddrs addr 
)

Abstraction for BSD sockets bind(2). This function should emulate its namesake system call exactly.

Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::Blocking ( int  fd)

Make a file descriptor blocking.

Parameters
fda file descriptor to set to blocking mode
Returns
0 on success, -1 on failure, errno is set appropriately.
bool SocketEngine::BoundsCheckFd ( EventHandler eh)
virtual

Returns true if the file descriptors in the given event handler are within sensible ranges which can be handled by the socket engine.

void SocketEngine::ChangeEventMask ( EventHandler eh,
int  event_mask 
)

If you call this function and pass it an event handler, that event handler will receive the next available write event, even if the socket is a readable socket only. Developers should avoid constantly keeping an eventhandler in the writeable state, as this will consume large amounts of CPU time.

Parameters
ehThe event handler to change
event_maskThe changes to make to the wait state
int SocketEngine::Close ( EventHandler fd)

Abstraction for BSD sockets close(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::Close ( int  fd)

Abstraction for BSD sockets close(2). This function should emulate its namesake system call exactly. This function should emulate its namesake system call exactly.

Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::Connect ( EventHandler fd,
const sockaddr *  serv_addr,
socklen_t  addrlen 
)

Abstraction for BSD sockets connect(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
serv_addrThe server IP address and port.
addrlenThe size of the sockaddr parameter.
Returns
This method should return exactly the same values as the system call it emulates.
virtual void SocketEngine::DelFd ( EventHandler eh)
pure virtual

Delete an event handler from the engine. This function call deletes an EventHandler from the engine, returning true if it succeeded and false if it failed. This does not free the EventHandler pointer using delete, if this is required you must do this yourself.

Parameters
ehThe event handler object to remove

Implemented in PollEngine, PortsEngine, KQueueEngine, EPollEngine, and SelectEngine.

virtual int SocketEngine::DispatchEvents ( )
pure virtual

Waits for events and dispatches them to handlers. Please note that this doesn't wait long, only a couple of milliseconds. It returns the number of events which occurred during this call. This method will dispatch events to their handlers by calling their EventHandler::HandleEvent() methods with the necessary EventType value.

Returns
The number of events which have occured.

Implemented in PollEngine, PortsEngine, KQueueEngine, EPollEngine, and SelectEngine.

void SocketEngine::DispatchTrialWrites ( )
virtual

Dispatch trial reads and writes. This causes the actual socket I/O to happen when writes have been pre-buffered.

std::string SocketEngine::GetError ( int  errnum)
static

Returns the error for the given error num, strerror(errnum) on *nix

int SocketEngine::GetMaxFds ( ) const
inline

Returns the highest file descriptor you may store in the socket engine

Returns
The maximum fd value
virtual std::string SocketEngine::GetName ( )
pure virtual

Returns the socket engines name. This returns the name of the engine for use in /VERSION responses.

Returns
The socket engine name

Implemented in PollEngine, PortsEngine, KQueueEngine, EPollEngine, and SelectEngine.

EventHandler * SocketEngine::GetRef ( int  fd)
virtual

Returns the EventHandler attached to a specific fd. If the fd isnt in the socketengine, returns NULL.

Parameters
fdThe event handler to look for
Returns
A pointer to the event handler, or NULL

Reimplemented in PollEngine.

void SocketEngine::GetStats ( float &  kbitpersec_in,
float &  kbitpersec_out,
float &  kbitpersec_total 
)

Get data transfer statistics, kilobits per second in and out and total.

int SocketEngine::GetUsedFds ( ) const
inline

Returns the number of file descriptors being queried

Returns
The set size
bool SocketEngine::HasFd ( int  fd)
virtual

Returns true if a file descriptor exists in the socket engine's list.

Parameters
fdThe event handler to look for
Returns
True if this fd has an event handler
bool SocketEngine::IgnoreError ( )
inlinestatic

Should we ignore the error in errno? Checks EAGAIN and WSAEWOULDBLOCK

std::string SocketEngine::LastError ( )
static

Return the last socket related error. strrerror(errno) on *nix

int SocketEngine::Listen ( int  sockfd,
int  backlog 
)

Abstraction for BSD sockets listen(2). This function should emulate its namesake system call exactly.

Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::NonBlocking ( int  fd)

Make a file descriptor nonblocking.

Parameters
fdA file descriptor to set to nonblocking mode
Returns
0 on success, -1 on failure, errno is set appropriately.
void SocketEngine::RecoverFromFork ( )
virtual

This function is called immediately after fork(). Some socket engines (notably kqueue) cannot have their handles inherited by forked processes. This method allows for the socket engine to re-create its handle after the daemon forks as the socket engine is created long BEFORE the daemon forks.

Returns
void, but it is acceptable for this function to bail back to the shell or operating system on fatal error.

Reimplemented in KQueueEngine.

int SocketEngine::Recv ( EventHandler fd,
void *  buf,
size_t  len,
int  flags 
)

Abstraction for BSD sockets recv(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
bufThe buffer in which the data that is read is stored.
lenThe size of the buffer.
flagsA flag value that controls the reception of the data.
Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::RecvFrom ( EventHandler fd,
void *  buf,
size_t  len,
int  flags,
sockaddr *  from,
socklen_t *  fromlen 
)

Abstraction for BSD sockets recvfrom(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
bufThe buffer in which the data that is read is stored.
lenThe size of the buffer.
flagsA flag value that controls the reception of the data.
fromThe remote IP address and port.
fromlenThe size of the from parameter.
Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::Send ( EventHandler fd,
const void *  buf,
size_t  len,
int  flags 
)

Abstraction for BSD sockets send(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
bufThe buffer in which the data that is sent is stored.
lenThe size of the buffer.
flagsA flag value that controls the sending of the data.
Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::SendTo ( EventHandler fd,
const void *  buf,
size_t  len,
int  flags,
const sockaddr *  to,
socklen_t  tolen 
)

Abstraction for BSD sockets sendto(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
bufThe buffer in which the data that is sent is stored.
lenThe size of the buffer.
flagsA flag value that controls the sending of the data.
toThe remote IP address and port.
tolenThe size of the to parameter.
Returns
This method should return exactly the same values as the system call it emulates.
void SocketEngine::SetReuse ( int  sockfd)

Set SO_REUSEADDR and SO_LINGER on this file descriptor

int SocketEngine::Shutdown ( EventHandler fd,
int  how 
)

Abstraction for BSD sockets shutdown(2). This function should emulate its namesake system call exactly.

Parameters
fdThis version of the call takes an EventHandler instead of a bare file descriptor.
howWhat part of the socket to shut down
Returns
This method should return exactly the same values as the system call it emulates.
int SocketEngine::Shutdown ( int  fd,
int  how 
)

Abstraction for BSD sockets shutdown(2). This function should emulate its namesake system call exactly.

Returns
This method should return exactly the same values as the system call it emulates.

Member Data Documentation

int SocketEngine::CurrentSetSize
protected

Current number of descriptors in the engine

EventHandler** SocketEngine::ref
protected

Reference table, contains all current handlers

std::set<int> SocketEngine::trials
protected

List of handlers that want a trial read/write


The documentation for this class was generated from the following files: