diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 10eae12b..96e5a758 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -41,6 +41,7 @@ #include #include #include +#include using namespace drogon; using namespace std::placeholders; @@ -51,7 +52,27 @@ drogon::InitBeforeMainFunction drogon::HttpAppFrameworkImpl::_initFirst([]() { LOG_TRACE << "Initialize the main event loop in the main thread"; }); }); +namespace drogon +{ +class DrogonFileLocker : public trantor::NonCopyable +{ + public: + DrogonFileLocker() + { + _fd = open("/tmp/drogon.lock", O_TRUNC | O_CREAT, 0755); + flock(_fd, LOCK_EX); + } + ~DrogonFileLocker() + { + close(_fd); + } + + private: + int _fd = 0; +}; + +} // namespace drogon static void godaemon(void) { printf("Initializing daemon mode\n"); @@ -297,9 +318,27 @@ void HttpAppFrameworkImpl::run() { auto ip = std::get<0>(listener); bool isIpv6 = ip.find(":") == std::string::npos ? false : true; - auto serverPtr = std::make_shared(loopThreadPtr->getLoop(), - InetAddress(ip, std::get<1>(listener), isIpv6), - "drogon"); + std::shared_ptr serverPtr; + if (i == 0) + { + DrogonFileLocker lock; + // Check whether the port is in use. + TcpServer server(getLoop(), + InetAddress(ip, std::get<1>(listener), isIpv6), + "drogonPortTest", + true, + false); + serverPtr = std::make_shared(loopThreadPtr->getLoop(), + InetAddress(ip, std::get<1>(listener), isIpv6), + "drogon"); + } + else + { + serverPtr = std::make_shared(loopThreadPtr->getLoop(), + InetAddress(ip, std::get<1>(listener), isIpv6), + "drogon"); + } + if (std::get<2>(listener)) { #ifdef USE_OPENSSL @@ -566,7 +605,7 @@ void HttpAppFrameworkImpl::onNewWebsockRequest(const HttpRequestImplPtr &req, std::vector> HttpAppFrameworkImpl::getHandlersInfo() const { auto ret = _httpSimpleCtrlsRouter.getHandlersInfo(); - auto v=_httpCtrlsRouter.getHandlersInfo(); + auto v = _httpCtrlsRouter.getHandlersInfo(); ret.insert(ret.end(), v.begin(), v.end()); v = _websockCtrlsRouter.getHandlersInfo(); ret.insert(ret.end(), v.begin(), v.end());