InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
inspsocket.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) 2007 Dennis Friis <peavey@inspircd.org>
7  * Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8  * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd. InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #ifndef INSPSOCKET_H
25 #define INSPSOCKET_H
26 
27 #include "timer.h"
28 
32 enum BufferedSocketState
33 {
35  I_DISCONNECTED,
37  I_CONNECTING,
39  I_CONNECTED,
41  I_ERROR
42 };
43 
47 enum BufferedSocketError
48 {
50  I_ERR_NONE,
52  I_ERR_DISCONNECT,
54  I_ERR_TIMEOUT,
56  I_ERR_SOCKET,
58  I_ERR_CONNECT,
60  I_ERR_BIND,
62  I_ERR_WRITE,
64  I_ERR_NOMOREFDS,
66  I_ERR_OTHER
67 };
68 
69 /* Required forward declarations */
70 class BufferedSocket;
71 
74 class CoreExport SocketTimeout : public Timer
75 {
76  private:
79  BufferedSocket* sock;
80 
83  int sfd;
84 
85  public:
92  SocketTimeout(int fd, BufferedSocket* thesock, long secs_from_now, time_t now) : Timer(secs_from_now, now), sock(thesock), sfd(fd) { }
93 
96  virtual void Tick(time_t now);
97 };
98 
103 class CoreExport StreamSocket : public EventHandler
104 {
106  reference<Module> IOHook;
109  std::deque<std::string> sendq;
111  size_t sendq_len;
113  std::string error;
114  protected:
115  std::string recvq;
116  public:
117  StreamSocket() : sendq_len(0) {}
118  inline Module* GetIOHook();
119  inline void AddIOHook(Module* m);
120  inline void DelIOHook();
124  virtual void HandleEvent(EventType et, int errornum = 0);
126  virtual void DoRead();
128  virtual void DoWrite();
129 
131  void SetError(const std::string& err) { if (error.empty()) error = err; }
132 
134  const std::string& getError() const { return error; }
135 
137  virtual void OnDataReady() = 0;
139  virtual void OnError(BufferedSocketError e) = 0;
140 
143  void WriteData(const std::string& data);
149  bool GetNextLine(std::string& line, char delim = '\n');
151  inline size_t getSendQSize() const { return sendq_len; }
152 
156  virtual void Close();
158  virtual CullResult cull();
159 };
169 class CoreExport BufferedSocket : public StreamSocket
170 {
171  public:
175 
181  BufferedSocketState state;
182 
183  BufferedSocket();
191  BufferedSocket(int newfd);
192 
202  void DoConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip);
203 
207  virtual void OnConnected();
208 
212  virtual void OnDataReady() = 0;
213 
223  virtual void OnTimeout();
224 
225  virtual ~BufferedSocket();
226  protected:
227  virtual void DoWrite();
228  BufferedSocketError BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned long timeout);
229  BufferedSocketError BeginConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip);
230 };
231 
232 #include "modules.h"
233 
234 inline Module* StreamSocket::GetIOHook() { return IOHook; }
235 inline void StreamSocket::AddIOHook(Module* m) { IOHook = m; }
236 inline void StreamSocket::DelIOHook() { IOHook = NULL; }
237 #endif
virtual CullResult cull()
Definition: base.cpp:34
Definition: timer.h:35
virtual void HandleEvent(EventType et, int errornum=0)=0
virtual void OnDataReady()=0
Definition: socket.h:58
void SetError(const std::string &err)
Definition: inspsocket.h:131
Definition: inspsocket.h:103
BufferedSocketState state
Definition: inspsocket.h:181
const std::string & getError() const
Definition: inspsocket.h:134
SocketTimeout(int fd, BufferedSocket *thesock, long secs_from_now, time_t now)
Definition: inspsocket.h:92
SocketTimeout * Timeout
Definition: inspsocket.h:174
Definition: modules.h:350
size_t getSendQSize() const
Definition: inspsocket.h:151
virtual void Tick(time_t TIME)=0
Definition: inspsocket.h:74
Definition: socketengine.h:164
Definition: inspsocket.h:169
Definition: base.h:32
virtual void DoWrite()
Definition: inspsocket.cpp:222