+ /** Do not test this socket for writeability
+ */
+ FD_WANT_NO_WRITE = 0x10,
+ /** Give a write event at all times when writes will not block.
+ *
+ * You probably shouldn't use this state; if it's likely that the write
+ * will not block, try it first, then use FD_WANT_FAST_WRITE if it
+ * fails. If it's likely to block (or you are using polling-style reads)
+ * then use FD_WANT_SINGLE_WRITE.
+ */
+ FD_WANT_POLL_WRITE = 0x20,
+ /** Give a write event when writes don't block any more
+ *
+ * An event MUST be sent if writes will not block, and the most recent
+ * write/send() on this FD returned EAGAIN, or connect() returned
+ * EINPROGRESS. An event MAY be sent at any time that writes will not
+ * block.
+ *
+ * Before calling HandleEvent, a socket engine MAY change the state of
+ * the FD back to FD_WANT_EDGE_WRITE if it is simpler (for example, if a
+ * one-shot notification was registered). If further writes are needed,
+ * it is the responsibility of the event handler to change the state to
+ * one that will generate the required notifications
+ */
+ FD_WANT_FAST_WRITE = 0x40,
+ /** Give an optional write event on edge-triggered write unblock.
+ *
+ * This state is useful to avoid system calls when moving to/from
+ * FD_WANT_FAST_WRITE when writing data to a mostly-unblocked socket.
+ */
+ FD_WANT_EDGE_WRITE = 0x80,
+ /** Request a one-shot poll-style write notification. The socket will
+ * return to the FD_WANT_NO_WRITE state before HandleEvent is called.
+ */
+ FD_WANT_SINGLE_WRITE = 0x100,
+
+ /** Mask for all write events */
+ FD_WANT_WRITE_MASK = 0x1F0,
+
+ /** Add a trial read. During the next DispatchEvents invocation, this
+ * will call HandleEvent with EVENT_READ unless reads are known to be
+ * blocking.
+ */
+ FD_ADD_TRIAL_READ = 0x1000,
+ /** Assert that reads are known to block. This cancels FD_ADD_TRIAL_READ.
+ * Reset by SE before running EVENT_READ
+ */
+ FD_READ_WILL_BLOCK = 0x2000,
+
+ /** Add a trial write. During the next DispatchEvents invocation, this
+ * will call HandleEvent with EVENT_WRITE unless writes are known to be
+ * blocking.
+ *
+ * This could be used to group several writes together into a single
+ * send() syscall, or to ensure that writes are blocking when attempting
+ * to use FD_WANT_FAST_WRITE.
+ */
+ FD_ADD_TRIAL_WRITE = 0x4000,
+ /** Assert that writes are known to block. This cancels FD_ADD_TRIAL_WRITE.
+ * Reset by SE before running EVENT_WRITE
+ */
+ FD_WRITE_WILL_BLOCK = 0x8000,
+
+ /** Mask for trial read/trial write */
+ FD_TRIAL_NOTE_MASK = 0x5000
+};
+
+/** This class is a basic I/O handler class.
+ * Any object which wishes to receive basic I/O events
+ * from the socketengine must derive from this class and
+ * implement the HandleEvent() method. The derived class
+ * must then be added to SocketEngine using the method
+ * SocketEngine::AddFd(), after which point the derived
+ * class will receive events to its HandleEvent() method.
+ * The derived class should also implement one of Readable()
+ * and Writeable(). In the current implementation, only
+ * Readable() is used. If this returns true, the socketengine
+ * inserts a readable socket. If it is false, the socketengine
+ * inserts a writeable socket. The derived class should never
+ * change the value this function returns without first
+ * deleting the socket from the socket engine. The only
+ * requirement beyond this for an event handler is that it
+ * must have a file descriptor. What this file descriptor
+ * is actually attached to is completely up to you.
+ */
+class CoreExport EventHandler : public classbase
+{
+ private:
+ /** Private state maintained by socket engine */
+ int event_mask;
+
+ void SetEventMask(int mask) { event_mask = mask; }
+
+ protected:
+ /** File descriptor.
+ * All events which can be handled must have a file descriptor. This
+ * allows you to add events for sockets, fifo's, pipes, and various
+ * other forms of IPC. Do not change this while the object is
+ * registered with the SocketEngine
+ */
+ int fd;
+ public:
+ /** Get the current file descriptor
+ * @return The file descriptor of this handler
+ */
+ inline int GetFd() const { return fd; }
+
+ inline int GetEventMask() const { return event_mask; }
+
+ /** Set a new file desciptor
+ * @param FD The new file descriptor. Do not call this method without
+ * first deleting the object from the SocketEngine if you have
+ * added it to a SocketEngine instance.
+ */
+ void SetFd(int FD);
+
+ /** Constructor
+ */
+ EventHandler();
+
+ /** Destructor
+ */
+ virtual ~EventHandler() {}
+
+ /** Process an I/O event.
+ * You MUST implement this function in your derived
+ * class, and it will be called whenever read or write
+ * events are received.
+ * @param et either one of EVENT_READ for read events,
+ * EVENT_WRITE for write events and EVENT_ERROR for
+ * error events.
+ * @param errornum The error code which goes with an EVENT_ERROR.
+ */
+ virtual void HandleEvent(EventType et, int errornum = 0) = 0;
+
+ friend class SocketEngine;
+};
+
+/** Provides basic file-descriptor-based I/O support.