]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/socket.h
Convert more by-values to const references, optimise ConfigReader a bit
[user/henk/code/inspircd.git] / include / socket.h
index bb172a3ebe39bceba606c57fe5382aa4199b17ce..e36bbd2312ea67e93181aab0ede50043a6974591 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
 #include <netinet/in.h>
 #include <sstream>
 #include <string>
+#include "dns.h"
+#include <deque>
 
 /**
  * States which a socket may be in
  */
-enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
+enum InspSocketState { I_DISCONNECTED, I_RESOLVING, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
 
 /**
  * Error types which a socket may exhibit
  */
-enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND };
+enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE };
 
 /**
  * InspSocket is an extendable socket class which modules
@@ -53,6 +55,11 @@ private:
         */
         int fd;
 
+       /**
+        * The resolver for this socket
+        */
+       DNS dns;
+
        /**
         * The hostname connected to
         */
@@ -99,11 +106,16 @@ private:
        /**
         * Socket input buffer, used by read(). The class which
         * extends InspSocket is expected to implement an extendable
-        * buffer which can grow much larger than 16k,
+        * buffer which can grow much larger than 64k,
         * this buffer is just designed to be temporary storage.
         * space.
         */
-       char ibuf[16384];
+       char ibuf[65535];
+
+       /**
+        * The output buffer for this socket
+        */
+       std::deque<std::string> outbuffer;
 
        /**
         * The IP address being connected
@@ -130,6 +142,12 @@ private:
         */
        socklen_t length;
 
+       /** Flushes the write buffer
+        */
+       bool FlushWriteBuffer();
+
+       void SetQueues(int nfd);
+
 public:
 
        /**
@@ -150,14 +168,16 @@ public:
 
        /**
         * This constructor is used to create a new
-        * socket, either listening for connections,
-        * or an outbound connection to another host.
+        * socket, either listening for connections, or an outbound connection to another host.
+        * Note that if you specify a hostname in the 'host' parameter, then there will be an extra
+        * step involved (a nonblocking DNS lookup) which will cause your connection to be established
+        * slower than if it was an IP. Therefore, use an IP address where it is available instead.
         * @param host The hostname to connect to, or bind to
         * @param port The port number to connect to, or bind to
         * @param listening true to listen on the given host:port pair, or false to connect to them
         * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
         */
-       InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
+       InspSocket(const std::string &host, int port, bool listening, unsigned long maxtime);
 
        /**
         * This method is called when an outbound
@@ -197,9 +217,13 @@ public:
        /**
         * When an outbound connection fails, and the
         * attempt times out, you will receive this event.
-        * The mthod will trigger once maxtime secons are
+        * The method will trigger once maxtime seconds are
         * reached (as given in the constructor) just
         * before the socket's descriptor is closed.
+        * A failed DNS lookup may cause this event if
+        * the DNS server is not responding, as well as
+        * a failed connect() call, because DNS lookups are
+        * nonblocking as implemented by this class.
         */
        virtual void OnTimeout();
 
@@ -240,7 +264,7 @@ public:
         * returns or linefeeds are appended to the string.
         * @param data The data to send
         */
-       virtual int Write(std::string data);
+       virtual int Write(const std::string &data);
 
        /**
         * If your socket is a listening socket, when a new
@@ -299,6 +323,9 @@ public:
         * used for this socket.
         */
        virtual ~InspSocket();
+
+       virtual bool DoResolve();
+       virtual bool DoConnect();
 };
 
 #endif