isearch/comm/stat/dtcutils.h
2021-03-19 18:38:34 +08:00

96 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __DTC_UTILS__
#define __DTC_UTILS__
#include <vector>
#include <string>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <fcntl.h>
#include <arpa/inet.h>
/*此文件放置dtc的工具函数*/
namespace dtc
{
namespace utils
{
/*************************************************
获取本机的ip tomchen
**************************************************/
inline std::string GetLocalIP()
{
int iClientSockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(iClientSockfd < 0 )
return "0.0.0.0";
struct sockaddr_in stINETAddr;
stINETAddr.sin_addr.s_addr = inet_addr("192.168.0.1");
stINETAddr.sin_family = AF_INET;
stINETAddr.sin_port = htons(8888);
int iCurrentFlag = fcntl(iClientSockfd, F_GETFL, 0);
fcntl(iClientSockfd, F_SETFL, iCurrentFlag | FNDELAY);
if(connect(iClientSockfd, (struct sockaddr *)&stINETAddr, sizeof(sockaddr)) != 0)
{
close(iClientSockfd);
return "0.0.0.0";
}
struct sockaddr_in stINETAddrLocal;
socklen_t iAddrLenLocal = sizeof(stINETAddrLocal);
getsockname(iClientSockfd, (struct sockaddr *)&stINETAddrLocal, &iAddrLenLocal);
close(iClientSockfd);
return inet_ntoa(stINETAddrLocal.sin_addr);
}
/*************************************************
切割字符串strOri, 以_Ch为分隔符结果为theVec
**************************************************/
inline void SplitStr(std::string strOri, char _Ch, std::vector<std::string>& theVec)
{
std::string::size_type nLastPos = 0;
std::string strSub;
std::string::size_type iIndex = strOri.find(_Ch);
if (std::string::npos == iIndex)
{
theVec.push_back(strOri);
return;
}
while( std::string::npos != iIndex)
{
strSub = strOri.substr(nLastPos, iIndex - nLastPos);
nLastPos = iIndex + 1;
iIndex = strOri.find(_Ch, nLastPos);
theVec.push_back(strSub);
}
if (nLastPos != 0)
{
strSub = strOri.substr(nLastPos, strOri.length() - nLastPos);
theVec.push_back(strSub);
}
}
inline int GetBID()
{
char buf[1024];
getcwd(buf,sizeof(buf));
std::vector<std::string> pathVec;
dtc::utils::SplitStr(std::string(buf), '/', pathVec);
if (pathVec.size() < 5)
{
return 0;
}
std::string strAccessKey = pathVec[4];
std::string strModuleId = strAccessKey.substr(0,8);
return atoi(strModuleId.c_str());
}
}
}
#endif