From 27b05c969f191bcce130c8bc171cca94d6b50b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Thu, 17 Mar 2011 20:22:00 +0100 Subject: Sluift: Put a timeout on connecting to a server. diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 0223556..458c44d 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -29,7 +29,8 @@ using namespace Swift; * Forward declarations ******************************************************************************/ -bool debug = false; +static bool debug = false; +static int globalTimeout = 30000; /******************************************************************************* * Helper classes @@ -68,9 +69,14 @@ class SluiftClient { } void waitConnected() { - while (client->isActive() && !client->isAvailable()) { + Watchdog watchdog(globalTimeout, networkFactories.getTimerFactory()); + while (!watchdog.getTimedOut() && client->isActive() && !client->isAvailable()) { eventLoop.runUntilEvents(); } + if (watchdog.getTimedOut()) { + client->disconnect(); + throw SluiftException("Timeout while connecting"); + } } bool isConnected() const { @@ -694,22 +700,30 @@ static int sluift_sleep(lua_State *L) { } static int sluift_index(lua_State *L) { - luaL_checkstring(L, 2); - if (std::string(lua_tostring(L, 2)) == "debug") { + std::string key(luaL_checkstring(L, 2)); + if (key == "debug") { lua_pushboolean(L, debug); return 1; } + else if (key == "timeout") { + lua_pushnumber(L, globalTimeout); + return 1; + } else { return luaL_error(L, "Invalid index"); } } static int sluift_newindex(lua_State *L) { - luaL_checkstring(L, 2); - if (std::string(lua_tostring(L, 2)) == "debug") { + std::string key(luaL_checkstring(L, 2)); + if (key == "debug") { debug = lua_toboolean(L, 3); return 0; } + else if (key == "timeout") { + globalTimeout = luaL_checknumber(L, 3); + return 0; + } else { return luaL_error(L, "Invalid index"); } -- cgit v0.10.2-6-g49f6