Merge pull request #9618 from yinjimmy/bindAddress

support bind address for console
This commit is contained in:
minggo 2014-12-29 09:52:01 +08:00
commit 06cd28f524
2 changed files with 30 additions and 1 deletions

View File

@ -279,6 +279,7 @@ Console::Console()
, _running(false)
, _endThread(false)
, _sendDebugStrings(false)
, _bindAddress("")
{
// VS2012 doesn't support initializer list, so we create a new array and assign its elements to '_command'.
Command commands[] = {
@ -365,6 +366,22 @@ bool Console::listenOnTCP(int port)
continue; /* error, try next one */
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
// bind address
if (_bindAddress.length() > 0)
{
if (res->ai_family == AF_INET)
{
struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin_addr);
}
else if (res->ai_family == AF_INET6)
{
struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin6_addr);
}
}
if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
@ -1160,6 +1177,9 @@ void Console::loop()
_running = false;
}
void Console::setBindAddress(const std::string &address)
{
_bindAddress = address;
}
NS_CC_END

View File

@ -100,6 +100,13 @@ public:
void addCommand(const Command& cmd);
/** log something in the console */
void log(const char *buf);
/**
* set bind address
*
* @address : 127.0.0.1
*/
void setBindAddress(const std::string &address);
protected:
void loop();
@ -143,6 +150,8 @@ protected:
std::vector<std::string> _DebugStrings;
intptr_t _touchId;
std::string _bindAddress;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Console);
};