]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/socket.h
And the bit I missed... :/
[user/henk/code/inspircd.git] / include / socket.h
index c56494c0142828455fda6b3fefd4e16a0e76b8a0..f8b15ae53cae017fd3188a53c828ff0503f20809 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 <deque>
+#include "dns.h"
 
 /**
  * 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
@@ -48,15 +50,22 @@ class InspSocket
 {
 private:
 
+       std::deque<std::string> outbuffer;
+
        /**
         * The file descriptor of this socket
         */
         int fd;
 
+       /**
+        * The resolver for this socket
+        */
+       DNS dns;
+
        /**
         * The hostname connected to
         */
-       std::string host;
+       char host[MAXBUF];
 
        /**
         * The port connected to, or the port
@@ -105,17 +114,12 @@ private:
         */
        char ibuf[65535];
 
-       /**
-        * The output buffer for this socket
-        */
-       std::string Buffer;
-
        /**
         * The IP address being connected
         * to stored in string form for
         * easy retrieval by accessors.
         */
-       std::string IP;
+       char IP[MAXBUF];
 
        /**
         * Client sockaddr structure used
@@ -137,7 +141,21 @@ private:
 
        /** Flushes the write buffer
         */
-       void FlushWriteBuffer();
+       bool FlushWriteBuffer();
+
+       /** Set the queue sizes
+        * This private method sets the operating system queue
+        * sizes for this socket to 65535 so that it can queue
+        * more information without application-level queueing
+        * which was required in older software.
+        */
+       void SetQueues(int nfd);
+
+       /** When the socket has been marked as closing, this flag
+        * will be set to true, then the next time the socket is
+        * examined, the socket is deleted and closed.
+        */
+       bool ClosePending;
 
 public:
 
@@ -159,14 +177,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
@@ -206,9 +226,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();
 
@@ -249,7 +273,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
@@ -308,6 +332,29 @@ public:
         * used for this socket.
         */
        virtual ~InspSocket();
+
+       /**
+        * This method attempts to resolve the hostname,
+        * if a hostname is given and not an IP,
+        * before a connection can occur. This method is
+        * asyncronous.
+        */
+       virtual bool DoResolve();
+
+       /**
+        * This method attempts to connect to a hostname.
+        * This only occurs on a non-listening socket. This
+        * method is asyncronous.
+        */
+       virtual bool DoConnect();
+
+       /**
+        * This method marks the socket closed.
+        * The next time the core examines a socket marked
+        * as closed, the socket will be closed and the 
+        * memory reclaimed.
+        */
+       void MarkAsClosed();
 };
 
 #endif