InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
socketengine.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  * Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
7  * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd. InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef SOCKETENGINE_H
24 #define SOCKETENGINE_H
25 
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include "inspircd_config.h"
30 #include "socket.h"
31 #include "base.h"
32 
40 enum EventType
41 {
43  EVENT_READ = 0,
45  EVENT_WRITE = 1,
47  EVENT_ERROR = 2
48 };
49 
53 enum EventMask
54 {
57  FD_WANT_NO_READ = 0x1,
60  FD_WANT_POLL_READ = 0x2,
67  FD_WANT_FAST_READ = 0x4,
74  FD_WANT_EDGE_READ = 0x8,
75 
77  FD_WANT_READ_MASK = 0x0F,
78 
81  FD_WANT_NO_WRITE = 0x10,
89  FD_WANT_POLL_WRITE = 0x20,
103  FD_WANT_FAST_WRITE = 0x40,
109  FD_WANT_EDGE_WRITE = 0x80,
113  FD_WANT_SINGLE_WRITE = 0x100,
114 
116  FD_WANT_WRITE_MASK = 0x1F0,
117 
122  FD_ADD_TRIAL_READ = 0x1000,
126  FD_READ_WILL_BLOCK = 0x2000,
127 
136  FD_ADD_TRIAL_WRITE = 0x4000,
140  FD_WRITE_WILL_BLOCK = 0x8000,
141 
143  FD_TRIAL_NOTE_MASK = 0x5000
144 };
145 
164 class CoreExport EventHandler : public classbase
165 {
166  private:
168  int event_mask;
169  protected:
176  int fd;
177  public:
181  inline int GetFd() const { return fd; }
182 
183  inline int GetEventMask() const { return event_mask; }
184 
190  void SetFd(int FD);
191 
194  EventHandler();
195 
198  virtual ~EventHandler() {}
199 
209  virtual void HandleEvent(EventType et, int errornum = 0) = 0;
210 
211  friend class SocketEngine;
212 };
213 
232 class CoreExport SocketEngine
233 {
234  protected:
243  std::set<int> trials;
244 
245  int MAX_DESCRIPTORS;
246 
247  size_t indata;
248  size_t outdata;
249  time_t lastempty;
250 
251  void UpdateStats(size_t len_in, size_t len_out);
252 
253  virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask) = 0;
254  void SetEventMask(EventHandler* eh, int value);
255 public:
256 
257  unsigned long TotalEvents;
258  unsigned long ReadEvents;
259  unsigned long WriteEvents;
260  unsigned long ErrorEvents;
261 
270  SocketEngine();
271 
276  virtual ~SocketEngine();
277 
285  virtual bool AddFd(EventHandler* eh, int event_mask) = 0;
286 
298  void ChangeEventMask(EventHandler* eh, int event_mask);
299 
303  inline int GetMaxFds() const { return MAX_DESCRIPTORS; }
304 
308  inline int GetUsedFds() const { return CurrentSetSize; }
309 
318  virtual void DelFd(EventHandler* eh) = 0;
319 
325  virtual bool HasFd(int fd);
326 
332  virtual EventHandler* GetRef(int fd);
333 
342  virtual int DispatchEvents() = 0;
343 
347  virtual void DispatchTrialWrites();
348 
353  virtual std::string GetName() = 0;
354 
358  virtual bool BoundsCheckFd(EventHandler* eh);
359 
367  int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
368 
374  int Close(EventHandler* fd);
375 
381  int Close(int fd);
382 
391  int Send(EventHandler* fd, const void *buf, size_t len, int flags);
392 
401  int Recv(EventHandler* fd, void *buf, size_t len, int flags);
402 
413  int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen);
414 
425  int SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen);
426 
434  int Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen);
435 
440  int Blocking(int fd);
441 
446  int NonBlocking(int fd);
447 
454  int Shutdown(EventHandler* fd, int how);
455 
460  int Shutdown(int fd, int how);
461 
466  int Bind(int fd, const irc::sockets::sockaddrs& addr);
467 
472  int Listen(int sockfd, int backlog);
473 
476  void SetReuse(int sockfd);
477 
487  virtual void RecoverFromFork();
488 
491  void GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total);
492 
496  static bool IgnoreError();
497 
500  static std::string LastError();
501 
504  static std::string GetError(int errnum);
505 };
506 
508 {
509  if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
510  return true;
511 
512 #ifdef _WIN32
513  if (WSAGetLastError() == WSAEWOULDBLOCK)
514  return true;
515 #endif
516 
517  return false;
518 }
519 
520 SocketEngine* CreateSocketEngine();
521 
522 #endif
523 
int GetMaxFds() const
Definition: socketengine.h:303
Definition: socket.h:58
Definition: base.h:42
virtual ~EventHandler()
Definition: socketengine.h:198
std::set< int > trials
Definition: socketengine.h:243
int GetUsedFds() const
Definition: socketengine.h:308
EventHandler ** ref
Definition: socketengine.h:240
Definition: socketengine.h:232
int CurrentSetSize
Definition: socketengine.h:237
Definition: socketengine.h:164
static bool IgnoreError()
Definition: socketengine.h:507
int GetFd() const
Definition: socketengine.h:181
int fd
Definition: socketengine.h:176