diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-03-17 19:22:00 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-03-17 19:22:00 (GMT) |
commit | 27b05c969f191bcce130c8bc171cca94d6b50b64 (patch) | |
tree | 23516c7afc901087df0a5b038290d9e5e64e3cf3 /Sluift | |
parent | 45b3444328fc495c4a5a7d05d3dcaa6116585e41 (diff) | |
download | swift-contrib-27b05c969f191bcce130c8bc171cca94d6b50b64.zip swift-contrib-27b05c969f191bcce130c8bc171cca94d6b50b64.tar.bz2 |
Sluift: Put a timeout on connecting to a server.
Diffstat (limited to 'Sluift')
-rw-r--r-- | Sluift/sluift.cpp | 26 |
1 files changed, 20 insertions, 6 deletions
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"); } |