Skip to content

Commit

Permalink
tc_file add toAbsolute
Browse files Browse the repository at this point in the history
tc_port add getCwd
Application first bind adminObj, then initialize, then bind other objs
EndpointInfo add toEndpointF
  • Loading branch information
ruanshudong committed Jan 21, 2024
1 parent 64a457d commit b8354c6
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 25 deletions.
2 changes: 2 additions & 0 deletions servant/libservant/AdminServant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void AdminServant::shutdown(CurrentPtr current)
{
TLOGERROR("[TARS][AdminServant::shutdown] from node" << endl);

_application->getApplicationCommunicator()->terminate();
_application->terminate();
#if TARGET_PLATFORM_WINDOWS
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if (hProcess == NULL)
Expand Down
10 changes: 8 additions & 2 deletions servant/libservant/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,9 @@ void Application::initializeAdapter()
lsPtr->setProtocol(AppProtocol::parse);

adapters.push_back(lsPtr);
// _epollServer->bind(lsPtr);

//admin端口先做绑定, 服务就算一直卡着initialize, 也能被外部管理了.
_epollServer->bind(lsPtr);
}

_epollServer->setAdapter(adapters);
Expand Down Expand Up @@ -1558,7 +1560,11 @@ void Application::bindAdapters()
{
for(auto adapter : _epollServer->getBindAdapters())
{
_epollServer->bind(adapter);
if(!adapter->getSocket().isValid())
{
//未绑定, 才绑定
_epollServer->bind(adapter);
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions servant/libservant/EndpointInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,27 @@ string EndpointInfo::createCompareDesc()
return ss.str();
}


EndpointF EndpointInfo::toEndpointF(const TC_Endpoint &tep, const string &nodeName)
{
EndpointF ep;
ep.host = tep.getHost();
ep.port = tep.getPort();
ep.istcp = tep.getType();
ep.nodeName = nodeName;
if(ep.nodeName.empty())
{
ep.nodeName = tep.getHost();
}
ep.timeout = tep.getTimeout();
ep.authType = tep.getAuthType();
ep.qos = tep.getQos();
ep.weight = tep.getWeight();
ep.weightType = tep.getWeightType();

return ep;
}


///////////////////////////////////////////////////////////
}
2 changes: 1 addition & 1 deletion servant/protocol
7 changes: 7 additions & 0 deletions servant/servant/EndpointInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class EndpointInfo
*/
EndpointInfo(const EndpointF &ep);

/**
* 转换
* @param ep
* @return
*/
static EndpointF toEndpointF(const TC_Endpoint &ep, const string &nodeName);

/**
* get endpoint
* @return
Expand Down
7 changes: 7 additions & 0 deletions util/include/util/tc_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class TC_File
*/
static bool isAbsolute(const string &sFullFileName);

/**
* 如果是相对路径则转换为绝对路径
* @param sFullFileName
* @return
*/
static string toAbsolute(const string &sFullFileName);

/**
* @brief 判断给定路径的文件是否存在.
* 如果文件是符号连接,则以符号连接判断而不是以符号连接指向的文件判断
Expand Down
6 changes: 6 additions & 0 deletions util/include/util/tc_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ class TC_Port
*/
static void setEnv(const std::string &name, const std::string &value);

/**
* 获取当前进程的目录
* @return
*/
static string getCwd();

/**
* kill某个pid的进程
* @param pid
Expand Down
45 changes: 23 additions & 22 deletions util/src/tc_epoll_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1908,17 +1908,23 @@ TC_EpollServer::TC_EpollServer(unsigned int iNetThreadNum)
{
#if TARGET_PLATFORM_WINDOWS
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
WSAStartup(MAKEWORD(2, 2), &wsadata);
#endif
//
// _epoller.create(10240);
// _epoller.setName("epollserver-epoller");

_epoller = new TC_Epoller();
_epoller->create(10240);
_epoller->setName("epollserver-epoller");

}

TC_EpollServer::~TC_EpollServer()
{
terminate();

delete _epoller;

_epoller = NULL;

#if TARGET_PLATFORM_WINDOWS
WSACleanup();
#endif
Expand Down Expand Up @@ -2011,15 +2017,7 @@ bool TC_EpollServer::accept(int fd, int domain)

return true;
}
// else
// {
// //直到发生EAGAIN才不继续accept
// if (TC_Socket::isPending())
// {
// return false;
// }
// }
// return true;

return false;
}

Expand Down Expand Up @@ -2105,9 +2103,7 @@ void TC_EpollServer::listenCallback(weak_ptr<BindAdapter> adapterPtr)

void TC_EpollServer::waitForShutdown()
{
_epoller = new TC_Epoller();
_epoller->create(10240);
_epoller->setName("epollserver-epoller");
_epoller->reset();

_readyThreadNum = 0;

Expand All @@ -2122,7 +2118,7 @@ void TC_EpollServer::waitForShutdown()
_epoller->postRepeated(5000, false, [&](){ _hf(this);});
}

for(auto it : _bindAdapters)
for(const auto& it : _bindAdapters)
{
if(it->getEndpoint().isTcp())
{
Expand Down Expand Up @@ -2192,9 +2188,9 @@ void TC_EpollServer::waitForShutdown()
bindAdapter->getHandles().clear();
}

delete _epoller;

_epoller = NULL;
// delete _epoller;
//
// _epoller = NULL;

std::unique_lock<std::mutex> lock(_readyMutex);

Expand All @@ -2205,11 +2201,15 @@ void TC_EpollServer::waitForShutdown()

void TC_EpollServer::terminate()
{
if(_epoller == NULL || _epoller->isTerminate())
if(_epoller->isTerminate())
{
return;
}

// if(_epoller == NULL || _epoller->isTerminate())
// {
// return;
// }
//
//先停止网络线程
_epoller->terminate();

Expand Down Expand Up @@ -2242,6 +2242,7 @@ int TC_EpollServer::bind(BindAdapterPtr & lsPtr)
{
throw TC_Exception("bind name '" + lsPtr->getName() + "' conflicts.");
}

++it;
}

Expand Down
9 changes: 9 additions & 0 deletions util/src/tc_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ bool TC_File::isAbsolute(const string &sFullFileName)
#endif
}

string TC_File::toAbsolute(const string &sFullFileName)
{
if(!isAbsolute(sFullFileName))
{
return simplifyDirectory(TC_Port::getCwd() + FILE_SEP + sFullFileName);
}
return sFullFileName;
}

bool TC_File::isFileExist(const string &sFullFileName, mode_t iFileType)
{
TC_Port::stat_t f_stat;
Expand Down
13 changes: 13 additions & 0 deletions util/src/tc_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ void TC_Port::setEnv(const string &name, const string &value)
#endif
}

string TC_Port::getCwd()
{
char currentDirectory[FILENAME_MAX] = {0x00};
#if TARGET_PLATFORM_WINDOWS
_getcwd(currentDirectory, sizeof(currentDirectory));
#else
getcwd(currentDirectory, sizeof(currentDirectory));
#endif
return currentDirectory;
}

void TC_Port::kill(int64_t pid)
{
#if TARGET_PLATFORM_WINDOWS
Expand Down Expand Up @@ -627,6 +638,7 @@ BOOL WINAPI TC_Port::HandlerRoutine(DWORD dwCtrlType)
}
#endif

#if TARGET_PLATFORM_LINUX

static int64_t reSize(int64_t i, const char unit)
{
Expand All @@ -643,6 +655,7 @@ static int64_t reSize(int64_t i, const char unit)
}
return i;
}
#endif

// 获取指定进程占用物理内存大小, 单位(字节)
int64_t TC_Port::getPidMemUsed(int64_t pid, const char unit)
Expand Down

0 comments on commit b8354c6

Please sign in to comment.