From 4c8290e3cf0c644e84083c17d5004e15e66b7d86 Mon Sep 17 00:00:00 2001 From: Roger Date: Tue, 22 Oct 2013 15:36:24 +0100 Subject: Add port option to Sluift client connect Provide a mechanism to allow setting both the port and hostname for a Sluift client connection. By default Sluift leave port to -1 to use the port discovered by SRV lookups, with 5222 as a fallback. With this change, the port is now manually configurable as well. Change-Id: Idef5457e95127cfa86181b462d9ef42aa4f096ff diff --git a/Sluift/SluiftClient.cpp b/Sluift/SluiftClient.cpp index 8a8d772..bfae621 100644 --- a/Sluift/SluiftClient.cpp +++ b/Sluift/SluiftClient.cpp @@ -52,9 +52,10 @@ void SluiftClient::connect() { client->connect(options); } -void SluiftClient::connect(const std::string& host) { +void SluiftClient::connect(const std::string& host, int port) { rosterReceived = false; options.manualHostname = host; + options.manualPort = port; client->connect(options); } diff --git a/Sluift/SluiftClient.h b/Sluift/SluiftClient.h index bedd6ba..60eae16 100644 --- a/Sluift/SluiftClient.h +++ b/Sluift/SluiftClient.h @@ -75,7 +75,7 @@ namespace Swift { } void connect(); - void connect(const std::string& host); + void connect(const std::string& host, int port); void waitConnected(); bool isConnected() const; diff --git a/Sluift/client.cpp b/Sluift/client.cpp index df43075..04ce8f4 100644 --- a/Sluift/client.cpp +++ b/Sluift/client.cpp @@ -48,16 +48,20 @@ SLUIFT_LUA_FUNCTION(Client, async_connect) { SluiftClient* client = getClient(L); std::string host; + int port = -1; if (lua_istable(L, 2)) { if (boost::optional hostString = Lua::getStringField(L, 2, "host")) { host = *hostString; } + if (boost::optional portInt = Lua::getIntField(L, 2, "port")) { + port = *portInt; + } } - if (host.empty()) { + if (host.empty() && port == -1) { client->connect(); } else { - client->connect(host); + client->connect(host, port); } return 0; } @@ -315,6 +319,14 @@ SLUIFT_LUA_FUNCTION(Client, send) { SLUIFT_LUA_FUNCTION(Client, set_options) { SluiftClient* client = getClient(L); Lua::checkType(L, 2, LUA_TTABLE); + lua_getfield(L, 2, "host"); + if (!lua_isnil(L, -1)) { + client->getOptions().manualHostname = lua_tostring(L, -1); + } + lua_getfield(L, 2, "port"); + if (!lua_isnil(L, -1)) { + client->getOptions().manualPort = lua_tointeger(L, -1); + } lua_getfield(L, 2, "ack"); if (!lua_isnil(L, -1)) { client->getOptions().useAcks = lua_toboolean(L, -1); -- cgit v0.10.2-6-g49f6