summaryrefslogtreecommitdiff
path: root/src/network/core/tcp.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-01-20 11:28:18 +0000
committerrubidium <rubidium@openttd.org>2009-01-20 11:28:18 +0000
commit28a641066e2e9963f6662366281079b7d00ac835 (patch)
tree6b344cc0bfeff2bffba2764265342a0ed65238d2 /src/network/core/tcp.h
parentc9436c8d88961032ab66f4d06892615d239014ab (diff)
downloadopenttd-28a641066e2e9963f6662366281079b7d00ac835.tar.xz
(svn r15163) -Change/Fix: use a non-blocking method to resolve the hostname and connect to game servers.
Diffstat (limited to 'src/network/core/tcp.h')
-rw-r--r--src/network/core/tcp.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h
index da3a55adf..962ea45e5 100644
--- a/src/network/core/tcp.h
+++ b/src/network/core/tcp.h
@@ -10,6 +10,7 @@
#ifdef ENABLE_NETWORK
#include "os_abstraction.h"
+#include "address.h"
#include "core.h"
#include "packet.h"
@@ -31,6 +32,62 @@ public:
~NetworkTCPSocketHandler();
};
+/**
+ * "Helper" class for creating TCP connections in a non-blocking manner
+ */
+class TCPConnecter {
+private:
+ class ThreadObject *thread; ///< Thread used to create the TCP connection
+ bool connected; ///< Whether we succeeded in making the connection
+ bool aborted; ///< Whether we bailed out (i.e. connection making failed)
+ bool killed; ///< Whether we got killed
+ SOCKET sock; ///< The socket we're connecting with
+
+ /** The actual connection function */
+ void Connect();
+
+ /**
+ * Entry point for the new threads.
+ * @param param the TCPConnecter instance to call Connect on.
+ */
+ static void ThreadEntry(void *param);
+
+protected:
+ /** Address we're connecting to */
+ NetworkAddress address;
+
+public:
+ /**
+ * Create a new connecter for the given address
+ * @param address the (un)resolved address to connect to
+ */
+ TCPConnecter(const NetworkAddress &address);
+ /** Silence the warnings */
+ virtual ~TCPConnecter() {}
+
+ /**
+ * Callback when the connection succeeded.
+ * @param s the socket that we opened
+ */
+ virtual void OnConnect(SOCKET s) {}
+
+ /**
+ * Callback for when the connection attempt failed.
+ */
+ virtual void OnFailure() {}
+
+ /**
+ * Check whether we need to call the callback, i.e. whether we
+ * have connected or aborted and call the appropriate callback
+ * for that. It's done this way to ease on the locking that
+ * would otherwise be needed everywhere.
+ */
+ static void CheckCallbacks();
+
+ /** Kill all connection attempts. */
+ static void KillAll();
+};
+
#endif /* ENABLE_NETWORK */
#endif /* NETWORK_CORE_TCP_H */