1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __SOCKETENGINE_IOCP__
15 #define __SOCKETENGINE_IOCP__
17 #define READ_BUFFER_SIZE 500
20 #include "inspircd_config.h"
21 #include "inspircd_win32wrapper.h"
24 #include "socketengine.h"
28 SOCKET_IO_EVENT_READ_READY = 0,
29 SOCKET_IO_EVENT_WRITE_READY = 1,
30 SOCKET_IO_EVENT_ACCEPT = 2,
31 SOCKET_IO_EVENT_ERROR = 3,
32 NUM_SOCKET_IO_EVENTS = 4,
39 SocketIOEvent m_event;
41 Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
43 memset(&m_overlap, 0, sizeof(OVERLAPPED));
53 class IOCPEngine : public SocketEngine
55 /** Creates a "fake" file descriptor for use with an IOCP socket.
56 * @return -1 if there are no free slots, and an integer if it finds one.
58 __inline int GenerateFd()
61 for(; i < MAX_DESCRIPTORS; ++i)
67 /** Global I/O completion port that sockets attach to.
69 HANDLE m_completionPort;
71 /** This is kinda shitty... :/ for getting an address from a real fd.
73 map<int, EventHandler*> m_binding;
76 /** Creates an IOCP Socket Engine
77 * @param Instance The creator of this object
79 IOCPEngine(InspIRCd * Instance);
81 /** Deletes an IOCP socket engine and all the attached sockets
85 /** Adds an event handler to the completion port, and sets up initial events.
86 * @param eh EventHandler to add
87 * @return True if success, false if no room
89 bool AddFd(EventHandler* eh);
91 /** Gets the maximum number of file descriptors that this engine can handle.
92 * @return The number of file descriptors
94 __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
96 /** Gets the number of free/remaining file descriptors under this engine.
97 * @return Remaining count
99 __inline int GetRemainingFds()
101 register int count = 0;
103 for(; i < MAX_DESCRIPTORS; ++i)
109 /** Removes a file descriptor from the set, preventing it from receiving any more events
110 * @return True if remove was successful, false otherwise
112 bool DelFd(EventHandler* eh, bool force = false);
114 /** Called every loop to handle input/output events for all sockets under this engine
115 * @return The number of "changed" sockets.
117 int DispatchEvents();
119 /** Gets the name of this socket engine as a string.
120 * @return string of socket engine name
122 std::string GetName();
124 /** Queues a Write event on the specified event handler.
125 * @param eh EventHandler that needs data sent on
127 void WantWrite(EventHandler* eh);
129 /** Posts a completion event on the specified socket.
130 * @param eh EventHandler for message
131 * @param type Event Type
132 * @param param Event Parameter
133 * @return True if added, false if not
135 bool PostCompletionEvent(EventHandler * eh, SocketIOEvent type, int param);
137 /** Posts a read event on the specified socket
138 * @param eh EventHandler (socket)
140 void PostReadEvent(EventHandler * eh);
142 /** Posts an accept event on the specified socket
143 * @param eh EventHandler (socket)
145 void PostAcceptEvent(EventHandler * eh);
147 /** Returns the EventHandler attached to a specific fd.
148 * If the fd isnt in the socketengine, returns NULL.
149 * @param fd The event handler to look for
150 * @return A pointer to the event handler, or NULL
152 EventHandler* GetRef(int fd);
154 /** Returns true if a file descriptor exists in
155 * the socket engine's list.
156 * @param fd The event handler to look for
157 * @return True if this fd has an event handler
161 /** Returns the EventHandler attached to a specific fd.
162 * If the fd isnt in the socketengine, returns NULL.
163 * @param fd The event handler to look for
164 * @return A pointer to the event handler, or NULL
166 EventHandler* GetIntRef(int fd);
169 //typedef void(*OpHandler)(EventHandler)
170 /** Event Handler Array
173 /** Creates a SocketEngine
175 class SocketEngineFactory
178 /** Create a new instance of SocketEngine based on IOCPEngine
180 SocketEngine* Create(InspIRCd* Instance) { return new IOCPEngine(Instance); }