InspIRCd  3.0
inspsocket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
6  * Copyright (C) 2007 Dennis Friis <[email protected]>
7  * Copyright (C) 2006-2007 Craig Edwards <[email protected]>
8  * Copyright (C) 2006 Oliver Lupton <[email protected]>
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 #pragma once
25 
26 #include "timer.h"
27 
28 class IOHook;
29 
33 enum BufferedSocketState
34 {
36  I_DISCONNECTED,
38  I_CONNECTING,
40  I_CONNECTED,
42  I_ERROR
43 };
44 
48 enum BufferedSocketError
49 {
51  I_ERR_NONE,
53  I_ERR_DISCONNECT,
55  I_ERR_TIMEOUT,
57  I_ERR_SOCKET,
59  I_ERR_CONNECT,
61  I_ERR_BIND,
63  I_ERR_WRITE,
65  I_ERR_NOMOREFDS,
67  I_ERR_OTHER
68 };
69 
70 /* Required forward declarations */
71 class BufferedSocket;
72 
75 class CoreExport SocketTimeout : public Timer
76 {
77  private:
80  BufferedSocket* sock;
81 
84  int sfd;
85 
86  public:
92  SocketTimeout(int fd, BufferedSocket* thesock, unsigned int secs_from_now)
93  : Timer(secs_from_now)
94  , sock(thesock)
95  , sfd(fd)
96  {
97  }
98 
101  bool Tick(time_t now) CXX11_OVERRIDE;
102 };
103 
108 class CoreExport StreamSocket : public EventHandler
109 {
110  public:
113  class SendQueue
114  {
115  public:
118  typedef std::string Element;
119 
122  typedef std::deque<Element> Container;
123 
126  typedef Container::const_iterator const_iterator;
127 
128  SendQueue() : nbytes(0) { }
129 
133  bool empty() const { return (nbytes == 0); }
134 
138  Container::size_type size() const { return data.size(); }
139 
143  size_t bytes() const { return nbytes; }
144 
148  const Element& front() const { return data.front(); }
149 
155  const_iterator begin() const { return data.begin(); }
156 
160  const_iterator end() const { return data.end(); }
161 
164  void pop_front()
165  {
166  nbytes -= data.front().length();
167  data.pop_front();
168  }
169 
173  void erase_front(Element::size_type n)
174  {
175  nbytes -= n;
176  data.front().erase(0, n);
177  }
178 
182  void push_front(const Element& newdata)
183  {
184  data.push_front(newdata);
185  nbytes += newdata.length();
186  }
187 
191  void push_back(const Element& newdata)
192  {
193  data.push_back(newdata);
194  nbytes += newdata.length();
195  }
196 
199  void clear()
200  {
201  data.clear();
202  nbytes = 0;
203  }
204 
205  void moveall(SendQueue& other)
206  {
207  nbytes += other.bytes();
208  data.insert(data.end(), other.data.begin(), other.data.end());
209  other.clear();
210  }
211 
212  private:
215  Container data;
216 
219  size_t nbytes;
220  };
221 
223  enum Type
224  {
225  SS_UNKNOWN,
226  SS_USER
227  };
228 
229  private:
231  bool closeonempty;
232 
234  bool closing;
235 
237  IOHook* iohook;
238 
241  SendQueue sendq;
242 
244  std::string error;
245 
249  void CheckError(BufferedSocketError err);
250 
253  void DoRead();
254 
259  void FlushSendQ(SendQueue& sq);
260 
265  int ReadToRecvQ(std::string& rq);
266 
275  int HookChainRead(IOHook* hook, std::string& rq);
276 
277  protected:
279  std::string recvq;
280 
284  void SwapInternals(StreamSocket& other);
285 
286  public:
287  const Type type;
288  StreamSocket(Type sstype = SS_UNKNOWN)
289  : closeonempty(false)
290  , closing(false)
291  , iohook(NULL)
292  , type(sstype)
293  {
294  }
295  IOHook* GetIOHook() const;
296  void AddIOHook(IOHook* hook);
297  void DelIOHook();
298 
301  void DoWrite();
302 
305  void OnEventHandlerRead() CXX11_OVERRIDE;
306 
309  void OnEventHandlerWrite() CXX11_OVERRIDE;
310 
314  void OnEventHandlerError(int errcode) CXX11_OVERRIDE;
315 
317  void SetError(const std::string& err) { if (error.empty()) error = err; }
318 
320  const std::string& getError() const { return error; }
321 
323  virtual void OnDataReady() = 0;
325  virtual void OnError(BufferedSocketError e) = 0;
326 
332  virtual bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote);
333 
336  void WriteData(const std::string& data);
342  bool GetNextLine(std::string& line, char delim = '\n');
344  size_t getSendQSize() const;
345 
346  SendQueue& GetSendQ() { return sendq; }
347 
351  virtual void Close();
352 
354  void Close(bool writeblock);
355 
357  CullResult cull() CXX11_OVERRIDE;
358 
363  IOHook* GetModHook(Module* mod) const;
364 };
374 class CoreExport BufferedSocket : public StreamSocket
375 {
376  public:
380 
386  BufferedSocketState state;
387 
388  BufferedSocket();
396  BufferedSocket(int newfd);
397 
406  void DoConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned int maxtime);
407 
411  virtual void OnConnected();
412 
416  void OnDataReady() CXX11_OVERRIDE = 0;
417 
427  virtual void OnTimeout();
428 
429  virtual ~BufferedSocket();
430  protected:
431  void OnEventHandlerWrite() CXX11_OVERRIDE;
432  BufferedSocketError BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned int timeout);
433 };
434 
435 inline IOHook* StreamSocket::GetIOHook() const { return iohook; }
436 inline void StreamSocket::DelIOHook() { iohook = NULL; }
StreamSocket::SendQueue::push_back
void push_back(const Element &newdata)
Definition: inspsocket.h:191
StreamSocket::SendQueue::const_iterator
Container::const_iterator const_iterator
Definition: inspsocket.h:126
SocketTimeout::SocketTimeout
SocketTimeout(int fd, BufferedSocket *thesock, unsigned int secs_from_now)
Definition: inspsocket.h:92
BufferedSocket::state
BufferedSocketState state
Definition: inspsocket.h:386
StreamSocket::SendQueue::clear
void clear()
Definition: inspsocket.h:199
Timer::Tick
virtual bool Tick(time_t TIME)=0
IOHook
Definition: iohook.h:66
classbase::cull
virtual CullResult cull()
Definition: base.cpp:36
EventHandler
Definition: socketengine.h:151
StreamSocket::getError
const std::string & getError() const
Definition: inspsocket.h:320
StreamSocket::SendQueue::erase_front
void erase_front(Element::size_type n)
Definition: inspsocket.h:173
StreamSocket::Type
Type
Definition: inspsocket.h:223
StreamSocket
Definition: inspsocket.h:108
StreamSocket::SendQueue::bytes
size_t bytes() const
Definition: inspsocket.h:143
irc::sockets::sockaddrs
Definition: socket.h:58
StreamSocket::SendQueue::Container
std::deque< Element > Container
Definition: inspsocket.h:122
StreamSocket::recvq
std::string recvq
Definition: inspsocket.h:279
StreamSocket::SendQueue::push_front
void push_front(const Element &newdata)
Definition: inspsocket.h:182
StreamSocket::SendQueue::front
const Element & front() const
Definition: inspsocket.h:148
BufferedSocket
Definition: inspsocket.h:374
SocketTimeout
Definition: inspsocket.h:75
StreamSocket::SendQueue::size
Container::size_type size() const
Definition: inspsocket.h:138
StreamSocket::SendQueue::end
const_iterator end() const
Definition: inspsocket.h:160
Module
Definition: modules.h:242
CullResult
Definition: base.h:31
StreamSocket::SendQueue::pop_front
void pop_front()
Definition: inspsocket.h:164
StreamSocket::SendQueue::empty
bool empty() const
Definition: inspsocket.h:133
Timer
Definition: timer.h:36
BufferedSocket::Timeout
SocketTimeout * Timeout
Definition: inspsocket.h:379
StreamSocket::SendQueue::Element
std::string Element
Definition: inspsocket.h:118
irc
Definition: hashcomp.h:69
EventHandler::SwapInternals
void SwapInternals(EventHandler &other)
Definition: socketengine.cpp:53
EventHandler::OnEventHandlerRead
virtual void OnEventHandlerRead()=0
StreamSocket::SendQueue::begin
const_iterator begin() const
Definition: inspsocket.h:155
StreamSocket::SendQueue
Definition: inspsocket.h:113