diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-27 09:41:39 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-27 09:41:39 +0800 |
commit | 3750d1933b328af0e81e9cdc2f15e1607f08478e (patch) | |
tree | 51bf652e51c49b13bbcc408f0eed1feaf9310f79 | |
parent | b23b56ea0d2a651ac95c8db4ad9a97da23592cd6 (diff) |
SexyAppFramework: Bind to a random port if bind to the default port failed
-rw-r--r-- | osframework/source/SexyAppFramework/TcpLogListener.cpp | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/osframework/source/SexyAppFramework/TcpLogListener.cpp b/osframework/source/SexyAppFramework/TcpLogListener.cpp index 9fc9763..179d92f 100644 --- a/osframework/source/SexyAppFramework/TcpLogListener.cpp +++ b/osframework/source/SexyAppFramework/TcpLogListener.cpp @@ -1,6 +1,5 @@ #include "TcpLogListener.h" #include "SexyLogManager.h" -#include "SexyTimer.h" #include "Common.h" #if defined(WIN32) || defined(_WIN32) @@ -28,6 +27,7 @@ TcpLogListener::TcpLogListener(const std::string& target) : mMaxLogRecordSize = GetEnvIntOption("SEXY_TCP_LOG_BUFFER_SIZE", 1024 * 1024); mLogRecordSeq = 0; mLogRecordSize = 0; + mDone = true; std::string s = target; @@ -44,27 +44,41 @@ TcpLogListener::TcpLogListener(const std::string& target) : } mDone = true; - mSock = new TCPServerSocket(mHost, atoi(mPort.c_str())); - if (mSock->hasError()) + for (;;) { - // printf("TcpLogListener: Listening on %s:%s\n", mHost.c_str(), mPort.c_str()); + mSock = new TCPServerSocket(); + if (!mSock->hasError() && + mSock->setLocalAddressAndPort(mHost, atoi(mPort.c_str())) && + mSock->setListen(5)) + { + mPort = StrFormat("%d", mSock->getLocalPort()); + + //printf("TcpLogListener: Listening on %s:%s\n", + //mHost.c_str(), mPort.c_str()); + break; + } delete mSock; mSock = 0; + + if (mPort == "0") + break; + else + mPort = "0"; } - else - { - mServiceInfo.mName = "sexytcplog"; - mServiceInfo.mDesc = "The log service for SexyAppFramework"; - mServiceInfo.mType = "tcp"; - mServiceInfo.mAddr = mSock->getLocalAddress(); - mServiceInfo.mPort = mPort; + if (!mSock) + return; - ServiceManager& mgr = ServiceManager::getInstance(); - mgr.registerService(mServiceInfo); + mServiceInfo.mName = "sexytcplog"; + mServiceInfo.mDesc = "The log service for SexyAppFramework"; + mServiceInfo.mType = "tcp"; + mServiceInfo.mAddr = mSock->getLocalAddress(); + mServiceInfo.mPort = mPort; - mDone = false; - mThread = Thread::Create(serverProc, this); - } + ServiceManager& mgr = ServiceManager::getInstance(); + mgr.registerService(mServiceInfo); + + mDone = false; + mThread = Thread::Create(serverProc, this); } TcpLogListener::~TcpLogListener() |