merge taf, and update auth crypo to des3

This commit is contained in:
jarodruan 2020-02-22 21:20:30 +08:00
parent de54376bcf
commit 9a500bdc2a
111 changed files with 6089 additions and 9520 deletions

View File

@ -23,28 +23,28 @@
#module name
modulename = TestApp.SSLClient
#server crt
ca = ../examples/SSLDemo/certs/server.crt
ca = ./examples/SSLDemo/certs/server.crt
#can be empty
cert = ../examples/SSLDemo/certs/client.crt
cert = ./examples/SSLDemo/certs/client.crt
#can be empty
key = ../examples/SSLDemo/certs/client.key
key = ./examples/SSLDemo/certs/client.key
<TestApp.SSLServer.SSL1Obj>
#server crt
ca = ../examples/SSLDemo/certs/server1.crt
ca = ./examples/SSLDemo/certs/server1.crt
#can be empty
# cert = ../examples/SSLDemo/certs/client1.crt
# cert = ./examples/SSLDemo/certs/client1.crt
#can be empty
# key = ../examples/SSLDemo/certs/client1.key
# key = ./examples/SSLDemo/certs/client1.key
</TestApp.SSLServer.SSL1Obj>
<TestApp.SSLServer.SSL2Obj>
#server crt
ca = ../examples/SSLDemo/certs/server1.crt
ca = ./examples/SSLDemo/certs/server1.crt
#can be empty
cert = ../examples/SSLDemo/certs/client1.crt
cert = ./examples/SSLDemo/certs/client1.crt
#can be empty
key = ../examples/SSLDemo/certs/client1.key
key = ./examples/SSLDemo/certs/client1.key
</TestApp.SSLServer.SSL2Obj>
<TestApp.SSLServer.SSL3Obj>
@ -53,11 +53,11 @@
#auth secret key
secretkey = 123456
#server crt
ca = ../examples/SSLDemo/certs/server1.crt
ca = ./examples/SSLDemo/certs/server1.crt
#can be empty
cert = ../examples/SSLDemo/certs/client1.crt
cert = ./examples/SSLDemo/certs/client1.crt
#can be empty
key = ../examples/SSLDemo/certs/client1.key
key = ./examples/SSLDemo/certs/client1.key
</TestApp.SSLServer.SSL3Obj>
</client>
</application>

View File

@ -52,9 +52,9 @@
# log = tars.tarslog.LogObj
#client crt, it can be empty when verifyclient is 0
ca = ../examples/SSLDemo/certs/client.crt
cert = ../examples/SSLDemo/certs/server.crt
key = ../examples/SSLDemo/certs/server.key
ca = ./examples/SSLDemo/certs/client.crt
cert = ./examples/SSLDemo/certs/server.crt
key = ./examples/SSLDemo/certs/server.key
#default is 0
verifyclient = 1
@ -90,9 +90,9 @@
queuecap = 1000000
#tars protocol
protocol = tars
# ca = ../examples/SSLDemo/certs/client1.crt
cert = ../examples/SSLDemo/certs/server1.crt
key = ../examples/SSLDemo/certs/server1.key
# ca = ./examples/SSLDemo/certs/client1.crt
cert = ./examples/SSLDemo/certs/server1.crt
key = ./examples/SSLDemo/certs/server1.key
#default is 0
verifyclient = 0
</Hello1Adapter>
@ -112,9 +112,9 @@
queuecap = 1000000
#tars protocol
protocol = tars
ca = ../examples/SSLDemo/certs/client1.crt
cert = ../examples/SSLDemo/certs/server1.crt
key = ../examples/SSLDemo/certs/server1.key
ca = ./examples/SSLDemo/certs/client1.crt
cert = ./examples/SSLDemo/certs/server1.crt
key = ./examples/SSLDemo/certs/server1.key
#default is 0
verifyclient = 1
</Hello2Adapter>
@ -138,9 +138,9 @@
accesskey = tars-test-user
#auth secret key
secretkey = 123456
ca = ../examples/SSLDemo/certs/client1.crt
cert = ../examples/SSLDemo/certs/server1.crt
key = ../examples/SSLDemo/certs/server1.key
ca = ./examples/SSLDemo/certs/client1.crt
cert = ./examples/SSLDemo/certs/server1.crt
key = ./examples/SSLDemo/certs/server1.key
#default is 0
verifyclient = 1
</Hello3Adapter>

View File

@ -69,13 +69,13 @@ AdapterProxy::AdapterProxy(ObjectProxy * pObjectProxy,const EndpointInfo &ep,Com
_timeoutLogFlag = _communicator->getTimeoutLogFlag();
}
if (ep.type() == EndpointInfo::UDP)
if (ep.isTcp())
{
_trans.reset(new UdpTransceiver(this, ep));
_trans.reset(new TcpTransceiver(this, ep));
}
else
{
_trans.reset(new TcpTransceiver(this, ep));
_trans.reset(new UdpTransceiver(this, ep));
}
//初始化stat的head信息
@ -176,7 +176,7 @@ int AdapterProxy::invoke(ReqMessage * msg)
//链表是空的, 则直接发送当前这条数据, 如果链表非空或者发送失败了, 则放到队列中, 等待下次发送
if(_timeoutQueue->sendListEmpty() && _trans->sendRequest(msg->sReqData) != Transceiver::eRetError)
{
TLOGTARS("[TARS][AdapterProxy::invoke push (send) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id:" << msg->request.iRequestId << endl);
TLOGTARS("[TARS][AdapterProxy::invoke push (send) obj:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id:" << msg->request.iRequestId << endl);
//请求发送成功了,单向调用直接返回
if(msg->eType == ReqMessage::ONE_WAY)
@ -198,7 +198,7 @@ int AdapterProxy::invoke(ReqMessage * msg)
msg->eStatus = ReqMessage::REQ_EXC;
finishInvoke(msg);
return 0;
}
}
@ -215,7 +215,6 @@ int AdapterProxy::invoke(ReqMessage * msg)
msg->eStatus = ReqMessage::REQ_EXC;
finishInvoke(msg);
return 0;
}
}
@ -268,7 +267,7 @@ void AdapterProxy::doInvoke()
void AdapterProxy::finishInvoke(bool bFail)
{
TLOGTARS("[TARS][AdapterProxy::finishInvoke(bool) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",bFail:" << bFail << endl);
TLOGTARS("[TARS][AdapterProxy::finishInvoke(bool) obj:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",bFail:" << bFail << endl);
time_t now = TNOW;
@ -369,8 +368,12 @@ void AdapterProxy::finishInvoke(bool bFail)
}
}
int AdapterProxy::getConTimeout()
{
return _objectProxy->getConTimeout();
}
bool AdapterProxy::checkActive(bool bForceConnect)
bool AdapterProxy::checkActive(bool bForceConnect, bool onlyCheck)
{
time_t now = TNOW;
@ -466,11 +469,6 @@ void AdapterProxy::setConTimeout(bool bConTimeout)
}
}
int AdapterProxy::getConTimeout()
{
return _objectProxy->getConTimeout();
}
//屏蔽结点
void AdapterProxy::setInactive()
{
@ -485,32 +483,8 @@ void AdapterProxy::setInactive()
void AdapterProxy::finishInvoke(shared_ptr<ResponsePacket> & rsp)
{
TLOGTARS("[TARS][AdapterProxy::finishInvoke(ResponsePacket) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc()
<< ",id:" << rsp->iRequestId << endl);
// if (_trans->getAuthState() != AUTH_SUCC)
// {
// std::string ret(rsp->sBuffer.begin(), rsp->sBuffer.end());
// tars::AUTH_STATE tmp = AUTH_SUCC;
// tars::stoe(ret, tmp);
// int newstate = tmp;
//
// TLOGTARS("[TARS]AdapterProxy::finishInvoke from state " << _trans->getAuthState() << " to " << newstate << endl);
// _trans->setAuthState(newstate);
//
// if (newstate == AUTH_SUCC)
// {
// // flush old buffered msg when auth is not complete
// doInvoke();
// }
// else
// {
// TLOGERROR("newstate is " << newstate << ", error close!\n");
// _trans->close();
// }
//
// return;
// }
// TLOGTARS("[TARS][AdapterProxy::finishInvoke(ResponsePacket) obj:" << _objectProxy->name() << ", desc:" << _endpoint.desc()
// << ", id:" << rsp->iRequestId << endl);
ReqMessage * msg = NULL;
@ -544,7 +518,7 @@ void AdapterProxy::finishInvoke(shared_ptr<ResponsePacket> & rsp)
{
if(_timeoutLogFlag)
{
TLOGERROR("[TARS][AdapterProxy::finishInvoke(ResponsePacket) objname:"<< _objectProxy->name() << ",get req-ptr NULL,may be timeout,id:" << rsp->iRequestId
TLOGERROR("[TARS][AdapterProxy::finishInvoke(ResponsePacket) obj:"<< _objectProxy->name() << ",get req-ptr NULL,may be timeout,id:" << rsp->iRequestId
<< ",desc:" << _endpoint.desc() << endl);
}
return ;
@ -564,14 +538,18 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
{
assert(msg->eStatus != ReqMessage::REQ_REQ);
TLOGTARS("[TARS][AdapterProxy::finishInvoke(ReqMessage) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << " ,id:" << msg->response->iRequestId << endl);
TLOGTARS("[TARS][AdapterProxy::finishInvoke(ReqMessage) obj:" << _objectProxy->name() << ", desc:" << _endpoint.desc()
<< " ,id:" << msg->response->iRequestId
<< ", status:" << msg->eStatus
<< ", ret: " << msg->response->iRet << endl);
#ifdef _USE_OPENTRACKING
finishTrack(msg);
#endif
//单向调用
if(msg->eType == ReqMessage::ONE_WAY)
{
#ifdef _USE_OPENTRACKING
finishTrack(msg);
#endif
delete msg;
msg = NULL;
return ;
@ -579,9 +557,7 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
//stat 上报调用统计
stat(msg);
#ifdef _USE_OPENTRACKING
finishTrack(msg);
#endif
//超时屏蔽统计,异常不算超时统计
if(msg->eStatus != ReqMessage::REQ_EXC && !msg->bPush)
{
@ -596,8 +572,8 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
assert(msg->pMonitor);
TC_ThreadLock::Lock sync(*(msg->pMonitor));
msg->bMonitorFin = true;
msg->pMonitor->notify();
msg->bMonitorFin = true;
}
else
{
@ -621,13 +597,15 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
{
msg->callback->onDispatch(msgPtr);
}
catch(exception & e)
catch (exception & e)
{
TLOGERROR("[TARS][AdapterProxy::finishInvoke(ReqMessage) ex:" << e.what() << ",line:" << __LINE__ << endl);
//FDLOG("taferror")<<"[TAF]AdapterProxy::finishInvoke(ReqMessage) exp:"<<e.what()<<" ,line:"<<__LINE__<<endl;
TLOGERROR("[TAF]AdapterProxy::finishInvoke(ReqMessage) exp:" << e.what() << " ,line:" << __LINE__ << endl);
}
catch(...)
catch (...)
{
TLOGERROR("[TARS]AdapterProxy::finishInvoke(ReqMessage) ex:unknown,line:" << __LINE__ << endl);
//FDLOG("taferror")<<"[TAF]AdapterProxy::finishInvoke(ReqMessage) exp:unknown line:"<<__LINE__<<endl;
TLOGERROR("[TARS]AdapterProxy::finishInvoke(ReqMessage) exp:unknown line:|" << __LINE__ << endl);
}
}
else
@ -639,10 +617,10 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
else
{
CoroParallelBasePtr ptr = msg->callback->getCoroParallelBasePtr();
if(ptr)
if (ptr)
{
ptr->insert(msg);
if(ptr->checkAllReqReturn())
if (ptr->checkAllReqReturn())
{
msg->sched->put(msg->iCoroId);
}
@ -663,13 +641,17 @@ void AdapterProxy::finishInvoke(ReqMessage * msg)
return;
}
//ObjectProxy * AdapterProxy::getObjProxy()
//{
// return _objectProxy;
//}
void AdapterProxy::doTimeout()
{
ReqMessage * msg;
while(_timeoutQueue->timeout(msg))
{
TLOGTARS("[TARS][AdapterProxy::doTimeout objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id " << msg->request.iRequestId << endl);
TLOGTARS("[TARS][AdapterProxy::doTimeout obj:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id " << msg->request.iRequestId << endl);
assert(msg->eStatus == ReqMessage::REQ_REQ);
@ -763,12 +745,11 @@ void AdapterProxy::finishTrack(ReqMessage * msg)
void AdapterProxy::stat(ReqMessage * msg)
{
TLOGTARS("[TARS][AdapterProxy::stat(ReqMessage) objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << ",id:" << msg->response->iRequestId << endl);
if(msg->bPush)
if (msg->bPush)
{
return ;
}
TLOGTARS("[TARS]AdapterProxy::stat(ReqMessage) " << _objectProxy->name() << ", " << _endpoint.desc() << " ,id:" << msg->response->iRequestId << endl);
StatMicMsgBody body;
int64_t sptime = 0;
@ -837,7 +818,7 @@ void AdapterProxy::merge(const StatMicMsgBody& inBody,StatMicMsgBody& outBody/*o
void AdapterProxy::mergeStat(map<StatMicMsgHead, StatMicMsgBody> & mStatMicMsg)
{
TLOGTARS("[TARS][AdapterProxy::doStat objname:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << endl);
TLOGTARS("[TARS][AdapterProxy::doStat obj:" << _objectProxy->name() << ",desc:" << _endpoint.desc() << endl);
for (const auto& kv : _statBody)
{
@ -883,7 +864,7 @@ void AdapterProxy::addConnExc(bool bExc)
TLOGERROR("[TARS][AdapterProxy::addConnExc desc:"<< _endpoint.desc() << ",connect exception status is false!(connect ok)"<<endl);
}
_connExc = false;
_connExc = false;
_connExcCnt = 0;
if(!_activeStatus)

View File

@ -13,7 +13,7 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "util/tc_platform.h"
#include "servant/AdminServant.h"
#include "servant/Application.h"
#include "servant/NotifyObserver.h"
@ -40,7 +40,18 @@ void AdminServant::destroy()
void AdminServant::shutdown(TarsCurrentPtr current)
{
Application::terminate();
#if TARGET_PLATFORM_WINDOWS
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if (hProcess == NULL)
{
return;
}
::TerminateProcess(hProcess, 0);
#else
kill(getpid(), SIGINT); //通过给自己发信号的方式结束, 避免处理线程结束时自己join自己
// Application::terminate();
#endif
}
string AdminServant::notify(const string &command, TarsCurrentPtr current)

View File

@ -30,6 +30,8 @@
namespace tars
{
class Transceiver;
vector<char> ProxyProtocol::tarsRequest(RequestPacket& request, Transceiver *)
{
TarsOutputStream<BufferWriterVector> os;

View File

@ -36,9 +36,6 @@
#endif
// #include <signal.h>
// #include <sys/resource.h>
#if TARS_SSL
#include "util/tc_openssl.h"
#endif
@ -76,11 +73,11 @@ std::string ServerConfig::Notify; //信息通知中心
std::string ServerConfig::LogPath; //logpath
int ServerConfig::LogSize; //log大小(字节)
int ServerConfig::LogNum; //log个数()
std::string ServerConfig::LogLevel; //log日志级别
std::string ServerConfig::LogLevel; //log日志级别
std::string ServerConfig::ConfigFile; //框架配置文件路径
int ServerConfig::ReportFlow; //是否服务端上报所有接口stat流量 0不上报 1上报 (用于非tars协议服务流量统计)
int ServerConfig::IsCheckSet; //是否对按照set规则调用进行合法性检查 0,不检查1检查
bool ServerConfig::OpenCoroutine; //是否启用协程处理方式
int ServerConfig::ReportFlow = 1; //是否服务端上报所有接口stat流量 0不上报 1上报 (用于非taf协议服务流量统计)
int ServerConfig::IsCheckSet = 1; //是否对按照set规则调用进行合法性检查 0,不检查1检查
bool ServerConfig::OpenCoroutine = false; //是否启用协程处理方式
size_t ServerConfig::CoroutineMemSize; //协程占用内存空间的最大大小
uint32_t ServerConfig::CoroutineStackSize; //每个协程的栈大小(默认128k)
bool ServerConfig::ManualListen = false; //手工启动监听端口
@ -108,7 +105,6 @@ PropertyReportPtr g_pReportRspQueue;
/**上报服务端发送队列大小的间隔时间**/
#define REPORT_SEND_QUEUE_INTERVAL 10
///////////////////////////////////////////////////////////////////////////////////////////
Application::Application()
{
@ -126,6 +122,11 @@ Application::~Application()
#endif
}
string Application::getTarsVersion()
{
return TARS_VERSION;
}
TC_Config& Application::getConfig()
{
return _conf;
@ -175,6 +176,16 @@ void heartBeatFunc(const string& adapterName)
TARS_KEEPALIVE(adapterName);
}
void Application::manualListen()
{
vector<TC_EpollServer::BindAdapterPtr> v = getEpollServer()->getBindAdapters();
for(auto &b : v)
{
b->manualListen();
}
}
void Application::waitForShutdown()
{
_epollServer->setCallbackFunctor(reportRspQueue);
@ -712,8 +723,8 @@ void Application::main(const TC_Option &option)
//设置服务的core limit
TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_CLOSE_CORE, Application::cmdCloseCoreDump);
//重新加载locator信息
TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_RELOAD_LOCATOR, Application::cmdReloadLocator);
//设置是否标准输出
TARS_ADD_ADMIN_CMD_PREFIX(TARS_CMD_CLOSE_COUT, Application::cmdCloseCout);
//上报版本
TARS_REPORTVERSION(TARS_VERSION);
@ -855,6 +866,7 @@ string Application::toDefault(const string &s, const string &sDefault)
}
return s;
}
string Application::setDivision()
{
bool bEnableSet = TC_Common::lower(_conf.get("/tars/application<enableset>", "n"))=="y"?true:false;;
@ -939,6 +951,9 @@ void Application::initializeServer()
exe = _conf.get("/tars/application/server<localip>");
}
ServerConfig::ServerName = toDefault(_conf.get("/tars/application/server<server>"), exe);
#if TARGET_PLATFORM_WINDOWS
ServerConfig::BasePath = TC_File::simplifyDirectory(_conf.get("/tars/application/server<basepath.win>")) + FILE_SEP;
if (ServerConfig::BasePath == FILE_SEP)
@ -964,7 +979,6 @@ void Application::initializeServer()
#endif
ServerConfig::TarsPath = TC_File::simplifyDirectory(ServerConfig::LogPath + FILE_SEP + ".." + FILE_SEP) + FILE_SEP;
ServerConfig::ServerName = toDefault(_conf.get("/tars/application/server<server>"), exe);
ServerConfig::LogSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server<logsize>"), "52428800"), 52428800);
ServerConfig::LogNum = TC_Common::strto<int>(toDefault(_conf.get("/tars/application/server<lognum>"), "10"));
@ -976,9 +990,9 @@ void Application::initializeServer()
ServerConfig::Notify = _conf.get("/tars/application/server<notify>");
ServerConfig::ReportFlow = _conf.get("/tars/application/server<reportflow>")=="0"?0:1;
ServerConfig::IsCheckSet = _conf.get("/tars/application/server<checkset>","1")=="0"?0:1;
ServerConfig::OpenCoroutine = TC_Common::strto<bool>(toDefault(_conf.get("/tars/application/server<opencoroutine>"), "0"));
ServerConfig::CoroutineMemSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinememsize>"), "1073741824"), 1073741824);
ServerConfig::CoroutineStackSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinestack>"), "131072"), 131072);
ServerConfig::OpenCoroutine = TC_Common::strto<bool>(toDefault(_conf.get("/tars/application/server<opencoroutine>"), "0"));
ServerConfig::CoroutineMemSize = TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinememsize>"), "1073741824"), 1073741824);
ServerConfig::CoroutineStackSize= (uint32_t)TC_Common::toSize(toDefault(_conf.get("/tars/application/server<coroutinestack>"), "131072"), 131072);
ServerConfig::ManualListen = _conf.get("/tars/application/server<manuallisten>", "0") == "0" ? false : true;
ServerConfig::MergeNetImp = _conf.get("/tars/application/server<mergenetimp>", "0") == "0" ? false : true;
ServerConfig::NetThread = TC_Common::strto<int>(toDefault(_conf.get("/tars/application/server<nethread>"), "1"));
@ -1021,23 +1035,20 @@ void Application::initializeServer()
//输出信息
outServer(cout);
string sNetThread = _conf.get("/tars/application/server<netthread>", "1");
unsigned int iNetThreadNum = TC_Common::strto<unsigned int>(sNetThread);
if(iNetThreadNum < 1)
if(ServerConfig::NetThread < 1)
{
iNetThreadNum = 1;
ServerConfig::NetThread = 1;
cout << OUT_LINE << "\nwarning:netThreadNum < 1." << endl;
}
//网络线程的配置数目不能15个
if(iNetThreadNum > 15)
if(ServerConfig::NetThread > 15)
{
iNetThreadNum = 15;
ServerConfig::NetThread = 15;
cout << OUT_LINE << "\nwarning:netThreadNum > 15." << endl;
}
_epollServer = new TC_EpollServer(iNetThreadNum);
_epollServer = new TC_EpollServer(ServerConfig::NetThread);
//初始化服务是否对空链接进行超时检查
bool bEnable = (_conf.get("/tars/application/server<emptyconcheck>","0")=="1")?true:false;
@ -1108,8 +1119,6 @@ void Application::initializeServer()
setAdapter(lsPtr, "AdminAdapter");
// lsPtr->setName("AdminAdapter");
lsPtr->setEndpoint(ServerConfig::Local);
lsPtr->setMaxConns(TC_EpollServer::BindAdapter::DEFAULT_MAX_CONN);
@ -1279,6 +1288,7 @@ void Application::checkServantNameValid(const string& servant, const string& sPr
cout << os.str() << endl;
exit(-1);
}
}
@ -1297,7 +1307,6 @@ void Application::outAdapter(ostream &os, const string &v, TC_EpollServer::BindA
// os << outfill("queuesize") << lsPtr->getRecvBufferSize() << endl;
os << TC_Common::outfill("connections") << lsPtr->getNowConnection() << endl;
os << TC_Common::outfill("protocol") << lsPtr->getProtocolName() << endl;
// os << TC_Common::outfill("handlegroup") << lsPtr->getHandleGroupName() << endl;
os << TC_Common::outfill("handlethread") << lsPtr->getHandleNum() << endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -128,51 +128,5 @@ void AsyncProcThread::callback(ReqMessage * msg)
TLOGERROR("[TARS][AsyncProcThread exception.]" << endl);
}
}
// void AsyncProcThread::run()
// {
// while (!_terminate)
// {
// ReqMessage * msg = NULL;
// //异步请求回来的响应包处理
// if(_msgQueue->empty())
// {
// TC_ThreadLock::Lock lock(*this);
// timedWait(1000);
// }
// if (_msgQueue->pop_front(msg))
// {
// //从回调对象把线程私有数据传递到回调线程中
// ServantProxyThreadData * pServantProxyThreadData = ServantProxyThreadData::getData();
// assert(pServantProxyThreadData != NULL);
// //把染色的消息设置在线程私有数据里面
// pServantProxyThreadData->_dyeing = msg->bDyeing;
// pServantProxyThreadData->_dyeingKey = msg->sDyeingKey;
// if(msg->adapter)
// {
// // snprintf(pServantProxyThreadData->_szHost, sizeof(pServantProxyThreadData->_szHost), "%s", msg->adapter->endpoint().desc().c_str());
// pServantProxyThreadData->_szHost = msg->adapter->endpoint().desc();
// }
// try
// {
// ReqMessagePtr msgPtr = msg;
// msg->callback->onDispatch(msgPtr);
// }
// catch (exception& e)
// {
// TLOGERROR("[TARS][AsyncProcThread exception]:" << e.what() << endl);
// }
// catch (...)
// {
// TLOGERROR("[TARS][AsyncProcThread exception.]" << endl);
// }
// }
// }
// }
/////////////////////////////////////////////////////////////////////////
}

View File

@ -15,7 +15,7 @@
*/
#include "util/tc_epoll_server.h"
#include "util/tc_tea.h"
#include "util/tc_des.h"
#include "util/tc_sha.h"
#include "util/tc_md5.h"
#include "servant/Application.h"
@ -169,18 +169,18 @@ int processAuthReqHelper(const BasicAuthPackage& pkg, const BasicAuthInfo& info)
tmp[i] |= pt[i];
}
tmpKey = TC_MD5::md5bin(tmp);
tmpKey = TC_MD5::md5str(tmp);
}
string secret1;
{
vector<char> dec;
// vector<char> dec;
try
{
TC_Tea::decrypt(tmpKey.data(), pkg.sSignature.data(), pkg.sSignature.size(), dec);
secret1.assign(dec.begin(), dec.end());
secret1 = TC_Des::decrypt3(tmpKey.data(), pkg.sSignature.data(), pkg.sSignature.size());
// secret1.assign(dec.begin(), dec.end());
}
catch (const TC_Tea_Exception& )
catch (const TC_DES_Exception& )
{
return AUTH_DEC_FAIL;
}
@ -248,7 +248,7 @@ string defaultCreateAuthReq(const BasicAuthInfo& info /*, const string& hashMeth
string secret2 = TC_SHA::sha1str(secret1.data(), secret1.size());
// create tmpKey
string tmpKey;
string tmpKey;
{
string tmp = secret2;
const char* pt = (const char* )&pkg.iTime;
@ -256,16 +256,17 @@ string defaultCreateAuthReq(const BasicAuthInfo& info /*, const string& hashMeth
{
tmp[i] |= pt[i];
}
// 保证key是16字节
tmpKey = TC_MD5::md5bin(tmp);
// 只用了前面24字节
tmpKey = TC_MD5::md5str(tmp);
}
// then use tmpKey to enc secret1, show server that I know secret1, ie, I know secret.
vector<char> secret1Enc;
// vector<char> secret1Enc;
TC_Tea::encrypt(tmpKey.data(), secret1.data(), secret1.size(), secret1Enc);
// TC_Tea::encrypt(tmpKey.data(), secret1.data(), secret1.size(), secret1Enc);
pkg.sSignature = TC_Des::encrypt3(tmpKey.data(), secret1.data(), secret1.size());
pkg.sSignature.assign(secret1Enc.begin(), secret1Enc.end());
// pkg.sSignature.assign(secret1Enc.begin(), secret1Enc.end());
pkg.writeTo(os);
return os.getByteBuffer();

View File

@ -50,6 +50,10 @@ Communicator::Communicator()
, _traceManager(NULL)
#endif
{
#if TARGET_PLATFORM_WINDOWS
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
#endif
memset(_communicatorEpoll,0,sizeof(_communicatorEpoll));
}
@ -67,6 +71,10 @@ Communicator::Communicator(TC_Config& conf, const string& domain/* = CONFIG_ROOT
Communicator::~Communicator()
{
terminate();
#if TARGET_PLATFORM_WINDOWS
WSACleanup();
#endif
}
bool Communicator::isTerminating()
@ -170,12 +178,20 @@ void Communicator::setProperty(TC_Config& conf, const string& domain/* = CONFIG_
}
}
initClientConfig();
// initClientConfig();
}
void Communicator::initClientConfig()
void Communicator::initialize()
{
ClientConfig::SetOpen = TC_Common::lower(getProperty("enableset", "n"))=="y"?true:false;
TC_LockT<TC_ThreadRecMutex> lock(*this);
if (_initialized) return;
_initialized = true;
ClientConfig::TarsVersion = TARS_VERSION;
ClientConfig::SetOpen = TC_Common::lower(getProperty("enableset", "n")) == "y" ? true : false;
if (ClientConfig::SetOpen)
{
@ -185,9 +201,7 @@ void Communicator::initClientConfig()
string sWildCard = "*";
if (vtSetDivisions.size()!=3
|| vtSetDivisions[0]==sWildCard
|| vtSetDivisions[1]==sWildCard)
if (vtSetDivisions.size()!=3 || vtSetDivisions[0]==sWildCard || vtSetDivisions[1]==sWildCard)
{
//set分组名不对时默认没有打开set分组
ClientConfig::SetOpen = false;
@ -223,10 +237,116 @@ void Communicator::initClientConfig()
//取失败则使用ip代替进程名
exe = ClientConfig::LocalIp;
}
ClientConfig::ModuleName = getProperty("modulename", exe);
#if TARS_SSL
string ca = getProperty("ca");
string cert = getProperty("cert");
string key = getProperty("key");
if(!ca.empty()) {
_ctx = TC_OpenSSL::newCtx(ca, cert, key, false);
if(!_ctx)
{
TLOGERROR("[TARS]load client ssl error, ca:" << ca << endl);
exit(-1);
}
}
#endif
_servantProxyFactory = new ServantProxyFactory(this);
_clientThreadNum = TC_Common::strto<size_t>(getProperty("netthread","1"));
if(0 == _clientThreadNum)
{
_clientThreadNum = 1;
}
else if(MAX_CLIENT_THREAD_NUM < _clientThreadNum)
{
_clientThreadNum = MAX_CLIENT_THREAD_NUM;
}
//异步线程数
_asyncThreadNum = TC_Common::strto<size_t>(getProperty("asyncthread", "3"));
if(_asyncThreadNum == 0)
{
_asyncThreadNum = 3;
}
if(_asyncThreadNum > MAX_CLIENT_ASYNCTHREAD_NUM)
{
_asyncThreadNum = MAX_CLIENT_ASYNCTHREAD_NUM;
}
bool merge = TC_Common::strto<bool>(getProperty("mergenetasync", "0"));
//异步队列的大小
size_t iAsyncQueueCap = TC_Common::strto<size_t>(getProperty("asyncqueuecap", "100000"));
if(iAsyncQueueCap < 10000)
{
iAsyncQueueCap = 10000;
}
//第一个通信器才去启动回调线程
for (size_t i = 0; i < _asyncThreadNum; ++i) {
_asyncThread.push_back(new AsyncProcThread(iAsyncQueueCap, merge));
}
//stat总是有对象, 保证getStat返回的对象总是有效
_statReport = new StatReport(_clientThreadNum);
for(size_t i = 0; i < _clientThreadNum; ++i)
{
_communicatorEpoll[i] = new CommunicatorEpoll(this, i);
_communicatorEpoll[i]->start();
}
//异步队列数目上报
_reportAsyncQueue= getStatReport()->createPropertyReport(ClientConfig::ModuleName + ".asyncqueue", PropertyReport::avg());
//初始化统计上报接口
string statObj = getProperty("stat", "");
string propertyObj = getProperty("property", "");
int iReportInterval = TC_Common::strto<int>(getProperty("report-interval", "60000"));
int iReportTimeout = TC_Common::strto<int>(getProperty("report-timeout", "5000"));
int iSampleRate = TC_Common::strto<int>(getProperty("sample-rate", "1000"));
int iMaxSampleCount = TC_Common::strto<int>(getProperty("max-sample-count", "100"));
int iMaxReportSize = TC_Common::strto<int>(getProperty("max-report-size", "1400"));
_timeoutLogFlag = TC_Common::strto<bool>(getProperty("timeout-log-flag", "1"));
_minTimeout = TC_Common::strto<int64_t>(getProperty("min-timeout", "100"));
if(_minTimeout < 1)
_minTimeout = 1;
StatFPrx statPrx = NULL;
if (!statObj.empty())
{
statPrx = stringToProxy<StatFPrx>(statObj);
}
//上报Property信息的代理
PropertyFPrx propertyPrx = NULL;
if (!propertyObj.empty())
{
propertyPrx = stringToProxy<PropertyFPrx>(propertyObj);
}
string sSetDivision = ClientConfig::SetOpen?ClientConfig::SetDivision:"";
_statReport->setReportInfo(statPrx, propertyPrx, ClientConfig::ModuleName, ClientConfig::LocalIp, sSetDivision, iReportInterval, iSampleRate, iMaxSampleCount, iMaxReportSize, iReportTimeout);
ClientConfig::ModuleName = getProperty("modulename", exe);
#if _USE_OPENTRACKING
string collector_host = getProperty("collector_host", "");
string collector_host = getProperty("collector_host", "");
string collector_port = getProperty("collector_port", "");
if(!collector_host.empty() && !collector_port.empty())
{
@ -234,7 +354,7 @@ void Communicator::initClientConfig()
zipkin::ZipkinOtTracerOptions options;
options.service_name = ClientConfig::ModuleName;
options.service_address = {zipkin::IpVersion::v4, ClientConfig::LocalIp};
options.sample_rate = strtod(getProperty("sample_rate", "1.0").c_str(), NULL);
options.collector_host = collector_host;
options.collector_port = atoi(collector_port.c_str());
@ -245,13 +365,12 @@ void Communicator::initClientConfig()
}
void Communicator::setProperty(const map<string, string>& properties)
{
TC_LockT<TC_ThreadRecMutex> lock(*this);
_properties = properties;
initClientConfig();
}
void Communicator::setProperty(const string& name, const string& value)
@ -259,8 +378,6 @@ void Communicator::setProperty(const string& name, const string& value)
TC_LockT<TC_ThreadRecMutex> lock(*this);
_properties[name] = value;
initClientConfig();
}
string Communicator::getProperty(const string& name, const string& dft/* = ""*/)
@ -333,127 +450,6 @@ int Communicator::reloadProperty(string & sResult)
return 0;
}
void Communicator::initialize()
{
TC_LockT<TC_ThreadRecMutex> lock(*this);
if (_initialized)
return;
_initialized = true;
#if TARS_SSL
string ca = getProperty("ca");
string cert = getProperty("cert");
string key = getProperty("key");
if(!ca.empty()) {
_ctx = TC_OpenSSL::newCtx(ca, cert, key, false);
if(!_ctx)
{
TLOGERROR("[TARS]load client ssl error, ca:" << ca << endl);
exit(-1);
}
}
#endif
_servantProxyFactory = new ServantProxyFactory(this);
_clientThreadNum = TC_Common::strto<size_t>(getProperty("netthread","1"));
if(0 == _clientThreadNum)
{
_clientThreadNum = 1;
}
else if(MAX_CLIENT_THREAD_NUM < _clientThreadNum)
{
_clientThreadNum = MAX_CLIENT_THREAD_NUM;
}
//异步线程数
_asyncThreadNum = TC_Common::strto<size_t>(getProperty("asyncthread", "3"));
if(_asyncThreadNum == 0)
{
_asyncThreadNum = 3;
}
if(_asyncThreadNum > MAX_CLIENT_ASYNCTHREAD_NUM)
{
_asyncThreadNum = MAX_CLIENT_ASYNCTHREAD_NUM;
}
bool merge = TC_Common::strto<bool>(getProperty("mergenetasync", "0"));
//异步队列的大小
size_t iAsyncQueueCap = TC_Common::strto<size_t>(getProperty("asyncqueuecap", "100000"));
if(iAsyncQueueCap < 10000)
{
iAsyncQueueCap = 10000;
}
//第一个通信器才去启动回调线程
for (size_t i = 0; i < _asyncThreadNum; ++i) {
_asyncThread.push_back(new AsyncProcThread(iAsyncQueueCap, merge));
}
//stat总是有对象, 保证getStat返回的对象总是有效
_statReport = new StatReport(_clientThreadNum);
for(size_t i = 0; i < _clientThreadNum; ++i)
{
_communicatorEpoll[i] = new CommunicatorEpoll(this, i);
_communicatorEpoll[i]->start();
}
//异步队列数目上报
string moduleName = getProperty("modulename", "");
if(!moduleName.empty())
{
_reportAsyncQueue= getStatReport()->createPropertyReport(moduleName + ".asyncqueue", PropertyReport::avg());
}
//初始化统计上报接口
string statObj = getProperty("stat", "");
string propertyObj = getProperty("property", "");
int iReportInterval = TC_Common::strto<int>(getProperty("report-interval", "60000"));
int iReportTimeout = TC_Common::strto<int>(getProperty("report-timeout", "5000"));
int iSampleRate = TC_Common::strto<int>(getProperty("sample-rate", "1000"));
int iMaxSampleCount = TC_Common::strto<int>(getProperty("max-sample-count", "100"));
int iMaxReportSize = TC_Common::strto<int>(getProperty("max-report-size", "1400"));
_timeoutLogFlag = TC_Common::strto<bool>(getProperty("timeout-log-flag", "1"));
_minTimeout = TC_Common::strto<int64_t>(getProperty("min-timeout", "100"));
if(_minTimeout < 1)
_minTimeout = 1;
StatFPrx statPrx = NULL;
if (!statObj.empty())
{
statPrx = stringToProxy<StatFPrx>(statObj);
}
//上报Property信息的代理
PropertyFPrx propertyPrx = NULL;
if (!propertyObj.empty())
{
propertyPrx = stringToProxy<PropertyFPrx>(propertyObj);
}
string sSetDivision = ClientConfig::SetOpen?ClientConfig::SetDivision:"";
_statReport->setReportInfo(statPrx, propertyPrx, ClientConfig::ModuleName, ClientConfig::LocalIp, sSetDivision, iReportInterval, iSampleRate, iMaxSampleCount, iMaxReportSize, iReportTimeout);
}
vector<TC_Endpoint> Communicator::getEndpoint(const string & objName)
{
ServantProxy * pServantProxy = getServantProxy(objName);
@ -466,26 +462,6 @@ vector<TC_Endpoint> Communicator::getEndpoint4All(const string & objName)
return pServantProxy->getEndpoint4All();
}
void Communicator::doStat()
{
if(_reportAsyncQueue)
{
size_t n = 0;
for(size_t i = 0;i < _asyncThreadNum; ++i)
{
n = n + _asyncThread[i]->getSize();
}
_reportAsyncQueue->report(n);
}
}
void Communicator::pushAsyncThreadQueue(ReqMessage * msg)
{
//先不考虑每个线程队列数目不一致的情况
_asyncThread[(++_asyncSeq)%_asyncThreadNum]->push_back(msg);
}
void Communicator::terminate()
{
{
@ -526,6 +502,8 @@ void Communicator::terminate()
}
}
//把锁释放掉, 再来等待线程停止, 避免死锁
//因为通信器线程运行过程中, 有可能加上上面那把锁
if (_initialized)
{
for (size_t i = 0; i < _clientThreadNum; ++i)
@ -548,7 +526,25 @@ void Communicator::terminate()
_servantProxyFactory = NULL;
}
}
}
void Communicator::pushAsyncThreadQueue(ReqMessage * msg)
{
//先不考虑每个线程队列数目不一致的情况
_asyncThread[(_asyncSeq++)%_asyncThreadNum]->push_back(msg);
}
void Communicator::doStat()
{
//队列长度上报
if (_reportAsyncQueue) {
size_t n = 0;
for (size_t i = 0; i < _asyncThreadNum; ++i) {
n = n + _asyncThread[i]->getSize();
}
_reportAsyncQueue->report((int) n);
}
}
ServantProxy * Communicator::getServantProxy(const string& objectName,const string& setName)

View File

@ -30,10 +30,7 @@ CommunicatorEpoll::CommunicatorEpoll(Communicator * pCommunicator,size_t netThre
, _nextTime(0)
, _nextStatTime(0)
, _objectProxyFactory(NULL)
// , _asyncThreadNum(3)
// , _asyncSeq(0)
, _netThreadSeq(netThreadSeq)
// , _reportAsyncQueue(NULL)
, _noSendQueueLimit(1000)
, _timeoutCheckInterval(100)
{
@ -53,12 +50,6 @@ CommunicatorEpoll::CommunicatorEpoll(Communicator * pCommunicator,size_t netThre
_noSendQueueLimit = 1000;
}
// //epollwait的超时时间
// _waitTimeout = TC_Common::strto<int64_t>(pCommunicator->getProperty("epollwaittimeout", "100"));
// if(_waitTimeout < 1)
// {
// _waitTimeout = 1;
// }
//检查超时请求的时间间隔,单位:ms
_timeoutCheckInterval = TC_Common::strto<int64_t>(pCommunicator->getProperty("timeoutcheckinterval", "100"));
@ -72,55 +63,10 @@ CommunicatorEpoll::CommunicatorEpoll(Communicator * pCommunicator,size_t netThre
_notify[i] = NULL;
}
// if(isFirstNetThread()) {
// //异步线程数
// _asyncThreadNum = TC_Common::strto<size_t>(pCommunicator->getProperty("asyncthread", "3"));
// if(_asyncThreadNum == 0)
// {
// _asyncThreadNum = 3;
// }
// if(_asyncThreadNum > MAX_CLIENT_ASYNCTHREAD_NUM)
// {
// _asyncThreadNum = MAX_CLIENT_ASYNCTHREAD_NUM;
// }
// //第一个通信器才去启动回调线程
// for (size_t i = 0; i < _asyncThreadNum; ++i) {
// _asyncThread.push_back(new AsyncProcThread(iAsyncQueueCap, merge));
// }
// //异步队列数目上报
// string moduleName = pCommunicator->getProperty("modulename", "");
// if(!moduleName.empty())
// {
// PropertyReportPtr asyncQueuePtr = pCommunicator->getStatReport()->createPropertyReport(moduleName + ".asyncqueue", PropertyReport::avg());
// _reportAsyncQueue = asyncQueuePtr.get();
// }
// }
}
CommunicatorEpoll::~CommunicatorEpoll()
{
// if(isFirstNetThread()) {
// for(size_t i = 0;i < _asyncThreadNum; ++i)
// {
// if(_asyncThread[i])
// {
// if (_asyncThread[i]->isAlive())
// {
// _asyncThread[i]->terminate();
// _asyncThread[i]->getThreadControl().join();
// }
// delete _asyncThread[i];
// _asyncThread[i] = NULL;
// }
// }
// }
for(size_t i = 0;i < MAX_CLIENT_NOTIFYEVENT_NUM;++i)
{
if(_notify[i])
@ -413,16 +359,7 @@ void CommunicatorEpoll::doStat()
if(isFirstNetThread()) {
_communicator->doStat();
// //异步队列长度上报
// if(_reportAsyncQueue)
// {
// size_t n = 0;
// for(size_t i = 0;i < _asyncThreadNum; ++i)
// {
// n = n + _asyncThread[i]->getSize();
// }
// _reportAsyncQueue->report(n);
// }
}
StatReport::MapStatMicMsg mStatMicMsg;
@ -443,14 +380,7 @@ void CommunicatorEpoll::doStat()
void CommunicatorEpoll::pushAsyncThreadQueue(ReqMessage * msg)
{
_communicator->pushAsyncThreadQueue(msg);
// //先不考虑每个线程队列数目不一致的情况
// _asyncThread[_asyncSeq]->push_back(msg);
// _asyncSeq ++;
// if(_asyncSeq == _asyncThreadNum)
// {
// _asyncSeq = 0;
// }
}
void CommunicatorEpoll::run()
@ -465,14 +395,10 @@ void CommunicatorEpoll::run()
{
try
{
// int iTimeout = ((_waitTimeout < _timeoutCheckInterval) ? _waitTimeout : _timeoutCheckInterval);
//考虑到检测超时等的情况 这里就wait100ms吧
int num = _ep.wait(100);
if (_terminate) break;
if(_terminate)
{
break;
}
//先处理epoll的网络事件
for (int i = 0; i < num; ++i)
@ -481,10 +407,9 @@ void CommunicatorEpoll::run()
uint64_t data = TC_Epoller::getU64(ev);
if(data == 0)
{
continue; //data非指针, 退出循环
}
if(data == 0) continue; //data非指针, 退出循环
// int64_t ms = TNOWMS;
handle((FDInfo*)data, ev);
}

View File

@ -756,11 +756,14 @@ void CoroutineScheduler::terminate()
uint32_t CoroutineScheduler::generateId()
{
while (++_uniqId < 1);
uint32_t i = ++_uniqId;
if(i == 0) {
i = ++_uniqId;
}
assert(_uniqId <= _poolSize);
assert(i <= _poolSize);
return _uniqId;
return i;
}
// void CoroutineScheduler::switchCoro(CoroutineInfo *from, CoroutineInfo *to)

View File

@ -22,46 +22,22 @@
namespace tars
{
EndpointInfo::EndpointInfo()
: _port(0)
, _grid(0)
, _qos(0)
, _type(TCP)
, _weight(-1)
, _weighttype(0)
, _authType(AUTH_TYPENONE)
, _isIPv6(false)
, _addressSucc(false)
{
_setDivision.clear();
memset(&_addr,0,sizeof(_addr));
// memset(&_addr,0,sizeof(struct sockaddr_in));
memset(&_addr,0,sizeof(_addr));
//7暂时写死 修改默认值一定要修改这个7
// memcpy(_host,"0.0.0.0",7);
//snprintf(_host, sizeof(_host), "%s", "0.0.0.0");
}
EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::EType type, int32_t grid, const string & setDivision, int qos, int weight, unsigned int weighttype, int authType)
: _host(host)
, _port(port)
, _grid(grid)
, _qos(qos)
, _type(type)
EndpointInfo::EndpointInfo(const string& host, uint16_t port, TC_Endpoint::EType type, int32_t grid, const string & setDivision, int qos, int weight, unsigned int weighttype, int authType)
: _ep(host, port, 60000, type, grid, qos, weight, weighttype, authType)
, _setDivision(setDivision)
, _weight(weight)
, _weighttype(weighttype)
, _authType((AUTH_TYPE)authType)
, _addressSucc(false)
{
_isIPv6 = TC_Socket::addressIsIPv6(host);
if(_weighttype == 0)
{
_weight = -1;
}
else
{
if(_weight == -1)
{
_weight = 100;
}
_weight = (_weight > 100 ? 100 : _weight);
}
_cmpDesc = createCompareDesc();
@ -72,13 +48,13 @@ void EndpointInfo::parseAddress()
{
// try
// {
if (_isIPv6)
if (isIPv6())
{
TC_Socket::parseAddrWithPort(_host, _port, _addr.in6);
TC_Socket::parseAddrWithPort(_ep.getHost(), _ep.getPort(), _addr.in6);
}
else
{
TC_Socket::parseAddrWithPort(_host, _port, _addr.in);
TC_Socket::parseAddrWithPort(_ep.getHost(), _ep.getPort(), _addr.in);
}
// }
// catch (...)
@ -89,43 +65,13 @@ void EndpointInfo::parseAddress()
string EndpointInfo::createCompareDesc()
{
ostringstream os;
if (_type == EndpointInfo::UDP) { os << "udp:"; }
if (_type == EndpointInfo::TCP) { os << "tcp:"; }
if (_type == EndpointInfo::SSL) { os << "ssl:"; }
os << _grid << ":" << _host << ":" << _port
<< ":" << _setDivision << ":" << _qos << ":" << _weight << ":" << _weighttype << ":" << _authType;
return _ep.toString();
return os.str();
}
string EndpointInfo::createDesc() const
{
ostringstream os;
if (_type == EndpointInfo::TCP)
os << "tcp";
else if (_type == EndpointInfo::UDP)
os << "udp";
else
os << "ssl";
os << " -h " << host();
os << " -p " << port();
if(0 != _grid)
os << " -g " << _grid;
if (!_setDivision.empty())
{
os << " -s " << _setDivision;
}
if(0 != _qos)
os << " -q " << _qos;
if(0 != _authType)
os << " -e " << _authType;
return os.str();
return _ep.toString();
}
bool EndpointInfo::operator == (const EndpointInfo& r) const
@ -138,19 +84,25 @@ bool EndpointInfo::operator < (const EndpointInfo& r) const
return (_cmpDesc < r._cmpDesc);
}
string EndpointInfo::host() const
const string &EndpointInfo::host() const
{
return string(_host);
return _ep.getHost();
}
int32_t EndpointInfo::grid() const
{
return _grid;
return _ep.getGrid();
}
uint16_t EndpointInfo::port() const
{
return _port;
return _ep.getPort();
}
bool EndpointInfo::isTcp() const
{
return _ep.isTcp();
}
const struct sockaddr_in& EndpointInfo::addr() const
@ -160,13 +112,13 @@ const struct sockaddr_in& EndpointInfo::addr() const
const struct sockaddr * EndpointInfo::addrPtr() const
{
return _isIPv6 ? (struct sockaddr *)&_addr.in6 : (struct sockaddr *)&_addr.in;
}
EndpointInfo::EType EndpointInfo::type() const
{
return _type;
return _ep.isIPv6() ? (struct sockaddr *)&_addr.in6 : (struct sockaddr *)&_addr.in;
}
//
//EndpointInfo::EType EndpointInfo::type() const
//{
// return _type;
//}
const string& EndpointInfo::setDivision() const
{

View File

@ -13,7 +13,7 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "util/tc_port.h"
#include "servant/EndpointManager.h"
#include "servant/ObjectProxy.h"
#include "servant/TarsLogger.h"
@ -48,7 +48,7 @@ QueryEpBase::QueryEpBase(Communicator * pComm, bool bFirstNetThread,bool bInterf
setNoDelete(true);
}
void QueryEpBase::callback_findObjectById4All(tars::Int32 ret, const vector<tars::EndpointF>& activeEp, const vector<tars::EndpointF>& inactiveEp)
void QueryEpBase::callback_findObjectById4All(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
{
TLOGTARS("[TARS][callback_findObjectById4All _objName:" << _objName << "|ret:" << ret
<< ",active:" << activeEp.size()
@ -57,14 +57,14 @@ void QueryEpBase::callback_findObjectById4All(tars::Int32 ret, const vector<tars
doEndpoints(activeEp,inactiveEp,ret);
}
void QueryEpBase::callback_findObjectById4All_exception(tars::Int32 ret)
void QueryEpBase::callback_findObjectById4All_exception(Int32 ret)
{
TLOGERROR("[TARS][callback_findObjectById4All_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
doEndpointsExp(ret);
}
void QueryEpBase::callback_findObjectById4Any(tars::Int32 ret, const vector<tars::EndpointF>& activeEp, const vector<tars::EndpointF>& inactiveEp)
void QueryEpBase::callback_findObjectById4Any(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
{
TLOGTARS("[TARS][callback_findObjectById4Any _objName:" << _objName << "|ret:" << ret
<< ",active:" << activeEp.size()
@ -73,14 +73,14 @@ void QueryEpBase::callback_findObjectById4Any(tars::Int32 ret, const vector<tars
doEndpoints(activeEp,inactiveEp,ret);
}
void QueryEpBase::callback_findObjectById4Any_exception(tars::Int32 ret)
void QueryEpBase::callback_findObjectById4Any_exception(Int32 ret)
{
TLOGERROR("[TARS][callback_findObjectById4Any_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
doEndpointsExp(ret);
}
void QueryEpBase::callback_findObjectByIdInSameGroup(tars::Int32 ret, const vector<tars::EndpointF>& activeEp, const vector<tars::EndpointF>& inactiveEp)
void QueryEpBase::callback_findObjectByIdInSameGroup(Int32 ret, const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp)
{
TLOGTARS("[TARS][callback_findObjectByIdInSameGroup _objName:" << _objName << "|ret:"<<ret
<< ",active:" << activeEp.size()
@ -89,14 +89,14 @@ void QueryEpBase::callback_findObjectByIdInSameGroup(tars::Int32 ret, const vect
doEndpoints(activeEp,inactiveEp,ret);
}
void QueryEpBase::callback_findObjectByIdInSameGroup_exception(tars::Int32 ret)
void QueryEpBase::callback_findObjectByIdInSameGroup_exception(Int32 ret)
{
TLOGERROR("[TARS][callback_findObjectByIdInSameGroup_exception _objName:" << _objName << "|ret:" << ret << "]" << endl);
doEndpointsExp(ret);
}
void QueryEpBase::callback_findObjectByIdInSameSet( Int32 ret, const vector<tars::EndpointF> &activeEp, const vector<tars::EndpointF> & inactiveEp)
void QueryEpBase::callback_findObjectByIdInSameSet( Int32 ret, const vector<EndpointF> &activeEp, const vector<EndpointF> & inactiveEp)
{
TLOGTARS("[TARS][callback_findObjectByIdInSameSet _objName:" << _objName << "|ret:" << ret
<< ",active:" << activeEp.size()
@ -112,7 +112,7 @@ void QueryEpBase::callback_findObjectByIdInSameSet_exception( Int32 ret)
doEndpointsExp(ret);
}
void QueryEpBase::callback_findObjectByIdInSameStation( Int32 ret, const vector<tars::EndpointF> &activeEp, const vector<tars::EndpointF> &inactiveEp)
void QueryEpBase::callback_findObjectByIdInSameStation( Int32 ret, const vector<EndpointF> &activeEp, const vector<EndpointF> &inactiveEp)
{
TLOGTARS("[TARS][callback_findObjectByIdInSameStation _objName:" << _objName << "|ret:" << ret
<< ",active:" << activeEp.size()
@ -150,11 +150,11 @@ bool QueryEpBase::init(const string & sObjName,const string & sLocator,const str
void QueryEpBase::setObjName(const string & sObjName)
{
string sEndpoints("");
string sInactiveEndpoints("");
string::size_type pos = sObjName.find_first_of('@');
string sEndpoints;
string sInactiveEndpoints;
if (pos != string::npos)
{
//[直接连接]指定服务的IP和端口列表
@ -280,17 +280,18 @@ void QueryEpBase::setEndpoints(const string & sEndpoints, set<EndpointInfo> & se
{
TC_Endpoint ep(vEndpoints[i]);
EndpointInfo::EType type;
if (ep.isSSL())
type = EndpointInfo::SSL;
else if (ep.isTcp())
type = EndpointInfo::TCP;
else
type = EndpointInfo::UDP;
// TC_Endpoint::EType type = ep.getType();
// if (ep.isSSL())
// type = TC_Endpoint::SSL;
// else if (ep.isTcp())
// type = TC_Endpoint::TCP;
// else
// type = TC_Endpoint::UDP;
string sSetDivision;
//解析set分组信息
if (!_direct)
{
string sep = " -s ";
size_t pos = vEndpoints[i].rfind(sep);
@ -320,7 +321,7 @@ void QueryEpBase::setEndpoints(const string & sEndpoints, set<EndpointInfo> & se
}
}
EndpointInfo epi(ep.getHost(), ep.getPort(), type, ep.getGrid(), sSetDivision, ep.getQos(), ep.getWeight(), ep.getWeightType(), ep.getAuthType());
EndpointInfo epi(ep.getHost(), ep.getPort(), ep.getType(), ep.getGrid(), sSetDivision, ep.getQos(), ep.getWeight(), ep.getWeightType(), ep.getAuthType());
setEndpoints.insert(epi);
}
@ -381,8 +382,8 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName)
{
if(bSync)
{
vector<tars::EndpointF> activeEp;
vector<tars::EndpointF> inactiveEp;
vector<EndpointF> activeEp;
vector<EndpointF> inactiveEp;
int iRet = 0;
switch(type)
{
@ -468,7 +469,7 @@ void QueryEpBase::refreshReg(GetEndpointType type, const string & sName)
}
}
void QueryEpBase::doEndpoints(const vector<tars::EndpointF>& activeEp, const vector<tars::EndpointF>& inactiveEp, int iRet, bool bSync)
void QueryEpBase::doEndpoints(const vector<EndpointF>& activeEp, const vector<EndpointF>& inactiveEp, int iRet, bool bSync)
{
if(iRet != 0)
{
@ -525,8 +526,8 @@ void QueryEpBase::doEndpoints(const vector<tars::EndpointF>& activeEp, const vec
// tars istcp意思和这里枚举值对应
EndpointInfo::EType type = EndpointInfo::EType(activeEp[i].istcp);
EndpointInfo ep(activeEp[i].host, activeEp[i].port, type, activeEp[i].grid, activeEp[i].setId, activeEp[i].qos, activeEp[i].weight, activeEp[i].weightType, activeEp[i].authType);
// EndpointInfo::EType type = EndpointInfo::EType(activeEp[i].istcp);
EndpointInfo ep(activeEp[i].host, activeEp[i].port, (TC_Endpoint::EType)activeEp[i].istcp, activeEp[i].grid, activeEp[i].setId, activeEp[i].qos, activeEp[i].weight, activeEp[i].weightType, activeEp[i].authType);
activeEps.insert(ep);
}
@ -535,8 +536,8 @@ void QueryEpBase::doEndpoints(const vector<tars::EndpointF>& activeEp, const vec
for (uint32_t i = 0; i < inactiveEp.size(); ++i)
{
// tars istcp意思和这里枚举值对应
EndpointInfo::EType type = EndpointInfo::EType(inactiveEp[i].istcp);
EndpointInfo ep(inactiveEp[i].host, inactiveEp[i].port, type, inactiveEp[i].grid, inactiveEp[i].setId, inactiveEp[i].qos, inactiveEp[i].weight, inactiveEp[i].weightType, inactiveEp[i].authType);
// EndpointInfo::EType type = EndpointInfo::EType(inactiveEp[i].istcp);
EndpointInfo ep(inactiveEp[i].host, inactiveEp[i].port, (TC_Endpoint::EType)activeEp[i].istcp, inactiveEp[i].grid, inactiveEp[i].setId, inactiveEp[i].qos, inactiveEp[i].weight, inactiveEp[i].weightType, inactiveEp[i].authType);
inactiveEps.insert(ep);
}
@ -792,23 +793,27 @@ void EndpointManager::doNotify()
_objectProxy->doInvoke();
}
bool EndpointManager::selectAdapterProxy(ReqMessage * msg,AdapterProxy * & pAdapterProxy)
bool EndpointManager::selectAdapterProxy(ReqMessage * msg,AdapterProxy * & pAdapterProxy, bool onlyCheck)
{
pAdapterProxy = NULL;
//刷新主控
refreshReg(E_DEFAULT,"");
//无效的数据 返回true
if(!_valid)
{
return true;
}
if(!onlyCheck)
{
//刷新主控
refreshReg(E_DEFAULT,"");
//无效的数据 返回true
if(!_valid)
{
return true;
}
}
//如果有hash则先使用hash策略
if (msg->bHash)
{
pAdapterProxy = getHashProxy(msg->iHashCode, msg->bConHash);
pAdapterProxy = getHashProxy(msg->iHashCode, msg->bConHash, onlyCheck);
return false;
}
@ -820,18 +825,18 @@ bool EndpointManager::selectAdapterProxy(ReqMessage * msg,AdapterProxy * & pAdap
if(_weightType == E_STATIC_WEIGHT || msg->eType == ReqMessage::ONE_WAY)
bStaticWeighted = true;
pAdapterProxy = getWeightedProxy(bStaticWeighted);
pAdapterProxy = getWeightedProxy(bStaticWeighted, onlyCheck);
}
else
{
//普通轮询模式
pAdapterProxy = getNextValidProxy();
pAdapterProxy = getNextValidProxy(onlyCheck);
}
return false;
}
AdapterProxy * EndpointManager::getNextValidProxy()
AdapterProxy * EndpointManager::getNextValidProxy(bool onlyCheck)
{
if (_activeProxys.empty())
{
@ -847,7 +852,7 @@ AdapterProxy * EndpointManager::getNextValidProxy()
if(_lastRoundPosition >= _activeProxys.size())
_lastRoundPosition = 0;
if(_activeProxys[_lastRoundPosition]->checkActive())
if(_activeProxys[_lastRoundPosition]->checkActive(false, onlyCheck))
{
return _activeProxys[_lastRoundPosition];
}
@ -857,52 +862,54 @@ AdapterProxy * EndpointManager::getNextValidProxy()
conn.push_back(_activeProxys[_lastRoundPosition]);
}
if(conn.size() > 0)
{
//都有问题, 随机选择一个没有connect超时或者链接异常的发送
AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())];
if(!onlyCheck) {
if(conn.size() > 0)
{
//都有问题, 随机选择一个没有connect超时或者链接异常的发送
AdapterProxy * adapterProxy = conn[((uint32_t)rand() % conn.size())];
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true);
return adapterProxy;
}
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true, false);
return adapterProxy;
}
//所有adapter都有问题 选不到结点,随机找一个重试
AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
//所有adapter都有问题 选不到结点,随机找一个重试
AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true);
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true, false);
}
return NULL;
}
AdapterProxy* EndpointManager::getHashProxy(int64_t hashCode, bool bConsistentHash)
AdapterProxy* EndpointManager::getHashProxy(int64_t hashCode, bool bConsistentHash, bool onlyCheck)
{
if(_weightType == E_STATIC_WEIGHT)
{
if(bConsistentHash)
{
return getConHashProxyForWeight(hashCode, true);
return getConHashProxyForWeight(hashCode, true, onlyCheck);
}
else
{
return getHashProxyForWeight(hashCode, true, _hashStaticRouterCache);
return getHashProxyForWeight(hashCode, true, _hashStaticRouterCache, onlyCheck);
}
}
else
{
if(bConsistentHash)
{
return getConHashProxyForNormal(hashCode);
return getConHashProxyForNormal(hashCode, onlyCheck);
}
else
{
return getHashProxyForNormal(hashCode);
return getHashProxyForNormal(hashCode, onlyCheck);
}
}
}
AdapterProxy* EndpointManager::getHashProxyForWeight(int64_t hashCode, bool bStatic, vector<size_t> &vRouterCache)
AdapterProxy* EndpointManager::getHashProxyForWeight(int64_t hashCode, bool bStatic, vector<size_t> &vRouterCache, bool onlyCheck)
{
if(_vRegProxys.empty())
{
@ -965,7 +972,7 @@ AdapterProxy* EndpointManager::getHashProxyForWeight(int64_t hashCode, bool bSta
hash = hash % thisHash.size();
}
if (thisHash[hash]->checkActive())
if (thisHash[hash]->checkActive(false, onlyCheck))
{
return thisHash[hash];
}
@ -978,38 +985,40 @@ AdapterProxy* EndpointManager::getHashProxyForWeight(int64_t hashCode, bool bSta
}
while(!thisHash.empty());
if(conn.size() > 0)
{
hash = ((int64_t)hashCode) % conn.size();
if(!onlyCheck) {
if(conn.size() > 0)
{
hash = ((int64_t)hashCode) % conn.size();
//这里做判断的原因是32位系统下如果hashCode为负值hash经过上面的计算会是一个超大值导致越界
if(hash >= conn.size())
{
hash = hash % conn.size();
}
//这里做判断的原因是32位系统下如果hashCode为负值hash经过上面的计算会是一个超大值导致越界
if(hash >= conn.size())
{
hash = hash % conn.size();
}
//都有问题, 随机选择一个没有connect超时或者链接异常的发送
AdapterProxy *adapterProxy = conn[hash];
//都有问题, 随机选择一个没有connect超时或者链接异常的发送
AdapterProxy *adapterProxy = conn[hash];
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true);
return adapterProxy;
}
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true, false);
return adapterProxy;
}
//所有adapter都有问题 选不到结点,随机找一个重试
AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true);
//所有adapter都有问题 选不到结点,随机找一个重试
AdapterProxy * adapterProxy = _activeProxys[((uint32_t)rand() % _activeProxys.size())];
//该proxy可能已经被屏蔽,需重新连一次
adapterProxy->checkActive(true, false);
}
return NULL;
}
}
return getHashProxyForNormal(hashCode);
return getHashProxyForNormal(hashCode, onlyCheck);
}
AdapterProxy* EndpointManager::getConHashProxyForWeight(int64_t hashCode, bool bStatic)
AdapterProxy* EndpointManager::getConHashProxyForWeight(int64_t hashCode, bool bStatic, bool onlyCheck)
{
if(_vRegProxys.empty())
{
@ -1107,7 +1116,7 @@ AdapterProxy* EndpointManager::getConHashProxyForWeight(int64_t hashCode, bool b
}
}
return getHashProxyForNormal(hashCode);
return getHashProxyForNormal(hashCode, onlyCheck);
}
bool EndpointManager::checkHashStaticWeightChange(bool bStatic)
@ -1312,7 +1321,7 @@ void EndpointManager::updateConHashProxyWeighted(bool bStatic, vector<AdapterPro
conHash.sortNode();
}
AdapterProxy* EndpointManager::getHashProxyForNormal(int64_t hashCode)
AdapterProxy* EndpointManager::getHashProxyForNormal(int64_t hashCode, bool onlyCheck)
{
if(_vRegProxys.empty())
{
@ -1398,7 +1407,7 @@ AdapterProxy* EndpointManager::getHashProxyForNormal(int64_t hashCode)
}
}
AdapterProxy* EndpointManager::getConHashProxyForNormal(int64_t hashCode)
AdapterProxy* EndpointManager::getConHashProxyForNormal(int64_t hashCode, bool onlyCheck)
{
if(_vRegProxys.empty())
{
@ -1497,15 +1506,15 @@ AdapterProxy* EndpointManager::getConHashProxyForNormal(int64_t hashCode)
}
}
return getHashProxyForNormal(hashCode);
return getHashProxyForNormal(hashCode, onlyCheck);
}
AdapterProxy* EndpointManager::getWeightedProxy(bool bStaticWeighted)
AdapterProxy* EndpointManager::getWeightedProxy(bool bStaticWeighted, bool onlyCheck)
{
return getWeightedForNormal(bStaticWeighted);
return getWeightedForNormal(bStaticWeighted, onlyCheck);
}
AdapterProxy* EndpointManager::getWeightedForNormal(bool bStaticWeighted)
AdapterProxy* EndpointManager::getWeightedForNormal(bool bStaticWeighted, bool onlyCheck)
{
if (_activeProxys.empty())
{

View File

@ -67,6 +67,16 @@ ObjectProxy::~ObjectProxy()
void ObjectProxy::initialize()
{
}
//
//ServantProxy * ObjectProxy::getServantProxy()
//{
// return _pServantProxy;
//}
//
//void ObjectProxy::setServantProxy(ServantProxy * pServantProxy)
//{
// _pServantProxy = pServantProxy;
//}
int ObjectProxy::loadLocator()
{
@ -92,6 +102,22 @@ int ObjectProxy::loadLocator()
return 0;
}
void ObjectProxy::setPushCallbacks(const ServantProxyCallbackPtr& cb)
{
_pushCallback = cb;
}
ServantProxyCallbackPtr ObjectProxy::getPushCallback()
{
return _pushCallback;
}
//
//const string& ObjectProxy::name() const
//{
// return _name;
//}
void ObjectProxy::setProxyProtocol(const ProxyProtocol& protocol)
{
if(_hasSetProtocol)
@ -108,7 +134,8 @@ ProxyProtocol& ObjectProxy::getProxyProtocol()
return _proxyProtocol;
}
void ObjectProxy::setSocketOpt(int level, int optname, const void *optval, socklen_t optlen)
void ObjectProxy::setSocketOpt(int level, int optname, const void *optval, SOCKET_LEN_TYPE optlen)
{
SocketOpt socketOpt;
@ -124,16 +151,24 @@ vector<SocketOpt>& ObjectProxy::getSocketOpt()
{
return _socketOpts;
}
void ObjectProxy::setPushCallbacks(const ServantProxyCallbackPtr& cb)
{
_pushCallback = cb;
}
ServantProxyCallbackPtr ObjectProxy::getPushCallback()
{
return _pushCallback;
}
//
//bool ObjectProxy::invoke_sync(ReqMessage * msg)
//{
// TLOGTAF("[TAF][ObjectProxy::invoke_sync, " << _name << ", begin]" << endl);
//
// //选择一个远程服务的Adapter来调用
// AdapterProxy * pAdapterProxy = NULL;
// //选一个活的
// _endpointManger->selectAdapterProxy(msg, pAdapterProxy, true);
//
// if(!pAdapterProxy)
// {
// return false;
// }
//
// msg->adapter = pAdapterProxy;
// return pAdapterProxy->invoke_sync(msg);
//}
void ObjectProxy::invoke(ReqMessage * msg)
{
@ -141,7 +176,7 @@ void ObjectProxy::invoke(ReqMessage * msg)
//选择一个远程服务的Adapter来调用
AdapterProxy * pAdapterProxy = NULL;
bool bFirst = _endpointManger->selectAdapterProxy(msg, pAdapterProxy);
bool bFirst = _endpointManger->selectAdapterProxy(msg, pAdapterProxy, false);
if(bFirst)
{
@ -167,8 +202,6 @@ void ObjectProxy::invoke(ReqMessage * msg)
return ;
}
// pAdapterProxy = pAdapterProxy->clone();
msg->adapter = pAdapterProxy;
pAdapterProxy->invoke(msg);
}
@ -188,7 +221,7 @@ void ObjectProxy::doInvoke()
//选择一个远程服务的Adapter来调用
AdapterProxy * pAdapterProxy = NULL;
_endpointManger->selectAdapterProxy(msg,pAdapterProxy);
_endpointManger->selectAdapterProxy(msg,pAdapterProxy, false);
if(!pAdapterProxy)
{
@ -202,8 +235,6 @@ void ObjectProxy::doInvoke()
return;
}
// pAdapterProxy = pAdapterProxy->clone();
msg->adapter = pAdapterProxy;
pAdapterProxy->invoke(msg);
@ -293,7 +324,7 @@ void ObjectProxy::doInvokeException(ReqMessage * msg)
void ObjectProxy::doTimeout()
{
// TLOGTARS("[TARS][ObjectProxy::doInvokeException, objname:" << _name << "]" << endl);
const vector<AdapterProxy*> & vAdapterProxy = _endpointManger->getAdapters();
for(size_t iAdapter=0; iAdapter< vAdapterProxy.size();++iAdapter)
{

View File

@ -23,6 +23,7 @@ ObjectProxyFactory::ObjectProxyFactory(CommunicatorEpoll * pCommunicatorEpoll)
: _communicatorEpoll(pCommunicatorEpoll)
, _objNum(0)
{
// memset(_vpObjectProxys,0,sizeof(_vpObjectProxys));
}
ObjectProxyFactory::~ObjectProxyFactory()

View File

@ -26,8 +26,6 @@ namespace tars
{
thread_local shared_ptr<CallbackThreadData> CallbackThreadData::g_sp;
// TC_ThreadMutex CallbackThreadData::_mutex;
// pthread_key_t CallbackThreadData::_key = 0;
Servant::Servant():_handle(NULL)
{
@ -92,7 +90,7 @@ int Servant::dispatch(TarsCurrentPtr current, vector<char> &buffer)
return ret;
}
TC_ThreadQueue<ReqMessagePtr>& Servant::getResponseQueue()
TC_CasQueue<ReqMessagePtr>& Servant::getResponseQueue()
{
return _asyncResponseQueue;
}
@ -143,35 +141,6 @@ CallbackThreadData * CallbackThreadData::getData()
g_sp.reset(new CallbackThreadData());
}
return g_sp.get();
// if(_key == 0)
// {
// TC_LockT<TC_ThreadMutex> lock(_mutex);
// if(_key == 0)
// {
// int iRet = ::pthread_key_create(&_key, CallbackThreadData::destructor);
// if (iRet != 0)
// {
// TLOGERROR("[TARS][CallbackThreadData pthread_key_create fail:" << errno << ":" << strerror(errno) << "]" << endl);
// return NULL;
// }
// }
// }
// CallbackThreadData * pCbtd = (CallbackThreadData*)pthread_getspecific(_key);
// if(!pCbtd)
// {
// TC_LockT<TC_ThreadMutex> lock(_mutex);
// pCbtd = new CallbackThreadData();
// int iRet = pthread_setspecific(_key, (void *)pCbtd);
// assert(iRet == 0);
// }
// return pCbtd;
}

View File

@ -39,7 +39,7 @@ ServantHandle::ServantHandle()
ServantHandle::~ServantHandle()
{
map<string, ServantPtr>::iterator it = _servants.begin();
auto it = _servants.begin();
while(it != _servants.end())
{
@ -71,17 +71,19 @@ void ServantHandle::run()
{
initialize();
if (!ServerConfig::OpenCoroutine) {
handleImp();
}
else {
unsigned int iThreadNum = getEpollServer()->getLogicThreadNum();
if (!ServerConfig::OpenCoroutine)
{
handleImp();
}
else
{
//by goodenpei, 判断是否启用顺序模式
_bindAdapter->initThreadRecvQueue(getHandleIndex());
size_t iThreadNum = getEpollServer()->getLogicThreadNum();
size_t iCoroutineNum =
(ServerConfig::CoroutineMemSize > ServerConfig::CoroutineStackSize) ? (ServerConfig::CoroutineMemSize
/ (ServerConfig::CoroutineStackSize * iThreadNum)) : 1;
if (iCoroutineNum < 1)
iCoroutineNum = 1;
size_t iCoroutineNum = (ServerConfig::CoroutineMemSize > ServerConfig::CoroutineStackSize) ? (ServerConfig::CoroutineMemSize / (ServerConfig::CoroutineStackSize * iThreadNum)) : 1;
if (iCoroutineNum < 1) iCoroutineNum = 1;
startHandle();
@ -97,7 +99,8 @@ void ServantHandle::run()
pSptd->_sched = _coroSched;
while (!getEpollServer()->isTerminate()) {
while (!getEpollServer()->isTerminate())
{
_coroSched->tars_run();
}
@ -215,7 +218,6 @@ void ServantHandle::handleRequest()
}
}
// void ServantHandle::handleRecvData(TC_EpollServer::tagRecvData *stRecvData)
void ServantHandle::handleRecvData(const shared_ptr<TC_EpollServer::RecvContext> &data)
{
try
@ -250,11 +252,11 @@ void ServantHandle::handleAsyncResponse()
{
ReqMessagePtr resp;
map<string, ServantPtr>::iterator it = _servants.begin();
auto it = _servants.begin();
while (it != _servants.end())
{
while (it->second->getResponseQueue().pop_front(resp, 0))
while (it->second->getResponseQueue().pop_front(resp))
{
try
{
@ -302,7 +304,7 @@ void ServantHandle::handleAsyncResponse()
void ServantHandle::handleCustomMessage(bool bExpectIdle)
{
for (map<string, ServantPtr>::iterator it = _servants.begin(); it != _servants.end(); it++)
for (auto it = _servants.begin(); it != _servants.end(); it++)
{
//业务处理附加的自有消息
try
@ -323,7 +325,7 @@ void ServantHandle::handleCustomMessage(bool bExpectIdle)
bool ServantHandle::allFilterIsEmpty()
{
map<string, ServantPtr>::iterator it = _servants.begin();
auto it = _servants.begin();
while (it != _servants.end())
{
@ -351,7 +353,7 @@ void ServantHandle::initialize()
exit(-1);
}
map<string, ServantPtr>::iterator it = _servants.begin();
auto it = _servants.begin();
if(it == _servants.end())
{
@ -392,38 +394,6 @@ void ServantHandle::initialize()
}
}
// void ServantHandle::heartbeat()
// {
// time_t fcur = TNOW;
// map<string, TC_EpollServer::BindAdapterPtr>::iterator it;
// map<string, TC_EpollServer::BindAdapterPtr>& adapters = _handleGroup->adapters;
// for (it = adapters.begin(); it != adapters.end(); ++it)
// {
// if (abs(fcur - it->second->getHeartBeatTime()) > HEART_BEAT_INTERVAL)
// {
// it->second->setHeartBeatTime(fcur);
// TARS_KEEPALIVE(it->second->getName());
// //上报连接数 比率
// if (it->second->_pReportConRate)
// {
// it->second->_pReportConRate->report(it->second->getNowConnection()*1000/it->second->getMaxConns());
// }
// //有队列, 且队列长度>0才上报
// if (it->second->_pReportQueue)
// {
// it->second->_pReportQueue->report(it->second->getRecvBufferSize());
// }
// }
// }
// }
void ServantHandle::heartbeat()
{
time_t fcur = TNOW;
@ -509,7 +479,7 @@ void ServantHandle::handleClose(const shared_ptr<TC_EpollServer::RecvContext> &d
TarsCurrentPtr current = createCloseCurrent(data);
map<string, ServantPtr>::iterator sit = _servants.find(current->getServantName());
auto sit = _servants.find(current->getServantName());
if (sit == _servants.end())
{
@ -830,7 +800,7 @@ void ServantHandle::handleTarsProtocol(const TarsCurrentPtr &current)
//处理tracking信息
processTracking(current);
#endif
map<string, ServantPtr>::iterator sit = _servants.find(current->getServantName());
auto sit = _servants.find(current->getServantName());
if (sit == _servants.end())
{
@ -901,7 +871,7 @@ void ServantHandle::handleNoTarsProtocol(const TarsCurrentPtr &current)
<< current->getPort() << "|"
<< current->getServantName() << endl);
map<string, ServantPtr>::iterator sit = _servants.find(current->getServantName());
auto sit = _servants.find(current->getServantName());
assert(sit != _servants.end());

View File

@ -233,6 +233,7 @@ string ServantProxy::TARS_MASTER_KEY = "TARS_MASTER_KEY";
string ServantProxy::STATUS_TRACK_KEY = "STATUS_TRACK_KEY";
////////////////////////////////////
ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObjectProxy, size_t iClientThreadNum)
: _communicator(pCommunicator)
, _objectProxy(ppObjectProxy)
@ -256,34 +257,17 @@ ServantProxy::ServantProxy(Communicator * pCommunicator, ObjectProxy ** ppObject
{
_minTimeout = 1;
}
// get AK/SK
// const TC_Config& conf = Application::getConfig();
// vector<string> adapterNames;
//
// cout << "accessKey:" << conf.get("/tars/application/client/TestApp.AuthServer.AuthObj<accessKey>") << ", obj:" << tars_name() << endl;
//
// if (conf.getDomainVector("/tars/application/client", adapterNames))
// {
// cout << TC_Common::tostr(adapterNames.begin(), adapterNames.end()) << ", " << tars_name() << endl;
// auto it = std::find(adapterNames.begin(), adapterNames.end(), tars_name());
// if (it != adapterNames.end())
// {
// string accessKey = conf.get("/tars/application/client/" + *it + "<accesskey>");
// string secretKey = conf.get("/tars/application/client/" + *it + "<secretkey>");
//
// cout << "accessKey:" << accessKey << ", secretKey:" << secretKey << endl;
// for(size_t i = 0;i < _objectProxyNum; ++i)
// {
// _objectProxy[i]->setAccessKey(pCommunicator->getServantProperty(tars_name(), "accesskey"));
// _objectProxy[i]->setSecretKey(pCommunicator->getServantProperty(tars_name(), "secretkey"));
// }
// }
// }
}
ServantProxy::~ServantProxy()
{
// if (_endpointInfo)
// {
// delete _endpointInfo;
// _endpointInfo = NULL;
// }
if(_objectProxy)
{
//set _objectProxy to NULL
@ -374,7 +358,7 @@ void ServantProxy::tars_set_protocol(const ProxyProtocol& protocol)
}
void ServantProxy::tars_set_sockopt(int level, int optname, const void * optval, socklen_t optlen)
void ServantProxy::tars_set_sockopt(int level, int optname, const void * optval, SOCKET_LEN_TYPE optlen)
{
TC_LockT<TC_ThreadMutex> lock(*this);
@ -450,6 +434,14 @@ ServantProxy* ServantProxy::tars_hash(int64_t key)
return this;
}
//ServantProxy* ServantProxy::tars_direct()
//{
// ServantProxyThreadData *pSptd = ServantProxyThreadData::getData();
//
// pSptd->_direct = true;
// return this;
//}
ServantProxy* ServantProxy::tars_consistent_hash(int64_t key)
{
ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
@ -496,6 +488,55 @@ void ServantProxy::tars_set_push_callback(const ServantProxyCallbackPtr & cb)
}
}
//
//void ServantProxy::invoke_async(ReqMessage *msg, ServantProxyThreadData *pSptd, ReqInfoQueue *pReqQ, bool bCoroAsync)
//{
//// assert(ReqMessage::ASYNC_CALL == msg->eType);
//
// if (bCoroAsync)
// {
// if (pSptd->_sched)
// {
// CoroParallelBasePtr coroPtr = msg->callback->getCoroParallelBasePtr();
// if (coroPtr)
// {
// coroPtr->incReqCount();
//
// msg->bCoroFlag = true;
// msg->sched = pSptd->_sched;
// msg->iCoroId = pSptd->_sched->getCoroutineId();
// }
// else
// {
// TLOGERROR("[TAF][ServantProxy::invoke_async use coroutine's callback not set CoroParallelBasePtr]" << endl);
// delete msg;
// msg = NULL;
// throw TarsUseCoroException("ServantProxy::invoke_async use coroutine's callback not set CoroParallelBasePtr");
// }
// }
// else
// {
// TLOGERROR("[TAF][ServantProxy::invoke coroutine mode invoke not open]" << endl);
// delete msg;
// msg = NULL;
// throw TarsUseCoroException("coroutine mode invoke not open");
// }
// }
//
// //通知网络线程
// bool bEmpty;
// if (!pReqQ->push_back(msg, bEmpty))
// {
// TLOGERROR("[TAF][ServantProxy::invoke_async msgQueue push_back error num:" << pSptd->_netSeq << "]" << endl);
// msg->pObjectProxy->getCommunicatorEpoll()->notify(pSptd->_reqQNo, pReqQ);
// delete msg;
// throw TarsClientQueueException("client queue full");
// }
//
// msg->pObjectProxy->getCommunicatorEpoll()->notify(pSptd->_reqQNo, pReqQ);
//
//}
void ServantProxy::invoke(ReqMessage * msg, bool bCoroAsync)
{
msg->proxy = this;
@ -673,10 +714,7 @@ void ServantProxy::invoke(ReqMessage * msg, bool bCoroAsync)
os << ",servant:" << msg->pObjectProxy->name() << ",func:" << msg->request.sFuncName;
if(msg->adapter)
{
os << ",adapter:" << msg->adapter->endpoint().desc();
}
if (msg->adapter) os << ",adapter:" << msg->adapter->endpoint().desc();
os << ",reqid:" << msg->request.iRequestId << "]";
@ -699,16 +737,7 @@ void ServantProxy::invoke(ReqMessage * msg, bool bCoroAsync)
}
}
//同步调用返回,唤醒等待的业务线程
void ServantProxy::finished(ReqMessage * msg)
{
if(msg->pMonitor)
{
TC_ThreadLock::Lock sync(*(msg->pMonitor));
msg->pMonitor->notify();
msg->bMonitorFin = true;
}
}
//////////////////////////////////////////////////////////////////
void ServantProxy::tars_invoke_async(char cPacketType,
@ -721,16 +750,16 @@ void ServantProxy::tars_invoke_async(char cPacketType,
{
ReqMessage * msg = new ReqMessage();
msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY,NULL,sFuncName);
msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY);
msg->callback = callback;
msg->request.iVersion = TARSVERSION;
msg->request.cPacketType = (callback ? cPacketType : TARSONEWAY);
msg->request.sFuncName = sFuncName;
msg->request.sServantName = (*_objectProxy)->name();
msg->request.sFuncName = sFuncName;
buf.swap(msg->request.sBuffer);
// msg->request.sBuffer = buf;
msg->request.context = context;
msg->request.status = status;
msg->request.iTimeout = _asyncTimeout;
@ -755,15 +784,14 @@ shared_ptr<ResponsePacket> ServantProxy::tars_invoke(char cPacketType,
{
ReqMessage * msg = new ReqMessage();
msg->init(ReqMessage::SYNC_CALL,NULL,sFuncName);
msg->init(ReqMessage::SYNC_CALL);
msg->request.iVersion = TARSVERSION;
msg->request.cPacketType = cPacketType;
msg->request.sFuncName = sFuncName;
msg->request.sServantName = (*_objectProxy)->name();
msg->request.sFuncName = sFuncName;
buf.swap(msg->request.sBuffer);
// msg->request.sBuffer = buf;
msg->request.context = context;
msg->request.status = status;
msg->request.iTimeout = _syncTimeout;
@ -798,11 +826,12 @@ void ServantProxy::rpc_call(uint32_t iRequestId,
{
ReqMessage * msg = new ReqMessage();
msg->init(ReqMessage::SYNC_CALL,NULL,sFuncName);
msg->init(ReqMessage::SYNC_CALL);
msg->bFromRpc = true;
msg->request.sFuncName = sFuncName;
msg->request.iRequestId = iRequestId;
msg->request.sFuncName = sFuncName;
msg->request.sBuffer.assign(buff, buff + len);
invoke(msg);
@ -822,48 +851,17 @@ void ServantProxy::rpc_call_async(uint32_t iRequestId,
{
ReqMessage * msg = new ReqMessage();
msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY,NULL,sFuncName);
msg->init(callback?ReqMessage::ASYNC_CALL:ReqMessage::ONE_WAY);
msg->request.sFuncName = sFuncName;
msg->bFromRpc = true;
msg->callback = callback;
msg->request.iRequestId = iRequestId;
msg->request.sFuncName = sFuncName;
msg->request.sBuffer.assign(buff, buff + len);
invoke(msg, bCoro);
}
//
//
//void ServantProxy::http1_call(const std::string& method,
// const std::string& uri,
// const std::map<std::string, std::string>& headers,
// const std::string& body,
// std::map<std::string, std::string>& rheaders,
// std::string& rbody)
//{
// ReqMessage* msg = new ReqMessage();
//
// msg->init(ReqMessage::SYNC_CALL, NULL, "");
//
// msg->bFromRpc = true;
// msg->request.sServantName = uri;
// msg->request.sFuncName = method;
// msg->request.iRequestId = this->tars_gen_requestid();
//
// // 使用下面两个字段保存头部和包体
// msg->request.context = headers;
//
// msg->request.sBuffer.assign(body.begin(), body.end());
//
// invoke(msg);
//
// rheaders.swap(msg->response->status);
// rbody.assign(msg->response->sBuffer.begin(), msg->response->sBuffer.end());
//
// delete msg;
// msg = NULL;
//}
void ServantProxy::http_call(const std::string& method,
const std::string& uri,
@ -874,7 +872,7 @@ void ServantProxy::http_call(const std::string& method,
{
ReqMessage* msg = new ReqMessage();
msg->init(ReqMessage::SYNC_CALL, NULL, "");
msg->init(ReqMessage::SYNC_CALL);
msg->bFromRpc = true;
msg->request.sServantName = uri;
@ -901,7 +899,7 @@ void ServantProxy::http_call_async(const std::string& method,
{
ReqMessage * msg = new ReqMessage();
msg->init(ReqMessage::ASYNC_CALL, NULL, "");
msg->init(ReqMessage::ASYNC_CALL);
msg->bFromRpc = true;
msg->request.sServantName = uri;

View File

@ -439,8 +439,7 @@ void StatReport::doSample(const string& strSlaveName,
int StatReport::reportMicMsg(MapStatMicMsg& msg,bool bFromClient)
{
if(msg.empty())
return 0;
if (msg.empty()) return 0;
try
{
int iLen = 0;
@ -759,6 +758,16 @@ void StatReport::run()
{
while(!_terminate)
{
{
Lock lock(*this);
if (_terminate)
return;
timedWait(1000);
}
try
{
time_t tNow = TNOW;
@ -804,9 +813,6 @@ void StatReport::run()
_time = tNow;
}
Lock lock(*this);
timedWait(1000);
}
catch ( exception& e )
{

View File

@ -48,6 +48,8 @@ Transceiver::Transceiver(AdapterProxy * pAdapterProxy,const EndpointInfo &ep)
Transceiver::~Transceiver()
{
close();
}
void Transceiver::checkTimeout()
@ -63,7 +65,7 @@ void Transceiver::checkTimeout()
bool Transceiver::isSSL() const
{
return _adapterProxy->endpoint().type() == EndpointInfo::SSL;
return _adapterProxy->endpoint().type() == TC_Endpoint::SSL;
}
void Transceiver::reconnect()
@ -90,7 +92,7 @@ void Transceiver::connect()
//每次连接前都重新解析一下地址, 避免dns变了!
_ep.parseAddress();
if (_ep.type() == EndpointInfo::UDP)
if (_ep.type() == TC_Endpoint::UDP)
{
fd = NetworkUtil::createSocket(true, false, _ep.isIPv6());
_connStatus = eConnected;
@ -202,7 +204,7 @@ void Transceiver::doAuthReq()
{
ObjectProxy* obj = _adapterProxy->getObjProxy();
TLOGTARS("[TARS][Transceiver::doAuthReq obj:" << obj->name() << ", auth type:" << etos(_adapterProxy->endpoint().authType()) << endl);
TLOGTARS("[TARS][Transceiver::doAuthReq obj:" << obj->name() << ", auth type:" << etos((AUTH_TYPE)_adapterProxy->endpoint().authType()) << endl);
if (_adapterProxy->endpoint().authType() == AUTH_TYPENONE)
{
@ -220,6 +222,102 @@ void Transceiver::doAuthReq()
}
}
//
//void Transceiver::sendRecv(ReqMessage *msg)//const shared_ptr<TC_NetWorkBuffer::SendBuffer> &sbuff, BasePacket &rsp, int timeout)
//{
// if(_syncSock == NULL)
// {
// if(_ep.isTcp())
// {
// _syncSock = new TC_TCPClient(_ep.host(), _ep.port(), msg->request.iTimeout);
// }
// else
// {
// _syncSock = new TC_UDPClient(_ep.host(), _ep.port(), msg->request.iTimeout);
// }
// }
//
// int iRet = _syncSock->send(msg->sReqData->buffer(), (uint32_t) msg->sReqData->length());
// if (iRet < 0)
// {
// msg->eStatus = ReqMessage::REQ_NET;
// msg->response->sResultDesc = "send failed ret:" + TC_Common::tostr(iRet);
// return;
// }
//
//// cout << _syncSock->getSocket()->getSendBufferSize() << ", " << _syncSock->getSocket()->getRecvBufferSize() << endl;
//
// TC_NetWorkBuffer recvBuffer(NULL);
//
// do
// {
// try
// {
// size_t iHeaderLen = BUFFER_SIZE;
// // char rbuff[BUFFER_SIZE] = {0x00};
// vector<char> rbuff(iHeaderLen);
//
// iRet = _syncSock->recv(rbuff.data(), iHeaderLen);
// if(iRet < 0)
// {
// if (iRet == TC_ClientSocket::EM_TIMEOUT)
// {
// msg->eStatus = ReqMessage::REQ_TIME;
// msg->response->sResultDesc = "recv info timeout, ret:" + TC_Common::tostr(iRet);
// }
// else
// {
// msg->eStatus = ReqMessage::REQ_NET;
// msg->response->sResultDesc = "recv body failed, ret:" + TC_Common::tostr(iRet);
// }
// break;
// }
//
// // recvBuffer.addBuffer(rbuff, iHeaderLen);
// rbuff.resize(iHeaderLen);
// recvBuffer.addSwapBuffer(rbuff);
//
// TC_NetWorkBuffer::PACKET_TYPE ret = _pAdapterProxy->getObjProxy()->getProxyProtocol().responseFunc(recvBuffer, *msg->response.get());
//
// if (ret == TC_NetWorkBuffer::PACKET_ERR)
// {
// TLOGERROR("[TAF][tcp doResponse," << _pAdapterProxy->getObjProxy()->name() << ",fd:" << _iFd << "," << _ep.desc() << ",tcp recv decode error" << endl);
// msg->eStatus = ReqMessage::REQ_NET;
// msg->response->sResultDesc = "recv packet decode failed";
//
// }
// else if (ret == TC_NetWorkBuffer::PACKET_FULL) {
// msg->eStatus = ReqMessage::REQ_RSP;
// }
// else {
// continue;
// }
// }
// catch (exception & ex) {
// TLOGERROR(
// "[TAF][tcp doResponse," << _pAdapterProxy->getObjProxy()->name() << ",fd:" << _iFd << "," << _ep.desc() << ",tcp recv decode error:" << ex.what() << endl);
// msg->eStatus = ReqMessage::REQ_NET;
// msg->response->sResultDesc = "recv packet decode failed";
//
// assert(msg->request.iRequestId == msg->response->iRequestId);
// }
// catch (...) {
// TLOGERROR(
// "[TAF][tcp doResponse," << _pAdapterProxy->getObjProxy()->name() << ",fd:" << _iFd << "," << _ep.desc() << ",tcp recv decode error." << endl);
// msg->eStatus = ReqMessage::REQ_NET;
// msg->response->sResultDesc = "recv packet decode failed";
// }
//
// break;
// }while(true);
//
// if(msg->eStatus != ReqMessage::REQ_RSP)
// {
// _syncSock->close();
// }
//}
void Transceiver::finishInvoke(shared_ptr<ResponsePacket> &rsp)
{
if (_authState != AUTH_SUCC)
@ -303,7 +401,10 @@ bool Transceiver::sendAuthData(const BasicAuthInfo& info)
void Transceiver::close()
{
if(!isValid()) return;
// if(_adapterProxy->getObjProxy()->getPushCallback())
// {
// _adapterProxy->getObjProxy()->getPushCallback()->onClose();
// }
#if TARS_SSL
if (_openssl)
{
@ -442,11 +543,6 @@ int Transceiver::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> &buff)
return eRetError;
}
static std::atomic<int> totalSend{0};
totalSend += iRet;
// cout << "totalSend:" << totalSend << endl;
//没有全部发送完,写buffer 返回成功
if(iRet < (int)buff->length())
{
@ -456,34 +552,6 @@ int Transceiver::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> &buff)
}
return eRetOk;
// #if TARS_SSL
// // 握手数据已加密,直接发送,会话数据需加密
// std::string out;
// if (isSSL() && _openssl->isHandshaked())
// {
// out = _openssl->Write(pData, iSize);
// pData = out.data();
// iSize = out.size();
// }
// #endif
// int iRet = this->send(pData,iSize,0);
// //失败,直接返回
// if(iRet < 0)
// {
// return eRetError;
// }
// //没有全部发送完,写buffer 返回成功
// if(iRet < (int)iSize)
// {
// _sendBuffer.PushData(pData+iRet,iSize-iRet);
// return eRetFull;
// }
// return eRetOk;
}
//////////////////////////////////////////////////////////
@ -500,18 +568,12 @@ int TcpTransceiver::doResponse()
int iRet = 0;
int recvCount = 0;
static std::atomic<int> totalRecv{0};
do
{
char buff[BUFFER_SIZE] = {0x00};
if ((iRet = this->recv(buff, BUFFER_SIZE, 0)) > 0)
{
totalRecv += iRet;
// cout << "totalRecv:" << totalRecv << endl;
TC_NetWorkBuffer *rbuf = &_recvBuffer;
#if TARS_SSL
@ -571,7 +633,9 @@ int TcpTransceiver::doResponse()
else {
break;
}
}while(true);
}
while (ret == TC_NetWorkBuffer::PACKET_FULL);
//接收的数据小于buffer大小, 内核会再次通知你
if(iRet < BUFFER_SIZE)
@ -601,7 +665,7 @@ int TcpTransceiver::doResponse()
}
while (iRet>0);
TLOGTARS("[TARS][tcp doResponse, " << _adapterProxy->getObjProxy()->name() << ",fd:" << _fd << ", all recvbuf:" << _recvBuffer.getBufferLength() << "]" << endl);
// TLOGTARS("[TARS][tcp doResponse, " << _adapterProxy->getObjProxy()->name() << ",fd:" << _fd << ", all recvbuf:" << _recvBuffer.getBufferLength() << "]" << endl);
return 0;
}

@ -1 +1 @@
Subproject commit 5f8fc7a0ed15dd784c0d41cf89a465656d7a23e0
Subproject commit 7c22d46777d76c8c08c2161f7cb4d9f0f45991d7

View File

@ -50,12 +50,6 @@ public:
*/
~AdapterProxy();
/**
* clone
* @return
*/
// AdapterProxy *clone();
/**
* server端对象方法
*/
@ -78,7 +72,7 @@ public:
* @onlyCheck:
* @return bool
*/
bool checkActive(bool bForceConnect = false);
bool checkActive(bool bForceConnect = false, bool onlyCheck = false);
/**
*

View File

@ -24,7 +24,6 @@
#include "tup/RequestF.h"
#include "tup/tup.h"
#include "servant/BaseF.h"
#include "util/tc_epoll_server.h"
#include "util/tc_network_buffer.h"
using namespace std;
@ -50,6 +49,9 @@ T net2host(T len)
assert(true);
return 0;
}
class Transceiver;
//////////////////////////////////////////////////////////////////////
/**
*
@ -347,7 +349,108 @@ public:
return TC_NetWorkBuffer::PACKET_FULL;
}
/**
* wup响应包(wup的响应会放在ResponsePacket的buffer中)
* @param request
* @param buff
*/
static TC_NetWorkBuffer::PACKET_TYPE jsonResponse(TC_NetWorkBuffer &in, ResponsePacket& done)
{
return jsonResponseLen<TARS_NET_MIN_PACKAGE_SIZE, TARS_NET_MAX_PACKAGE_SIZE>(in, done);
}
template<uint32_t iMinLength, uint32_t iMaxLength>
static TC_NetWorkBuffer::PACKET_TYPE jsonResponseLen(TC_NetWorkBuffer &in, ResponsePacket& rsp)
{
uint32_t len = (uint32_t)in.getBufferLength();
//收到的字节数太少, 还需要继续接收
if (len < sizeof(uint32_t))
return TC_NetWorkBuffer::PACKET_LESS;
//获取包总体长度
uint32_t iHeaderLen = in.getValueOf4();
//做一下保护,长度大于10M
if (iHeaderLen < iMinLength || iHeaderLen > iMaxLength)
{
throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
}
//包没有接收全
if (len < iHeaderLen)
{
//看看包头是否正确
static const uint32_t head = 20;
if (len >= head)
{
string buffer;
in.getHeader(head, buffer);
TarsInputStream<BufferReader> is;
is.setBuffer(buffer.c_str() + sizeof(tars::Int32), head);
is.read(rsp.iVersion, 1, false);
if (rsp.iVersion != JSONVERSION)
{
throw TarsDecodeException("json version not correct, version:" + TC_Common::tostr(rsp.iVersion));
}
is.read(rsp.cPacketType, 2, false);
if (rsp.cPacketType != TARSNORMAL)
{
throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)rsp.cPacketType));
}
is.read(rsp.iMessageType, 3, false);
is.read(rsp.iRequestId, 4, false);
is.read(rsp.iRet, 5, false);
if (rsp.iRet < TARSSERVERUNKNOWNERR)
{
throw TarsDecodeException("response value not correct, value:" + TC_Common::tostr(rsp.iRet));
}
}
return TC_NetWorkBuffer::PACKET_LESS;
}
else
{
vector<char> buffer;
bool ret = in.parseBufferOf4(buffer, iMinLength, iMaxLength);
if (!ret)
{
throw TarsDecodeException("parse buffer exception");
}
TarsInputStream<BufferReader> is;
is.setBuffer(buffer.data(), buffer.size());
rsp.readFrom(is);
if (rsp.iVersion != JSONVERSION)
{
throw TarsDecodeException("json version not correct, version:" + TC_Common::tostr(rsp.iVersion));
}
if (rsp.cPacketType != TARSNORMAL)
{
throw TarsDecodeException("packettype not correct, packettype:" + TC_Common::tostr((int)rsp.cPacketType));
}
if (rsp.iRet < TARSSERVERUNKNOWNERR)
{
throw TarsDecodeException("response value not correct, value:" + TC_Common::tostr(rsp.iRet));
}
}
return TC_NetWorkBuffer::PACKET_FULL;
}
public:
/**
* tars请求包

View File

@ -23,8 +23,8 @@
#include "util/tc_autoptr.h"
#include "util/tc_config.h"
#include "util/tc_epoll_server.h"
#include "util/tc_option.h"
#include "util/tc_epoll_server.h"
#include "servant/BaseNotify.h"
#include "servant/ServantHelper.h"
#include "servant/ServantHandle.h"
@ -143,13 +143,6 @@ class PropertyReport;
*/
class Application : public BaseNotify
{
public:
enum
{
REPORT_SEND_QUEUE_INTERVAL = 10, /**上报服务端发送队列大小的间隔时间**/
};
public:
/**
*
@ -200,6 +193,11 @@ public:
*/
static void terminate();
/**
* tarsservant框架的版本
*/
static string getTarsVersion();
/**
* Config
* @param filename
@ -217,6 +215,25 @@ public:
*/
void manualListen();
/**
* Servant
* @param T
* @param id
*/
template<typename T>
void addServant(const string &id)
{
ServantHelperManager::getInstance()->addServant<T>(id, this, true);
}
/**
* taf协议serverServant的协议解析器
* @param protocol
* @param servant
*/
void addServantProtocol(const string &servant, const TC_NetWorkBuffer::protocol_functor &protocol);
protected:
/**
* ,
@ -355,24 +372,13 @@ protected:
protected:
/**
* Servant
* @param T
* @param id
*/
template<typename T> void addServant(const string &id)
{
ServantHelperManager::getInstance()->addServant<T>(id, this, true);
}
/**
* tars协议serverServant的协议解析器
* @param protocol
* @param servant
*/
void addServantProtocol(const string& servant, const TC_NetWorkBuffer::protocol_functor& protocol);
/**
*Servant的连接断开回调
*
*
* @param command
* @param params
* @param result
*
* @return bool
*/
void addServantOnClose(const string& servant, const TC_EpollServer::close_functor& f);
@ -424,13 +430,9 @@ protected:
/**
* bind server adapter
*/
void bindAdapter(vector<TC_EpollServer::BindAdapterPtr>& adapters);
void bindAdapter(vector<TC_EpollServer::BindAdapterPtr> &adapters);
/**
* set adapter
* @param adapter
*/
void setAdapter(TC_EpollServer::BindAdapterPtr& adapter, const string &name);
void setAdapter(TC_EpollServer::BindAdapterPtr& adapter, const string &name);
/**
* @param servant

View File

@ -19,6 +19,8 @@
#include "servant/Message.h"
#include "servant/Global.h"
#include "util/tc_cas_queue.h"
#include "util/tc_thread_queue.h"
namespace tars
{

View File

@ -1,4 +1,4 @@
#include "servant/Auth.h"
#include "servant/AuthF.h"
#include "util/tc_epoll_server.h"
namespace tars

View File

@ -99,16 +99,17 @@ public:
*/
~Communicator();
/**
*
* @param T
* @param objectName
* @param setName set调用的setid
* @return T
*/
template<class T> T stringToProxy(const string& objectName,const string& setName="")
{
T prx = NULL;
public:
/**
*
* @param T
* @param objectName
* @param setName set调用的setid
* @return T
*/
template<class T> T stringToProxy(const string& objectName, const string& setName = "")
{
T prx = NULL;
stringToProxy<T>(objectName, prx,setName);
@ -251,11 +252,6 @@ protected:
*/
bool isTerminating();
/**
* Property初始化客户端配置
*/
void initClientConfig();
/**
*
* @return ServantProxyFactoryPtr
@ -392,11 +388,8 @@ protected:
/*
* 线seq
*/
size_t _asyncSeq;
size_t _asyncSeq = 0;
//#if TARS_SSL
// shared_ptr<TC_OpenSSL> _ctx;
//#endif
#ifdef _USE_OPENTRACKING
public:
struct TraceManager:public TC_HandleBase{

View File

@ -41,9 +41,9 @@ struct FDInfo
{
enum
{
ET_C_TERMINATE = 0,
ET_C_NOTIFY = 1,
ET_C_NET = 2,
ET_C_TERMINATE = 3,
};
/**

View File

@ -24,7 +24,7 @@
#include <map>
#include "util/tc_platform.h"
#include "util/tc_fcontext.h"
#include "util/tc_thread_queue.h"
#include "util/tc_cas_queue.h"
#include "util/tc_monitor.h"
#include <functional>
#include "util/tc_thread.h"
@ -210,17 +210,6 @@ protected:
//在协程里执行实际逻辑的入口函数
static void corotineProc(void * args, transfer_t t);
// /**
// * 协程的入口函数
// */
// static void corotineEntry(intptr_t q);
// /**
// * 在协程里执行实际逻辑的入口函数
// */
// static void corotineProc(void * args);
public:
/**
*
@ -285,7 +274,6 @@ public:
/**
*
*/
// inline fcontext_t* getCtx() { return (!_main ? _ctx_to : &_ctx_from); }
inline fcontext_t getCtx() { return _ctx; }
inline void setCtx(fcontext_t ctx) { _ctx = ctx; }
public:
@ -325,12 +313,6 @@ private:
*
*/
fcontext_t _ctx;
// fcontext_t* _ctx_to;
/*
*
*/
// fcontext_t _ctx_from;
/*
*
@ -601,15 +583,10 @@ private:
*/
CoroutineInfo _free;
/*
*
*/
// standard_stack_allocator _alloc;
/*
* 线使
*/
TC_ThreadQueue<uint32_t, deque<uint32_t> > _activeCoroQueue;
TC_CasQueue<uint32_t, deque<uint32_t> > _activeCoroQueue;
/*
*

View File

@ -19,7 +19,11 @@
#include "servant/Global.h"
#include "util/tc_socket.h"
#include "Auth.h"
#include "AuthF.h"
#if TARGET_PLATFORM_WINDOWS
#include <WS2tcpip.h>
#endif
using namespace std;
@ -34,7 +38,7 @@ namespace tars
class EndpointInfo
{
public:
enum EType { UDP = 0, TCP = 1, SSL = 2};
// enum EType { UDP = TC_ClientSocket::, TCP = 1, SSL = 2};
/**
*
@ -47,7 +51,7 @@ public:
* @param port
* @param type
*/
EndpointInfo(const string& host, uint16_t port, EndpointInfo::EType type, int32_t grid, const string & setDivision, int qos, int weight = -1, unsigned int weighttype = 0, int authType = 0);
EndpointInfo(const string& host, uint16_t port, TC_Endpoint::EType type, int32_t grid, const string & setDivision, int qos, int weight = -1, unsigned int weighttype = 0, int authType = 0);
/**
* ,set信息
@ -81,14 +85,14 @@ public:
*
* @return string
*/
const string& fulldesc() const;
bool isTcp() const;
/**
*
*
* @return const string&
*/
string host() const;
const string &host() const;
/**
*
@ -106,17 +110,17 @@ public:
/*
* qos的descp值
*/
int32_t qos() const {return _qos;}
int32_t qos() const {return _ep.getQos();}
/*
*
*/
int weight() const {return _weight;}
int weight() const {return _ep.getWeight();}
/**
* @brief 使
*/
unsigned int getWeightType() const { return _weighttype; }
unsigned int getWeightType() const { return _ep.getWeightType(); }
/**
*
@ -142,7 +146,7 @@ public:
*
* @return EndpointInfo::EType
*/
EndpointInfo::EType type() const;
TC_Endpoint::EType type() const { return _ep.getType(); }
/**
*set分组信息
@ -161,13 +165,13 @@ public:
/*
*
*/
AUTH_TYPE authType() const { return _authType; }
int authType() const { return _ep.getAuthType(); }
/**
* @brief is ipv6 socket or not
* @return true if is ipv6
*/
bool isIPv6() const { return _isIPv6; }
bool isIPv6() const { return _ep.isIPv6(); }
/**
*
@ -210,46 +214,13 @@ protected:
string createCompareDesc();
private:
/**
* IP
*/
string _host;
/**
*
*/
uint16_t _port;
/**
*
*/
int32_t _grid;
/*
* qos的dscp值
*/
int32_t _qos;
/**
*
*/
EndpointInfo::EType _type;
TC_Endpoint _ep;
/**
*set分组信息
*/
string _setDivision;
/**
*
*/
int _weight;
/**
* 使
*/
unsigned int _weighttype;
/**
*
@ -270,15 +241,6 @@ private:
*/
string _desc;
/**
*
*/
tars::AUTH_TYPE _authType;
/**
* _host is IPv6 or not
*/
bool _isIPv6;
/**
*

View File

@ -17,12 +17,12 @@
#ifndef __TARS_ENDPOINT_MANAGER_H_
#define __TARS_ENDPOINT_MANAGER_H_
#include "util/tc_spin_lock.h"
#include "util/tc_consistent_hash_new.h"
#include "servant/EndpointInfo.h"
#include "servant/EndpointF.h"
#include "servant/QueryF.h"
#include "servant/AppProtocol.h"
#include "util/tc_spin_lock.h"
#include "util/tc_consistent_hash_new.h"
namespace tars
{
@ -343,7 +343,7 @@ public:
/**
*
*/
bool selectAdapterProxy(ReqMessage * msg, AdapterProxy * & pAdapterProxy);
bool selectAdapterProxy(ReqMessage * msg, AdapterProxy * & pAdapterProxy, bool onlyCheck);
/**
*
@ -358,33 +358,33 @@ private:
/*
*
*/
AdapterProxy * getNextValidProxy();
AdapterProxy * getNextValidProxy(bool onlyCheck);
/*
* hash值选取一个结点
*/
AdapterProxy* getHashProxy(int64_t hashCode, bool bConsistentHash = false);
AdapterProxy* getHashProxy(int64_t hashCode, bool bConsistentHash = false, bool onlyCheck = false);
/*
* hash值按取模方式
*/
AdapterProxy* getHashProxyForNormal(int64_t hashCode);
AdapterProxy* getHashProxyForNormal(int64_t hashCode, bool onlyCheck);
/*
* hash值按一致性hash方式
*/
AdapterProxy* getConHashProxyForNormal(int64_t hashCode);
AdapterProxy* getConHashProxyForNormal(int64_t hashCode, bool onlyCheck);
/*
* hash值按取模方式
*/
AdapterProxy* getHashProxyForWeight(int64_t hashCode, bool bStatic, vector<size_t> &vRouterCache);
AdapterProxy* getHashProxyForWeight(int64_t hashCode, bool bStatic, vector<size_t> &vRouterCache, bool onlyCheck);
/*
* hash值按一致性hash方式
*/
AdapterProxy* getConHashProxyForWeight(int64_t hashCode, bool bStatic);
AdapterProxy* getConHashProxyForWeight(int64_t hashCode, bool bStatic, bool onlyCheck);
/*
*
@ -409,12 +409,12 @@ private:
/*
*
*/
AdapterProxy* getWeightedProxy(bool bStaticWeighted);
AdapterProxy* getWeightedProxy(bool bStaticWeighted, bool onlyCheck);
/*
*
*/
AdapterProxy* getWeightedForNormal(bool bStaticWeighted);
AdapterProxy* getWeightedForNormal(bool bStaticWeighted, bool onlyCheck);
/*
*

View File

@ -25,14 +25,7 @@
#include <deque>
#include <vector>
#include <memory>
#include <errno.h>
#include <iomanip>
//#include <sys/ipc.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include "util/tc_platform.h"
#include "util/tc_clientsocket.h"
#include "util/tc_autoptr.h"
#include "util/tc_common.h"
@ -75,7 +68,7 @@ typedef TC_AutoPtr<StatFProxy> StatFPrx;
typedef TC_AutoPtr<StatReport> StatReportPtr;
typedef TC_AutoPtr<FDReactor> FDReactorPtr;
// typedef TC_AutoPtr<ServantProxyFactory> ServantProxyFactoryPtr;
typedef TC_AutoPtr<ObjectProxyFactory> ObjectProxyFactoryPtr;
//typedef TC_AutoPtr<ObjectProxyFactory> ObjectProxyFactoryPtr;
typedef TC_AutoPtr<AsyncProcThread> AsyncProcThreadPtr;
//////////////////////////////////////////////////////////////

View File

@ -17,17 +17,17 @@
#ifndef __TARS_MESSAGE_H_
#define __TARS_MESSAGE_H_
#include "servant/Global.h"
#include <util/tc_network_buffer.h>
#include "util/tc_autoptr.h"
#include "util/tc_monitor.h"
#include "util/tc_loop_queue.h"
#include <util/tc_network_buffer.h>
#include "servant/CoroutineScheduler.h"
#include "servant/Global.h"
namespace tars
{
/////////////////////////////////////////////////////////////////////////
class CoroutineScheduler;
/**
*
*
@ -221,7 +221,7 @@ struct ReqMessage : public TC_HandleBase
/*
*
*/
void init(CallType eCallType, ObjectProxy * pObj, const string & sFuncName)
void init(CallType eCallType)
{
eStatus = ReqMessage::REQ_REQ;
eType = eCallType;
@ -229,10 +229,9 @@ struct ReqMessage : public TC_HandleBase
callback = NULL;
proxy = NULL;
pObjectProxy = pObj;
pObjectProxy = NULL;
response = std::make_shared<ResponsePacket>();
// sReqData.clear();
sReqData = std::make_shared<TC_NetWorkBuffer::Buffer>();
pMonitor = NULL;
bMonitorFin = false;
@ -269,7 +268,7 @@ struct ReqMessage : public TC_HandleBase
shared_ptr<TC_NetWorkBuffer::Buffer> sReqData; //请求消息体
ReqMonitor * pMonitor; //用于同步的monitor
bool bMonitorFin; //同步请求timewait是否结束
volatile bool bMonitorFin; //同步请求timewait是否结束
int64_t iBeginTime; //请求时间
int64_t iEndTime; //完成时间
@ -296,7 +295,7 @@ struct ReqMessage : public TC_HandleBase
};
typedef TC_AutoPtr<ReqMessage> ReqMessagePtr;
typedef TC_LoopQueue<ReqMessage*,1000> ReqInfoQueue;
typedef TC_LoopQueue<ReqMessage*> ReqInfoQueue;
#define HTTP2 "http2"

View File

@ -17,23 +17,6 @@
#ifndef __TARS_NETWORK_UTIL_H_
#define __TARS_NETWORK_UTIL_H_
// #include <unistd.h>
// #include <fcntl.h>
// #include <sys/socket.h>
// #include <sys/time.h>
// #include <sys/select.h>
// #include <sys/types.h>
// #include <sys/ioctl.h>
// #include <net/if.h>
// #include <net/if_arp.h>
// #include <netinet/in.h>
// #include <netinet/tcp.h>
// #include <arpa/inet.h>
// #include <netdb.h>
// #include <string>
// #include <vector>
#include "util/tc_socket.h"
#if TARGET_PLATFORM_WINDOWS
#include <WS2tcpip.h>
@ -51,36 +34,5 @@ struct NetworkUtil
static std::string errorToString(int);
};
// struct NetworkUtil
// {
// static const int INVALID_SOCKET = -1;
// static const int SOCKET_ERROR = -1;
// static int createSocket(bool, bool isLocal = false, bool isIpv6 = false);
// static void closeSocketNoThrow(int);
// static void setBlock(int, bool);
// static void setTcpNoDelay(int);
// static void setKeepAlive(int);
// static void doBind(int, struct sockaddr_in&);
// static void doBind(int, const struct sockaddr *, socklen_t);
// static bool doConnect(int, const struct sockaddr_in&);
// static bool doConnect(int, const struct sockaddr *, socklen_t);
// static void getAddress(const std::string&, int, struct sockaddr_in&);
// static void getAddress(const std::string&, int, struct sockaddr_in6&);
// static std::string errorToString(int);
// };
}
#endif

View File

@ -100,9 +100,9 @@ public:
ProxyProtocol& getProxyProtocol();
/**
*
*/
void setSocketOpt(int level, int optname, const void *optval, socklen_t optlen);
*
*/
void setSocketOpt(int level, int optname, const void *optval, SOCKET_LEN_TYPE optlen);
/**
*

View File

@ -17,9 +17,10 @@
#ifndef __TARS_PROPERTY_REPORT_H_
#define __TARS_PROPERTY_REPORT_H_
#include "util/tc_lock.h"
#include "util/tc_autoptr.h"
#include "util/tc_thread_mutex.h"
//#include "util/tc_thread_mutex.h"
#include "util/tc_spin_lock.h"
#include <tuple>
#include <vector>
#include <string>
@ -48,7 +49,7 @@ public:
/**
*
*/
std::string getMasterName() { return _sMasterName; }
const std::string &getMasterName() const { return _sMasterName; }
public:
@ -169,7 +170,7 @@ typedef TC_AutoPtr<PropertyReport> PropertyReportPtr;
*/
template <typename... Params>
class PropertyReportImp : public PropertyReport, public TC_ThreadMutex
class PropertyReportImp : public PropertyReport, public TC_SpinLock
{
public:
using PropertyReportData = std::tuple<Params...>;
@ -194,7 +195,7 @@ public:
*/
void report(int iValue) override
{
TC_LockT<TC_ThreadMutex> lock(*this);
TC_LockT<TC_SpinLock> lock(*this);
Helper<std::tuple_size<decltype(_propertyReportData)>::value>::Report(*this, iValue);
}
@ -206,7 +207,7 @@ public:
*/
vector<pair<string, string> > get() override
{
TC_LockT<TC_ThreadMutex> lock(*this);
TC_LockT<TC_SpinLock> lock(*this);
return Helper<std::tuple_size<decltype(_propertyReportData)>::value>::Get(*this);
}

View File

@ -19,6 +19,8 @@
#include "util/tc_autoptr.h"
#include "util/tc_epoll_server.h"
#include "util/tc_thread_pool.h"
#include "util/tc_cas_queue.h"
#include "servant/ServantProxy.h"
#include "servant/TarsCurrent.h"
#include "servant/BaseNotify.h"
@ -171,7 +173,7 @@ public:
*
* @return TC_ThreadQueue<ReqMessagePtr>&
*/
TC_ThreadQueue<ReqMessagePtr>& getResponseQueue();
TC_CasQueue<ReqMessagePtr>& getResponseQueue();
protected:
/**
@ -190,9 +192,11 @@ protected:
TC_EpollServer::Handle* _handle;
/**
*
* , Servant一个队列, ServantImp中,
* Callback使用ServantCallback时, Servant::doResponse中响应, ServantImp是同一个线程
* Servant::onDispatch, ServantImp时, 线
*/
TC_ThreadQueue<ReqMessagePtr> _asyncResponseQueue;
TC_CasQueue<ReqMessagePtr> _asyncResponseQueue;
};
typedef TC_AutoPtr<Servant> ServantPtr;
@ -219,6 +223,14 @@ public:
*/
virtual int onDispatch(ReqMessagePtr msg);
/**
*
* @param msg
* @return int
*/
virtual void onClose()
{
}
/**
* servant
* @return const ServantPtr&
@ -250,8 +262,6 @@ class CallbackThreadData //: public TC_ThreadPool::ThreadData
{
public:
static thread_local shared_ptr<CallbackThreadData> g_sp;
// static TC_ThreadMutex _mutex; //全局的互斥锁
// static pthread_key_t _key; //私有线程数据key
/**
*

View File

@ -21,11 +21,11 @@
#include <string>
#include <memory>
#include <deque>
#include "util/tc_platform.h"
#include "util/tc_monitor.h"
#include "util/tc_epoll_server.h"
#include "servant/Servant.h"
#include "servant/StatReport.h"
// #include <ucontext.h>
#include "servant/CoroutineScheduler.h"
#ifdef _USE_OPENTRACKING
#include "opentracing/span.h"
@ -110,10 +110,11 @@ protected:
*/
virtual void handleClose(const shared_ptr<TC_EpollServer::RecvContext> &data);
/**
* handleFilter拆分的第一部分
*/
virtual void handleAsyncResponse();
/**
* handleFilter拆分的第一部分
*/
virtual void handleAsyncResponse();
/**
* handleFilter拆分的第二部分
@ -197,12 +198,8 @@ protected:
/**
*
*/
map<string, ServantPtr> _servants;
unordered_map<string, ServantPtr> _servants;
/**
*
*/
// TC_ThreadLock _monitor;
/**
*

View File

@ -22,6 +22,7 @@
#include <vector>
#include "util/tc_autoptr.h"
#include "util/tc_singleton.h"
#include "util/tc_spin_lock.h"
#include "servant/Servant.h"
namespace tars
@ -157,7 +158,7 @@ public:
/**
*
*/
bool isDyeing() {return _isDyeing;}
bool isDyeing() const {return _isDyeing;}
protected:

View File

@ -198,6 +198,7 @@ public:
{
TC_LockT<TC_SpinLock> lock(_mutex);
vRet.swap(_vReqMessage);
_vReqMessage.clear();
}
return vRet;
@ -278,7 +279,7 @@ public:
/**
* coro并行请求的共享智能指针
*/
virtual const tars::CoroParallelBasePtr& getCoroParallelBasePtr() { return _pPtr; }
virtual const CoroParallelBasePtr& getCoroParallelBasePtr() { return _pPtr; }
/**
* 线
@ -519,7 +520,7 @@ public:
/**
*
*/
void tars_set_sockopt(int level, int optname, const void *optval, socklen_t optlen);
void tars_set_sockopt(int level, int optname, const void *optval, SOCKET_LEN_TYPE optlen);
/**
*
@ -545,7 +546,13 @@ public:
*/
virtual ServantProxy* tars_consistent_hash(int64_t key);
/**
// /**
// * 直接同步调用
// * @return
// */
// virtual ServantProxy* taf_direct();
/**
* Hash数据
*
* @param key
@ -587,25 +594,6 @@ public:
*/
virtual void tars_set_push_callback(const ServantProxyCallbackPtr& cb);
/**
* TARS协议同步方法调用
*/
virtual shared_ptr<ResponsePacket> tars_invoke(char cPacketType,
const string& sFuncName,
tars::TarsOutputStream<tars::BufferWriterVector>& buf,
const map<string, string>& context,
const map<string, string>& status);
/**
* TARS协议异步方法调用
*/
virtual void tars_invoke_async(char cPacketType,
const string& sFuncName,
tars::TarsOutputStream<tars::BufferWriterVector> &buf,
const map<string, string>& context,
const map<string, string>& status,
const ServantProxyCallbackPtr& callback,
bool bCoro = false);
/**
*
@ -655,6 +643,25 @@ public:
*/
virtual void tars_setMasterFlag(bool bMasterFlag) {_masterFlag = bMasterFlag;}
/**
* TARS协议同步方法调用
*/
virtual shared_ptr<ResponsePacket> tars_invoke(char cPacketType,
const string& sFuncName,
tars::TarsOutputStream<tars::BufferWriterVector>& buf,
const map<string, string>& context,
const map<string, string>& status);
/**
* TARS协议异步方法调用
*/
virtual void tars_invoke_async(char cPacketType,
const string& sFuncName,
tars::TarsOutputStream<tars::BufferWriterVector> &buf,
const map<string, string>& context,
const map<string, string>& status,
const ServantProxyCallbackPtr& callback,
bool bCoro = false);
private:
/**
@ -662,16 +669,26 @@ private:
* @param req
* @return int
*/
void invoke(ReqMessage * msg, bool bCoroAsync = false);
void invoke(ReqMessage *msg, bool bCoroAsync = false);
/**
*
* @param req
* @return int
*/
void finished(ReqMessage * msg);
// /**
// * invoke 异步
// * @param msg
// * @param pSptd
// * @param pReqQ
// * @param bCoroAsync
// */
// void invoke_async(ReqMessage *msg, ServantProxyThreadData *pSptd, ReqInfoQueue *pReqQ, bool bCoroAsync);
/**
// /**
// * invoke 同步
// * @param msg
// * @param pSptd
// * @param pReqQ
// */
// void invoke_sync(ReqMessage *msg, ServantProxyThreadData *pSptd, ReqInfoQueue *pReqQ);
/**
* 线
* @param pSptd
* @return void

View File

@ -27,7 +27,7 @@ namespace tars
/**
* ServantProxy对象object在进程空间只有一个ServantProxy实例
*/
class ServantProxyFactory : public TC_HandleBase, public TC_ThreadRecMutex
class ServantProxyFactory : public TC_ThreadRecMutex
{
public:
/**

View File

@ -22,6 +22,7 @@
#include "servant/CommunicatorEpoll.h"
#include "servant/AuthLogic.h"
#include <list>
#include <util/tc_network_buffer.h>
using namespace std;
@ -56,8 +57,11 @@ public:
enum ReturnStatus
{
eRetError = -1,
eRetOk = 0,
eRetFull = 1,
eRetOk =0,
eRetFull=1,
eRetTimeout=2,
eRetPacket=3,
};
/*
@ -283,7 +287,9 @@ protected:
#if TARS_SSL
std::shared_ptr<TC_OpenSSL> _openssl;
#endif
//同步调用的fd
// TC_ClientSocket *_syncSock = NULL;
/*
* buffer
*/
@ -374,7 +380,6 @@ public:
* @param flag
* @return int
*/
virtual int send(const void* buf, uint32_t len, uint32_t flag);
/**
@ -391,8 +396,7 @@ public:
* @param done
* @return int
*/
// virtual int doResponse(list<ResponsePacket>& done);
virtual int doResponse();
virtual int doResponse();
private:
/*

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
/* A Bison parser, made by GNU Bison 3.0.4. */
/* A Bison parser, made by GNU Bison 3.2.2. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
Copyright (C) 1984, 1989-1990, 2000-2015, 2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -30,6 +30,9 @@
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Undocumented macros, especially those whose name start with YY_,
are private implementation details. Do not rely on them. */
#ifndef YY_YY_TARS_TAB_HPP_INCLUDED
# define YY_YY_TARS_TAB_HPP_INCLUDED
/* Debug traces. */

View File

@ -55,6 +55,17 @@ public:
*/
static string decode(const string &data);
/**
* @brief base64编码.
*
* @param buffer buffer指针
* @param length
* @param bChangeLine
* (RFC中建议每76个字符后加入回车换行
* @return string
*/
static string encode(const char *buffer, size_t length, bool bChangeLine = false);
/**
* @brief base64编码 .
*
@ -65,7 +76,7 @@ public:
* RFC中建议每76个字符后加入回车换行
* @return
*/
static int encode(const unsigned char* pSrc, int nSrcLen, char* pDst, bool bChangeLine = false);
static int encode(const unsigned char* pSrc, size_t nSrcLen, char* pDst, bool bChangeLine = false);
/**
* @brief base64解码.
@ -75,7 +86,7 @@ public:
* @param pDst
* @return
*/
static int decode(const char* pSrc, int nSrcLen, unsigned char* pDst);
static int decode(const char* pSrc, size_t nSrcLen, unsigned char* pDst);
protected:

View File

@ -15,9 +15,8 @@ namespace tars
/**
* @file tc_cas_queue.h
* @brief 线
#include <condition_variable>队列类.
*
* @author jarodruan@upchina.com
* @author ruanshudong@qq.com
*/
/////////////////////////////////////////////////

View File

@ -70,7 +70,7 @@ public:
* @param timeout, ,
* @param type, SOCK_STREAM或SOCK_DGRAM
*/
TC_Endpoint(const string& host, int port, int timeout, int type = 1, int grid = 0, int qos = 0, int weight = -1, unsigned int weighttype = 0, int authType = 0)
TC_Endpoint(const string& host, int port, int timeout, EType type = TCP, int grid = 0, int qos = 0, int weight = -1, unsigned int weighttype = 0, int authType = 0)
{
init(host, port, timeout, type, grid, qos, weight, weighttype, authType);
}
@ -133,7 +133,7 @@ public:
*
* @return bool
*/
bool operator == (const TC_Endpoint& l)
bool operator == (const TC_Endpoint& l) const
{
return (_host == l._host && _port == l._port && _timeout == l._timeout && _type == l._type &&
_grid == l._grid && _qos == l._qos && _weight == l._weight && _weighttype == l._weighttype &&
@ -144,14 +144,14 @@ public:
* @brief ip
* @param str
*/
void setHost(const string& host) { _host = host; }
void setHost(const string& host) { _host = host; _isIPv6 = TC_Socket::addressIsIPv6(_host); }
/**
* @brief ip
*
* @return const string&
*/
string getHost() const { return _host; }
const string &getHost() const { return _host; }
/**
* @brief
@ -185,6 +185,7 @@ public:
* @return bool
*/
int isTcp() const { return _type == TCP || _type == SSL; }
/**
* @brief SSL
*
@ -196,17 +197,18 @@ public:
* @brief TCP或UDP
* @param bTcp
*/
void setTcp(bool bTcp) { _type = bTcp; }
int isUdp() const { return _type == UDP; }
// void setTcp(bool bTcp) { _type = bTcp; }
/**
* @brief TCP/UDP/SSL
* @param type
*/
void setType(int type) { _type = type; }
void setType(EType type) { _type = type; }
/**
* @brief
*/
int getType() const { return _type; }
EType getType() const { return _type; }
/**
* @brief
* @param grid
@ -283,7 +285,7 @@ public:
*
* @return string
*/
string toString()
string toString() const
{
ostringstream os;
if (_type == TCP)
@ -323,7 +325,7 @@ public:
void parse(const string &desc);
private:
void init(const string& host, int port, int timeout, int istcp, int grid, int qos, int weight, unsigned int weighttype, int authType);
void init(const string& host, int port, int timeout, EType type, int grid, int qos, int weight, unsigned int weighttype, int authType);
protected:
/**
@ -344,7 +346,7 @@ protected:
/**
*
*/
int _type;
EType _type;
/**
*
@ -427,10 +429,21 @@ public:
*/
virtual int recv(char *sRecvBuffer, size_t &iRecvLen) = 0;
/**
*
*/
virtual void close();
/**
* socket
* @return
*/
TC_Socket *getSocket() { return &_socket; }
/**
* @brief
*/
enum
enum EM_CIENT_SOCKET_TYPE
{
EM_SUCCESS = 0, /** EM_SUCCESS:发送成功*/
EM_SEND = -1, /** EM_SEND:发送错误*/
@ -465,10 +478,10 @@ protected:
*/
int _timeout;
/**
* _ip is ipv6 or not
*/
int _isIPv6;
/**
* IPv6
*/
bool _isIPv6 = false;
};
/**
@ -499,7 +512,7 @@ public:
* @param iSendLen buffer的长度
* @return int 0 ,<0
*/
int send(const char *sSendBuffer, size_t iSendLen);
virtual int send(const char *sSendBuffer, size_t iSendLen);
/**
* @brief iRecvLen的字节
@ -507,7 +520,7 @@ public:
* @param iRecvLen ,
* @return int 0 ,<0
*/
int recv(char *sRecvBuffer, size_t &iRecvLen);
virtual int recv(char *sRecvBuffer, size_t &iRecvLen);
/**
* @brief (,

View File

@ -226,7 +226,6 @@ public:
*/
static string tm2str(const time_t &t, const string &sFormat = "%Y%m%d%H%M%S");
/**
* @brief tm.
*
@ -324,7 +323,6 @@ public:
template<typename T>
static T strto(const string &sStr, const string &sDefault);
// typedef bool (*depthJudge)(const string& str1, const string& str2);
/**
* @brief ,,vector里
*
@ -339,11 +337,10 @@ public:
* @param sStr
* @param sSep ()
* @param withEmpty true代表空的也算一个元素, false时空的过滤
* @param depthJudge
* @return vector
*/
template<typename T>
static vector<T> sepstr(const string &sStr, const string &sSep, bool withEmpty = false);//, depthJudge judge = nullptr);
static vector<T> sepstr(const string &sStr, const string &sSep, bool withEmpty = false);
/**
* @brief T型转换成字符串T能够使用ostream对象用<<,
@ -351,7 +348,12 @@ public:
* @return
*/
template<typename T>
static string tostr(const T &t);
inline static string tostr(const T &t)
{
ostringstream sBuffer;
sBuffer << t;
return sBuffer.str();
}
/**
* @brief vector转换成string.
@ -405,7 +407,7 @@ public:
* @param sSep
* @return
*/
template <typename InputIter>
template<typename InputIter>
static string tostr(InputIter iFirst, InputIter iLast, const string &sSep = "|");
/**
@ -466,7 +468,7 @@ public:
* @param mSrcDest map<,>
* @return string
*/
static string replace(const string &sString, const map<string,string>& mSrcDest);
static string replace(const string &sString, const map<string, string>& mSrcDest);
/**
* @brief .pat中*
@ -701,7 +703,7 @@ namespace p
{
if(!sStr.empty())
{
return atof(sStr.c_str());
return (float) atof(sStr.c_str());
}
return 0;
}
@ -770,7 +772,6 @@ T TC_Common::strto(const string &sStr, const string &sDefault)
return strto<T>(s);
}
template<typename T>
vector<T> TC_Common::sepstr(const string &sStr, const string &sSep, bool withEmpty)
{
@ -814,7 +815,6 @@ vector<T> TC_Common::sepstr(const string &sStr, const string &sSep, bool withEmp
return vt;
}
template<>
string TC_Common::tostr<bool>(const bool &t);
@ -857,14 +857,6 @@ string TC_Common::tostr<long double>(const long double &t);
template<>
string TC_Common::tostr<std::string>(const std::string &t);
template<typename T>
string TC_Common::tostr(const T &t)
{
ostringstream sBuffer;
sBuffer << t;
return sBuffer.str();
}
template<typename T>
string TC_Common::tostr(const vector<T> &t)
{
@ -939,7 +931,7 @@ string TC_Common::tostr(const pair<F, S> &itPair)
return sBuffer;
}
template <typename InputIter>
template<typename InputIter>
string TC_Common::tostr(InputIter iFirst, InputIter iLast, const string &sSep)
{
string sBuffer;
@ -963,7 +955,6 @@ string TC_Common::tostr(InputIter iFirst, InputIter iLast, const string &sSep)
return sBuffer;
}
template<typename V,typename E>
bool TC_Common::equal(const V& x, const V& y,E eps)
{

View File

@ -63,7 +63,6 @@ struct TC_Config_Exception : public TC_Exception
struct TC_ConfigNoParam_Exception : public TC_Exception
{
TC_ConfigNoParam_Exception(const string &buffer) : TC_Exception(buffer){};
TC_ConfigNoParam_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_ConfigNoParam_Exception() throw(){};
};
@ -239,7 +238,6 @@ public:
return new TC_ConfigDomain(*this);
}
protected:
/**
* @brief .
*
@ -316,28 +314,17 @@ protected:
*
* <Main>
* <Domain>
* Name = Value
* </Domain>
* <Domain1>
* Name = Value
* </Domain1>
* </Main>
* :conf["/Main/Domain<Name>"] Map:
* getDomainMap("/Main/Domain", m); m得到Name/Value对
* Vector: getDomainVector("/Main", v); v得到Domain列表
* Vector: getDomainVector("/Main", v); v得到Domain列表
*/
class TC_Config
@ -390,7 +377,7 @@ public:
* @return
* @throws TC_Config_Exception
*/
string operator[](const string &sName);
string operator[](const string &sName) const;
/**
* @brief , ,.
@ -400,6 +387,12 @@ public:
*/
string get(const string &sName, const string &sDefault="") const;
/**
* @brief .
* @param sName , : /Main/Domain<Param>
*/
void set(const string &sName, const string &value);
/**
* @brief GetDomainParamMap获取域下面的参数值对.
*

View File

@ -38,7 +38,7 @@ enum TC_HashAlgorithmType
class TC_HashAlgorithm : public TC_HandleBase
{
public:
virtual int32_t hash(const string & sKey) = 0;
virtual int32_t hash(const char *sKey, size_t length) = 0;
virtual TC_HashAlgorithmType getHashType() = 0;
protected:
@ -54,7 +54,7 @@ typedef TC_AutoPtr<TC_HashAlgorithm> TC_HashAlgorithmPtr;
class TC_KetamaHashAlg : public TC_HashAlgorithm
{
public:
virtual int32_t hash(const string & sKey);
virtual int32_t hash(const char *sKey, size_t length);
virtual TC_HashAlgorithmType getHashType();
};
@ -64,7 +64,7 @@ public:
class TC_DefaultHashAlg : public TC_HashAlgorithm
{
public:
virtual int32_t hash(const string & sKey);
virtual int32_t hash(const char *sKey, size_t length);
virtual TC_HashAlgorithmType getHashType();
};

259
util/include/util/tc_cron.h Normal file
View File

@ -0,0 +1,259 @@
#pragma once
#include <mutex>
#include <condition_variable>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <bitset>
#include "util/tc_common.h"
#include "util/tc_ex.h"
#include <ctime>
namespace tars
{
struct TC_CronException : public TC_Exception
{
public:
TC_CronException(const string& buffer) : TC_Exception(buffer) {};
// TC_CronException(const string& buffer, bool err) : TC_Exception(buffer, err) {};
~TC_CronException() throw() {};
};
typedef uint8_t cron_int;
class TC_Cron
{
public:
enum class CronField
{
second,
minute,
hour_of_day,
day_of_week,
day_of_month,
month,
year
};
static const std::time_t INVALID_TIME = static_cast<std::time_t>(-1);
static const size_t INVALID_INDEX = static_cast<size_t>(-1);
public:
/**
* @brief cron对象
* @return cron
*/
// 字段分别为 <seconds> <minutes> <hours> <days of month> <months> <days of week>
// 通配符以及含义
// * all values selects all values within a field
// - range specify ranges
// , comma specify additional values
// / slash speficy increments
// DAYS = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
// MONTHS = { "NIL", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
// 各类例子
// CRON Description
// * * * * * * Every second
// */5 * * * * * Every 5 seconds
// 0 */5 */2 * * * Every 5 minutes, every 2 hours
// 0 */2 */2 * */2 */2 Every 2 minutes, every 2 hours, every 2 days of the week, every 2 months
// 0 15 10 * * * * 10:15 AM every day
// 0 0/5 14 * * * Every 5 minutes starting at 2 PM and ending at 2:55 PM, every day
// 0 10,44 14 * 3 WED 2:10 PM and at 2:44 PM every Wednesday of March
// 0 15 10 * * MON-FRI 10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday
// 0 0 12 1/5 * * 12 PM every 5 days every month, starting on the first day of the month
// 0 11 11 11 11 * Every November 11th at 11:11 AM
static TC_Cron makecron(const string & expr);
/**
* @brief cron对象的下一个时间点
* @param cron
* @return INVALID_TIME
*/
static std::time_t nextcron( const TC_Cron& cron, std::time_t timestamp);
/**
* @brief cron对象的下一个时间点
* @param cron
* @return INVALID_TIME
*/
static std::time_t nextcron(const TC_Cron& cron);
public:
std::bitset<60> seconds;
std::bitset<60> minutes;
std::bitset<24> hours;
std::bitset<7> days_of_week;
std::bitset<31> days_of_month;
std::bitset<12> months;
bool isset = false;
public:
static const cron_int CRON_MIN_SECONDS = 0;
static const cron_int CRON_MAX_SECONDS = 59;
static const cron_int CRON_MIN_MINUTES = 0;
static const cron_int CRON_MAX_MINUTES = 59;
static const cron_int CRON_MIN_HOURS = 0;
static const cron_int CRON_MAX_HOURS = 23;
static const cron_int CRON_MIN_DAYS_OF_WEEK = 0;
static const cron_int CRON_MAX_DAYS_OF_WEEK = 6;
static const cron_int CRON_MIN_DAYS_OF_MONTH = 1;
static const cron_int CRON_MAX_DAYS_OF_MONTH = 31;
static const cron_int CRON_MIN_MONTHS = 1;
static const cron_int CRON_MAX_MONTHS = 12;
static const cron_int CRON_MAX_YEARS_DIFF = 4;
static const std::vector<std::string> DAYS;
static const std::vector<std::string> MONTHS;
protected:
/**
* @brief
* @return true ;false
*/
static bool contains(const std::string &text, char ch) ;
/**
* @brief crontab合法数字
* @return cron_int
*/
static cron_int to_cron_int(const std::string& text);
/**
* @brief
*/
static std::time_t tm_to_time(std::tm& tmt);
static std::tm* time_to_tm(std::time_t const* date, std::tm* const out);
protected:
/**
* @brief /
*/
static std::string replaceOrdinals(std::string text, const std::vector<std::string> & replacement);
/**
* @brief
*/
static std::pair<cron_int, cron_int> makeRange(std::string field, cron_int minval, cron_int maxval);
/**
* @brief crontab不同的位置标记
*/
template <size_t N>
static void setCronField(std::string value, std::bitset<N>& target, cron_int minval, cron_int maxval);
static void setCronDaysOfWeek(std::string value, std::bitset<7>& target);
static void setCronDaysOfMonth(std::string value, std::bitset<31>& target);
static void setCronMonth(std::string value, std::bitset<12>& target);
/**
* @brief
*/
template <size_t N>
static size_t findNext(const std::bitset<N> & target, std::tm& tmt, unsigned int minimum, unsigned int maximum, unsigned int value,
CronField field, CronField next_field, const std::bitset<7> & marked_fields);
static bool findNext(const TC_Cron & cron, std::tm& tmt, size_t dot);
static size_t findNextDay(std::tm& tmt,std::bitset<31> const& days_of_month,size_t day_of_month,const std::bitset<7> & days_of_week,size_t day_of_week,const std::bitset<7> & marked_fields);
/**
* @brief
*/
template <size_t N>
static size_t nextSetbit(const std::bitset<N> & target, size_t minimum, size_t maximum, size_t offset);
static void addToField(std::tm& tmt, CronField field, int val);
static void setField(std::tm& tmt, CronField field, int val);
static void resetField(std::tm& tmt, CronField field);
static void resetAllFields(std::tm& tmt, const std::bitset<7> & marked_fields);
static void markField(std::bitset<7>& orders, CronField field);
};
template <size_t N>
void TC_Cron::setCronField(std::string value,std::bitset<N>& target,cron_int minval,cron_int maxval)
{
if (value.length() > 0 && value[value.length() - 1] == ',')
throw TC_CronException("[TC_Cron::setCronField] value cannot end with comma");
auto fields = TC_Common::sepstr<string>(value, ",");
if (fields.empty())
throw TC_CronException("[TC_Cron::setCronField] expression parsing error");
for (auto const& field : fields)
{
if (!contains(field, '/'))
{
auto item = makeRange(field, minval, maxval);
auto first = item.first;
auto last = item.second;
for (cron_int i = first - minval; i <= last - minval; ++i)
{
target.set(i);
}
}
else
{
auto parts = TC_Common::sepstr<string>(field, "/");
if (parts.size() != 2)
throw TC_CronException("[TC_Cron::setCronField] incrementer must have two fields");
auto item = makeRange(parts[0], minval, maxval);
auto first = item.first;
auto last = item.second;
if (!contains(parts[0], '-'))
{
last = maxval;
}
auto delta = to_cron_int(parts[1]);
if (delta <= 0)
throw TC_CronException("[TC_Cron::setCronField] incrementer must be a positive value");
for (cron_int i = first - minval; i <= last - minval; i += delta)
{
target.set(i);
}
}
}
}
template <size_t N>
size_t TC_Cron::nextSetbit(const std::bitset<N> & target,size_t minimum,size_t maximum,size_t offset)
{
for (auto i = offset; i < N; ++i)
{
if (target.test(i)) return i;
}
return INVALID_INDEX;
}
template <size_t N>
size_t TC_Cron::findNext(const std::bitset<N> & target, std::tm& tmt, unsigned int minimum, unsigned int maximum, unsigned int value,
CronField field, CronField next_field, const std::bitset<7> & marked_fields)
{
auto next_value = nextSetbit(target, minimum, maximum, value);
if (INVALID_INDEX == next_value)
{
addToField(tmt, next_field, 1);
resetField(tmt, field);
next_value = nextSetbit(target, minimum, maximum, 0);
}
if (INVALID_INDEX == next_value || next_value != value)
{
setField(tmt, field, static_cast<int>(next_value));
resetAllFields(tmt, marked_fields);
}
return next_value;
}
}

147
util/include/util/tc_des.h Normal file
View File

@ -0,0 +1,147 @@
#ifndef __TC_DES_H
#define __TC_DES_H
#include <string>
#include <stdint.h>
#include "util/tc_ex.h"
using namespace std;
namespace tars
{
/////////////////////////////////////////////////
/**
* @file tc_des.h
* @brief des加解密类(c代码)
*
* @author ruanshudong@qq.com
*/
/////////////////////////////////////////////////
/**
* @brief des异常.
*/
struct TC_DES_Exception : public TC_Exception
{
TC_DES_Exception(const string &buffer) : TC_Exception(buffer){};
~TC_DES_Exception() throw(){};
};
/**
* @brief des/3des加密解密源码, .
*
* d3des.h d3des.c修改完成.
*
* des加密,8,80x00,8,8.
*
* 8,:10x01,20x02,...
*
* 8,0x08.
*
* 3des加解密,:3des-ecb加密方式
*
* 24,240x00,24,24;
*
* 810x01,20x02,...
*
* 80x08.
*
* Key必须是null结束的字符串.
*
*/
class TC_Des
{
public:
/**
* @brief des加密.
*
* @param key key, 8
* @param sIn buffer
* @param iInLen buffer长度
* @return string
*/
static string encrypt(const char *key, const char *sIn, size_t iInlen);
/**
* @brief des解密.
*
* @param key key, 8
* @param sIn buffer
* @param iInlen buffer长度
* @return string , ,
*/
static string decrypt(const char *key, const char *sIn, size_t iInlen);
/**
* @brief 3des加密.
*
* @param key key, 24
* @param sIn buffer
* @param iInLen buffer长度
* @return string
*/
static string encrypt3(const char *key, const char *sIn, size_t iInlen);
/**
* @brief 3des解密.
* @param key key, 24
* @param sIn buffer
* @param iInlen buffer长度
* @return string解码后的内容, ,
*/
static string decrypt3(const char *key, const char *sIn, size_t iInlen);
/**
* @brief / .
*/
enum
{
EN0 = 0, /**加密*/
DE1 = 1 /**解密*/
};
protected:
/**
* @brief key.
*
* @param key key值
* @param mode 0, 1
*/
static void deskey(const char *key, short mode, uint32_t *k);
/**
* @brief des加密/.
*
* @param from 8
* @param to 8
*/
static void des(const char *from, char *to, uint32_t *KnL);
/**
* @brief key.
*
* @param key key值
* @param mode 0, 1
*/
static void des3key(const char *key, short mode, uint32_t *KnL, uint32_t *KnR, uint32_t *Kn3);
/**
* @brief 3des.
*
* @param from 8
* @param into 8
*/
static void des3(const char *from, char *into, uint32_t *KnL, uint32_t *KnR, uint32_t *Kn3);
private:
static void cookey(register uint32_t *raw1, uint32_t *k);
static void scrunch(register const char *outof, register uint32_t *into);
static void unscrun(register uint32_t *outof, register char *into);
static void desfunc(register uint32_t *block, register uint32_t *keys);
};
}
#endif

View File

@ -20,6 +20,7 @@
#include <string>
#include <memory>
#include <map>
#include <unordered_map>
#include <vector>
#include <list>
#include <algorithm>
@ -57,13 +58,10 @@ namespace tars
class PropertyReport;
class TC_EpollServer : public TC_ThreadLock, public TC_HandleBase
class TC_EpollServer : public TC_HandleBase
{
public:
////////////////////////////////////////////////////////////////////////////
/**
*
*/
enum EM_CLOSE_T
{
EM_CLIENT_CLOSE = 0, //客户端主动关闭
@ -83,14 +81,11 @@ public:
TCP_CONNECTION = 0,
UDP_CONNECTION = 1,
};
/**
*
* 线
*/
// using protocol_functor = std::function<TC_NetWorkBuffer::PACKET_TYPE(string&, string&)>;
// using header_filter_functor = std::function<int (int, string&)>;
// using conn_protocol_functor = std::function<TC_NetWorkBuffer::PACKET_TYPE(string&, string&, void*)>;
typedef std::function<TC_NetWorkBuffer::PACKET_TYPE(TC_NetWorkBuffer::PACKET_TYPE, vector<char>&)> header_filter_functor;
class NetThread;
@ -102,6 +97,7 @@ public:
typedef TC_AutoPtr<Handle> HandlePtr;
class RecvContext;
/**
*
* RecvContext创建出来
@ -159,8 +155,6 @@ public:
shared_ptr<SendContext> createSendContext() { return std::make_shared<SendContext>(shared_from_this(), 's'); }
shared_ptr<SendContext> createCloseContext() { return std::make_shared<SendContext>(shared_from_this(), 'c'); }
// int64_t _recvUS;
protected:
uint32_t _uid; /**连接标示*/
string _ip; /**远程连接的ip*/
@ -190,6 +184,7 @@ public:
int timeout;
int iLastRefreshTime;
};
////////////////////////////////////////////////////////////////////////////
/**
* @brief
@ -198,7 +193,7 @@ public:
/**
*
*/
class Handle : public TC_Thread, public TC_ThreadLock, public TC_HandleBase
class Handle : public TC_Thread, public TC_HandleBase
{
public:
/**
@ -223,6 +218,12 @@ public:
*/
TC_EpollServer* getEpollServer();
/**
* adapter
* @return
*/
BindAdapter *getBindAdapter() const { return _bindAdapter; }
/**
* Handle的索引(0~handle个数-1)
* @return
@ -276,7 +277,7 @@ public:
virtual void initialize() {};
/**
* HandleGroup中的handle线程
* handle对应的处理线程
*/
virtual void notifyFilter();
@ -320,14 +321,11 @@ public:
*/
virtual void handleOverload(const shared_ptr<TC_EpollServer::RecvContext> &data);
/**
* Filter的消息
*/
//virtual void handleFilter() {};
/**
*
*/
virtual void handleAsyncResponse() {}
/**
* handleFilter拆分的第二部分
* bExpectIdle参数
@ -390,7 +388,6 @@ public:
*/
TC_EpollServer *_pEpollServer;
/**
* handle对应的网路线程(线handle线程合并的情况下有效)
*/
@ -411,10 +408,6 @@ public:
*/
uint32_t _handleIndex;
/**
* handle组
*/
// HandleGroupPtr _handleGroup;
};
using close_functor = std::function<void (void*, EM_CLOSE_T )>;
@ -422,7 +415,7 @@ public:
////////////////////////////////////////////////////////////////////////////
// 服务端口管理,监听socket信息
class BindAdapter : public TC_ThreadLock, public TC_HandleBase
class BindAdapter : public TC_HandleBase
{
public:
/**
@ -617,13 +610,13 @@ public:
* ip
* @return vector<string>: ip列表
*/
vector<string> getAllow() const;
const vector<string> &getAllow() const;
/**
* ip
* @return vector<string>: ip列表
*/
vector<string> getDeny() const;
const vector<string> &getDeny() const;
/**
* allow deny次序
@ -670,7 +663,7 @@ public:
* EpollServer
* @return TC_EpollServer*
*/
TC_EpollServer* getEpollServer();
TC_EpollServer* getEpollServer() const { return _pEpollServer; }
/**
* 线
@ -679,11 +672,6 @@ public:
*/
inline NetThread* getNetThreadOfFd(int fd) const { return _pEpollServer->getNetThreadOfFd(fd); }
/**
*
* @param pp
*/
// void setProtocol(const protocol_functor& pf, int iHeaderLen = 0, const header_filter_functor& hf = echo_header_filter);
/**
*
* @param pp
@ -743,12 +731,6 @@ public:
*/
int getHeaderFilterLen();
/**
* ServantHandle数目
* @param n
*/
void setHandleNum(int n);
/**
* handle组的handle数(handle一个对象)
* @return int
@ -776,7 +758,7 @@ public:
_iHandleNum = _pEpollServer->_netThreadNum;
}
for (size_t i = 0; i < _iHandleNum ; ++i)
for (int32_t i = 0; i < _iHandleNum ; ++i)
{
HandlePtr handle = new T();
@ -801,15 +783,15 @@ public:
return _handles[index];
}
/**
*
*/
void setBackPacketBuffLimit(size_t iLimitSize);
// /**
// * 设置服务端回包缓存的大小限制
// */
// void setBackPacketBuffLimit(size_t iLimitSize);
/**
*
*/
size_t getBackPacketBuffLimit();
// /**
// * 获取服务端回包缓存的大小限制
// */
// size_t getBackPacketBuffLimit();
/**
@ -874,23 +856,23 @@ public:
public:
//统计上报的对象
PropertyReport * _pReportQueue = NULL;
PropertyReport * _pReportConRate = NULL;
PropertyReport * _pReportQueue = NULL;
PropertyReport * _pReportConRate = NULL;
PropertyReport * _pReportTimeoutNum = NULL;
protected:
friend class TC_EpollServer;
/**
*
*/
TC_EpollServer *_pEpollServer;
/**
*
*/
mutable std::mutex _mutex;
/**
*
*/
TC_EpollServer *_pEpollServer = NULL;
/**
* Adapter所用的HandleGroup
*/
@ -910,6 +892,7 @@ public:
* adapter的名字
*/
string _name;
/**
* fd
*/
@ -987,8 +970,8 @@ public:
*/
string _protocolName;
//回包缓存限制大小
size_t _iBackPacketBuffLimit;
// 回包缓存限制大小
// size_t _iBackPacketBuffLimit;
//队列模式
bool _queueMode = false;
@ -1187,10 +1170,10 @@ public:
* Udp
*/
int recvUdp();
/**
*
* @param o
* @return int: <0:, 0:, 1:
*/
int parseProtocol(TC_NetWorkBuffer &rbuf);
@ -1258,7 +1241,7 @@ public:
/**
* buffer
*/
TC_NetWorkBuffer _recvBuffer;
TC_NetWorkBuffer _recvBuffer;
/**
* buffer
@ -1306,7 +1289,7 @@ public:
/**
* map
*/
class ConnectionList : public TC_ThreadLock
class ConnectionList
{
public:
/**
@ -1395,7 +1378,7 @@ public:
/**
*
*/
NetThread *_pEpollServer;
NetThread *_pEpollServer;
/**
*
@ -1432,6 +1415,7 @@ public:
*/
uint32_t _iConnectionMagic;
};
////////////////////////////////////////////////////////////////////////////
class NetThread : public TC_Thread, public TC_HandleBase
{
@ -1455,12 +1439,6 @@ public:
*/
int getIndex() const { return _threadIndex; }
/**
* socket
* @param ls
*/
// int bind(BindAdapterPtr &lsPtr);
/**
* 线
*/
@ -1474,7 +1452,7 @@ public:
/**
* epoll
*/
void createEpoll(uint32_t iIndex = 0);
void createEpoll(uint32_t maxAllConn);
/**
* udp监听
@ -1531,31 +1509,25 @@ public:
*
* @param s
*/
void debug(const string &s);
void debug(const string &s) const;
/**
* INFO日志
* @param s
*/
void info(const string &s);
void info(const string &s) const;
/**
* TARS日志
* @param s
*/
void tars(const string &s);
void tars(const string &s) const;
/**
*
* @param s
*/
void error(const string &s);
/**
* tars日志
* @param s
*/
void tars(const string &s) const;
void error(const string &s) const;
/**
*
@ -1609,7 +1581,7 @@ public:
* @param bEraseList
* @param closeType ,0:1:;2:
*/
void delConnection(Connection *cPtr, bool bEraseList = true,EM_CLOSE_T closeType=EM_CLIENT_CLOSE);
void delConnection(Connection *cPtr, bool bEraseList = true, EM_CLOSE_T closeType=EM_CLIENT_CLOSE);
/**
*
@ -1639,7 +1611,7 @@ public:
/**
*
*/
TC_EpollServer *_epollServer;
TC_EpollServer *_epollServer;
/**
* net线程的id
@ -1661,11 +1633,6 @@ public:
*/
bool _bTerminate;
/**
* handle是否已经启动
*/
bool _handleStarted;
/**
* epoll
*/
@ -1686,6 +1653,7 @@ public:
*/
bool _bEmptyConnAttackCheck;
/**
* ,,2s,
* adapter自身的超时时间
@ -1725,11 +1693,6 @@ public:
*/
void setEmptyConnTimeout(int timeout);
/**
*NetThread的内存池信息
*/
// void setNetThreadBufferPoolInfo(size_t minBlock, size_t maxBlock, size_t maxBytes);
/**
*
* @param plocalLogger
@ -1754,70 +1717,6 @@ public:
*/
inline bool isMergeHandleNetThread() const { return _mergeHandleNetThread; }
// /**
// * 绑定到一个已经有的handle组上
// * @param groupName
// * @param handleNum
// * @param adapter
// */
// void setHandleGroup(const string& groupName, BindAdapterPtr adapter)
// {
// map<string, HandleGroupPtr>::iterator it = _handleGroups.find(groupName);
// if (it != _handleGroups.end())
// {
// it->second->adapters[adapter->getName()] = adapter;
// adapter->_handleGroup = it->second;
// }
// }
// /**
// * 创建一个handle对象组如果已经存在则直接返回
// * @param name
// * @return HandlePtr
// */
// template<class T> void setHandleGroup(const string& groupName, int32_t handleNum, BindAdapterPtr adapter)
// {
// map<string, HandleGroupPtr>::iterator it = _handleGroups.find(groupName);
// if (it == _handleGroups.end())
// {
// HandleGroupPtr hg = new HandleGroup();
// hg->name = groupName;
// adapter->_handleGroup = hg;
// for (int32_t i = 0; i < handleNum; ++i)
// {
// HandlePtr handle = new T();
// handle->setEpollServer(this);
// handle->setHandleGroup(hg);
// hg->handles.push_back(handle);
// }
// _handleGroups[groupName] = hg;
// it = _handleGroups.find(groupName);
// }
// it->second->adapters[adapter->getName()] = adapter;
// adapter->_handleGroup = it->second;
// }
// /**
// * 选择网络线程
// * @param fd
// */
// NetThread* getNetThreadOfFd(int fd)
// {
// return _netThreads[fd % _netThreads.size()];
// }
/**
* socket
* @param ls
@ -1908,25 +1807,25 @@ public:
*
* @param s
*/
void debug(const string &s);
void debug(const string &s) const;
/**
* INFO日志
* @param s
*/
void info(const string &s);
void info(const string &s) const;
/**
*
* @param s
*/
void error(const string &s);
void error(const string &s) const;
/**
* tars日志
* @param s
*/
void tars(const string &s);
void tars(const string &s) const;
/**
* 线
@ -1987,16 +1886,11 @@ protected:
void bind(const TC_Endpoint &ep, TC_Socket &s, bool manualListen);
static void applicationCallback(TC_EpollServer *epollServer);
// public:
//统计服务端相应队列大小的上报的对象
// PropertyReport * _pReportRspQueue;
private:
/**
* 线
*/
std::vector<NetThread*> _netThreads;
std::vector<NetThread*> _netThreads;
/*
* 线
@ -2027,12 +1921,12 @@ private:
* 线
*/
bool _mergeHandleNetThread = false;
/**
*
*/
RollWrapperInterface *_pLocalLogger;
/**
*
*/
@ -2053,12 +1947,10 @@ private:
*/
heartbeat_callback_functor _heartFunc;
/**
* handle对象
*/
// map<string, HandleGroupPtr> _handleGroups;
};
typedef TC_AutoPtr<TC_EpollServer> TC_EpollServerPtr;
}
#endif

View File

@ -31,6 +31,9 @@ namespace tars
*/
/////////////////////////////////////////////////
/**
* @brief .
*/
class TC_Exception : public exception
{
public:

View File

@ -75,6 +75,16 @@ public:
*/
static bool compress(const char *src, size_t length, vector<char>& buffer);
/**
* @brief
*
* @param src
* @param length
* @param buffer buffer
* @return bool
*/
static bool compress(const char *src, size_t length, string& buffer);
/**
* @brief
*

View File

@ -42,7 +42,6 @@ namespace tars
struct TC_HashMap_Exception : public TC_Exception
{
TC_HashMap_Exception(const string &buffer) : TC_Exception(buffer){};
TC_HashMap_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_HashMap_Exception() throw(){};
};
@ -891,7 +890,7 @@ public:
*
* @return int
*/
size_t getIndex() const { return _iIndex; }
int getIndex() const { return (int)_iIndex; }
/**
* @brief item
@ -1018,7 +1017,6 @@ public:
* @brief map头
*/
#pragma pack(1)
struct tagMapHead
{
char _cMaxVersion; /**大版本*/
@ -1749,13 +1747,13 @@ protected:
* @brief hash链表数据个数+1
* @param index
*/
void incListCount(uint32_t index) { update(&item(index)->_iListCount, item(index)->_iListCount+1); }
void incListCount(uint32_t index) { update(&item(index)->_iListCount, (uint32_t)item(index)->_iListCount+1); }
/**
* @brief hash链表数据个数+1
* @param index
*/
void delListCount(size_t index) { update(&item(index)->_iListCount, item(index)->_iListCount-1); }
void delListCount(size_t index) { update(&item(index)->_iListCount, (uint32_t)item(index)->_iListCount-1); }
/**
* @brief

View File

@ -44,7 +44,6 @@ namespace tars
struct TC_HashMapCompact_Exception : public TC_Exception
{
TC_HashMapCompact_Exception(const string &buffer) : TC_Exception(buffer){};
TC_HashMapCompact_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_HashMapCompact_Exception() throw(){};
};
@ -110,7 +109,7 @@ public:
uint32_t _iGetPrev; /**Get链上的上一个Block*/
uint32_t _iSyncTime; /**上次缓写时间*/
uint32_t _iExpireTime; /** 数据过期的绝对时间由设置或更新数据时提供0表示不关心此时间*/
uint8_t _iVersion; /** 数据版本1为初始版本0为保留*/
uint8_t _iVersion; /** 数据版本1为初始版本0为保留*/
bool _bDirty; /**是否是脏数据*/
bool _bOnlyKey; /**是否只有key, 没有内容*/
bool _bNextChunk; /**是否有下一个chunk*/
@ -1073,9 +1072,8 @@ public:
uint32_t _iBackupTail; //热备指针
uint32_t _iSyncTail; //回写链表
uint32_t _iOnlyKeyCount; // OnlyKey个数
bool _bInit; //是否已经完成初始化
bool _bInit; //是否已经完成初始化
char _cReserve[15]; //保留
//uint32_t _iReserve[4]; //保留
};
/**
@ -1107,7 +1105,6 @@ public:
uint32_t _iListCount; /**链表个数*/
};
#pragma pack()
/**
* @brief 64, 3264
* */
@ -1179,7 +1176,7 @@ public:
/**
* @brief hash处理器
*/
using hash_functor = std::function<size_t (const string& )>;
typedef std::function<size_t(const string &)> hash_functor;
//////////////////////////////////////////////////////////////////////////////////////////////
//map的接口定义

View File

@ -76,7 +76,7 @@ struct TC_Http_Exception : public TC_Exception
struct TC_HttpResponse_Exception : public TC_Http_Exception
{
TC_HttpResponse_Exception(const string &sBuffer) : TC_Http_Exception(sBuffer){};
~TC_HttpResponse_Exception() throw(){};
~TC_HttpResponse_Exception() {};
};
/**
@ -85,11 +85,10 @@ struct TC_HttpResponse_Exception : public TC_Http_Exception
struct TC_HttpRequest_Exception : public TC_Http_Exception
{
TC_HttpRequest_Exception(const string &sBuffer) : TC_Http_Exception(sBuffer){};
~TC_HttpRequest_Exception() throw(){};
~TC_HttpRequest_Exception() {};
};
class TC_TCPClient;
class TC_HttpRequest;
class TC_HttpResponse;
@ -293,14 +292,6 @@ protected:
*/
string toURL();
/**
* @brief .
*
* @param frag
* @return string
*/
//string getFragment(const string& frag) const;
/**
* @brief
*
@ -364,18 +355,7 @@ public:
*/
struct CmpCase
{
bool operator()(const string &s1, const string &s2) const
{
//return TC_Common::upper(s1) < TC_Common::upper(s2);
if(TC_Port::strcasecmp(s1.c_str(), s2.c_str()) < 0)
{
return true;
}
else
{
return false;
}
}
bool operator()(const string &s1, const string &s2) const;
};
typedef multimap<string, string, CmpCase> http_header_type;
@ -467,21 +447,7 @@ public:
* @param sHeadName header的名字
* @param sHeadValue header的值
*/
void setHeader(const string &sHeadName, const string &sHeadValue)
{
//Set-Cookie和Cookie可以有多个头
const char * pStr1 = "SET-COOKIE";
const char * pStr2 = "COOKIE";//原则上COOKIE只有一个担心有兼容性问题保留
if((TC_Port::strcasecmp(sHeadName.c_str(), pStr1) != 0) && (TC_Port::strcasecmp(sHeadName.c_str(), pStr2) != 0))
{
_headers.erase(sHeadName);
}
/* if(TC_Common::upper(sHeadName) != "SET-COOKIE" && TC_Common::upper(sHeadName) != "COOKIE")
{
_headers.erase(sHeadName);
}*/
_headers.insert(multimap<string, string>::value_type(sHeadName, sHeadValue));
}
void setHeader(const string &sHeadName, const string &sHeadValue);
/**
* @brief header使get/set方法设
@ -759,7 +725,7 @@ public:
*
* @return list<Cookie>&
*/
list<Cookie> getAllCookie();
const list<Cookie>& getAllCookie();
/**
* @brief CookieCookie不删除
@ -990,6 +956,13 @@ public:
*/
void parseResponseHeader(const char* szBuffer);
protected:
/**
* ,
* @param sBuffer
*/
void addContent(const string &sBuffer);
protected:
/**
@ -1016,6 +989,12 @@ protected:
* content length
*/
size_t _iTmpContentLength;
/**
*
*/
size_t _iRecvContentLength;
};
/********************* TC_HttpRequest ***********************/
@ -1124,6 +1103,7 @@ public:
* (, encode的时候创建的)
*/
void setRequest(const string& method, const string &sUrl, const std::string& body = "", bool bNewCreateHost = false);
/**
* @brief Get请求包.
*

View File

@ -38,7 +38,7 @@ namespace tars
* 1 线
* 2 http短连接
* 3 RequestCallback回调里面, onSucc和onFailed是对应的, , onSucc/onFailed其中之一会被唯一响应
* @author jarodruan@tencent.com
* @author ruanshudong@qq.com
*/
/////////////////////////////////////////////////
@ -434,6 +434,7 @@ protected:
bool _bindAddrSet;
};
}
#endif

View File

@ -1,30 +1,13 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

#ifndef __TC_JSON_H
#define __TC_JSON_H
#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <list>
#include <assert.h>
#include <stdio.h>
#include <sstream>
#include "util/tc_autoptr.h"
@ -38,110 +21,31 @@ namespace tars
*/
struct TC_Json_Exception : public TC_Exception
{
TC_Json_Exception(const string &buffer) : TC_Exception(buffer){};
~TC_Json_Exception() throw(){};
TC_Json_Exception(const string &buffer) : TC_Exception(buffer){};
~TC_Json_Exception() throw(){};
};
enum eJsonType
{
eJsonTypeString,
eJsonTypeNum,
eJsonTypeObj,
eJsonTypeArray,
eJsonTypeBoolean,
eJsonTypeNull,
eJsonTypeString,
eJsonTypeNum,
eJsonTypeObj,
eJsonTypeArray,
eJsonTypeBoolean
};
/*
* json字符串用到的
*/
class BufferJsonReader
{
const char * _buf; ///< 缓冲区
size_t _buf_len; ///< 缓冲区长度
size_t _cur; ///< 当前位置
public:
BufferJsonReader () :_buf(NULL),_buf_len(0), _cur(0) {}
void reset() { _cur = 0;}
void setBuffer(const char * buf, size_t len)
{
_buf = buf;
_buf_len = len;
_cur = 0;
}
void setBuffer(const std::vector<char> &buf)
{
_buf = buf.data();
_buf_len = buf.size();
_cur = 0;
}
const size_t &getCur() const { return _cur; }
const char * getPoint() const { return _buf+_cur; }
const char &read() { check(); _cur ++; return *(_buf+_cur-1); }
const char &get() const { check(); return *(_buf+_cur); }
const char &getBack() const { assert(_cur>0); return *(_buf+_cur-1); }
void back() { assert(_cur>0); _cur--; }
void check() const
{
if (_cur + 1 > _buf_len)
{
char s[64];
snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len);
throw TC_Json_Exception(s);
}
}
bool hasEnd() const { return _cur >= _buf_len; }
};
class JsonValue;
class JsonValueObj;
class JsonValueArray;
class JsonValueString;
class JsonValueBoolean;
class JsonValueNum;
class JsonValueNull;
typedef TC_AutoPtr<JsonValue> JsonValuePtr;
typedef TC_AutoPtr<JsonValueObj> JsonValueObjPtr;
typedef TC_AutoPtr<JsonValueNum> JsonValueNumPtr;
typedef TC_AutoPtr<JsonValueArray> JsonValueArrayPtr;
typedef TC_AutoPtr<JsonValueString> JsonValueStringPtr;
typedef TC_AutoPtr<JsonValueBoolean> JsonValueBooleanPtr;
typedef TC_AutoPtr<JsonValueNull> JsonValueNullPtr;
/*
* json类型的基类
*/
class JsonValue : public TC_HandleBase
{
public:
virtual eJsonType getType() const = 0;
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c) = 0;
virtual ~JsonValue() { }
public:
static JsonValuePtr createJsonValue(BufferJsonReader & reader);
static uint32_t getHex(BufferJsonReader & reader);
static bool isspace(char c);
static void writeString(const string &str, ostream& ostr);
protected:
JsonValue(){};
virtual eJsonType getType()=0;
virtual ~JsonValue()
{
}
};
typedef TC_AutoPtr<JsonValue> JsonValuePtr;
/*
* json类型 string类型 "dd\ndfd"
@ -149,21 +53,24 @@ protected:
class JsonValueString : public JsonValue
{
public:
JsonValueString(const string & s):value(s)
{
}
JsonValueString()
{
}
JsonValueString(const string & s):value(s)
{
}
JsonValueString()
{
eJsonType getType() const { return eJsonTypeString; }
}
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c);
virtual ~JsonValueString() { }
string value;
eJsonType getType()
{
return eJsonTypeString;
}
virtual ~JsonValueString()
{
}
string value;
};
typedef TC_AutoPtr<JsonValueString> JsonValueStringPtr;
/*
* json类型 number类型 1.5e8
@ -171,23 +78,24 @@ public:
class JsonValueNum : public JsonValue
{
public:
JsonValueNum(double d,bool b=false):value(d),isInt(b)
{
}
JsonValueNum()
{
isInt=false;
JsonValueNum(double d,bool b=false):value(d),isInt(b)
{
}
JsonValueNum()
{
isInt=false;
value=0.0f;
}
eJsonType getType() const { return eJsonTypeNum; }
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c);
virtual ~JsonValueNum(){}
}
eJsonType getType()
{
return eJsonTypeNum;
}
virtual ~JsonValueNum(){}
public:
double value;
bool isInt;
double value;
bool isInt;
};
typedef TC_AutoPtr<JsonValueNum> JsonValueNumPtr;
/*
* json类型 object类型 {"aa","bb"}
@ -195,14 +103,15 @@ public:
class JsonValueObj: public JsonValue
{
public:
eJsonType getType() const { return eJsonTypeObj; }
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c);
const JsonValuePtr &get(const char *name);
virtual ~JsonValueObj();
eJsonType getType()
{
return eJsonTypeObj;
}
virtual ~JsonValueObj(){}
public:
unordered_map<string, JsonValuePtr> value;
unordered_map<string,JsonValuePtr> value;
};
typedef TC_AutoPtr<JsonValueObj> JsonValueObjPtr;
/*
* json类型 array类型 ["aa","bb"]
@ -210,14 +119,19 @@ public:
class JsonValueArray: public JsonValue
{
public:
eJsonType getType() const { return eJsonTypeArray; }
void push_back(const JsonValuePtr & p) { value.push_back(p); }
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c = 0);
virtual ~JsonValueArray();
eJsonType getType()
{
return eJsonTypeArray;
}
void push_back(JsonValuePtr & p)
{
value.push_back(p);
}
virtual ~JsonValueArray(){}
public:
vector<JsonValuePtr> value;
vector<JsonValuePtr> value;
};
typedef TC_AutoPtr<JsonValueArray> JsonValueArrayPtr;
/*
* json类型 boolean类型 true
@ -225,29 +139,98 @@ public:
class JsonValueBoolean : public JsonValue
{
public:
eJsonType getType() const { return eJsonTypeBoolean; }
bool getValue() const { return value; }
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader,char c);
virtual ~JsonValueBoolean(){}
eJsonType getType()
{
return eJsonTypeBoolean;
}
bool getValue()
{
return value;
}
virtual ~JsonValueBoolean(){}
public:
bool value;
bool value;
};
typedef TC_AutoPtr<JsonValueBoolean> JsonValueBooleanPtr;
/*
* json类型 null类型 "dd\ndfd"
* json字符串用到的
*/
class JsonValueNull : public JsonValue
class BufferJsonReader
{
const char * _buf; ///< 缓冲区
size_t _buf_len; ///< 缓冲区长度
size_t _cur; ///< 当前位置
public:
JsonValueNull() { }
eJsonType getType() const { return eJsonTypeNull; }
BufferJsonReader () :_buf(NULL),_buf_len(0), _cur(0) {}
virtual void write(ostream& ostr) const;
virtual void read(BufferJsonReader & reader, char c);
void reset() { _cur = 0;}
virtual ~JsonValueNull() { }
void setBuffer(const char * buf, size_t len)
{
_buf = buf;
_buf_len = len;
_cur = 0;
}
void setBuffer(const std::vector<char> &buf)
{
_buf = buf.data();
_buf_len = buf.size();
_cur = 0;
}
size_t getCur()
{
return _cur;
}
const char * getPoint()
{
return _buf+_cur;
}
char read()
{
check();
_cur ++;
return *(_buf+_cur-1);
}
char get()
{
check();
return *(_buf+_cur);
}
char getBack()
{
assert(_cur>0);
return *(_buf+_cur-1);
}
void back()
{
assert(_cur>0);
_cur--;
}
void check()
{
if (_cur + 1 > _buf_len)
{
char s[64];
snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len);
throw TC_Json_Exception(s);
}
}
bool hasEnd()
{
return _cur >= _buf_len;
}
};
/*
@ -256,15 +239,71 @@ public:
class TC_Json
{
public:
//json类型到字符串的转换
static string writeValue(const JsonValuePtr &p);
//解析成智能指针
static JsonValuePtr getValue(const string & str);
//json类型到字符串的转换
static string writeValue(const JsonValuePtr & p);
static void writeValue(const JsonValuePtr & p, string& ostr);
//json字符串到json结构的转换
static JsonValuePtr getValue(const string & str);
private:
//string 类型到json字符串
static void writeString(const JsonValueStringPtr & p, string& ostr);
static void writeString(const string & s, string& ostr);
//num 类型到json字符串
static void writeNum(const JsonValueNumPtr & p, string& ostr);
//obj 类型到json字符串
static void writeObj(const JsonValueObjPtr & p, string& ostr);
//array 类型到json字符串
static void writeArray(const JsonValueArrayPtr & p, string& ostr);
//boolean 类型到json字符串
static void writeBoolean(const JsonValueBooleanPtr & p, string& ostr);
//读取json的value类型 也就是所有的json类型 如果不符合规范会抛异常
static JsonValuePtr getValue(BufferJsonReader & reader);
//读取json的object 如果不符合规范会抛异常
static JsonValueObjPtr getObj(BufferJsonReader & reader);
//读取json的array(数组) 如果不符合规范会抛异常
static JsonValueArrayPtr getArray(BufferJsonReader & reader);
//读取json的string 如 "dfdf" 如果不符合规范会抛异常
static JsonValueStringPtr getString(BufferJsonReader & reader,char head='\"');
//读取json的数字 如 -213.56 如果不符合规范会抛异常
static JsonValueNumPtr getNum(BufferJsonReader & reader,char head);
//读取json的boolean值 如 true false 如果不符合规范会抛异常
static JsonValueBooleanPtr getBoolean(BufferJsonReader & reader,char c);
//读取json的 null 如果不符合规范会抛异常
static JsonValuePtr getNull(BufferJsonReader & reader,char c);
//获取16进制形式的值 如\u003f 如果不符合规范会抛异常
static uint32_t getHex(BufferJsonReader & reader);
//判断一个字符是否符合json定义的空白字符
static bool isspace(char c);
};
class TC_JsonWriteOstream
{
public:
static void writeValue(const JsonValuePtr & p, ostream& ostr);
private:
//string 类型到json字符串
static void writeString(const JsonValueStringPtr & p, ostream& ostr);
static void writeString(const string & s, ostream& ostr);
//num 类型到json字符串
static void writeNum(const JsonValueNumPtr & p, ostream& ostr);
//obj 类型到json字符串
static void writeObj(const JsonValueObjPtr & p, ostream& ostr);
//array 类型到json字符串
static void writeArray(const JsonValueArrayPtr & p, ostream& ostr);
//boolean 类型到json字符串
static void writeBoolean(const JsonValueBooleanPtr & p, ostream& ostr);
};
}
#endif

View File

@ -40,7 +40,6 @@ namespace tars
struct TC_Lock_Exception : public TC_Exception
{
TC_Lock_Exception(const string &buffer) : TC_Exception(buffer){};
TC_Lock_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_Lock_Exception() throw() {};
};
@ -125,9 +124,9 @@ public:
protected:
/**
* @brief
* TC_LockT相似
*
* @brief
* TC_LockT相似
*
*/
TC_LockT(const T& mutex, bool) : _mutex(mutex)
{
@ -172,8 +171,8 @@ class TC_EmptyMutex
{
public:
/**
* @brief .
*
* @brief .
*
* @return int, 0
*/
int lock() const {return 0;}
@ -184,8 +183,8 @@ public:
int unlock() const {return 0;}
/**
* @brief .
*
* @brief .
*
* @return int, 0
*/
bool trylock() const {return true;}
@ -201,40 +200,40 @@ class TC_RW_RLockT
{
public:
/**
* @brief
*
* @brief
*
* @param lock
*/
TC_RW_RLockT(T& lock)
: _rwLock(lock),_acquired(false)
{
_rwLock.ReadLock();
_acquired = true;
}
TC_RW_RLockT(T& lock)
: _rwLock(lock),_acquired(false)
{
_rwLock.readLock();
_acquired = true;
}
/**
* @brief
* @brief
*/
~TC_RW_RLockT()
{
if (_acquired)
{
_rwLock.Unlock();
}
}
~TC_RW_RLockT()
{
if (_acquired)
{
_rwLock.unReadLock();
}
}
private:
/**
*
*/
const T& _rwLock;
/**
*
*/
const T& _rwLock;
/**
*
*/
mutable bool _acquired;
TC_RW_RLockT(const TC_RW_RLockT&);
TC_RW_RLockT& operator=(const TC_RW_RLockT&);
TC_RW_RLockT(const TC_RW_RLockT&);
TC_RW_RLockT& operator=(const TC_RW_RLockT&);
};
template <typename T>
@ -242,38 +241,38 @@ class TC_RW_WLockT
{
public:
/**
* @brief
*
* @brief
*
* @param lock
*/
TC_RW_WLockT(T& lock)
: _rwLock(lock),_acquired(false)
{
_rwLock.WriteLock();
_acquired = true;
}
TC_RW_WLockT(T& lock)
: _rwLock(lock),_acquired(false)
{
_rwLock.writeLock();
_acquired = true;
}
/**
* @brief
* @brief
*/
~TC_RW_WLockT()
{
if(_acquired)
{
_rwLock.Unlock();
}
}
~TC_RW_WLockT()
{
if(_acquired)
{
_rwLock.unWriteLock();
}
}
private:
/**
*
*/
const T& _rwLock;
/**
*
*/
const T& _rwLock;
/**
*
*/
mutable bool _acquired;
TC_RW_WLockT(const TC_RW_WLockT&);
TC_RW_WLockT& operator=(const TC_RW_WLockT&);
TC_RW_WLockT(const TC_RW_WLockT&);
TC_RW_WLockT& operator=(const TC_RW_WLockT&);
};
};

View File

@ -33,16 +33,16 @@ namespace tars
*/
/////////////////////////////////////////////////
template<typename T, int Size=5>
template<typename T>
class TC_LoopQueue
{
public:
typedef vector<T> queue_type;
TC_LoopQueue(uint32_t iSize=Size)
TC_LoopQueue(size_t iSize)
{
//做个保护 最多不能超过 1000000
assert(iSize<1000000);
// assert(iSize<1000000);
_iBegin = 0;
_iEnd = 0;
_iCapacitySub = iSize;
@ -56,7 +56,7 @@ public:
//delete _p;
}
bool push_back(const T &t,bool & bEmpty,uint32_t & iBegin,uint32_t & iEnd)
bool push_back(const T &t,bool & bEmpty, size_t & iBegin, size_t & iEnd)
{
bEmpty = false;
//uint32_t iEnd = _iEnd;
@ -90,7 +90,7 @@ public:
bool push_back(const T &t,bool & bEmpty)
{
bEmpty = false;
uint32_t iEnd = _iEnd;
size_t iEnd = _iEnd;
if((iEnd > _iBegin && iEnd - _iBegin < 2) ||
( _iBegin > iEnd && _iBegin - iEnd > (_iCapacity-2) ) )
{
@ -128,7 +128,7 @@ public:
bool push_back(const queue_type &vt)
{
uint32_t iEnd=_iEnd;
size_t iEnd=_iEnd;
if(vt.size()>(_iCapacity-1) ||
(iEnd>_iBegin && (iEnd-_iBegin)<(vt.size()+1)) ||
( _iBegin>iEnd && (_iBegin-iEnd)>(_iCapacity-vt.size()-1) ) )
@ -137,7 +137,7 @@ public:
}
else
{
for(uint32_t i=0;i<vt.size();i++)
for(size_t i=0;i<vt.size();i++)
{
memcpy(_p+_iBegin,&vt[i],sizeof(T));
//*(_p+_iBegin) = vt[i];
@ -199,29 +199,29 @@ public:
return false;
}
uint32_t size()
size_t size()
{
uint32_t iBegin=_iBegin;
uint32_t iEnd=_iEnd;
size_t iBegin=_iBegin;
size_t iEnd=_iEnd;
if(iBegin<iEnd)
return iBegin+_iCapacity-iEnd;
return iBegin-iEnd;
}
uint32_t getCapacity()
size_t getCapacity()
{
return _iCapacity;
}
private:
T * _p;
uint32_t _iCapacity;
uint32_t _iCapacitySub;
uint32_t _iBegin;
uint32_t _iEnd;
size_t _iCapacity;
size_t _iCapacitySub;
size_t _iBegin;
size_t _iEnd;
};
}
#endif
#endif /* ----- #ifndef _TC_LOOP_QUEUE_H_ ----- */

View File

@ -166,7 +166,6 @@ namespace tars
void deallocate(void *pAddr);
#pragma pack(1)
struct tagSpanAlloc
{
size_t _iSpanSize; /**span区块大小*/
@ -663,7 +662,6 @@ namespace tars
*
*/
#pragma pack(1)
struct tagChunkAllocatorHead
{
bool _bProtectedArea;

View File

@ -2,6 +2,7 @@
#define __TC_MD5_H
#include <string>
#include "util/tc_ex.h"
#include "util/tc_common.h"
using namespace std;
@ -41,6 +42,15 @@ namespace tars
}
#endif
/**
* @brief md5异常.
*/
struct TC_MD5_Exception : public TC_Exception
{
TC_MD5_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_MD5_Exception() throw(){};
};
class TC_MD5
{
typedef unsigned char *POINTER;
@ -75,7 +85,8 @@ public:
* @param sString
* @return string ,16
*/
static string md5bin(const string &sString);
static vector<char> md5bin(const string &sString);
static vector<char> md5bin(const char *buffer, size_t length);
/**
* @brief md5处理
@ -83,12 +94,14 @@ public:
* @param sString
* @return string ,32
*/
static string md5str (const string &sString);
static string md5str(const string &sString);
static string md5str (const char *buffer, size_t length);
/**
* @brief md5处理.
*
* @param fileName
* @throw TC_MD5_Exception,
* @return string
*/
static string md5file(const string& fileName);

View File

@ -159,7 +159,6 @@ public:
* @brief chunk头部
*/
#pragma pack(1)
struct tagChunkHead
{
size_t _iBlockSize; /**区块大小*/
@ -326,7 +325,6 @@ public:
* @brief
*/
#pragma pack(1)
struct tagChunkAllocatorHead
{
size_t _iSize;
@ -540,7 +538,6 @@ public:
* @brief
*/
#pragma pack(1)
struct tagChunkAllocatorHead
{
size_t _iSize; /**当前块大小*/

View File

@ -64,6 +64,15 @@ public:
* @param pAddr
*/
void connect(void *pAddr, size_t iSize);
/**
* @author goodenpei
* 2016-09-12
* @brief
* @param sOut
* @return bool,true:, false: ,,
*/
bool front(string &sOut);
/**
* @brief
@ -151,7 +160,6 @@ protected:
* @brief
*/
#pragma pack(1)
struct CONTROL_BLOCK
{
size_t iMemSize; /**内存大小*/

View File

@ -37,7 +37,6 @@ namespace tars
struct TC_MemVectorException : public TC_Exception
{
TC_MemVectorException(const string &buffer) : TC_Exception(buffer){};
TC_MemVectorException(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_MemVectorException() throw(){};
};
@ -94,7 +93,7 @@ public:
/**
* @brief
* @param iCount
* @param iCount
* @return size_t
*/
static size_t calcMemSize(size_t iCount)
@ -233,14 +232,14 @@ public:
*
* @return TC_MemVectorIterator
*/
TC_MemVectorIterator begin() { return TC_MemVectorIterator(this, 0); }
TC_MemVectorIterator begin() { return TC_MemVectorIterator(this, 0); }
/**
*
*
* @return TC_MemVectorIterator
*/
TC_MemVectorIterator end() { return TC_MemVectorIterator(this, _pHead->_iBlockCount); }
TC_MemVectorIterator end() { return TC_MemVectorIterator(this, _pHead->_iBlockCount); }
/**
* @brief
@ -251,8 +250,8 @@ public:
{
if(iIndex >= _pHead->_iBlockCount)
{
ostringstream s;
s << string("[TC_MemVector::get] index beyond : index = ") << iIndex << " > " << _pHead->_iBlockCount;
ostringstream s;
s << string("[TC_MemVector::get] index beyond : index = ") << iIndex << " > " << _pHead->_iBlockCount;
throw TC_MemVectorException(s.str());
}
@ -271,7 +270,6 @@ public:
* @brief
*/
#pragma pack(1)
struct tagMemQueueHead
{
size_t _iSize; //内存大小

View File

@ -44,7 +44,6 @@ namespace tars
struct TC_Multi_HashMap_Exception : public TC_Exception
{
TC_Multi_HashMap_Exception(const string &buffer) : TC_Exception(buffer){};
TC_Multi_HashMap_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_Multi_HashMap_Exception() throw(){};
};
@ -142,7 +141,7 @@ public:
uint32_t _iGetNext; /**主key Get链的下一个主key*/
uint32_t _iGetPrev; /**主key Get链的上一个主key*/
uint32_t _iBlockCount; /**主key下数据个数*/
uint8_t _iBitset; /** 8个bit用于标识不同的bool值各bit的含义见BITWISE枚举定义*/
uint8_t _iBitset; /** 8个bit用于标识不同的bool值各bit的含义见BITWISE枚举定义*/
union
{
uint32_t _iNextChunk; /** 下一个Chunk块地址, _bNextChunk=true时有效*/
@ -435,7 +434,6 @@ public:
* @brief block数据头
*/
#pragma pack(1)
struct tagBlockHead
{
uint32_t _iSize; /**block的容量大小*/
@ -447,9 +445,9 @@ public:
uint32_t _iSetNext; /**Set链上的上一个Block, 没有则为0*/
uint32_t _iSetPrev; /**Set链上的上一个Block, 没有则为0*/
uint32_t _iMainKey; /**指向所属主key头*/
time_t _iSyncTime; /**上次缓写时间*/
uint8_t _iVersion; /**数据版本1为初始版本0为保留*/
uint8_t _iBitset; /**8个bit用于标识不同的bool值各bit的含义见BITWISE枚举定义*/
time_t _iSyncTime; /**上次缓写时间*/
uint8_t _iVersion; /**数据版本1为初始版本0为保留*/
uint8_t _iBitset; /**8个bit用于标识不同的bool值各bit的含义见BITWISE枚举定义*/
union
{
uint32_t _iNextChunk; /**下一个Chunk块, _iBitwise中的NEXTCHUNK_BIT为1时有效*/
@ -1434,15 +1432,15 @@ public:
bool _bReadOnly; /**是否只读*/
bool _bAutoErase; /**是否可以自动淘汰*/
char _cEraseMode; /**淘汰方式:0x00:按照Get链淘汰, 0x01:按照Set链淘汰*/
size_t _iMemSize; /**内存大小*/
size_t _iMinDataSize; /**最小数据块大小*/
size_t _iMaxDataSize; /**最大数据块大小*/
float _fFactor; /**因子*/
float _fHashRatio; /**chunks个数/hash个数*/
float _fMainKeyRatio; /**chunks个数/主key hash个数*/
size_t _iElementCount; /**总元素个数*/
size_t _iEraseCount; /**每次淘汰个数*/
size_t _iDirtyCount; /**脏数据个数*/
size_t _iMemSize; /**内存大小*/
size_t _iMinDataSize; /**最小数据块大小*/
size_t _iMaxDataSize; /**最大数据块大小*/
float _fFactor; /**因子*/
float _fHashRatio; /**chunks个数/hash个数*/
float _fMainKeyRatio; /**chunks个数/主key hash个数*/
size_t _iElementCount; /**总元素个数*/
size_t _iEraseCount; /**每次淘汰个数*/
size_t _iDirtyCount; /**脏数据个数*/
uint32_t _iSetHead; /**Set时间链表头部*/
uint32_t _iSetTail; /**Set时间链表尾部*/
uint32_t _iGetHead; /**Get时间链表头部*/
@ -1450,14 +1448,14 @@ public:
uint32_t _iDirtyTail; /**脏数据链尾部*/
uint32_t _iBackupTail; /**热备指针*/
uint32_t _iSyncTail; /**回写链表*/
time_t _iSyncTime; /**回写时间*/
size_t _iUsedChunk; /**已经使用的内存块*/
size_t _iGetCount; /**get次数*/
size_t _iHitCount; /**命中次数*/
size_t _iMKOnlyKeyCount; /**主key的onlykey个数*/
size_t _iOnlyKeyCount; /**主键的OnlyKey个数, 这个数通常为0*/
size_t _iMaxBlockCount; /**主key链下最大的记录数这个数值要监控不能太大否则会导致查询变慢*/
size_t _iReserve[4]; /**保留*/
time_t _iSyncTime; /**回写时间*/
size_t _iUsedChunk; /**已经使用的内存块*/
size_t _iGetCount; /**get次数*/
size_t _iHitCount; /**命中次数*/
size_t _iMKOnlyKeyCount; /**主key的onlykey个数*/
size_t _iOnlyKeyCount; /**主键的OnlyKey个数, 这个数通常为0*/
size_t _iMaxBlockCount; /**主key链下最大的记录数这个数值要监控不能太大否则会导致查询变慢*/
size_t _iReserve[4]; /**保留*/
};
/**

View File

@ -17,15 +17,13 @@
#ifndef __TC_MYSQL_H
#define __TC_MYSQL_H
#include "util/tc_ex.h"
#include "util/tc_platform.h"
#include "mysql.h"
#include "util/tc_ex.h"
#include <map>
#include <vector>
#include <stdlib.h>
struct st_mysql;
typedef struct st_mysql MYSQL;
#include <functional>
namespace tars
{
@ -190,6 +188,13 @@ public:
*/
void init(const TC_DBConf& tcDBConf);
/**
* @brief mysql配置
*
* @return tcDBConf
*/
TC_DBConf getDBConf();
/**
* @brief .
*
@ -219,13 +224,13 @@ public:
MYSQL *getMysql();
/**
* @brief .
* @brief , ,
*
* @param sFrom
* @param sTo
* @return
*/
string escapeString(const string& sFrom);
string realEscapeString(const string& sFrom);
/**
* @brief .
@ -300,6 +305,16 @@ public:
*/
MysqlData queryRecord(const string& sSql);
/**
* @brief Query Record.
*
* @param sSql sql语句
* @param pdatafunc map<string,string> ,key为column value为数据
* @throws TC_Mysql_Exception
* @return MysqlData类型的数据
*/
size_t travelRecord(const string& sSql, const std::function<void(const map<string,string> &)> & pdatafunc);
/**
* @brief
* DB_INT:
@ -337,6 +352,16 @@ public:
*/
size_t insertRecord(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief ().
*
* @param sTableName
* @param mpColumns /
* @throws TC_Mysql_Exception
* @return size_t
*/
size_t insertRecord(const string &sTableName, const map<string, pair<FT,vector<string>>> &mpColumns);
/**
* @brief .
*
@ -347,6 +372,16 @@ public:
*/
size_t replaceRecord(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief ().
*
* @param sTableName
* @param mpColumns /
* @throws TC_Mysql_Exception
* @return size_t
*/
size_t replaceRecord(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns);
/**
* @brief .
*
@ -412,6 +447,15 @@ public:
*/
string buildInsertSQL(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief Insert-SQL语句().
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
string buildInsertSQL(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
/**
* @brief Replace-SQL语句.
*
@ -421,6 +465,15 @@ public:
*/
string buildReplaceSQL(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief Replace-SQL语句().
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
string buildReplaceSQL(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
/**
* @brief Update-SQL语句.
*
@ -443,6 +496,63 @@ public:
* @return int
*/
size_t getAffectedRows();
/**
* @brief , ().
*
* @param sFrom
* @param sTo
* @return
*/
static string escapeString(const string& sFrom);
/**
* @brief Insert-SQL语句.
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
static string buildInsertSQLNoSafe(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief Insert-SQL语句().
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
static string buildInsertSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
/**
* @brief Replace-SQL语句.
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
static string buildReplaceSQLNoSafe(const string &sTableName, const map<string, pair<FT, string> > &mpColumns);
/**
* @brief Replace-SQL语句().
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
static string buildReplaceSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns);
/**
* @brief Update-SQL语句.
*
* @param sTableName
* @param mpColumns /
* @param sCondition where子语句
* @return string Update-SQL语句
*/
static string buildUpdateSQLNoSafe(const string &sTableName,const map<string, pair<FT, string> > &mpColumns, const string &sCondition);
protected:
/**
* @brief copy contructor,,使
@ -455,6 +565,17 @@ protected:
*/
TC_Mysql &operator=(const TC_Mysql &tcMysql);
/**
* @brief SQL语句().
*
* @param sTableName
* @param mpColumns /
* @return string insert-SQL语句
*/
string buildSQL(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns);
string buildBatchSQL(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns);
static string buildSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns);
static string buildBatchSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns);
private:

View File

@ -54,6 +54,14 @@ public:
*/
void decode(int argc, char *argv[]);
/**
* @brief (decode的区别是: , argv[0])
*
* @param command
*
*/
void decode(const char *command);
/**
* @brief --.
*

View File

@ -46,8 +46,7 @@ namespace tars
*/
struct TC_PackIn_Exception : public TC_Exception
{
TC_PackIn_Exception(const string &buffer) : TC_Exception(buffer){};
TC_PackIn_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
TC_PackIn_Exception(const string &buffer) : TC_Exception(buffer){};
~TC_PackIn_Exception() throw(){};
};
@ -105,11 +104,10 @@ protected:
return;
}
if(_nPos > _buffer.length())
{
assert(false);
//throw TC_PackIn_Exception("TC_PackIn cur has beyond error.");
}
// if(_nPos > _buffer.length())
// {
// throw TC_PackIn_Exception("TC_PackIn cur has beyond error.");
// }
if(_bInsert)
{

View File

@ -39,6 +39,8 @@
#if TARGET_PLATFORM_WINDOWS
#include <winsock2.h>
#include <windows.h>
#else
#include <unistd.h>
#endif
#endif

View File

@ -42,7 +42,6 @@ namespace tars
struct TC_RBTree_Exception : public TC_Exception
{
TC_RBTree_Exception(const string &buffer) : TC_Exception(buffer){};
TC_RBTree_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_RBTree_Exception() throw(){};
};
/**
@ -68,6 +67,11 @@ public:
struct RBTreeLockIterator;
struct RBTreeIterator;
friend struct Block;
friend struct BlockAllocator;
friend struct RBTreeLockIterator;
friend struct RBTreeLockItem;
/**
* @brief
*/
@ -1744,11 +1748,6 @@ public:
protected:
friend class Block;
friend class BlockAllocator;
friend struct RBTreeLockIterator;
friend class RBTreeLockItem;
//禁止copy构造
TC_RBTree(const TC_RBTree &mcm);
//禁止复制

View File

@ -17,11 +17,15 @@
#ifndef __TC_SEM_MUTEX_H
#define __TC_SEM_MUTEX_H
#include "util/tc_platform.h"
#if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#endif
#include "util/tc_lock.h"
namespace tars
@ -37,13 +41,16 @@ namespace tars
/**
* @brief
*/
struct TC_SemMutex_Exception : public TC_Lock_Exception
struct TC_SemMutex_Exception : public TC_Exception
{
TC_SemMutex_Exception(const string &buffer) : TC_Lock_Exception(buffer){};
TC_SemMutex_Exception(const string &buffer, int err) : TC_Lock_Exception(buffer, err){};
TC_SemMutex_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
~TC_SemMutex_Exception() throw() {};
};
//锁名称统一用数字类型
#if TARGET_PLATFORM_WINDOWS
typedef int key_t;
#endif
/**
* @brief , :.
*
@ -60,8 +67,13 @@ public:
TC_SemMutex();
/**
* @brief .
*
* @brief
*/
~TC_SemMutex();
/**
* @brief .
*
* @param iKey, key
* @throws TC_SemMutex_Exception
*/
@ -95,14 +107,14 @@ public:
*
*@return int
*/
int rlock() const;
void rlock() const;
/**
* @brief .
*
* @return int
*/
int unrlock() const;
void unrlock() const;
/**
* @brief .
@ -112,59 +124,75 @@ public:
bool tryrlock() const;
/**
* @brief .
*
* @brief .
*
* @return int
*/
int wlock() const;
void wlock() const;
/**
* @brief
*/
int unwlock() const;
void unwlock() const;
/**
* @brief .
*
* @brief .
*
* @throws TC_SemMutex_Exception
* @return bool : false, false
*/
bool trywlock() const;
/**
* @brief .
*
* @brief .
*
* @return int, 0
*/
int lock() const {return wlock();};
void lock() const {wlock();};
/**
* @brief
*/
int unlock() const {return unwlock();};
void unlock() const {unwlock();};
/**
* @brief .
*
* @brief .
*
* @throws TC_SemMutex_Exception
* @return int, 0
*/
bool trylock() const {return trywlock();};
#if TARGET_PLATFORM_WINDOWS
protected:
/**
void addWriter() const;
void removeWriter() const;
void unlockImp() const;
DWORD tryReadLockOnce() const ;
protected:
mutable HANDLE _mutex;
mutable HANDLE _readEvent;
mutable HANDLE _writeEvent;
mutable unsigned _readers;
mutable unsigned _writersWaiting;
mutable unsigned _writers;
#else
/**
* ID
*/
int _semID;
#endif
/**
* key
*/
key_t _semKey;
};
}
#endif
#endif
//#endif

View File

@ -52,7 +52,7 @@ namespace tars
*/
struct TC_SHA_Exception : public TC_Exception
{
TC_SHA_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
TC_SHA_Exception(const string &buffer, bool err) : TC_Exception(buffer, err){};
~TC_SHA_Exception() throw(){};
};

View File

@ -93,8 +93,33 @@ public:
}
/**
* @brief .
*
* @brief .
*
* @param t
*/
static void destroy(T *t)
{
delete t;
}
};
template<typename T>
class CreateUsingNew1
{
public:
/**
* @brief .
*
* @return T*
*/
static T* create()
{
return new T;
}
/**
* @brief .
*
* @param t
*/
static void destroy(T *t)
@ -194,14 +219,16 @@ struct NoDestroyLifetime
template
<
typename T,
template<class> class CreatePolicy = CreateUsingNew,
template<class> class LifetimePolicy = DefaultLifetime
template<typename> class CreatePolicy = CreateUsingNew,
template<typename> class LifetimePolicy = DefaultLifetime
>
class TC_Singleton
{
public:
typedef T instance_type;
typedef volatile T volatile_type;
typedef CreatePolicy<T> TCreatePolicy;
/**
* @brief
@ -229,8 +256,6 @@ public:
return (T*)_pInstance;
}
virtual ~TC_Singleton(){};
protected:
static void destroySingleton()
@ -248,6 +273,7 @@ protected:
protected:
TC_Singleton(){}
virtual ~TC_Singleton(){};
TC_Singleton (const TC_Singleton &);
TC_Singleton &operator=(const TC_Singleton &);
};

View File

@ -138,7 +138,7 @@ public:
*
* @return socket句柄
*/
int getfd() const { return _sock; }
SOCKET_TYPE getfd() const { return _sock; }
/**
* @brief socket是否有效.
@ -162,8 +162,7 @@ public:
* @throws TC_Socket_Exception
* @return
*/
void getPeerName(string &sPeerAddress, uint16_t &iPeerPort);
void getPeerName(string &sPeerAddress, uint16_t &iPeerPort) const;
#if TARGET_PLATFORM_LINUX||TARGET_PLATFORM_IOS
@ -227,7 +226,7 @@ public:
* @param level socket操作层次, socket层
* @return int
*/
int setSockOpt(int opt, const void *pvOptVal, socklen_t optLen, int level = SOL_SOCKET);
int setSockOpt(int opt, const void *pvOptVal, SOCKET_LEN_TYPE optLen, int level = SOL_SOCKET);
/**
* @brief socket选项值.
@ -238,7 +237,7 @@ public:
* @param level socket操作层次, socket层
* @return socket选项值
*/
int getSockOpt(int opt, void *pvOptVal, socklen_t &optLen, int level = SOL_SOCKET);
int getSockOpt(int opt, void *pvOptVal, SOCKET_LEN_TYPE &optLen, int level = SOL_SOCKET) const;
/**
* @brief accept.
@ -248,7 +247,7 @@ public:
* @param iSockLen pstSockAddr长度
* @return int : > 0 ,socket; <0,
*/
int accept(TC_Socket &tcSock, struct sockaddr *pstSockAddr, socklen_t &iSockLen);
SOCKET_TYPE accept(TC_Socket &tcSock, struct sockaddr *pstSockAddr, SOCKET_LEN_TYPE &iSockLen);
/**
* @brief ,AF_INET的socket有效.
@ -299,6 +298,16 @@ public:
*/
void listen(int connBackLog);
/**
* @brief .
*
* @param pstBindAddr
* @param iAddrLen pstBindAddr指向的结构的长度
* @throws TC_Socket_Exception
* @return
*/
void bind(const struct sockaddr *pstBindAddr, SOCKET_LEN_TYPE iAddrLen);
/**
* @brief (tcp).
*
@ -341,7 +350,7 @@ public:
* @param iFlag
* @return int
*/
int recvfrom(void *pvBuf, size_t iLen, struct sockaddr *pstFromAddr, socklen_t &iFromLen, int iFlags = 0);
int recvfrom(void *pvBuf, size_t iLen, struct sockaddr *pstFromAddr, SOCKET_LEN_TYPE &iFromLen, int iFlags = 0);
/**
* @brief (udp).
@ -365,7 +374,7 @@ public:
* @param iFlag
* @return int : >0 ;<=0,
*/
int sendto(const void *pvBuf, size_t iLen, struct sockaddr *pstToAddr, socklen_t iToLen, int iFlags = 0);
int sendto(const void *pvBuf, size_t iLen, struct sockaddr *pstToAddr, SOCKET_LEN_TYPE iToLen, int iFlags = 0);
/**
* @brief .
@ -429,7 +438,7 @@ public:
* @throws TC_Socket_Exception
* @return recv buffer
*/
int getRecvBufferSize();
int getRecvBufferSize() const;
/**
* @brief recv buffer .
@ -443,7 +452,7 @@ public:
* @param buffer大小
* @throws TC_Socket_Exception
*/
int getSendBufferSize();
int getSendBufferSize() const;
/**
* @brief buffer大小.
@ -458,14 +467,6 @@ public:
*/
void ignoreSigPipe();
/**
* @brief ip.
*
* @throws TC_Socket_Exception
* @return ip
*/
static vector<string> getLocalHosts(int domain = AF_INET);
/**
* @brief socket方式.
*
@ -474,11 +475,18 @@ public:
* @throws TC_Socket_Exception
* @return
*/
static void setblock(int fd, bool bBlock);
static void setblock(SOCKET_TYPE fd, bool bBlock);
/**
* @brief ip.
*
* @throws TC_Socket_Exception
* @return ip
*/
static vector<string> getLocalHosts(int domain = AF_INET);
/**
* @brief ,fd.
*
* @param fds
* @param bBlock true, ; false,
* @throws TC_Socket_Exception
@ -519,16 +527,6 @@ public:
#undef IPv6_ADDRESS_CHAR
}
/**
* @brief .
*
* @param pstBindAddr
* @param iAddrLen pstBindAddr指向的结构的长度
* @throws TC_Socket_Exception
* @return
*/
void bind(struct sockaddr *pstBindAddr, socklen_t iAddrLen);
/**
* @brief , (ip或域名), sockaddr_in结构.
*
@ -561,7 +559,6 @@ public:
*/
static void closeSocketNoThrow(int);
#if 0
/**
* @brief no implementation.
@ -585,7 +582,7 @@ protected:
* @param serverLen pstServerAddr指向的结构的长度
* @return int
*/
int connect(const struct sockaddr *pstServerAddr, socklen_t serverLen);
int connect(const struct sockaddr *pstServerAddr, SOCKET_LEN_TYPE serverLen);
/**
* @brief .
@ -595,7 +592,7 @@ protected:
* @throws TC_Socket_Exception
* @return
*/
void getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen) const;
void getPeerName(struct sockaddr *pstPeerAddr, SOCKET_LEN_TYPE &iPeerLen) const;
/**
* @brief ip和端口.
@ -605,12 +602,9 @@ protected:
* @throws TC_Socket_Exception
* @return
*/
void getSockName(struct sockaddr *pstSockAddr, socklen_t &iSockLen) const;
private:
void getSockName(struct sockaddr *pstSockAddr, SOCKET_LEN_TYPE &iSockLen) const;
protected:
// static const int INVALID_SOCKET = -1;
/**
* socket句柄

View File

@ -2,9 +2,7 @@
#ifndef __TC_SPIN_LOCK_H
#define __TC_SPIN_LOCK_H
//#include <mutex>
#include <atomic>
//#include <condition_variable>
#include <memory>
using namespace std;

View File

@ -18,13 +18,14 @@
#define __TC_SQueue_H__
#include <fcntl.h>
#include <unistd.h>
// #include <unistd.h>
#include <errno.h>
#include <stdexcept>
#include <iostream>
#include <assert.h>
#include <iostream>
#include <string>
#include "util/tc_ex.h"
/**
* queue线()
*
@ -34,231 +35,215 @@
namespace tars
{
/**
* @brief
*/
struct TC_SQueue_Exception : public TC_Exception
{
TC_SQueue_Exception(const std::string &buffer) : TC_Exception(buffer){};
~TC_SQueue_Exception() throw(){};
};
class TC_SQueue
{
public:
TC_SQueue() {_header = NULL;_data = NULL;}
~TC_SQueue() {_header = NULL;_data = NULL;}
TC_SQueue() {_header = NULL;_data = NULL;}
~TC_SQueue() {_header = NULL;_data = NULL;}
void attach(char* pBuf, unsigned long iBufSize) throw (TC_Exception)
{
if(iBufSize <= sizeof(Header)+MarkLen+ReserveLen)
{
throw TC_Exception("TC_SQueue::attach fail:iBufSize is too small");
}
void attach(char* pBuf, size_t iBufSize)
{
if(iBufSize <= sizeof(Header)+MarkLen+ReserveLen) {
throw TC_SQueue_Exception("TC_SQueue::attach fail:iBufSize is too small");
}
_header = (Header *)pBuf;
_data = pBuf+sizeof(Header);
if(_header->iBufSize != iBufSize - sizeof(Header))
throw TC_Exception("TC_SQueue::attach fail: iBufSize != iBufSize - sizeof(Header);");
if(_header->iReserveLen != ReserveLen)
throw TC_Exception("TC_SQueue::attach fail: iReserveLen != ReserveLen");
if(_header->iBegin >= _header->iBufSize)
throw TC_Exception("TC_SQueue::attach fail: iBegin > iBufSize - sizeof(Header);");
if(_header->iEnd > iBufSize - sizeof(Header))
throw TC_Exception("TC_SQueue::attach fail: iEnd > iBufSize - sizeof(Header);");
}
_header = (Header *)pBuf;
_data = pBuf+sizeof(Header);
if(_header->iBufSize != iBufSize - sizeof(Header))
throw TC_SQueue_Exception("TC_SQueue::attach fail: iBufSize != iBufSize - sizeof(Header);");
if(_header->iReserveLen != ReserveLen)
throw TC_SQueue_Exception("TC_SQueue::attach fail: iReserveLen != ReserveLen");
if(_header->iBegin >= _header->iBufSize)
throw TC_SQueue_Exception("TC_SQueue::attach fail: iBegin > iBufSize - sizeof(Header);");
if(_header->iEnd > iBufSize - sizeof(Header))
throw TC_SQueue_Exception("TC_SQueue::attach fail: iEnd > iBufSize - sizeof(Header);");
}
void create(char* pBuf, unsigned long iBufSize) throw (TC_Exception)
{
if(iBufSize <= sizeof(Header)+MarkLen+ReserveLen)
{
throw TC_Exception("TC_SQueue::create fail:iBufSize is too small");
}
void create(char* pBuf, size_t iBufSize)
{
if(iBufSize <= sizeof(Header)+MarkLen+ReserveLen) {
throw TC_SQueue_Exception("TC_SQueue::create fail:iBufSize is too small");
}
_header = (Header *)pBuf;
_data = pBuf+sizeof(Header);
_header->iBufSize = iBufSize - sizeof(Header);
_header->iReserveLen = ReserveLen;
_header->iBegin = 0;
_header->iEnd = 0;
_header->iNum = 0;
}
_header = (Header *)pBuf;
_data = pBuf+sizeof(Header);
_header->iBufSize = iBufSize - sizeof(Header);
_header->iReserveLen = ReserveLen;
_header->iBegin = 0;
_header->iEnd = 0;
_header->iNum = 0;
}
bool pop(string& buffer)
{
unsigned long iEnd=_header->iEnd;
unsigned tmp_num;
if(_header->iBegin == iEnd)
{
bool pop(std::string& buffer)
{
size_t iEnd=_header->iEnd;
size_t tmp_num;
if(_header->iBegin == iEnd) {
_header->iNum = 0;
return false;
return false;
}
else if(_header->iBegin<iEnd)
{
assert(_header->iBegin+MarkLen < iEnd);
unsigned long len = GetLen(_data+_header->iBegin);
assert(_header->iBegin+MarkLen+len <= iEnd);
buffer.assign(_data+_header->iBegin+MarkLen, len);
_header->iBegin += len+MarkLen;
tmp_num = _header->iNum;
if(tmp_num > 0)
_header->iNum = tmp_num-1;
}
else
{
// 被分段
assert(iEnd+ReserveLen <= _header->iBegin);
unsigned long len = 0;
unsigned long new_begin = 0;
char *data_from = NULL;
char *data_to = NULL;
assert(_header->iBegin < _header->iBufSize);
// 长度字段也被分段
if(_header->iBegin+MarkLen > _header->iBufSize)
{
char tmp[16];
memcpy(tmp,_data+_header->iBegin,_header->iBufSize-_header->iBegin);
memcpy(tmp+_header->iBufSize-_header->iBegin,_data,_header->iBegin+MarkLen-_header->iBufSize);
len = GetLen(tmp);
data_from = _data+(_header->iBegin+MarkLen-_header->iBufSize); //
new_begin = _header->iBegin+MarkLen-_header->iBufSize+len;
assert(new_begin <= iEnd);
}
else
{
len = GetLen(_data+_header->iBegin);
data_from = _data+_header->iBegin+MarkLen;
if(data_from == _data+_header->iBufSize) data_from = _data;
if(_header->iBegin+MarkLen+len < _header->iBufSize) {
new_begin = _header->iBegin+MarkLen+len;
} else { // 数据被分段
new_begin = _header->iBegin+MarkLen+len-_header->iBufSize;
assert(new_begin <= iEnd);
}
}
data_to = _data+new_begin;
else if(_header->iBegin<iEnd) {
assert(_header->iBegin+MarkLen < iEnd);
size_t len = GetLen(_data+_header->iBegin);
assert(_header->iBegin+MarkLen+len <= iEnd);
buffer.assign(_data+_header->iBegin+MarkLen, len);
_header->iBegin += len+MarkLen;
tmp_num = _header->iNum;
if(tmp_num > 0)
_header->iNum = tmp_num-1;
} else {
// 被分段
assert(iEnd+ReserveLen <= _header->iBegin);
size_t len = 0;
size_t new_begin = 0;
char *data_from = NULL;
char *data_to = NULL;
assert(_header->iBegin < _header->iBufSize);
// 长度字段也被分段
if(_header->iBegin+MarkLen > _header->iBufSize) {
char tmp[16];
memcpy(tmp,_data+_header->iBegin,_header->iBufSize-_header->iBegin);
memcpy(tmp+_header->iBufSize-_header->iBegin,_data,_header->iBegin+MarkLen-_header->iBufSize);
len = GetLen(tmp);
data_from = _data+(_header->iBegin+MarkLen-_header->iBufSize); //
new_begin = _header->iBegin+MarkLen-_header->iBufSize+len;
assert(new_begin <= iEnd);
} else {
len = GetLen(_data+_header->iBegin);
data_from = _data+_header->iBegin+MarkLen;
if(data_from == _data+_header->iBufSize) data_from = _data;
if(_header->iBegin+MarkLen+len < _header->iBufSize) {
new_begin = _header->iBegin+MarkLen+len;
} else { // 数据被分段
new_begin = _header->iBegin+MarkLen+len-_header->iBufSize;
assert(new_begin <= iEnd);
}
}
data_to = _data+new_begin;
if(data_to > data_from)
{
assert(data_to - data_from == (long)len);
buffer.assign(data_from, len);
}
else
{
buffer.assign(data_from, _data+_header->iBufSize-data_from);
buffer.append(_data, data_to-_data);
assert(_header->iBufSize-(data_from-data_to)== len);
}
_header->iBegin = new_begin;
tmp_num = _header->iNum;
if(tmp_num > 0)
_header->iNum = tmp_num-1;
}
if(data_to > data_from) {
assert(data_to - data_from == (long)len);
buffer.assign(data_from, len);
} else {
buffer.assign(data_from, _data+_header->iBufSize-data_from);
buffer.append(_data, data_to-_data);
assert(_header->iBufSize-(data_from-data_to)== len);
}
_header->iBegin = new_begin;
tmp_num = _header->iNum;
if(tmp_num > 0)
_header->iNum = tmp_num-1;
}
return true;
}
bool push(const string& buffer)
{
return push(buffer.c_str(), buffer.length());
}
// 写端使用
bool push(const char *buffer,unsigned long len)
{
if(len == 0) return true;
unsigned long iBegin = _header->iBegin;
if(_header->iEnd == iBegin)
{
return true;
}
bool push(const std::string& buffer)
{
return push(buffer.c_str(), buffer.length());
}
// 写端使用
bool push(const char *buffer, size_t len)
{
if(len == 0) return true;
size_t iBegin = _header->iBegin;
if(_header->iEnd == iBegin) {
_header->iNum = 0;
if(MarkLen+len+ReserveLen>_header->iBufSize)
return false;
}
else if(_header->iEnd > iBegin)
{
assert(iBegin+MarkLen < _header->iEnd);
if(_header->iBufSize - _header->iEnd + iBegin < MarkLen+len+ReserveLen)
return false;
}
else
{
assert(_header->iEnd+ReserveLen <= iBegin);
if(iBegin - _header->iEnd < MarkLen+len+ReserveLen)
return false;
}
if(MarkLen+len+ReserveLen>_header->iBufSize)
return false;
} else if(_header->iEnd > iBegin) {
assert(iBegin+MarkLen < _header->iEnd);
if(_header->iBufSize - _header->iEnd + iBegin < MarkLen+len+ReserveLen)
return false;
} else {
assert(_header->iEnd+ReserveLen <= iBegin);
if(iBegin - _header->iEnd < MarkLen+len+ReserveLen)
return false;
}
// 长度字段被分段
if(_header->iEnd+MarkLen > _header->iBufSize)
{
char tmp[16]; SetLen(tmp,len);
memcpy(_data+_header->iEnd,tmp,_header->iBufSize-_header->iEnd);
memcpy(_data,tmp+_header->iBufSize-_header->iEnd,_header->iEnd+MarkLen-_header->iBufSize);
memcpy(_data+_header->iEnd+MarkLen-_header->iBufSize,buffer,len);
_header->iEnd = len+_header->iEnd+MarkLen-_header->iBufSize;
assert(_header->iEnd+ReserveLen <= iBegin);
_header->iNum++;
}
// 数据被分段
else if(_header->iEnd+MarkLen+len > _header->iBufSize)
{
SetLen(_data+_header->iEnd,len);
memcpy(_data+_header->iEnd+MarkLen,buffer,_header->iBufSize-_header->iEnd-MarkLen);
memcpy(_data,buffer+_header->iBufSize-_header->iEnd-MarkLen,len-(_header->iBufSize-_header->iEnd-MarkLen));
_header->iEnd = len-(_header->iBufSize-_header->iEnd-MarkLen);
assert(_header->iEnd+ReserveLen <= iBegin);
_header->iNum++;
}
else
{
SetLen(_data+_header->iEnd,len);
memcpy(_data+_header->iEnd+MarkLen,buffer,len);
_header->iEnd = (_header->iEnd+MarkLen+len)%_header->iBufSize;
_header->iNum++;
}
return true;
}
// 长度字段被分段
if(_header->iEnd+MarkLen > _header->iBufSize) {
char tmp[16]; SetLen(tmp,len);
memcpy(_data+_header->iEnd,tmp,_header->iBufSize-_header->iEnd);
memcpy(_data,tmp+_header->iBufSize-_header->iEnd,_header->iEnd+MarkLen-_header->iBufSize);
memcpy(_data+_header->iEnd+MarkLen-_header->iBufSize,buffer,len);
_header->iEnd = len+_header->iEnd+MarkLen-_header->iBufSize;
assert(_header->iEnd+ReserveLen <= iBegin);
_header->iNum++;
}
// 数据被分段
else if(_header->iEnd+MarkLen+len > _header->iBufSize){
SetLen(_data+_header->iEnd,len);
memcpy(_data+_header->iEnd+MarkLen,buffer,_header->iBufSize-_header->iEnd-MarkLen);
memcpy(_data,buffer+_header->iBufSize-_header->iEnd-MarkLen,len-(_header->iBufSize-_header->iEnd-MarkLen));
_header->iEnd = len-(_header->iBufSize-_header->iEnd-MarkLen);
assert(_header->iEnd+ReserveLen <= iBegin);
_header->iNum++;
} else {
SetLen(_data+_header->iEnd,len);
memcpy(_data+_header->iEnd+MarkLen,buffer,len);
_header->iEnd = (_header->iEnd+MarkLen+len)%_header->iBufSize;
_header->iNum++;
}
return true;
}
// 读端使用
//bool isEmpty() const {unsigned long iEnd=_header->iEnd;return _header->iBegin == iEnd;}
bool empty() const {unsigned long iEnd=_header->iEnd;return _header->iBegin == iEnd;}
// 写端使用
bool full(unsigned long len) const
// 读端使用
//bool isEmpty() const {size_t iEnd=_header->iEnd;return _header->iBegin == iEnd;}
bool empty() const {size_t iEnd=_header->iEnd;return _header->iBegin == iEnd;}
// 写端使用
bool full(size_t len) const
{
size_t iBegin = _header->iBegin;
if(len==0) return false;
if(_header->iEnd == iBegin) {
if(len+MarkLen+ReserveLen > _header->iBufSize) return true;
return false;
} else if(_header->iEnd > iBegin) {
assert(iBegin+MarkLen < _header->iEnd);
return _header->iBufSize - _header->iEnd + iBegin < MarkLen+len+ReserveLen;
}
assert(_header->iEnd+ReserveLen <= iBegin);
return (iBegin - _header->iEnd < MarkLen+len+ReserveLen);
}
// 返回队列里的元素数量,不一定绝对准确,只能作为参考
size_t size() const
{
unsigned long iBegin = _header->iBegin;
if(len==0) return false;
if(_header->iEnd == iBegin)
{
if(len+MarkLen+ReserveLen > _header->iBufSize) return true;
return false;
}
else if(_header->iEnd > iBegin)
{
assert(iBegin+MarkLen < _header->iEnd);
return _header->iBufSize - _header->iEnd + iBegin < MarkLen+len+ReserveLen;
}
assert(_header->iEnd+ReserveLen <= iBegin);
return (iBegin - _header->iEnd < MarkLen+len+ReserveLen);
}
// 返回队列里的元素数量,不一定绝对准确,只能作为参考
unsigned long size() const
{
if (empty())
{
_header->iNum = 0;
if (empty()) {
_header->iNum = 0;
}
return _header->iNum;
}
private:
unsigned long GetLen(char *buf) {unsigned long u; memcpy((void *)&u,buf,MarkLen); return u;}
void SetLen(char *buf,unsigned long u) {memcpy(buf,(void *)&u,MarkLen);}
size_t GetLen(char *buf) {size_t u; memcpy((void *)&u,buf,MarkLen); return u;}
void SetLen(char *buf, size_t u) {memcpy(buf,(void *)&u,MarkLen);}
private:
const static unsigned long ReserveLen = 8;
const static unsigned long MarkLen = sizeof(unsigned long);
struct Header
{
unsigned long iBufSize;
unsigned long iReserveLen; // must be 8
unsigned long iBegin;
unsigned long iEnd;
unsigned long iNum; // 增加一个数量标记,不一定准确
};
const static size_t ReserveLen = 8;
const static size_t MarkLen = sizeof(size_t);
struct Header
{
size_t iBufSize;
size_t iReserveLen; // must be 8
size_t iBegin;
size_t iEnd;
size_t iNum; // 增加一个数量标记,不一定准确
};
Header *_header;
char *_data;
Header *_header;
char *_data;
};
}

View File

@ -17,7 +17,6 @@
#ifndef _TC_THREAD_COND_H
#define _TC_THREAD_COND_H
#include <cerrno>
#include <iostream>
#include <condition_variable>

View File

@ -20,7 +20,6 @@
#include "util/tc_platform.h"
#include "util/tc_thread.h"
#include <future>
#include <functional>
#include <iostream>
#include <queue>
#include <set>

View File

@ -31,22 +31,11 @@ namespace tars
/**
* @file tc_thread_rwlock.h
* @brief
* @author jarodruan@upchina.com
* @author ruanshudong@qq.com
*/
/////////////////////////////////////////////////
/**
* @brief读写锁异常类
*/
// struct TC_ThreadRW_Exception : public TC_Exception
// {
// TC_ThreadRW_Exception(const string &buffer) : TC_Exception(buffer){};
// TC_ThreadRW_Exception(const string &buffer, bool err) : TC_Exception(buffer, err){};
// ~TC_ThreadRW_Exception() throw() {};
// };
class TC_ThreadRWLocker
{
protected:

View File

@ -212,7 +212,7 @@ private:
void delFromNoSend(uint16_t id);
protected:
uint32_t _uniqId;
uint32_t _uniqId;
uint16_t _dataSize;
@ -314,7 +314,7 @@ template<typename T> uint32_t TC_TimeoutQueueMap<T>::generateId()
{
_freeTail = 0;
}
//cerr<<"generateId:"<<id<<endl;
return id;
}

View File

@ -21,9 +21,9 @@
#include <unordered_map>
#include <iostream>
#include <cassert>
#include <functional>
#include "util/tc_autoptr.h"
// #include "util/tc_monitor.h"
#include "util/tc_monitor.h"
// #include "util/tc_functor.h"
#include "util/tc_timeprovider.h"
using namespace std;
@ -71,8 +71,8 @@ public:
typename data_type::iterator dataIter;
};
/**
* @brief 5s超时.
*
* @brief 5s超时.
*
* @param timeout
* @param size
*/
@ -113,8 +113,8 @@ public:
}
/**
* @brief id的数据.
*
* @brief id的数据.
*
* @param id id
* @param T id的数据
* @return bool get的结果
@ -122,17 +122,17 @@ public:
bool get(uint32_t uniqId, T & t,bool bErase = true);
/**
* @brief .
*
* @param uniqId id
* @brief .
*
* @param uniqId id
* @param T
* @return bool
*/
bool erase(uint32_t uniqId, T & t);
/**
* @brief .
*
* @brief .
*
* @param ptr
* @param uniqId
* @param timeout
@ -151,9 +151,9 @@ public:
bool timeout(T & t);
/**
* @brief df对数据做处理
* @brief df对数据做处理
*/
void timeout(const data_functor &df);
void timeout(data_functor &df);
/**
* @brief .
@ -163,7 +163,7 @@ public:
size_t size() const { return _data.size(); }
protected:
uint32_t _uniqId;
atomic<uint32_t> _uniqId;
data_type _data;
time_type _time;
send_type _send;
@ -296,7 +296,7 @@ template<typename T> bool TC_TimeoutQueueNew<T>::timeout(T & t)
return true;
}
template<typename T> void TC_TimeoutQueueNew<T>::timeout(const data_functor& df)
template<typename T> void TC_TimeoutQueueNew<T>::timeout(data_functor &df)
{
while(true)
{

View File

@ -0,0 +1,217 @@
#ifndef __TC_TIMER_H_
#define __TC_TIMER_H_
#include <mutex>
#include <condition_variable>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include "util/tc_thread_pool.h"
#include "util/tc_timeprovider.h"
#include "util/tc_cron.h"
namespace tars
{
/////////////////////////////////////////////////
/**
* @file tc_timer.h
* @brief , , function
* 使:
* , ,
* 线, , tc_thread_pool实现线程池的
* tc_thread_pool的function对象(std::bind的对象), tc_timer
*
* @author ruanshuodong@qq.com
*/
/////////////////////////////////////////////////
/**
* @brief
*/
class TC_Timer
{
protected:
struct Func
{
Func( uint64_t fireMillseconds) : _fireMillseconds(fireMillseconds) { }
std::function<void()> _func;
uint64_t _fireMillseconds = 0; //事件触发时间
TC_Cron _cron; //crontab
};
typedef std::unordered_set<uint64_t> EVENT_SET;
typedef std::unordered_map<uint64_t, shared_ptr<Func>> MAP_EVENT;
typedef map<uint64_t, unordered_set<uint64_t>> MAP_TIMER;
public:
/**
*
*/
~TC_Timer();
/**
*
* @param numThread, 线, 1
*/
void startTimer(int numThread = 1);
/**
*
* ,
*/
void stopTimer();
/**
* @brief fireMillseconds时间执行
* @param fireMillseconds, ()
* @return Id
*/
template <class F, class... Args>
int64_t postAtTime(int64_t fireMillseconds, F &&f, Args &&... args)
{
return post(create(fireMillseconds, 0, f, args...));
}
/**
* @brief delayMillseconds时间执行
* @param delayMillseconds,
* @return Id
*/
template <class F, class... Args>
int64_t postDelayed(int64_t delayMillseconds, F &&f, Args &&... args)
{
//定义返回值类型
using RetType = decltype(f(args...));
auto task = std::make_shared < std::packaged_task
< RetType() >> (std::bind(std::forward<F>(f), std::forward<Args>(args)...));
uint64_t fireMillseconds = TC_TimeProvider::getInstance()->getNowMs() + delayMillseconds;
return post(create(fireMillseconds, 0,"", f, args...));
}
/**
* @brief repeatTime(), repeat是上一次执行完,
* @param repeatTime,
* @param execNow,
* @return Id
*/
template <class F, class... Args>
int64_t postRepeated(int64_t repeatTime, bool execNow, F &&f, Args &&... args)
{
//定义返回值类型
using RetType = decltype(f(args...));
auto task = std::make_shared < std::packaged_task
< RetType() >> (std::bind(std::forward<F>(f), std::forward<Args>(args)...));
uint64_t fireMillseconds;
if(execNow) {
fireMillseconds = TC_TimeProvider::getInstance()->getNowMs();
}else {
fireMillseconds = TC_TimeProvider::getInstance()->getNowMs() + repeatTime;
}
return post(create(fireMillseconds, repeatTime,"", f, args...));
}
/**
* @brief
* @param cronexpr, crontab <seconds> <minutes> <hours> <days of month> <months> <days of week> tc_cron.h
* @return Id
*/
// CRON Description
// * * * * * * Every second
// */5 * * * * * Every 5 seconds
// 0 */5 */2 * * * Every 5 minutes, every 2 hours
// 0 */2 */2 * */2 */2 Every 2 minutes, every 2 hours, every 2 days of the week, every 2 months
// 0 15 10 * * * 10:15 AM every day
// 0 0/5 14 * * * Every 5 minutes starting at 2 PM and ending at 2:55 PM, every day
// 0 10,44 14 * 3 WED 2:10 PM and at 2:44 PM every Wednesday of March
// 0 15 10 * * MON-FRI 10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday
// 0 0 12 1/5 * * 12 PM every 5 days every month, starting on the first day of the month
// 0 11 11 11 11 * Every November 11th at 11:11 AM
template <class F, class... Args>
int64_t postCron(const string&cronexpr, F&& f, Args&&... args)
{
//定义返回值类型
using RetType = decltype(f(args...));
auto task = std::make_shared < std::packaged_task
< RetType() >> (std::bind(std::forward<F>(f), std::forward<Args>(args)...));
return post(create(0, 0,cronexpr, f, args...));
}
/**
*
* @param uniqId
*/
void erase(int64_t uniqId);
protected:
template <class F, class... Args>
shared_ptr<Func> create(int64_t fireMillseconds, int64_t repeatTime, const string & cronexpr, F &&f, Args &&... args)
{
//定义返回值类型
using RetType = decltype(f(args...));
auto task = std::make_shared<std::packaged_task<RetType()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
shared_ptr<Func> fPtr = std::make_shared<Func>(fireMillseconds);
if (!cronexpr.empty())
{
fPtr->_cron = TC_Cron::makecron(cronexpr);
fPtr->_fireMillseconds = TC_Cron::nextcron(fPtr->_cron) * 1000;
}
fPtr->_func = [task, this, fPtr, repeatTime]() {
(*task)();
task->reset();
if (fPtr->_cron.isset)
{
fPtr->_fireMillseconds = TC_Cron::nextcron(fPtr->_cron,fPtr->_fireMillseconds/1000) * 1000;
this->post(fPtr);
}
else if(repeatTime > 0)
{
fPtr->_fireMillseconds = TC_TimeProvider::getInstance()->getNowMs() + repeatTime;
this->post(fPtr);
}
};
return fPtr;
}
int64_t post(const shared_ptr<Func> &event);
void fireEvent(const EVENT_SET &el);
void run();
protected:
std::mutex _mutex;
std::condition_variable _cond;
bool _terminate = false;
MAP_EVENT _mapEvent; //id, 事件
MAP_TIMER _mapTimer; //时间, 事件
TC_ThreadPool _tpool;
};
}
#endif

View File

@ -54,7 +54,7 @@ string TC_Base64::encode(const string &data, bool bChangeLine/* = false*/)
pDst = new char[iBufSize];
if(pDst == NULL)
return "";
int iDstLen = encode((unsigned char*)data.c_str(), data.size(), pDst, bChangeLine);
size_t iDstLen = encode((unsigned char*)data.c_str(), (int)data.size(), pDst, bChangeLine);
string ret(pDst,iDstLen);
delete [] pDst;
return ret;
@ -68,21 +68,37 @@ string TC_Base64::decode(const string &data)
pDst = new unsigned char[data.size()];
if(pDst == NULL)
return "";
int iDstLen = decode(data.c_str(), data.size(), pDst);
size_t iDstLen = decode(data.c_str(), data.size(), pDst);
string ret((char*)pDst,iDstLen);
delete [] pDst;
return ret;
}
int TC_Base64::encode(const unsigned char* pSrc, int nSrcLen, char* pDst, bool bChangeLine/* = false*/)
string TC_Base64::encode(const char *buffer, size_t length, bool bChangeLine)
{
if(buffer == NULL || length == 0)
return "";
//设原始串长度为a,结果串中算上回车换行及'/0',最终长度为(a/3+1)*4+(a/3+1)*4*2/76+1,约为1.369*a+6
char *pDst = NULL;
int iBufSize = (int)(length*1.4) + 6;
pDst = new char[iBufSize];
if(pDst == NULL)
return "";
size_t iDstLen = encode((unsigned char*)buffer, length, pDst, bChangeLine);
string ret(pDst,iDstLen);
delete [] pDst;
return ret;
}
int TC_Base64::encode(const unsigned char* pSrc, size_t nSrcLen, char* pDst, bool bChangeLine/* = false*/)
{
unsigned char c1, c2, c3;
int nDstLen = 0;
int nLineLen = 0;
int nDiv = nSrcLen / 3;
int nMod = nSrcLen % 3;
size_t nDiv = nSrcLen / 3;
size_t nMod = nSrcLen % 3;
// 每次取3个字节编码成4个字符
for (int i = 0; i < nDiv; i ++)
for (size_t i = 0; i < nDiv; i ++)
{
c1 = *pSrc++;
c2 = *pSrc++;
@ -131,11 +147,11 @@ int TC_Base64::encode(const unsigned char* pSrc, int nSrcLen, char* pDst, bool b
}
int TC_Base64::decode(const char* pSrc, int nSrcLen, unsigned char* pDst)
int TC_Base64::decode(const char* pSrc, size_t nSrcLen, unsigned char* pDst)
{
int nDstLen; // 输出的字符计数
int nValue; // 解码用到的整数
int i;
size_t nValue; // 解码用到的整数
size_t i;
i = 0;
nDstLen = 0;
@ -150,7 +166,7 @@ int TC_Base64::decode(const char* pSrc, int nSrcLen, unsigned char* pDst)
nValue = DeBase64Tab[int(*pSrc++)] << 18;
nValue += DeBase64Tab[int(*pSrc++)] << 12;
*pDst++ = (nValue & 0x00ff0000) >> 16;
*pDst++ = (unsigned char)((nValue & 0x00ff0000) >> 16);
nDstLen++;
if (*pSrc != '=')
{

View File

@ -32,12 +32,12 @@ TC_Endpoint::TC_Endpoint()
_grid = 0;
_qos = 0;
_weight = -1;
_weighttype = 0;
_authType = 0;
_isIPv6 = false;
_weighttype = 0;
_authType = 0;
_isIPv6 = TC_Socket::addressIsIPv6(_host);
}
void TC_Endpoint::init(const string& host, int port, int timeout, int type, int grid, int qos, int weight, unsigned int weighttype, int authType)
void TC_Endpoint::init(const string& host, int port, int timeout, EType type, int grid, int qos, int weight, unsigned int weighttype, int authType)
{
_host = host;
_port = port;
@ -219,10 +219,10 @@ void TC_Endpoint::parse(const string &str)
break;
}
default:
{
{
///throw TC_EndpointParse_Exception("TC_Endpoint::parse error : " + str);
}
}
}
}
}
if(_weighttype != 0)
@ -253,8 +253,6 @@ void TC_Endpoint::parse(const string &str)
/*************************************TC_TCPClient**************************************/
#define LEN_MAXRECV 8196
TC_ClientSocket::TC_ClientSocket() : _port(0),_timeout(3000)
{
}
@ -284,6 +282,14 @@ void TC_ClientSocket::init(const string &sIp, int iPort, int iTimeout)
_isIPv6 = TC_Socket::addressIsIPv6(sIp);
}
void TC_ClientSocket::close()
{
_socket.close();
}
/*************************************TC_TCPClient**************************************/
#define LEN_MAXRECV 8196
int TC_TCPClient::checkSocket()
{
@ -299,20 +305,22 @@ int TC_TCPClient::checkSocket()
_epoller->add(_socket.getfd(), 0, EPOLLOUT | EPOLLIN);
//设置非阻塞模式
//设置非阻塞模式
_socket.setblock(false);
_socket.setNoCloseWait();
int iRet;
int iRet;
#if TARGET_PLATFORM_LINUX
if(_port == 0)
{
iRet = _socket.connectNoThrow(_ip.c_str());
}
else
#endif
#endif
{
iRet = _socket.connectNoThrow(_ip, _port);
iRet = _socket.connectNoThrow(_ip, _port);
}
if(iRet < 0 && !TC_Socket::isInProgress())
@ -321,7 +329,6 @@ int TC_TCPClient::checkSocket()
return EM_CONNECT;
}
int iRetCode = _epoller->wait(_timeout);
if (iRetCode < 0)
{
_socket.close();
@ -452,7 +459,6 @@ int TC_TCPClient::recvBySep(string &sRecvBuffer, const string &sSep)
while(true)
{
int iRetCode = _epoller->wait(_timeout);
// int iRetCode = epoller.wait(_timeout);
if (iRetCode < 0)
{
_socket.close();
@ -465,7 +471,6 @@ int TC_TCPClient::recvBySep(string &sRecvBuffer, const string &sSep)
}
const epoll_event &ev = _epoller->get(0);
if(TC_Epoller::errorEvent(ev))
{
_socket.close();
@ -590,7 +595,7 @@ int TC_TCPClient::recvLength(char *sRecvBuffer, size_t iRecvLen)
if(TC_Epoller::errorEvent(ev))
{
_socket.close();
return EM_SELECT;
return EM_CLOSE;
}
#if TARGET_PLATFORM_IOS
else
@ -679,7 +684,7 @@ int TC_UDPClient::checkSocket()
_epoller->add(_socket.getfd(), 0, EPOLLIN | EPOLLOUT);
try
try
{
#if TARGET_PLATFORM_LINUX
@ -692,7 +697,7 @@ int TC_UDPClient::checkSocket()
}
}
else
#endif
#endif
{
_socket.connect(_ip, _port);
}
@ -758,7 +763,7 @@ int TC_UDPClient::recv(char *sRecvBuffer, size_t &iRecvLen, string &sRemoteIp, u
if(TC_Epoller::errorEvent(ev))
{
_socket.close();
return EM_SELECT;
return EM_CLOSE;
}
#if TARGET_PLATFORM_IOS
else

View File

@ -112,88 +112,87 @@ bool TC_Common::equal(const vector<float>& vx, const vector<float>& vy, double e
{
return equal(vx, vy, float(epsilon));
}
template <>
string TC_Common::tostr<bool>(const bool &t)
{
char buf[2];
buf[0] = t ? '1' : '0';
buf[1] = '\0';
return string(buf);
char buf[2];
buf[0] = t ? '1' : '0';
buf[1] = '\0';
return string(buf);
}
template <>
string TC_Common::tostr<char>(const char &t)
{
char buf[2];
snprintf(buf, 2, "%c", t);
return string(buf);
char buf[2];
snprintf(buf, 2, "%c", t);
return string(buf);
}
template <>
string TC_Common::tostr<unsigned char>(const unsigned char &t)
{
char buf[2];
snprintf(buf, 2, "%c", t);
return string(buf);
char buf[2];
snprintf(buf, 2, "%c", t);
return string(buf);
}
template <>
string TC_Common::tostr<short>(const short &t)
{
char buf[16];
snprintf(buf, 16, "%d", t);
return string(buf);
char buf[16];
snprintf(buf, 16, "%d", t);
return string(buf);
}
template <>
string TC_Common::tostr<unsigned short>(const unsigned short &t)
{
char buf[16];
snprintf(buf, 16, "%u", t);
return string(buf);
char buf[16];
snprintf(buf, 16, "%u", t);
return string(buf);
}
template <>
string TC_Common::tostr<int>(const int &t)
{
char buf[16];
snprintf(buf, 16, "%d", t);
return string(buf);
char buf[16];
snprintf(buf, 16, "%d", t);
return string(buf);
}
template <>
string TC_Common::tostr<unsigned int>(const unsigned int &t)
{
char buf[16];
snprintf(buf, 16, "%u", t);
return string(buf);
char buf[16];
snprintf(buf, 16, "%u", t);
return string(buf);
}
template <>
string TC_Common::tostr<long>(const long &t)
{
char buf[32];
snprintf(buf, 32, "%ld", t);
return string(buf);
char buf[32];
snprintf(buf, 32, "%ld", t);
return string(buf);
}
template <>
string TC_Common::tostr<long long>(const long long &t)
{
char buf[32];
snprintf(buf, 32, "%lld", t);
return string(buf);
char buf[32];
snprintf(buf, 32, "%lld", t);
return string(buf);
}
template <>
string TC_Common::tostr<unsigned long>(const unsigned long &t)
{
char buf[32];
snprintf(buf, 32, "%lu", t);
return string(buf);
char buf[32];
snprintf(buf, 32, "%lu", t);
return string(buf);
}
template <>
@ -202,35 +201,31 @@ string TC_Common::tostr<float>(const float &t)
//C++11 to_string默认保留后面6位小数
string s = std::to_string(t);
// char buf[32];
// snprintf(buf, 32, "%.5f", t);
// string s(buf);
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
if(bFlag)
s = s.substr(0, pos + 1);
if(bFlag)
s = s.substr(0, pos+1);
return s;
return s;
}
template <>
@ -238,78 +233,73 @@ string TC_Common::tostr<double>(const double &t)
{
//C++11 to_string默认保留后面6位小数
string s = std::to_string(t);
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
// char buf[32];
// snprintf(buf, 32, "%.5f", t);
// string s(buf);
if(bFlag)
s = s.substr(0, pos + 1);
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
if(bFlag)
s = s.substr(0, pos+1);
return s;
return s;
}
template <>
string TC_Common::tostr<long double>(const long double &t)
{
char buf[32];
snprintf(buf, 32, "%Lf", t);
string s(buf);
char buf[32];
snprintf(buf, 32, "%Lf", t);
string s(buf);
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
//去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
bool bFlag = false;
int pos = int(s.size() - 1);
for(; pos > 0; --pos)
{
if(s[pos] == '0')
{
bFlag = true;
if(s[pos-1] == '.')
{
//-2为了去掉"."号
pos -= 2;
break;
}
}
else
{
break;
}
}
if(bFlag)
s = s.substr(0, pos+1);
if(bFlag)
s = s.substr(0, pos + 1);
return s;
return s;
}
template <>
string TC_Common::tostr<std::string>(const std::string &t)
{
return t;
return t;
}
string TC_Common::trim(const string &sStr, const string &s, bool bChar)
@ -324,10 +314,10 @@ string TC_Common::trim(const string &sStr, const string &s, bool bChar)
*/
if(!bChar)
{
return trimright(trimleft(sStr, s, false), s, false);
return trimright(trimleft(sStr, s, false), s, false);
}
return trimright(trimleft(sStr, s, true), s, true);
return trimright(trimleft(sStr, s, true), s, true);
}
string TC_Common::trimleft(const string &sStr, const string &s, bool bChar)
@ -349,7 +339,7 @@ string TC_Common::trimleft(const string &sStr, const string &s, bool bChar)
if(sStr.compare(0, s.length(), s) == 0)
{
return sStr.substr(s.length());
return sStr.substr(s.length());
}
return sStr;
@ -371,7 +361,7 @@ string TC_Common::trimleft(const string &sStr, const string &s, bool bChar)
if(pos == 0) return sStr;
return sStr.substr(pos);
return sStr.substr(pos);
}
string TC_Common::trimright(const string &sStr, const string &s, bool bChar)
@ -393,7 +383,7 @@ string TC_Common::trimright(const string &sStr, const string &s, bool bChar)
if(sStr.compare(sStr.length() - s.length(), s.length(), s) == 0)
{
return sStr.substr(0, sStr.length() - s.length());
return sStr.substr(0, sStr.length() - s.length());
}
return sStr;
@ -415,7 +405,7 @@ string TC_Common::trimright(const string &sStr, const string &s, bool bChar)
if(pos == sStr.length()) return sStr;
return sStr.substr(0, pos);
return sStr.substr(0, pos);
}
string TC_Common::lower(const string &s)
@ -426,7 +416,7 @@ string TC_Common::lower(const string &s)
*iter = tolower(*iter);
}
return sString;
return sString;
}
string TC_Common::upper(const string &s)
@ -438,7 +428,7 @@ string TC_Common::upper(const string &s)
*iter = toupper(*iter);
}
return sString;
return sString;
}
bool TC_Common::isdigit(const string &sInput)
@ -472,26 +462,14 @@ public:
time_t secs, local_secs, gmt_secs;
time(&secs);
//带时区时间
TC_Port::localtime_r(&secs, &timeinfo);
// //带时区时间
// #if TARGET_PLATFORM_WINDOWS
// localtime_s(&timeinfo, &secs);
// #else
// localtime_r(&secs, &timeinfo);
// #endif
local_secs = ::mktime(&timeinfo);
//不带时区时间
TC_Port::gmtime_r(&secs, &timeinfo);
// #if TARGET_PLATFORM_WINDOWS
// gmtime_s(&timeinfo, &secs);
// #else
// gmtime_r(&secs, &timeinfo);
// #endif
gmt_secs = ::mktime(&timeinfo);
timezone_diff_secs = local_secs - gmt_secs;
}
@ -501,6 +479,7 @@ public:
int64_t TimezoneHelper::timezone_diff_secs = 0;
int TC_Common::str2tm(const string &sString, const string &sFormat, struct tm &stTm)
{
char *p = strptime(sString.c_str(), sFormat.c_str(), &stTm);
@ -521,7 +500,7 @@ time_t TC_Common::str2time(const string &sString, const string &sFormat)
int TC_Common::strgmt2tm(const string &sString, struct tm &stTm)
{
return str2tm(sString, "%a, %d %b %Y %H:%M:%S GMT", stTm);
return str2tm(sString, "%a, %d %b %Y %H:%M:%S GMT", stTm);
}
string TC_Common::tm2str(const struct tm &stTm, const string &sFormat)
@ -530,31 +509,13 @@ string TC_Common::tm2str(const struct tm &stTm, const string &sFormat)
strftime(sTimeString, sizeof(sTimeString), sFormat.c_str(), &stTm);
return string(sTimeString);
return string(sTimeString);
}
int TC_Common::gettimeofday(struct timeval &tv)
{
return TC_Port::gettimeofday(tv);
// #if TARGET_PLATFORM_WINDOWS
// static const DWORDLONG FILETIME_to_timeval_skew = 116444736000000000;
// FILETIME tfile;
// ::GetSystemTimeAsFileTime(&tfile);
// ULARGE_INTEGER tmp;
// tmp.LowPart = tfile.dwLowDateTime;
// tmp.HighPart = tfile.dwHighDateTime;
// tmp.QuadPart -= FILETIME_to_timeval_skew;
// ULARGE_INTEGER largeInt;
// largeInt.QuadPart = tmp.QuadPart / (10000 * 1000);
// tv.tv_sec = (long)(tmp.QuadPart / (10000 * 1000));
// tv.tv_usec = (long)((tmp.QuadPart % (10000 * 1000)) / 10);
// return 0;
// #else
// return ::gettimeofday(&tv, 0);
// #endif
}
void TC_Common::tm2time(const time_t &t, struct tm &tt)
@ -565,90 +526,67 @@ void TC_Common::tm2time(const time_t &t, struct tm &tt)
TC_Port::gmtime_r(&localt, &tt);
// #if TARGET_PLATFORM_WINDOWS
// //localtime_s
// gmtime_s(&tt, &localt);
// #else
// gmtime_r(&localt, &tt);
// #endif
}
string TC_Common::tm2str(const time_t &t, const string &sFormat)
{
struct tm tt;
tm2time(t, tt);
// localtime_r(&t, &tt);
struct tm tt;
tm2time(t, tt);
return tm2str(tt, sFormat);
return tm2str(tt, sFormat);
}
void TC_Common::tm2tm(const time_t &t, struct tm &tt)
{
static TimezoneHelper helper;
time_t localt = t + TimezoneHelper::timezone_diff_secs;
TC_Port::gmtime_r(&localt, &tt);
// #if TARGET_PLATFORM_WINDOWS
// gmtime_s(&tt, &localt);
// #else
// gmtime_r(&localt, &tt);
// #endif
// // gmtime_r(&localt, &stTm);
}
string TC_Common::now2str(const string &sFormat)
{
time_t t = time(NULL);
return tm2str(t, sFormat.c_str());
return tm2str(t, sFormat.c_str());
}
string TC_Common::now2GMTstr()
{
time_t t = time(NULL);
return tm2GMTstr(t);
return tm2GMTstr(t);
}
string TC_Common::tm2GMTstr(const time_t &t)
{
struct tm tt;
TC_Port::gmtime_r(&t, &tt);
// #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
// gmtime_r(&t, &tt);
// #elif TARGET_PLATFORM_WINDOWS
// _gmtime64_s(&tt, &t);
// #endif
TC_Port::gmtime_r(&t, &tt);
return tm2str(tt, "%a, %d %b %Y %H:%M:%S GMT");
// gmtime_r(&t, &tt);
// return tm2str(tt, "%a, %d %b %Y %H:%M:%S GMT");
}
string TC_Common::tm2GMTstr(const struct tm &stTm)
{
return tm2str(stTm, "%a, %d %b %Y %H:%M:%S GMT");
return tm2str(stTm, "%a, %d %b %Y %H:%M:%S GMT");
}
string TC_Common::nowdate2str()
{
return now2str("%Y%m%d");
return now2str("%Y%m%d");
}
string TC_Common::nowtime2str()
{
return now2str("%H%M%S");
return now2str("%H%M%S");
}
int64_t TC_Common::now2ms()
{
struct timeval tv;
struct timeval tv;
// gettimeofday(&tv, 0);
TC_Common::gettimeofday(tv);
return tv.tv_sec * (int64_t)1000 + tv.tv_usec/1000;
return tv.tv_sec * (int64_t)1000 + tv.tv_usec/1000;
}
int64_t TC_Common::now2us()
@ -656,7 +594,6 @@ int64_t TC_Common::now2us()
struct timeval tv;
TC_Common::gettimeofday(tv);
// gettimeofday(&tv, 0);
return tv.tv_sec * (int64_t)1000000 + tv.tv_usec;
}
@ -687,17 +624,17 @@ string TC_Common::bin2str(const void *buf, size_t len, const string &sSep, size_
}
}
return sOut;
return sOut;
}
string TC_Common::bin2str(const string &sBinData, const string &sSep, size_t lines)
{
return bin2str((const void *)sBinData.data(), sBinData.length(), sSep, lines);
return bin2str((const void *) sBinData.data(), sBinData.length(), sSep, lines);
}
int TC_Common::str2bin(const char *psAsciiData, unsigned char *sBinData, int iBinSize)
{
int iAsciiLength = strlen(psAsciiData);
int iAsciiLength = (int)strlen(psAsciiData);
int iRealLength = (iAsciiLength/2 > iBinSize)?iBinSize:(iAsciiLength/2);
for (int i = 0 ; i < iRealLength ; i++)
@ -711,9 +648,9 @@ string TC_Common::str2bin(const string &sString, const string &sSep, size_t line
{
const char *psAsciiData = sString.c_str();
int iAsciiLength = sString.length();
size_t iAsciiLength = sString.length();
string sBinData;
for (int i = 0 ; i < iAsciiLength ; i++)
for (size_t i = 0 ; i < iAsciiLength ; i++)
{
sBinData += x2c(psAsciiData + i);
i++;
@ -725,7 +662,7 @@ string TC_Common::str2bin(const string &sString, const string &sSep, size_t line
}
}
return sBinData;
return sBinData;
}
char TC_Common::x2c(const string &sWhat)
@ -761,33 +698,33 @@ string TC_Common::replace(const string &sString, const string &sSrc, const strin
pos += sDest.length();
}
return sBuf;
return sBuf;
}
string TC_Common::replace(const string &sString, const map<string,string>& mSrcDest)
{
if(sString.empty())
{
return sString;
}
if(sString.empty())
{
return sString;
}
string tmp = sString;
map<string,string>::const_iterator it = mSrcDest.begin();
string tmp = sString;
map<string,string>::const_iterator it = mSrcDest.begin();
while(it != mSrcDest.end())
{
while(it != mSrcDest.end())
{
string::size_type pos = 0;
while((pos = tmp.find(it->first, pos)) != string::npos)
{
tmp.replace(pos, it->first.length(), it->second);
pos += it->second.length();
}
string::size_type pos = 0;
while((pos = tmp.find(it->first, pos)) != string::npos)
{
tmp.replace(pos, it->first.length(), it->second);
pos += it->second.length();
}
++it;
}
++it;
}
return tmp;
return tmp;
}
bool TC_Common::matchPeriod(const string& s, const string& pat)
@ -954,7 +891,6 @@ size_t TC_Common::toSize(const string &s, size_t iDefaultSize)
return iDefaultSize;
}
string TC_Common::getHostName()
{
string hostName;
@ -971,6 +907,7 @@ string TC_Common::getHostName()
return hostName;
}
#if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
// Generate the randome string, a SHA1-sized random number
@ -991,6 +928,7 @@ void TC_Common::getRandomHexChars(char* p, unsigned int len)
}
#endif
}

View File

@ -66,38 +66,38 @@ TC_ConfigDomain::DomainPath TC_ConfigDomain::parseDomainName(const string& path,
if(bWithParam)
{
string::size_type pos1 = path.find_first_of(TC_CONFIG_PARAM_BEGIN);
if(pos1 == string::npos)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' is invalid!" );
}
string::size_type pos1 = path.find_first_of(TC_CONFIG_PARAM_BEGIN);
if(pos1 == string::npos)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' is invalid!" );
}
if(path[0] != TC_CONFIG_DOMAIN_SEP)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' must start with '/'!" );
}
if(path[0] != TC_CONFIG_DOMAIN_SEP)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' must start with '/'!" );
}
string::size_type pos2 = path.find_first_of(TC_CONFIG_PARAM_END);
if(pos2 == string::npos)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' is invalid!" );
}
string::size_type pos2 = path.find_first_of(TC_CONFIG_PARAM_END);
if(pos2 == string::npos)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' is invalid!" );
}
dp._domains = TC_Common::sepstr<string>(path.substr(1, pos1-1), TC_Common::tostr(TC_CONFIG_DOMAIN_SEP));
dp._param = path.substr(pos1+1, pos2 - pos1 - 1);
dp._param = path.substr(pos1 + 1, pos2 - pos1 - 1);
}
else
{
// if(path.length() <= 1 || path[0] != TC_CONFIG_DOMAIN_SEP)
// if(path.length() <= 1 || path[0] != TC_CONFIG_DOMAIN_SEP)
if(path[0] != TC_CONFIG_DOMAIN_SEP)
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' must start with '/'!" );
}
{
throw TC_Config_Exception("[TC_Config::parseDomainName] : param path '" + path + "' must start with '/'!" );
}
dp._domains = TC_Common::sepstr<string>(path.substr(1), TC_Common::tostr(TC_CONFIG_DOMAIN_SEP));
}
return dp;
return dp;
}
TC_ConfigDomain* TC_ConfigDomain::addSubDomain(const string& name)
@ -114,12 +114,12 @@ TC_ConfigDomain* TC_ConfigDomain::addSubDomain(const string& name)
string TC_ConfigDomain::getParamValue(const string &name) const
{
map<string, string>::const_iterator it = _param.find(name);
if( it == _param.end())
{
throw TC_ConfigNoParam_Exception("[TC_ConfigDomain::getParamValue] param '" + name + "' not exits!");
if( it == _param.end())
{
throw TC_ConfigNoParam_Exception("[TC_ConfigDomain::getParamValue] param '" + name + "' not exits!");
}
return it->second;
return it->second;
}
TC_ConfigDomain *TC_ConfigDomain::getSubTcConfigDomain(vector<string>::const_iterator itBegin, vector<string>::const_iterator itEnd)
@ -131,14 +131,14 @@ TC_ConfigDomain *TC_ConfigDomain::getSubTcConfigDomain(vector<string>::const_ite
map<string, TC_ConfigDomain*>::const_iterator it = _subdomain.find(*itBegin);
//根据匹配规则找不到匹配的子域
if(it == _subdomain.end())
{
return NULL;
}
//根据匹配规则找不到匹配的子域
if(it == _subdomain.end())
{
return NULL;
}
//继续在子域下搜索
return it->second->getSubTcConfigDomain(itBegin + 1, itEnd);
//继续在子域下搜索
return it->second->getSubTcConfigDomain(itBegin + 1, itEnd);
}
const TC_ConfigDomain *TC_ConfigDomain::getSubTcConfigDomain(vector<string>::const_iterator itBegin, vector<string>::const_iterator itEnd) const
@ -150,19 +150,22 @@ const TC_ConfigDomain *TC_ConfigDomain::getSubTcConfigDomain(vector<string>::con
map<string, TC_ConfigDomain*>::const_iterator it = _subdomain.find(*itBegin);
//根据匹配规则找不到匹配的子域
if(it == _subdomain.end())
{
return NULL;
}
//根据匹配规则找不到匹配的子域
if(it == _subdomain.end())
{
return NULL;
}
//继续在子域下搜索
return it->second->getSubTcConfigDomain(itBegin + 1, itEnd);
//继续在子域下搜索
return it->second->getSubTcConfigDomain(itBegin + 1, itEnd);
}
void TC_ConfigDomain::insertParamValue(const map<string, string> &m)
{
_param.insert(m.begin(), m.end());
for(auto e : m)
{
_param[e.first] = e.second;
}
map<string, string>::const_iterator it = m.begin();
while(it != m.end())
@ -290,7 +293,7 @@ string TC_ConfigDomain::parse(const string& s)
}
}
return param;
return param;
}
string TC_ConfigDomain::reverse_parse(const string &s)
@ -330,12 +333,12 @@ string TC_ConfigDomain::reverse_parse(const string &s)
}
}
return param;
return param;
}
string TC_ConfigDomain::getName() const
{
return _name;
return _name;
}
void TC_ConfigDomain::setName(const string& name)
@ -416,13 +419,13 @@ string TC_ConfigDomain::tostr(int i) const
}
buf << sTab << "</" << reverse_parse(_name) << ">" << endl;
buf << sTab << "</" << reverse_parse(_name) << ">" << endl;
return buf.str();
return buf.str();
}
/********************************************************************/
/* TC_Config implement */
/* TC_Config implement */
/********************************************************************/
TC_Config::TC_Config() : _root("")
@ -454,60 +457,60 @@ void TC_Config::parse(istream &is)
string line;
while(getline(is, line))
{
line = TC_Common::trim(line, " \r\n\t");
{
line = TC_Common::trim(line, " \r\n\t");
if(line.length() == 0)
{
continue;
}
if(line.length() == 0)
{
continue;
}
if(line[0] == '#')
{
continue;
}
else if(line[0] == '<')
{
string::size_type posl = line.find_first_of('>');
if(line[0] == '#')
{
continue;
}
else if(line[0] == '<')
{
string::size_type posl = line.find_first_of('>');
if(posl == string::npos)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error! line : " + line);
}
if(posl == string::npos)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error! line : " + line);
}
if(line[1] == '/')
{
string sName(line.substr(2, (posl - 2)));
if(line[1] == '/')
{
string sName(line.substr(2, (posl - 2)));
if(stkTcCnfDomain.size() <= 0)
if(stkTcCnfDomain.size() <= 0)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error! <" + sName + "> hasn't matched domain.");
}
if(stkTcCnfDomain.top()->getName() != sName)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error! <" + stkTcCnfDomain.top()->getName() + "> hasn't match <" + sName +">.");
}
{
throw TC_Config_Exception("[TC_Config::parse]:parse error! <" + stkTcCnfDomain.top()->getName() + "> hasn't match <" + sName +">.");
}
//弹出
stkTcCnfDomain.pop();
}
else
{
string name(line.substr(1, posl - 1));
stkTcCnfDomain.pop();
}
else
{
string name(line.substr(1, posl - 1));
stkTcCnfDomain.push(stkTcCnfDomain.top()->addSubDomain(name));
}
}
else
{
}
}
else
{
stkTcCnfDomain.top()->setParamValue(line);
}
}
}
}
if(stkTcCnfDomain.size() != 1)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error : hasn't match");
if(stkTcCnfDomain.size() != 1)
{
throw TC_Config_Exception("[TC_Config::parse]:parse error : hasn't match");
}
}
@ -515,7 +518,7 @@ void TC_Config::parseFile(const string &sFileName)
{
if(sFileName.length() == 0)
{
throw TC_Config_Exception("[TC_Config::parseFile]:file name is empty");
throw TC_Config_Exception("[TC_Config::parseFile]:file name is empty");
}
ifstream ff;
@ -536,18 +539,18 @@ void TC_Config::parseString(const string& buffer)
parse(iss);
}
string TC_Config::operator[](const string &path)
string TC_Config::operator[](const string &path) const
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(path, true);
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
throw TC_ConfigNoParam_Exception("[TC_Config::operator[]] path '" + path + "' not exits!");
}
if(pTcConfigDomain == NULL)
{
throw TC_ConfigNoParam_Exception("[TC_Config::operator[]] path '" + path + "' not exits!");
}
return pTcConfigDomain->getParamValue(dp._param);
return pTcConfigDomain->getParamValue(dp._param);
}
string TC_Config::get(const string &sName, const string &sDefault) const
@ -556,14 +559,15 @@ string TC_Config::get(const string &sName, const string &sDefault) const
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(sName, true);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
throw TC_ConfigNoParam_Exception("[TC_Config::get] path '" + sName + "' not exits!");
}
if(pTcConfigDomain == NULL)
{
return sDefault;
// throw TC_ConfigNoParam_Exception("[TC_Config::get] path '" + sName + "' not exits!");
}
return pTcConfigDomain->getParamValue(dp._param);
return pTcConfigDomain->getParamValue(dp._param);
}
catch ( TC_ConfigNoParam_Exception &ex )
{
@ -571,18 +575,40 @@ string TC_Config::get(const string &sName, const string &sDefault) const
}
}
void TC_Config::set(const string &sName, const string &value)
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(sName, true);
map<string, string> v;
v[dp._param] = value;
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
pTcConfigDomain = &_root;
for(size_t i = 0; i < dp._domains.size(); i++)
{
pTcConfigDomain = pTcConfigDomain->addSubDomain(dp._domains[i]);
}
}
pTcConfigDomain->insertParamValue(v);
}
bool TC_Config::getDomainMap(const string &path, map<string, string> &m) const
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(path, false);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
return false;
}
if(pTcConfigDomain == NULL)
{
return false;
}
m = pTcConfigDomain->getParamMap();
m = pTcConfigDomain->getParamMap();
return true;
}
@ -593,10 +619,10 @@ map<string, string> TC_Config::getDomainMap(const string &path) const
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(path, false);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain != NULL)
{
if(pTcConfigDomain != NULL)
{
m = pTcConfigDomain->getParamMap();
}
@ -609,14 +635,14 @@ vector<string> TC_Config::getDomainKey(const string &path) const
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(path, false);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain != NULL)
{
if(pTcConfigDomain != NULL)
{
v = pTcConfigDomain->getKey();
}
return v;
return v;
}
vector<string> TC_Config::getDomainLine(const string &path) const
@ -632,7 +658,7 @@ vector<string> TC_Config::getDomainLine(const string &path) const
v = pTcConfigDomain->getLine();
}
return v;
return v;
}
bool TC_Config::hasDomainVector(const string &path) const
@ -666,16 +692,16 @@ bool TC_Config::getDomainVector(const string &path, vector<string> &vtDomains) c
return !vtDomains.empty();
}
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
if(pTcConfigDomain == NULL)
{
return false;
}
}
vtDomains = pTcConfigDomain->getSubDomain();
return true;
return true;
}
vector<string> TC_Config::getDomainVector(const string &path) const
@ -688,12 +714,12 @@ vector<string> TC_Config::getDomainVector(const string &path) const
return _root.getSubDomain();
}
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
const TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
if(pTcConfigDomain == NULL)
{
return vector<string>();
}
}
return pTcConfigDomain->getSubDomain();
}
@ -701,27 +727,27 @@ vector<string> TC_Config::getDomainVector(const string &path) const
TC_ConfigDomain *TC_Config::newTcConfigDomain(const string &sName)
{
return new TC_ConfigDomain(sName);
return new TC_ConfigDomain(sName);
}
TC_ConfigDomain *TC_Config::searchTcConfigDomain(const vector<string>& domains)
{
return _root.getSubTcConfigDomain(domains.begin(), domains.end());
return _root.getSubTcConfigDomain(domains.begin(), domains.end());
}
const TC_ConfigDomain *TC_Config::searchTcConfigDomain(const vector<string>& domains) const
{
return _root.getSubTcConfigDomain(domains.begin(), domains.end());
return _root.getSubTcConfigDomain(domains.begin(), domains.end());
}
int TC_Config::insertDomain(const string &sCurDomain, const string &sAddDomain, bool bCreate)
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(sCurDomain, false);
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
if(pTcConfigDomain == NULL)
{
if(bCreate)
{
pTcConfigDomain = &_root;
@ -735,7 +761,7 @@ int TC_Config::insertDomain(const string &sCurDomain, const string &sAddDomain,
{
return -1;
}
}
}
pTcConfigDomain->addSubDomain(sAddDomain);
@ -746,10 +772,10 @@ int TC_Config::insertDomainParam(const string &sCurDomain, const map<string, str
{
TC_ConfigDomain::DomainPath dp = TC_ConfigDomain::parseDomainName(sCurDomain, false);
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
TC_ConfigDomain *pTcConfigDomain = searchTcConfigDomain(dp._domains);
if(pTcConfigDomain == NULL)
{
if(pTcConfigDomain == NULL)
{
if(bCreate)
{
pTcConfigDomain = &_root;
@ -763,7 +789,7 @@ int TC_Config::insertDomainParam(const string &sCurDomain, const map<string, str
{
return -1;
}
}
}
pTcConfigDomain->insertParamValue(m);
@ -782,7 +808,7 @@ string TC_Config::tostr() const
++it;
}
return buffer;
return buffer;
}
void TC_Config::joinConfig(const TC_Config &cf, bool bUpdate)

View File

@ -23,10 +23,10 @@ using namespace tars;
namespace tars
{
int32_t TC_KetamaHashAlg::hash(const string & sKey)
int32_t TC_KetamaHashAlg::hash(const char *sKey, size_t length)
{
string sMd5 = TC_MD5::md5bin(sKey);
const char *p = (const char *) sMd5.c_str();
vector<char> sMd5 = TC_MD5::md5bin(sKey, length);
const char *p = (const char *) sMd5.data();
int32_t hash = ((int32_t)(p[3] & 0xFF) << 24)
| ((int32_t)(p[2] & 0xFF) << 16)
@ -41,10 +41,10 @@ TC_HashAlgorithmType TC_KetamaHashAlg::getHashType()
return E_TC_CONHASH_KETAMAHASH;
}
int32_t TC_DefaultHashAlg::hash(const string & sKey)
int32_t TC_DefaultHashAlg::hash(const char *sKey, size_t length)
{
string sMd5 = TC_MD5::md5bin(sKey);
const char *p = (const char *) sMd5.c_str();
vector<char> sMd5 = TC_MD5::md5bin(sKey, length);
const char *p = (const char *) sMd5.data();
int32_t hash = (*(int*)(p)) ^ (*(int*)(p+4)) ^ (*(int*)(p+8)) ^ (*(int*)(p+12));
@ -165,8 +165,8 @@ int TC_ConsistentHashNew::addNode(const string & node, unsigned int index, int w
// TODO: 其中KEMATA 为参考memcached client 的hash 算法default 为原有的hash 算法,测试结论在表格里有
if (_ptrHashAlg->getHashType() == E_TC_CONHASH_KETAMAHASH)
{
string sMd5 = TC_MD5::md5bin(virtualNode);
char *p = (char *) sMd5.c_str();
vector<char> sMd5 = TC_MD5::md5bin(virtualNode);
char *p = (char *) sMd5.data();
for (int i = 0; i < 4; i++)
{
@ -180,7 +180,7 @@ int TC_ConsistentHashNew::addNode(const string & node, unsigned int index, int w
}
else
{
stItem.iHashCode = _ptrHashAlg->hash(virtualNode);
stItem.iHashCode = _ptrHashAlg->hash(virtualNode.c_str(), virtualNode.length());
_vHashList.push_back(stItem);
}
}
@ -196,7 +196,8 @@ int TC_ConsistentHashNew::getIndex(const string & key, unsigned int & iIndex)
return -1;
}
int32_t iCode = _ptrHashAlg->hash(TC_MD5::md5bin(key));
vector<char> data = TC_MD5::md5bin(key);
int32_t iCode = _ptrHashAlg->hash(data.data(), data.size());
return getIndex(iCode, iIndex);
}

386
util/src/tc_cron.cpp Normal file
View File

@ -0,0 +1,386 @@
#include "util/tc_cron.h"
#include "util/tc_platform.h"
namespace tars
{
const std::vector<std::string> TC_Cron::DAYS = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
const std::vector<std::string> TC_Cron::MONTHS = { "NIL", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
TC_Cron TC_Cron::makecron(const string& expr)
{
TC_Cron cron;
if (expr.empty())
{
throw TC_CronException("[TC_Cron makecron] invalid empty cron expression");
}
vector<string> fields = TC_Common::sepstr<string>(expr, " ");
if (fields.size() != 6)
throw TC_CronException("[TC_Cron makecron] cron expression must have six fields");
setCronField(fields[0], cron.seconds, CRON_MIN_SECONDS, CRON_MAX_SECONDS);
setCronField(fields[1], cron.minutes, CRON_MIN_MINUTES, CRON_MAX_MINUTES);
setCronField(fields[2], cron.hours, CRON_MIN_HOURS, CRON_MAX_HOURS);
setCronDaysOfWeek(fields[5], cron.days_of_week);
setCronDaysOfMonth(fields[3], cron.days_of_month);
setCronMonth(fields[4], cron.months);
cron.isset = true;
return cron;
}
std::time_t TC_Cron::nextcron(const TC_Cron& cron, std::time_t date)
{
std::tm val;
std::tm* dt = time_to_tm(&date, &val);
if (dt == nullptr) return INVALID_TIME;
time_t original = tm_to_time(*dt);
if (INVALID_TIME == original) return INVALID_TIME;
if (!findNext(cron, *dt, dt->tm_year))
return INVALID_TIME;
time_t calculated = tm_to_time(*dt);
if (INVALID_TIME == calculated) return calculated;
if (calculated == original)
{
addToField(*dt, CronField::second, 1);
if (!findNext(cron, *dt, dt->tm_year))
return INVALID_TIME;
}
return tm_to_time(*dt);
}
std::time_t TC_Cron::nextcron(const TC_Cron& cron)
{
std::time_t now = std::time(0);
return nextcron(cron, now);
}
std::tm* TC_Cron::time_to_tm(std::time_t const* date, std::tm* const out)
{
#if TARGET_PLATFORM_WINDOWS
errno_t err = localtime_s(out, date);
return 0 == err ? out : nullptr;
#else
return localtime_r(date, out);
#endif
}
std::time_t TC_Cron::tm_to_time(std::tm& tmt)
{
return std::mktime(&tmt);
}
bool TC_Cron::contains(const std::string& text, char ch)
{
return std::string::npos != text.find_first_of(ch);
}
cron_int TC_Cron::to_cron_int(const std::string& text)
{
try
{
return static_cast<cron_int>(std::stoul(text.data()));
}
catch (std::exception const& ex)
{
throw TC_CronException(string("[TC_Cron to_cron_int] ") + ex.what());
}
}
std::string TC_Cron::replaceOrdinals(std::string text, const std::vector<std::string> & replacement)
{
for (size_t i = 0; i < replacement.size(); ++i)
{
auto pos = text.find(replacement[i]);
if (std::string::npos != pos)
{
text.replace(pos, 3, std::to_string(i));
}
}
return text;
}
std::pair<cron_int, cron_int> TC_Cron::makeRange(std::string field, cron_int minval, cron_int maxval)
{
cron_int first = 0;
cron_int last = 0;
if (field.size() == 1 && field[0] == '*')
{
first = minval;
last = maxval;
}
else if (!contains(field, '-'))
{
first = to_cron_int(field);
last = first;
}
else
{
auto parts = TC_Common::sepstr<string>(field, "-");
if (parts.size() != 2)
throw TC_CronException("TC_Cron::makeRange] specified range requires two fields");
first = to_cron_int(parts[0]);
last = to_cron_int(parts[1]);
}
if (first > maxval || last > maxval)
{
throw TC_CronException("TC_Cron::makeRange] specified range exceeds maximum");
}
if (first < minval || last < minval)
{
throw TC_CronException("TC_Cron::makeRange] specified range is less than minimum");
}
if (first > last)
{
throw TC_CronException("TC_Cron::makeRange] specified range start exceeds range end");
}
return { first, last };
}
void TC_Cron::setCronDaysOfWeek(std::string value, std::bitset<7>& target)
{
auto days = TC_Common::upper(value);
auto days_replaced = replaceOrdinals(days, DAYS);
if (days_replaced.size() == 1 && days_replaced[0] == '?')
days_replaced[0] = '*';
setCronField(days_replaced,target,CRON_MIN_DAYS_OF_WEEK,CRON_MAX_DAYS_OF_WEEK);
}
void TC_Cron::setCronDaysOfMonth(std::string value, std::bitset<31>& target)
{
if (value.size() == 1 && value[0] == '?')
value[0] = '*';
setCronField(value, target, CRON_MIN_DAYS_OF_MONTH, CRON_MAX_DAYS_OF_MONTH);
}
void TC_Cron::setCronMonth(std::string value, std::bitset<12>& target)
{
auto month = TC_Common::upper(value);
auto month_replaced = replaceOrdinals(month, MONTHS);
setCronField(month_replaced, target, CRON_MIN_MONTHS, CRON_MAX_MONTHS);
}
void TC_Cron::addToField(std::tm& tmt, CronField field, int const val)
{
switch (field)
{
case CronField::second:
tmt.tm_sec += val;
break;
case CronField::minute:
tmt.tm_min += val;
break;
case CronField::hour_of_day:
tmt.tm_hour += val;
break;
case CronField::day_of_week:
case CronField::day_of_month:
tmt.tm_mday += val;
break;
case CronField::month:
tmt.tm_mon += val;
break;
case CronField::year:
tmt.tm_year += val;
break;
}
if (INVALID_TIME == tm_to_time(tmt))
throw TC_CronException("TC_Cron::addToField] invalid time expression");
}
void TC_Cron::setField(std::tm& tmt, CronField field, int const val)
{
switch (field)
{
case CronField::second:
tmt.tm_sec = val;
break;
case CronField::minute:
tmt.tm_min = val;
break;
case CronField::hour_of_day:
tmt.tm_hour = val;
break;
case CronField::day_of_week:
tmt.tm_wday = val;
break;
case CronField::day_of_month:
tmt.tm_mday = val;
break;
case CronField::month:
tmt.tm_mon = val;
break;
case CronField::year:
tmt.tm_year = val;
break;
}
if (INVALID_TIME == tm_to_time(tmt))
throw TC_CronException("TC_Cron::setField] invalid time expression");
}
void TC_Cron::resetField(std::tm& tmt, CronField field)
{
switch (field)
{
case CronField::second:
tmt.tm_sec = 0;
break;
case CronField::minute:
tmt.tm_min = 0;
break;
case CronField::hour_of_day:
tmt.tm_hour = 0;
break;
case CronField::day_of_week:
tmt.tm_wday = 0;
break;
case CronField::day_of_month:
tmt.tm_mday = 1;
break;
case CronField::month:
tmt.tm_mon = 0;
break;
case CronField::year:
tmt.tm_year = 0;
break;
}
if (INVALID_TIME == tm_to_time(tmt))
throw TC_CronException("TC_Cron::resetField] invalid time expression");
}
void TC_Cron::resetAllFields(std::tm& tmt, const std::bitset<7> & marked_fields)
{
for (size_t i = 0; i < marked_fields.size(); ++i)
{
if (marked_fields.test(i))
resetField(tmt, static_cast<CronField>(i));
}
}
void TC_Cron::markField(std::bitset<7>& orders, CronField field)
{
if (!orders.test(static_cast<size_t>(field)))
orders.set(static_cast<size_t>(field));
}
size_t TC_Cron::findNextDay(
std::tm& tmt,
std::bitset<31> const& days_of_month,
size_t day_of_month,
const std::bitset<7> & days_of_week,
size_t day_of_week,
const std::bitset<7> & marked_fields)
{
unsigned int count = 0;
unsigned int maximum = 366;
while (
(!days_of_month.test(day_of_month - CRON_MIN_DAYS_OF_MONTH) ||
!days_of_week.test(day_of_week - CRON_MIN_DAYS_OF_WEEK))
&& count++ < maximum)
{
addToField(tmt, CronField::day_of_month, 1);
day_of_month = tmt.tm_mday;
day_of_week = tmt.tm_wday;
resetAllFields(tmt, marked_fields);
}
return day_of_month;
}
bool TC_Cron::findNext(const TC_Cron & cron, std::tm& tmt, size_t const dot)
{
bool res = true;
std::bitset<7> marked_fields{ 0 };
std::bitset<7> empty_list{ 0 };
unsigned int second = tmt.tm_sec;
auto updated_second = findNext(cron.seconds,tmt,CRON_MIN_SECONDS,CRON_MAX_SECONDS,second,CronField::second,CronField::minute,empty_list);
if (second == updated_second)
{
markField(marked_fields, CronField::second);
}
unsigned int minute = tmt.tm_min;
auto update_minute = findNext(cron.minutes,tmt,CRON_MIN_MINUTES,CRON_MAX_MINUTES,minute,CronField::minute,CronField::hour_of_day,marked_fields);
if (minute == update_minute)
{
markField(marked_fields, CronField::minute);
}
else
{
res = findNext(cron, tmt, dot);
if (!res) return res;
}
unsigned int hour = tmt.tm_hour;
auto updated_hour = findNext(cron.hours,tmt,CRON_MIN_HOURS,CRON_MAX_HOURS,hour,CronField::hour_of_day,CronField::day_of_week,marked_fields);
if (hour == updated_hour)
{
markField(marked_fields, CronField::hour_of_day);
}
else
{
res = findNext(cron, tmt, dot);
if (!res) return res;
}
unsigned int day_of_week = tmt.tm_wday;
unsigned int day_of_month = tmt.tm_mday;
auto updated_day_of_month = findNextDay(tmt,cron.days_of_month,day_of_month,cron.days_of_week,day_of_week,marked_fields);
if (day_of_month == updated_day_of_month)
{
markField(marked_fields, CronField::day_of_month);
}
else
{
res = findNext(cron, tmt, dot);
if (!res) return res;
}
unsigned int month = tmt.tm_mon;
auto updated_month = findNext(cron.months,tmt,CRON_MIN_MONTHS,CRON_MAX_MONTHS,month,
CronField::month,CronField::year,marked_fields);
if (month != updated_month)
{
if (tmt.tm_year - dot > CRON_MAX_YEARS_DIFF)
return false;
res = findNext(cron, tmt, dot);
if (!res) return res;
}
return res;
}
}

581
util/src/tc_des.cpp Normal file
View File

@ -0,0 +1,581 @@
#include "util/tc_des.h"
#include <iostream>
#include <string.h>
#include <algorithm>
namespace tars
{
static const unsigned short bytebit[8] = {
0200, 0100, 040, 020, 010, 04, 02, 01 };
static const uint32_t bigbyte[24] = {
0x800000L, 0x400000L, 0x200000L, 0x100000L,
0x80000L, 0x40000L, 0x20000L, 0x10000L,
0x8000L, 0x4000L, 0x2000L, 0x1000L,
0x800L, 0x400L, 0x200L, 0x100L,
0x80L, 0x40L, 0x20L, 0x10L,
0x8L, 0x4L, 0x2L, 0x1L };
static const unsigned char pc1[56] = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 };
static const unsigned char totrot[16] = {
1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
static const unsigned char pc2[48] = {
13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
static const uint32_t SP1[64] = {
0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
static const uint32_t SP2[64] = {
0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
static const uint32_t SP3[64] = {
0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
static const uint32_t SP4[64] = {
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
static const uint32_t SP5[64] = {
0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
static const uint32_t SP6[64] = {
0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
static const uint32_t SP7[64] = {
0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
static const uint32_t SP8[64] = {
0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
void TC_Des::scrunch(register const char *outof, register uint32_t *into)
{
*into = (*outof++ & 0xffL) << 24;
*into |= (*outof++ & 0xffL) << 16;
*into |= (*outof++ & 0xffL) << 8;
*into++ |= (*outof++ & 0xffL);
*into = (*outof++ & 0xffL) << 24;
*into |= (*outof++ & 0xffL) << 16;
*into |= (*outof++ & 0xffL) << 8;
*into |= (*outof & 0xffL);
return;
}
void TC_Des::unscrun(register uint32_t *outof, register char *into)
{
*into++ = (*outof >> 24) & 0xffL;
*into++ = (*outof >> 16) & 0xffL;
*into++ = (*outof >> 8) & 0xffL;
*into++ = *outof++ & 0xffL;
*into++ = (*outof >> 24) & 0xffL;
*into++ = (*outof >> 16) & 0xffL;
*into++ = (*outof >> 8) & 0xffL;
*into = *outof & 0xffL;
return;
}
void TC_Des::desfunc(register uint32_t *block, register uint32_t *keys)
{
register uint32_t fval, work, right, leftt;
register int round;
leftt = block[0];
right = block[1];
work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >> 16) ^ right) & 0x0000ffffL;
right ^= work;
leftt ^= (work << 16);
work = ((right >> 2) ^ leftt) & 0x33333333L;
leftt ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
leftt ^= work;
right ^= (work << 8);
right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;
for( round = 0; round < 8; round++ )
{
work = (right << 28) | (right >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = right ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
leftt ^= fval;
work = (leftt << 28) | (leftt >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = leftt ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
right ^= fval;
}
right = (right << 31) | (right >> 1);
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = (leftt << 31) | (leftt >> 1);
work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
right ^= work;
leftt ^= (work << 8);
work = ((leftt >> 2) ^ right) & 0x33333333L;
right ^= work;
leftt ^= (work << 2);
work = ((right >> 16) ^ leftt) & 0x0000ffffL;
leftt ^= work;
right ^= (work << 16);
work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
leftt ^= work;
right ^= (work << 4);
*block++ = right;
*block = leftt;
return;
}
void TC_Des::cookey(register uint32_t *raw1, uint32_t *k)
{
register uint32_t *cook, *raw0;
uint32_t dough[32];
register int i;
cook = dough;
for( i = 0; i < 16; i++, raw1++ )
{
raw0 = raw1++;
*cook = (*raw0 & 0x00fc0000L) << 6;
*cook |= (*raw0 & 0x00000fc0L) << 10;
*cook |= (*raw1 & 0x00fc0000L) >> 10;
*cook++ |= (*raw1 & 0x00000fc0L) >> 6;
*cook = (*raw0 & 0x0003f000L) << 12;
*cook |= (*raw0 & 0x0000003fL) << 16;
*cook |= (*raw1 & 0x0003f000L) >> 4;
*cook++ |= (*raw1 & 0x0000003fL);
}
register uint32_t *to, *endp, *p;
to = k, endp = &k[32], p = dough;
while( to < endp )
*to++ = *p++;
}
void TC_Des::deskey(const char *key, short edf, uint32_t *k)
{
register int i, j, l, m, n;
unsigned char pc1m[56], pcr[56];
uint32_t kn[32];
for ( j = 0; j < 56; j++ )
{
l = pc1[j];
m = l & 07;
pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
}
for( i = 0; i < 16; i++ )
{
if( edf == DE1 ) m = (15 - i) << 1;
else m = i << 1;
n = m + 1;
kn[m] = kn[n] = 0L;
for( j = 0; j < 28; j++ )
{
l = j + totrot[i];
if( l < 28 ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l - 28];
}
for( j = 28; j < 56; j++ )
{
l = j + totrot[i];
if( l < 56 ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l - 28];
}
for( j = 0; j < 24; j++ )
{
if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
}
}
cookey(kn, k);
}
void TC_Des::des(const char *inblock, char *outblock, uint32_t *KnL)
{
uint32_t work[2];
scrunch(inblock, work);
desfunc(work, KnL);
unscrun(work, outblock);
}
void TC_Des::des3key(const char *hexkey, short mode, uint32_t *KnL, uint32_t *KnR, uint32_t *Kn3)
{
const char *first, *third;
short revmod;
if( mode == EN0 )
{
revmod = DE1;
first = hexkey;
third = &hexkey[16];
}
else
{
revmod = EN0;
first = &hexkey[16];
third = hexkey;
}
deskey(&hexkey[8], revmod, KnR);
deskey(third, mode, Kn3);
deskey(first, mode, KnL);
}
void TC_Des::des3(const char *from, char *into, uint32_t *KnL, uint32_t *KnR, uint32_t *Kn3)
{
uint32_t work[2];
scrunch(from, work);
desfunc(work, KnL);
desfunc(work, KnR);
desfunc(work, Kn3);
unscrun(work, into);
}
string TC_Des::encrypt(const char *key, const char * sIn, size_t iInlen)
{
char des_key[8] = {0};
char des_input[8] = {0};
char des_output[8] = {0};
//准备key
memcpy(des_key, key, sizeof(des_key));
uint32_t KnL[32] = {0};
deskey(des_key, EN0, KnL);
int mod = iInlen % sizeof(des_input);
size_t k = 0;
string ret;
for(k = 0; k < iInlen / sizeof(des_input); k++)
{
memcpy(des_input, (sIn + k * sizeof(des_input)), sizeof(des_input));
des(des_input, des_output, KnL);
for(size_t i = 0; i < sizeof(des_output); i++)
{
ret += des_output[i];
}
}
//设置填充值
memset(des_input, sizeof(des_input) - mod, sizeof(des_input));
memcpy(des_input, sIn + k * sizeof(des_input), mod);
des(des_input, des_output, KnL);
for(unsigned int i = 0; i < sizeof(des_output); i++)
{
ret += des_output[i];
}
return ret;
}
string TC_Des::decrypt(const char *key, const char * sIn, size_t iInlen)
{
char des_key[8] = {0};
char des_input[8] = {0};
char des_output[8] = {0};
//准备key
memcpy(des_key, key, sizeof(des_key));
uint32_t KnL[32] = {0};
deskey(des_key, DE1, KnL);
string ret;
size_t k = 0;
for(k = 0; k < iInlen / sizeof(des_input); k++)
{
memcpy(des_input, (sIn + k * sizeof(des_input)), sizeof(des_input));
des(des_input, des_output, KnL);
ret.append(des_output,sizeof(des_output));
#if 0
for(size_t i = 0; i < sizeof(des_output); i++)
{
ret += des_output[i];
}
#endif
}
if(ret.length() > 0)
{
//去掉填充字符
int pad = (int)(*(ret.end() - 1));
if(pad>0 && pad<=(int)sizeof(des_output)&& (size_t)pad<=ret.length())
{
bool bOk=true;
size_t iLengthBuf=ret.size();
for(size_t iBuf=0;iBuf<(size_t)(pad-1);iBuf++)
{
if(ret[iLengthBuf-iBuf-2] != ret[iLengthBuf-1])
{
bOk=false;
break;
}
}
if(bOk)
ret.erase(ret.end() - pad, ret.end());
}
}
return ret;
}
string TC_Des::encrypt3(const char *key, const char * sIn, size_t iInlen)
{
char des_key[24] = {0};
memcpy(des_key, key, std::min(sizeof(des_key), strlen(key)));
size_t len = (iInlen + ((size_t) 7)) & (~((size_t)7));
size_t post_len = iInlen % 8;
if(post_len == 0)
{
len += 8;
}
char *in_data = new char[len];
char *out_data = new char[len];
memcpy(in_data, sIn, iInlen);
//in_data不是8 的整数倍的话
if ( post_len != 0)
{
for(size_t i = 0; i < 8 - post_len; i++)
{
*(in_data + iInlen + i) = (char)(8-post_len);
}
}
else
{
for(size_t i = 0; i < 8; i++)
{
*(in_data + iInlen + i) = 0x08;
}
}
uint32_t KnL[32] = {0};
uint32_t KnR[32] = {0};
uint32_t Kn3[32] = {0};
des3key(des_key, EN0, KnL, KnR, Kn3);
size_t count = len/8;
for(size_t i = 0; i < count; ++i)
{
des3(in_data + i * 8, out_data + i * 8, KnL, KnR, Kn3);
}
string ret(out_data, len);
delete []in_data;
delete []out_data;
return ret;
}
string TC_Des::decrypt3(const char *key, const char * sIn, size_t iInlen)
{
try
{
char des_key[24] = {0};
memcpy(des_key, key, min(sizeof(des_key), strlen(key)));
size_t len = iInlen;
char *in_data = new char[len];
char *out_data = new char[len];
memcpy(in_data, sIn, iInlen);
uint32_t KnL[32] = {0};
uint32_t KnR[32] = {0};
uint32_t Kn3[32] = {0};
des3key(des_key, DE1, KnL, KnR, Kn3);
size_t count = len/8;
for(size_t i = 0; i < count; ++i)
{
des3(in_data + i * 8, out_data + i * 8, KnL, KnR, Kn3);
}
string ret((char*)out_data, len);
if(ret.length() > 0)
{
//去掉填充字符
int pad = (int)(*(ret.end() - 1));
ret.erase(ret.end() - pad, ret.end());
}
delete []in_data;
delete []out_data;
return ret;
}
catch(TC_DES_Exception &e)
{
string res = "TC_Des::decrypt3 error" + string(e.what());
throw TC_DES_Exception(res);
}
}
}

View File

@ -52,7 +52,7 @@ static const int BUFFER_SIZE = 8 * 1024;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// handle的实现
TC_EpollServer::Handle::Handle()
TC_EpollServer::Handle::Handle()
: _pEpollServer(NULL)
, _iWaitTime(100)
{
@ -305,7 +305,6 @@ TC_EpollServer::BindAdapter::BindAdapter(TC_EpollServer *pEpollServer)
, _iHeaderLen(0)
, _iHeartBeatTime(0)
, _protocolName("tars")
, _iBackPacketBuffLimit(0)
{
}
@ -317,14 +316,16 @@ TC_EpollServer::BindAdapter::~BindAdapter()
void TC_EpollServer::BindAdapter::setProtocolName(const string& name)
{
TC_ThreadLock::Lock lock(*this);
std::lock_guard<std::mutex> lock (_mutex);
_protocolName = name;
}
const string& TC_EpollServer::BindAdapter::getProtocolName()
{
return _protocolName;
std::lock_guard<std::mutex> lock (_mutex);
return _protocolName;
}
bool TC_EpollServer::BindAdapter::isTarsProtocol()
@ -334,26 +335,26 @@ bool TC_EpollServer::BindAdapter::isTarsProtocol()
bool TC_EpollServer::BindAdapter::isIpAllow(const string& ip) const
{
TC_ThreadLock::Lock lock(*this);
std::lock_guard<std::mutex> lock (_mutex);
if(_eOrder == ALLOW_DENY)
{
if(TC_Common::matchPeriod(ip,_vtAllow))
if(TC_Common::matchPeriod(ip, _vtAllow))
{
return true;
}
if(TC_Common::matchPeriod(ip,_vtDeny))
if(TC_Common::matchPeriod(ip, _vtDeny))
{
return false;
}
}
else
{
if(TC_Common::matchPeriod(ip,_vtDeny))
if(TC_Common::matchPeriod(ip, _vtDeny))
{
return false;
}
if(TC_Common::matchPeriod(ip,_vtAllow))
if(TC_Common::matchPeriod(ip, _vtAllow))
{
return true;
}
@ -465,12 +466,6 @@ size_t TC_EpollServer::BindAdapter::getRecvBufferSize() const
return _iBufferSize;
}
TC_EpollServer* TC_EpollServer::BindAdapter::getEpollServer()
{
return _pEpollServer;
}
TC_NetWorkBuffer::PACKET_TYPE TC_EpollServer::BindAdapter::echo_protocol(TC_NetWorkBuffer &r, vector<char> &o)
{
o = r.getBuffers();
@ -487,13 +482,13 @@ TC_NetWorkBuffer::PACKET_TYPE TC_EpollServer::BindAdapter::echo_header_filter(TC
void TC_EpollServer::BindAdapter::setName(const string &name)
{
TC_ThreadLock::Lock lock(*this);
std::lock_guard<std::mutex> lock (_mutex);
_name = name;
}
string TC_EpollServer::BindAdapter::getName() const
{
std::lock_guard<std::mutex> lock (_mutex);
return _name;
}
@ -502,11 +497,6 @@ int TC_EpollServer::BindAdapter::getHandleNum()
return _iHandleNum;
}
void TC_EpollServer::BindAdapter::setHandleNum(int n)
{
_iHandleNum = n;
}
int TC_EpollServer::BindAdapter::getQueueCapacity() const
{
return _iQueueCapacity;
@ -519,22 +509,17 @@ void TC_EpollServer::BindAdapter::setQueueCapacity(int n)
int TC_EpollServer::BindAdapter::isOverloadorDiscard()
{
// int iRecvBufferSize = _rbuffer.size();
int iRecvBufferSize = _iBufferSize;
if(iRecvBufferSize <= (_iQueueCapacity / 2))//未过载
{
return 0;
}
else if(iRecvBufferSize > (_iQueueCapacity / 2) && (iRecvBufferSize < _iQueueCapacity) && (_iQueueCapacity > 0))//overload
if(iRecvBufferSize > (int)(_iQueueCapacity / 5.*4) && (iRecvBufferSize < _iQueueCapacity) && (_iQueueCapacity > 0)) //overload
{
//超过队列4/5开始认为过载
return -1;
}
else//队列满需要丢弃接受的数据包
else if(iRecvBufferSize > (int)(_iQueueCapacity) && _iQueueCapacity > 0)//队列满需要丢弃接受的数据包
{
return -2;
}
return 0;
}
@ -558,7 +543,6 @@ int TC_EpollServer::BindAdapter::getQueueTimeout() const
void TC_EpollServer::BindAdapter::setEndpoint(const string &str)
{
std::lock_guard<std::mutex> lock (_mutex);
// TC_ThreadLock::Lock lock(*this);
_ep.parse(str);
}
@ -569,7 +553,7 @@ TC_Endpoint TC_EpollServer::BindAdapter::getEndpoint() const
return _ep;
}
TC_Socket& TC_EpollServer::BindAdapter::getSocket()
TC_Socket &TC_EpollServer::BindAdapter::getSocket()
{
return _s;
}
@ -618,33 +602,29 @@ TC_EpollServer::BindAdapter::EOrder TC_EpollServer::BindAdapter::getOrder() cons
return _eOrder;
}
vector<string> TC_EpollServer::BindAdapter::getAllow() const
const vector<string> &TC_EpollServer::BindAdapter::getAllow() const
{
std::lock_guard<std::mutex> lock (_mutex);
return _vtAllow;
}
vector<string> TC_EpollServer::BindAdapter::getDeny() const
const vector<string> &TC_EpollServer::BindAdapter::getDeny() const
{
std::lock_guard<std::mutex> lock (_mutex);
return _vtDeny;
}
bool TC_EpollServer::BindAdapter::isLimitMaxConnection() const
{
return (_iCurConns + 1 > _iMaxConns) || (_iCurConns + 1 > ((int)((uint32_t)1 << 22) -1));
return (_iCurConns + 1 > (size_t)_iMaxConns) || (_iCurConns + 1 > (int)((uint32_t)1 << 22) - 1);
}
void TC_EpollServer::BindAdapter::decreaseNowConnection()
{
_iCurConns++;
--_iCurConns;
}
void TC_EpollServer::BindAdapter::increaseNowConnection()
{
_iCurConns--;
++_iCurConns;
}
int TC_EpollServer::BindAdapter::getNowConnection() const
@ -657,7 +637,6 @@ vector<TC_EpollServer::ConnStatus> TC_EpollServer::BindAdapter::getConnStatus()
return _pEpollServer->getConnStatus(_s.getfd());
}
void TC_EpollServer::BindAdapter::setProtocol(const TC_NetWorkBuffer::protocol_functor &pf, int iHeaderLen, const TC_EpollServer::header_filter_functor &hf)
{
_pf = pf;
@ -682,19 +661,18 @@ int TC_EpollServer::BindAdapter::getHeaderFilterLen()
return _iHeaderLen;
}
void TC_EpollServer::BindAdapter::setBackPacketBuffLimit(size_t iLimitSize)
{
_iBackPacketBuffLimit = iLimitSize;
}
// void TC_EpollServer::BindAdapter::setBackPacketBuffLimit(size_t iLimitSize)
// {
// _iBackPacketBuffLimit = iLimitSize;
// }
size_t TC_EpollServer::BindAdapter::getBackPacketBuffLimit()
{
return _iBackPacketBuffLimit;
}
// size_t TC_EpollServer::BindAdapter::getBackPacketBuffLimit()
// {
// return _iBackPacketBuffLimit;
// }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 服务连接
TC_EpollServer::Connection::Connection(TC_EpollServer::BindAdapter *pBindAdapter, int lfd, int timeout, int fd, const string& ip, uint16_t port)
: _pBindAdapter(pBindAdapter)
, _uid(0)
@ -708,10 +686,14 @@ TC_EpollServer::Connection::Connection(TC_EpollServer::BindAdapter *pBindAdapter
, _bClose(false)
, _enType(EM_TCP)
, _bEmptyConn(true)
, _pRecvBuffer(NULL)
, _nRecvBufferSize(DEFAULT_RECV_BUFFERSIZE)
{
assert(fd != -1);
_iLastRefreshTime = TNOW;
_sock.init(fd, true, pBindAdapter->_ep.isIPv6() ? AF_INET6 : AF_INET);
}
@ -727,21 +709,23 @@ TC_EpollServer::Connection::Connection(BindAdapter *pBindAdapter, int fd)
, _bClose(false)
, _enType(EM_UDP)
, _bEmptyConn(false) /*udp is always false*/
, _pRecvBuffer(NULL)
, _nRecvBufferSize(DEFAULT_RECV_BUFFERSIZE)
{
_iLastRefreshTime = TNOW;
_sock.init(fd, false, pBindAdapter->_ep.isIPv6() ? AF_INET6 : AF_INET);
}
TC_EpollServer::Connection::~Connection()
{
if(_pRecvBuffer)
if (_pRecvBuffer)
{
delete _pRecvBuffer;
_pRecvBuffer = NULL;
}
// clearSlices(_sendBuffer);
if (isTcp())
{
assert(!_sock.isValid());
@ -759,8 +743,6 @@ void TC_EpollServer::Connection::tryInitAuthState(int initState)
void TC_EpollServer::Connection::close()
{
if(isTcp())
{
#if TARS_SSL
if (_openssl)
{
@ -768,10 +750,10 @@ void TC_EpollServer::Connection::close()
_openssl.reset();
}
#endif
if(_sock.isValid())
{
_sock.close();
}
if (isTcp() && _sock.isValid())
{
_sock.close();
}
}
@ -814,7 +796,7 @@ int TC_EpollServer::Connection::parseProtocol(TC_NetWorkBuffer &rbuf)
//需要过滤首包包头
if(_iHeaderLen > 0)
{
if(rbuf.getBufferLength() >= (unsigned) _iHeaderLen)
if(rbuf.getBufferLength() >= (unsigned)_iHeaderLen)
{
vector<char> header;
rbuf.getHeader(_iHeaderLen, header);
@ -835,6 +817,7 @@ int TC_EpollServer::Connection::parseProtocol(TC_NetWorkBuffer &rbuf)
vector<char> ro;
TC_NetWorkBuffer::PACKET_TYPE b = _pBindAdapter->getProtocol()(rbuf, ro);
if(b == TC_NetWorkBuffer::PACKET_LESS)
{
break;
@ -856,8 +839,8 @@ int TC_EpollServer::Connection::parseProtocol(TC_NetWorkBuffer &rbuf)
}
else
{
_pBindAdapter->getEpollServer()->error("recv [" + _ip + ":" + TC_Common::tostr(_port) + "],packet error.");
return -1; //协议解析错误
_pBindAdapter->getEpollServer()->error("recv [" + _ip + ":" + TC_Common::tostr(_port) + "], packet parse error.");
return -1; //协议解析错误
}
}
}
@ -891,9 +874,6 @@ int TC_EpollServer::Connection::recvTcp()
{
if (TC_Socket::isPending())
{
//#if TARGET_PLATFORM_WINDOWS
// _pBindAdapter->getNetThreadOfFd(_sock.getfd())->getEpoller()->mod(_sock.getfd(), getId(), EPOLLIN | EPOLLOUT);
//#endif
//没有数据了
break;
}
@ -912,7 +892,6 @@ int TC_EpollServer::Connection::recvTcp()
}
else
{
// cout << "totalRecv:" << totalRecv << endl;
#if TARS_SSL
if (_pBindAdapter->getEndpoint().isSSL())
@ -977,9 +956,6 @@ int TC_EpollServer::Connection::recvUdp()
{
if (TC_Socket::isPending())//errno == EAGAIN)
{
//#if TARGET_PLATFORM_WINDOWS
// _pBindAdapter->getNetThreadOfFd(_sock.getfd())->getEpoller()->mod(_sock.getfd(), getId(), EPOLLIN | EPOLLOUT);
//#endif
//没有数据了
break;
}
@ -1044,10 +1020,6 @@ int TC_EpollServer::Connection::sendBuffer()
{
if (TC_Socket::isPending())
{
//#if TARGET_PLATFORM_WINDOWS
// _pBindAdapter->getNetThreadOfFd(_sock.getfd())->getEpoller()->mod(_sock.getfd(), getId(), EPOLLIN | EPOLLOUT);
//#endif
break;
}
else
@ -1110,15 +1082,12 @@ int TC_EpollServer::Connection::sendUdp(const shared_ptr<SendContext> &sc)
{
//udp的直接发送即可
int iRet = _sock.sendto((const void *) sc->buffer()->buffer(), sc->buffer()->length(), sc->ip(), sc->port(), 0);
if (iRet < 0 && !TC_Socket::isPending())
if (iRet < 0)
{
_pBindAdapter->getEpollServer()->error("[TC_EpollServer::Connection] send [" + _ip + ":" + TC_Common::tostr(_port) + "] error");
return -1;
}
//#if TARGET_PLATFORM_WINDOWS
// _pBindAdapter->getNetThreadOfFd(_sock.getfd())->getEpoller()->mod(_sock.getfd(), getId(), EPOLLIN | EPOLLOUT);
//#endif
return 0;
}
@ -1155,12 +1124,12 @@ bool TC_EpollServer::Connection::setClose()
////////////////////////////////////////////////////////////////
//
TC_EpollServer::ConnectionList::ConnectionList(TC_EpollServer::NetThread *pEpollServer)
:_pEpollServer(pEpollServer)
,_total(0)
,_free_size(0)
,_vConn(NULL)
,_lastTimeoutTime(0)
,_iConnectionMagic(0)
: _pEpollServer(pEpollServer)
, _total(0)
, _free_size(0)
, _vConn(NULL)
, _lastTimeoutTime(0)
, _iConnectionMagic(0)
{
}
@ -1170,10 +1139,10 @@ void TC_EpollServer::ConnectionList::init(uint32_t size, uint32_t iIndex)
_total = size;
_free_size = 0;
_free_size = 0;
//初始化链接链表
if(_vConn) delete[] _vConn;
if (_vConn) delete[] _vConn;
//分配total+1个空间(多分配一个空间, 第一个空间其实无效)
_vConn = new list_data[_total+1];
@ -1193,7 +1162,7 @@ void TC_EpollServer::ConnectionList::init(uint32_t size, uint32_t iIndex)
uint32_t TC_EpollServer::ConnectionList::getUniqId()
{
TC_ThreadLock::Lock lock(*this);
TC_LockT<TC_SpinLock> lock(_mutex);
uint32_t uid = _free.front();
@ -1218,7 +1187,7 @@ TC_EpollServer::Connection* TC_EpollServer::ConnectionList::get(uint32_t uid)
void TC_EpollServer::ConnectionList::add(Connection *cPtr, time_t iTimeOutStamp)
{
TC_LockT<TC_SpinLock> lock(_mutex);
TC_LockT<TC_SpinLock> lock(_mutex);
uint32_t muid = cPtr->getId();
uint32_t magi = muid & (0xFFFFFFFF << 22);
@ -1231,7 +1200,7 @@ void TC_EpollServer::ConnectionList::add(Connection *cPtr, time_t iTimeOutStamp)
void TC_EpollServer::ConnectionList::refresh(uint32_t uid, time_t iTimeOutStamp)
{
TC_LockT<TC_SpinLock> lock(_mutex);
TC_LockT<TC_SpinLock> lock(_mutex);
uint32_t magi = uid & (0xFFFFFFFF << 22);
uid = uid & (0x7FFFFFFF >> 9);
@ -1261,7 +1230,6 @@ void TC_EpollServer::ConnectionList::checkTimeout(time_t iCurTime)
_lastTimeoutTime = iCurTime;
TC_LockT<TC_SpinLock> lock(_mutex);
// TC_ThreadLock::Lock lock(*this);
multimap<time_t, uint32_t>::iterator it = _tl.begin();
@ -1284,13 +1252,12 @@ void TC_EpollServer::ConnectionList::checkTimeout(time_t iCurTime)
}
//超时关闭
_pEpollServer->delConnection(_vConn[uid].first, false,EM_SERVER_TIMEOUT_CLOSE);
_pEpollServer->delConnection(_vConn[uid].first, false, EM_SERVER_TIMEOUT_CLOSE);
//从链表中删除
_del(uid);
}
if(_pEpollServer->isEmptyConnCheck())
{
it = _tl.begin();
@ -1304,7 +1271,7 @@ void TC_EpollServer::ConnectionList::checkTimeout(time_t iCurTime)
//获取空连接的超时时间点
time_t iEmptyTimeout = (it->first - _vConn[uid].first->getTimeout()) + (_pEpollServer->getEmptyConnTimeout()/1000);
//已经检查到当前时间点了, 后续不用在检查了
//已经检查到当前时间点了, 后续不用在检查了
if(iEmptyTimeout > iCurTime)
{
break;
@ -1318,7 +1285,7 @@ void TC_EpollServer::ConnectionList::checkTimeout(time_t iCurTime)
}
//超时关闭
_pEpollServer->delConnection(_vConn[uid].first, false,EM_SERVER_TIMEOUT_CLOSE);
_pEpollServer->delConnection(_vConn[uid].first, false, EM_SERVER_TIMEOUT_CLOSE);
//从链表中删除
_del(uid);
@ -1342,11 +1309,11 @@ vector<TC_EpollServer::ConnStatus> TC_EpollServer::ConnectionList::getConnStatus
{
TC_EpollServer::ConnStatus cs;
cs.iLastRefreshTime = _vConn[i].first->_iLastRefreshTime;
cs.ip = _vConn[i].first->getIp();
cs.port = _vConn[i].first->getPort();
cs.timeout = _vConn[i].first->getTimeout();
cs.uid = _vConn[i].first->getId();
cs.iLastRefreshTime = _vConn[i].first->_iLastRefreshTime;
cs.ip = _vConn[i].first->getIp();
cs.port = _vConn[i].first->getPort();
cs.timeout = _vConn[i].first->getTimeout();
cs.uid = _vConn[i].first->getId();
v.push_back(cs);
}
@ -1390,11 +1357,10 @@ size_t TC_EpollServer::ConnectionList::size()
}
//////////////////////////////NetThread//////////////////////////////////
TC_EpollServer::NetThread::NetThread(TC_EpollServer *epollServer, int index)
TC_EpollServer::NetThread::NetThread(TC_EpollServer *epollServer, int threadIndex)
: _epollServer(epollServer)
, _threadIndex(index)
, _threadIndex(threadIndex)
, _bTerminate(false)
, _handleStarted(false)
, _list(this)
, _bEmptyConnAttackCheck(false)
, _iEmptyCheckTimeout(MIN_EMPTY_CONN_TIMEOUT)
@ -1410,47 +1376,47 @@ TC_EpollServer::NetThread::~NetThread()
{
}
void TC_EpollServer::NetThread::debug(const string &s)
void TC_EpollServer::NetThread::debug(const string &s) const
{
_epollServer->debug(s);
}
void TC_EpollServer::NetThread::info(const string &s)
void TC_EpollServer::NetThread::info(const string &s) const
{
_epollServer->info(s);
}
void TC_EpollServer::NetThread::tars(const string &s)
void TC_EpollServer::NetThread::tars(const string &s) const
{
_epollServer->tars(s);
}
void TC_EpollServer::NetThread::error(const string &s)
void TC_EpollServer::NetThread::error(const string &s) const
{
_epollServer->error(s);
}
void TC_EpollServer::NetThread::enAntiEmptyConnAttack(bool bEnable)
{
_bEmptyConnAttackCheck = bEnable;
_bEmptyConnAttackCheck = bEnable;
}
void TC_EpollServer::NetThread::setEmptyConnTimeout(int timeout)
{
_iEmptyCheckTimeout = (timeout>=MIN_EMPTY_CONN_TIMEOUT)?timeout:MIN_EMPTY_CONN_TIMEOUT;
_iEmptyCheckTimeout = (timeout >= MIN_EMPTY_CONN_TIMEOUT) ? timeout : MIN_EMPTY_CONN_TIMEOUT;
}
void TC_EpollServer::NetThread::setUdpRecvBufferSize(size_t nSize)
{
_nUdpRecvBufferSize = (nSize >= 8192 && nSize <=DEFAULT_RECV_BUFFERSIZE)?nSize:DEFAULT_RECV_BUFFERSIZE;
_nUdpRecvBufferSize = (nSize >= 8192 && nSize <= DEFAULT_RECV_BUFFERSIZE) ? nSize : DEFAULT_RECV_BUFFERSIZE;
}
bool TC_EpollServer::NetThread::isEmptyConnCheck() const
{
return _bEmptyConnAttackCheck;
return _bEmptyConnAttackCheck;
}
int TC_EpollServer::NetThread::getEmptyConnTimeout() const
int TC_EpollServer::NetThread::getEmptyConnTimeout() const
{
return _iEmptyCheckTimeout;
}
@ -1531,7 +1497,7 @@ void TC_EpollServer::NetThread::addTcpConnection(TC_EpollServer::Connection *cPt
}
}
#endif
//注意epoll add必须放在最后, 否则可能导致执行完, 才调用上面语句
//注意epoll add必须放在最后, 否则可能导致执行完, 才调用上面语句
_epoller.add(cPtr->getfd(), cPtr->getId(), EPOLLIN | EPOLLOUT);
}
@ -1551,7 +1517,7 @@ vector<TC_EpollServer::ConnStatus> TC_EpollServer::NetThread::getConnStatus(int
return _list.getConnStatus(lfd);
}
void TC_EpollServer::NetThread::delConnection(TC_EpollServer::Connection *cPtr, bool bEraseList,EM_CLOSE_T closeType)
void TC_EpollServer::NetThread::delConnection(TC_EpollServer::Connection *cPtr, bool bEraseList, EM_CLOSE_T closeType)
{
//如果是TCP的连接才真正的关闭连接
if (cPtr->getListenfd() != -1)
@ -1559,7 +1525,7 @@ void TC_EpollServer::NetThread::delConnection(TC_EpollServer::Connection *cPtr,
//false的情况,是超时被主动删除
if(!bEraseList)
{
info("timeout [" + cPtr->getIp() + ":" + TC_Common::tostr(cPtr->getPort()) + "] del from list");
tars("timeout [" + cPtr->getIp() + ":" + TC_Common::tostr(cPtr->getPort()) + "] del from list");
}
uint32_t uid = cPtr->getId();
@ -1635,44 +1601,50 @@ void TC_EpollServer::NetThread::processPipe()
while(!_sbuffer.empty())
{
shared_ptr<SendContext> sc = _sbuffer.front();
shared_ptr<SendContext> sc = _sbuffer.front();
Connection *cPtr = getConnectionPtr(sc->uid());
if (cPtr) {
switch (sc->cmd()) {
case 'c': {
if (cPtr->setClose()) {
delConnection(cPtr, true, EM_SERVER_CLOSE);
}
break;
}
case 's': {
int ret = 0;
#if TARS_SSL
if (cPtr->getBindAdapter()->getEndpoint().isSSL()) {
if (!cPtr->_openssl->isHandshaked()) {
return;
}
}
ret = cPtr->send(sc);
#else
ret = cPtr->send(sc);
#endif
if (ret < 0) {
delConnection(cPtr, true, (ret == -1) ? EM_CLIENT_CLOSE : EM_SERVER_CLOSE);
}
else {
_list.refresh(sc->uid(), cPtr->getTimeout() + TNOW);
}
break;
}
default:
assert(false);
}
if (!cPtr)
{
continue;
}
_sbuffer.pop_front();
switch (sc->cmd())
{
case 'c':
{
if (cPtr->setClose())
{
delConnection(cPtr, true, EM_SERVER_CLOSE);
}
break;
}
case 's':
{
int ret = 0;
#if TARS_SSL
if (cPtr->getBindAdapter()->getEndpoint().isSSL()) {
if (!cPtr->_openssl->isHandshaked()) {
return;
}
}
ret = cPtr->send(sc);
#else
ret = cPtr->send(sc);
#endif
if (ret < 0)
{
delConnection(cPtr, true, (ret == -1) ? EM_CLIENT_CLOSE : EM_SERVER_CLOSE);
}
else
{
_list.refresh(sc->uid(), cPtr->getTimeout() + TNOW);
}
break;
}
default:
assert(false);
}
_sbuffer.pop_front();
}
}
@ -1697,7 +1669,6 @@ void TC_EpollServer::NetThread::processNet(const epoll_event &ev)
if (TC_Epoller::readEvent(ev))
{
int ret = cPtr->recv();
if (ret < 0)
{
delConnection(cPtr, true, EM_CLIENT_CLOSE);
@ -1735,7 +1706,7 @@ void TC_EpollServer::NetThread::run()
}
}
//循环监听网路连接请求
//循环监听网路连接请求
while(!_bTerminate)
{
_list.checkTimeout(TNOW);
@ -1783,6 +1754,7 @@ void TC_EpollServer::NetThread::run()
}
}
}
size_t TC_EpollServer::NetThread::getSendRspSize()
{
return _sbuffer.size();
@ -1792,7 +1764,7 @@ TC_EpollServer::TC_EpollServer(unsigned int iNetThreadNum)
: _netThreadNum(iNetThreadNum)
, _bTerminate(false)
, _handleStarted(false)
, _pLocalLogger(NULL)
, _pLocalLogger(NULL)
{
#if TARGET_PLATFORM_WINDOWS
WSADATA wsadata;
@ -1816,7 +1788,7 @@ TC_EpollServer::TC_EpollServer(unsigned int iNetThreadNum)
_notify.init(&_epoller);
_notify.add(_notify.notifyFd());
for (size_t i = 0; i < _netThreadNum; ++i)
for (size_t i = 0; i < _netThreadNum; ++i)
{
TC_EpollServer::NetThread* netThreads = new TC_EpollServer::NetThread(this, i);
_netThreads.push_back(netThreads);
@ -1854,7 +1826,6 @@ void TC_EpollServer::applicationCallback(TC_EpollServer *epollServer)
{
}
bool TC_EpollServer::accept(int fd, int domain)
{
struct sockaddr_in stSockAddr4;
@ -1946,7 +1917,7 @@ void TC_EpollServer::waitForShutdown()
while (!_bTerminate)
{
int iEvNum = _epoller.wait(1000);
int iEvNum = _epoller.wait(300);
if (_bTerminate)
break;
@ -2002,7 +1973,7 @@ void TC_EpollServer::waitForShutdown()
}
catch (...)
{
error("run exception");
error("TC_EpollServer::waitForShutdown unknown error");
}
}
}
@ -2049,7 +2020,6 @@ void TC_EpollServer::setEmptyConnTimeout(int timeout)
}
}
void TC_EpollServer::bind(const TC_Endpoint &ep, TC_Socket &s, bool manualListen)
{
#if TARGET_PLATFORM_WINDOWS
@ -2124,7 +2094,7 @@ int TC_EpollServer::bind(BindAdapterPtr &lsPtr)
return s.getfd();
}
void TC_EpollServer::addConnection(TC_EpollServer::Connection * cPtr, int fd, TC_EpollServer::CONN_TYPE iType)
void TC_EpollServer::addConnection(TC_EpollServer::Connection *cPtr, int fd, TC_EpollServer::CONN_TYPE iType)
{
TC_EpollServer::NetThread* netThread = getNetThreadOfFd(fd);
@ -2265,7 +2235,7 @@ void TC_EpollServer::send(const shared_ptr<SendContext> &data)
netThread->send(data);
}
void TC_EpollServer::debug(const string &s)
void TC_EpollServer::debug(const string &s) const
{
if(_pLocalLogger)
{
@ -2273,7 +2243,7 @@ void TC_EpollServer::debug(const string &s)
}
}
void TC_EpollServer::info(const string &s)
void TC_EpollServer::info(const string &s) const
{
if(_pLocalLogger)
{
@ -2281,7 +2251,7 @@ void TC_EpollServer::info(const string &s)
}
}
void TC_EpollServer::tars(const string &s)
void TC_EpollServer::tars(const string &s) const
{
if(_pLocalLogger)
{
@ -2289,7 +2259,7 @@ void TC_EpollServer::tars(const string &s)
}
}
void TC_EpollServer::error(const string &s)
void TC_EpollServer::error(const string &s) const
{
if(_pLocalLogger)
{
@ -2311,7 +2281,6 @@ vector<TC_EpollServer::ConnStatus> TC_EpollServer::getConnStatus(int lfd)
return vConnStatus;
}
unordered_map<int, TC_EpollServer::BindAdapterPtr> TC_EpollServer::getListenSocketInfo()
{
return _listeners;

View File

@ -22,10 +22,66 @@
#include <string.h>
#include <cassert>
namespace tars
{
bool TC_GZip::compress(const char *src, size_t length, string& buffer)
{
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
{
return false;
}
buffer.clear();
static char gz_simple_header[] = { '\037', '\213', '\010', '\000', '\000', '\000', '\000', '\000', '\002', '\377' };
size_t destLen = sizeof(gz_simple_header) + length * 2;
char *out = new char[destLen];
stream.next_out = (Bytef *)out;
stream.avail_out = destLen;
stream.next_in = (Bytef *)src;
stream.avail_in = length;
memcpy(stream.next_out, gz_simple_header, sizeof(gz_simple_header));
stream.next_out += sizeof(gz_simple_header);
stream.avail_out -= sizeof(gz_simple_header);
int r = deflate(&stream, Z_FINISH);
if (r != Z_STREAM_END)
{
delete[] out;
return false;
}
destLen = destLen - stream.avail_out;
uLong crc = crc32(0, Z_NULL, 0);
crc = crc32(crc, (const Bytef *)src, length);
memcpy(out + destLen, &crc, 4);
memcpy(out + destLen + 4, &length, 4);
destLen += 8;
buffer.append(out, destLen);
delete[] out;
deflateEnd(&stream);
return true;
}
bool TC_GZip::compress(const char *src, size_t length, vector<char>& buffer)
{
buffer.clear();

View File

@ -27,7 +27,7 @@ int TC_HashMap::Block::getBlockData(TC_HashMap::BlockData &data)
data._synct = getSyncTime();
string s;
int ret = get(s);
int ret = get(s);
if(ret != TC_HashMap::RT_OK)
{
@ -59,238 +59,241 @@ int TC_HashMap::Block::getBlockData(TC_HashMap::BlockData &data)
size_t TC_HashMap::Block::getLastBlockHead()
{
size_t iHead = _iHead;
size_t iHead = _iHead;
while(getBlockHead(iHead)->_iBlockNext != 0)
{
iHead = getBlockHead(iHead)->_iBlockNext;
}
return iHead;
while(getBlockHead(iHead)->_iBlockNext != 0)
{
iHead = getBlockHead(iHead)->_iBlockNext;
}
return iHead;
}
int TC_HashMap::Block::get(void *pData, size_t &iDataLen)
{
//没有下一个chunk, 一个chunk就可以装下数据了
if(!getBlockHead()->_bNextChunk)
{
memcpy(pData, getBlockHead()->_cData, min(getBlockHead()->_iDataLen, iDataLen));
iDataLen = getBlockHead()->_iDataLen;
return TC_HashMap::RT_OK;
}
else
{
size_t iUseSize = getBlockHead()->_iSize - sizeof(tagBlockHead);
size_t iCopyLen = min(iUseSize, iDataLen);
//没有下一个chunk, 一个chunk就可以装下数据了
if(!getBlockHead()->_bNextChunk)
{
memcpy(pData, getBlockHead()->_cData, min(getBlockHead()->_iDataLen, iDataLen));
iDataLen = getBlockHead()->_iDataLen;
return TC_HashMap::RT_OK;
}
else
{
size_t iUseSize = getBlockHead()->_iSize - sizeof(tagBlockHead);
size_t iCopyLen = min(iUseSize, iDataLen);
//copy到当前的block中
memcpy(pData, getBlockHead()->_cData, iCopyLen);
if (iDataLen < iUseSize)
{
return TC_HashMap::RT_NOTALL_ERR; //copy数据不完全
}
//copy到当前的block中
memcpy(pData, getBlockHead()->_cData, iCopyLen);
if (iDataLen < iUseSize)
{
return TC_HashMap::RT_NOTALL_ERR; //copy数据不完全
}
//已经copy长度
size_t iHasLen = iCopyLen;
//最大剩余长度
size_t iLeftLen = iDataLen - iCopyLen;
//已经copy长度
size_t iHasLen = iCopyLen;
//最大剩余长度
size_t iLeftLen = iDataLen - iCopyLen;
tagChunkHead *pChunk = getChunkHead(getBlockHead()->_iNextChunk);
while(iHasLen < iDataLen)
{
tagChunkHead *pChunk = getChunkHead(getBlockHead()->_iNextChunk);
while(iHasLen < iDataLen)
{
iUseSize = pChunk->_iSize - sizeof(tagChunkHead);
if(!pChunk->_bNextChunk)
{
//copy到当前的chunk中
size_t iCopyLen = min(pChunk->_iDataLen, iLeftLen);
memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
iDataLen = iHasLen + iCopyLen;
if(!pChunk->_bNextChunk)
{
//copy到当前的chunk中
size_t iCopyLen = min(pChunk->_iDataLen, iLeftLen);
memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
iDataLen = iHasLen + iCopyLen;
if(iLeftLen < pChunk->_iDataLen)
{
return TC_HashMap::RT_NOTALL_ERR; //copy不完全
}
if(iLeftLen < pChunk->_iDataLen)
{
return TC_HashMap::RT_NOTALL_ERR; //copy不完全
}
return TC_HashMap::RT_OK;
}
else
{
size_t iCopyLen = min(iUseSize, iLeftLen);
//copy当前的chunk
memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
if (iLeftLen <= iUseSize)
{
iDataLen = iHasLen + iCopyLen;
return TC_HashMap::RT_NOTALL_ERR; //copy不完全
}
return TC_HashMap::RT_OK;
}
else
{
size_t iCopyLen = min(iUseSize, iLeftLen);
//copy当前的chunk
memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
//copy当前chunk完全
iHasLen += iCopyLen;
iLeftLen -= iCopyLen;
// 这里有bug = 的时候正好可以,不能返回出错!
//if (iLeftLen <= iUseSize)
if (iLeftLen < iUseSize)
{
iDataLen = iHasLen + iCopyLen;
return TC_HashMap::RT_NOTALL_ERR; //copy不完全
}
pChunk = getChunkHead(pChunk->_iNextChunk);
}
}
}
//copy当前chunk完全
iHasLen += iCopyLen;
iLeftLen -= iCopyLen;
return TC_HashMap::RT_OK;
pChunk = getChunkHead(pChunk->_iNextChunk);
}
}
}
return TC_HashMap::RT_OK;
}
int TC_HashMap::Block::get(string &s)
{
size_t iLen = getDataLen();
size_t iLen = getDataLen();
char *cData = new char[iLen];
size_t iGetLen = iLen;
int ret = get(cData, iGetLen);
if(ret == TC_HashMap::RT_OK)
{
s.assign(cData, iGetLen);
}
char *cData = new char[iLen];
size_t iGetLen = iLen;
int ret = get(cData, iGetLen);
if(ret == TC_HashMap::RT_OK)
{
s.assign(cData, iGetLen);
}
delete[] cData;
delete[] cData;
return ret;
return ret;
}
int TC_HashMap::Block::set(const void *pData, size_t iDataLen, bool bOnlyKey, vector<TC_HashMap::BlockData> &vtData)
{
//首先分配刚刚够的长度, 不能多一个chunk, 也不能少一个chunk
int ret = allocate(iDataLen, vtData);
if(ret != TC_HashMap::RT_OK)
{
return ret;
}
//首先分配刚刚够的长度, 不能多一个chunk, 也不能少一个chunk
int ret = allocate(iDataLen, vtData);
if(ret != TC_HashMap::RT_OK)
{
return ret;
}
if(bOnlyKey)
{
//原始数据是脏数据
if(getBlockHead()->_bDirty)
{
_pMap->delDirtyCount();
}
if(bOnlyKey)
{
//原始数据是脏数据
if(getBlockHead()->_bDirty)
{
_pMap->delDirtyCount();
}
//数据被修改, 设置为脏数据
getBlockHead()->_bDirty = false;
//数据被修改, 设置为脏数据
getBlockHead()->_bDirty = false;
//原始数据不是OnlyKey数据
if(!getBlockHead()->_bOnlyKey)
{
_pMap->incOnlyKeyCount();
}
}
else
{
//原始数据不是脏数据
if(!getBlockHead()->_bDirty)
{
_pMap->incDirtyCount();
}
//原始数据不是OnlyKey数据
if(!getBlockHead()->_bOnlyKey)
{
_pMap->incOnlyKeyCount();
}
}
else
{
//原始数据不是脏数据
if(!getBlockHead()->_bDirty)
{
_pMap->incDirtyCount();
}
//数据被修改, 设置为脏数据
getBlockHead()->_bDirty = true;
//数据被修改, 设置为脏数据
getBlockHead()->_bDirty = true;
//原始数据是OnlyKey数据
if(getBlockHead()->_bOnlyKey)
{
_pMap->delOnlyKeyCount();
}
}
//原始数据是OnlyKey数据
if(getBlockHead()->_bOnlyKey)
{
_pMap->delOnlyKeyCount();
}
}
//设置是否只有Key
getBlockHead()->_bOnlyKey = bOnlyKey;
size_t iUseSize = getBlockHead()->_iSize - sizeof(tagBlockHead);
//没有下一个chunk, 一个chunk就可以装下数据了
if(!getBlockHead()->_bNextChunk)
{
memcpy(getBlockHead()->_cData, (char*)pData, iDataLen);
//先copy数据, 再复制数据长度
getBlockHead()->_iDataLen = iDataLen;
}
else
{
//copy到当前的block中
memcpy(getBlockHead()->_cData, (char*)pData, iUseSize);
//剩余程度
size_t iLeftLen = iDataLen - iUseSize;
size_t iCopyLen = iUseSize;
size_t iUseSize = getBlockHead()->_iSize - sizeof(tagBlockHead);
//没有下一个chunk, 一个chunk就可以装下数据了
if(!getBlockHead()->_bNextChunk)
{
memcpy(getBlockHead()->_cData, (char*)pData, iDataLen);
//先copy数据, 再复制数据长度
getBlockHead()->_iDataLen = iDataLen;
}
else
{
//copy到当前的block中
memcpy(getBlockHead()->_cData, (char*)pData, iUseSize);
//剩余程度
size_t iLeftLen = iDataLen - iUseSize;
size_t iCopyLen = iUseSize;
tagChunkHead *pChunk = getChunkHead(getBlockHead()->_iNextChunk);
while(true)
{
tagChunkHead *pChunk = getChunkHead(getBlockHead()->_iNextChunk);
while(true)
{
//计算chunk的可用大小
iUseSize = pChunk->_iSize - sizeof(tagChunkHead);
if(!pChunk->_bNextChunk)
{
if(!pChunk->_bNextChunk)
{
assert(iUseSize >= iLeftLen);
//copy到当前的chunk中
memcpy(pChunk->_cData, (char*)pData + iCopyLen, iLeftLen);
//最后一个chunk, 才有数据长度, 先copy数据再赋值长度
pChunk->_iDataLen = iLeftLen;
iCopyLen += iLeftLen;
iLeftLen -= iLeftLen;
break;
}
else
{
//copy到当前的chunk中
memcpy(pChunk->_cData, (char*)pData + iCopyLen, iUseSize);
iCopyLen += iUseSize;
iLeftLen -= iUseSize;
//copy到当前的chunk中
memcpy(pChunk->_cData, (char*)pData + iCopyLen, iLeftLen);
//最后一个chunk, 才有数据长度, 先copy数据再赋值长度
pChunk->_iDataLen = iLeftLen;
iCopyLen += iLeftLen;
iLeftLen -= iLeftLen;
break;
}
else
{
//copy到当前的chunk中
memcpy(pChunk->_cData, (char*)pData + iCopyLen, iUseSize);
iCopyLen += iUseSize;
iLeftLen -= iUseSize;
pChunk = getChunkHead(pChunk->_iNextChunk);
}
}
pChunk = getChunkHead(pChunk->_iNextChunk);
}
}
assert(iLeftLen == 0);
}
}
_pMap->doUpdate(true);
return TC_HashMap::RT_OK;
_pMap->doUpdate(true);
return TC_HashMap::RT_OK;
}
void TC_HashMap::Block::setDirty(bool b)
{
if(getBlockHead()->_bDirty != b)
{
if (b)
{
_pMap->incDirtyCount();
}
else
{
_pMap->delDirtyCount();
}
_pMap->update(&getBlockHead()->_bDirty, b);
if(getBlockHead()->_bDirty != b)
{
if (b)
{
_pMap->incDirtyCount();
}
else
{
_pMap->delDirtyCount();
}
_pMap->update(&getBlockHead()->_bDirty, b);
_pMap->doUpdate(true);
}
}
}
bool TC_HashMap::Block::nextBlock()
{
_iHead = getBlockHead()->_iBlockNext;
_iHead = getBlockHead()->_iBlockNext;
return _iHead != 0;
return _iHead != 0;
}
bool TC_HashMap::Block::prevBlock()
{
_iHead = getBlockHead()->_iBlockPrev;
_iHead = getBlockHead()->_iBlockPrev;
return _iHead != 0;
return _iHead != 0;
}
void TC_HashMap::Block::deallocate()
{
vector<size_t> v;
v.push_back(_iHead);
vector<size_t> v;
v.push_back(_iHead);
if(getBlockHead()->_bNextChunk)
{
deallocate(getBlockHead()->_iNextChunk);
}
if(getBlockHead()->_bNextChunk)
{
deallocate(getBlockHead()->_iNextChunk);
}
_pMap->_pDataAllocator->deallocateMemBlock(v);
_pMap->_pDataAllocator->deallocateMemBlock(v);
}
void TC_HashMap::Block::makeNew(size_t index, size_t iAllocSize)
@ -309,68 +312,68 @@ void TC_HashMap::Block::makeNew(size_t index, size_t iAllocSize)
getBlockHead()->_bDirty = true;
getBlockHead()->_bOnlyKey = false;
_pMap->incDirtyCount();
_pMap->incElementCount();
_pMap->incListCount(index);
_pMap->incDirtyCount();
_pMap->incElementCount();
_pMap->incListCount((uint32_t)index);
//挂在block链表上
if(_pMap->item(index)->_iBlockAddr == 0)
{
//当前hash桶没有元素
_pMap->update(&_pMap->item(index)->_iBlockAddr, _iHead);
_pMap->update(&getBlockHead()->_iBlockNext, (size_t)0);
_pMap->update(&getBlockHead()->_iBlockPrev, (size_t)0);
}
else
{
//当然hash桶有元素, 挂在桶开头
_pMap->update(&getBlockHead(_pMap->item(index)->_iBlockAddr)->_iBlockPrev, _iHead);
_pMap->update(&getBlockHead()->_iBlockNext, _pMap->item(index)->_iBlockAddr);
_pMap->update(&_pMap->item(index)->_iBlockAddr, _iHead);
_pMap->update(&getBlockHead()->_iBlockPrev, (size_t)0);
}
//挂在block链表上
if(_pMap->item(index)->_iBlockAddr == 0)
{
//当前hash桶没有元素
_pMap->update(&_pMap->item(index)->_iBlockAddr, _iHead);
_pMap->update(&getBlockHead()->_iBlockNext, (size_t)0);
_pMap->update(&getBlockHead()->_iBlockPrev, (size_t)0);
}
else
{
//当然hash桶有元素, 挂在桶开头
_pMap->update(&getBlockHead(_pMap->item(index)->_iBlockAddr)->_iBlockPrev, _iHead);
_pMap->update(&getBlockHead()->_iBlockNext, _pMap->item(index)->_iBlockAddr);
_pMap->update(&_pMap->item(index)->_iBlockAddr, _iHead);
_pMap->update(&getBlockHead()->_iBlockPrev, (size_t)0);
}
//挂在Set链表的头部
if(_pMap->_pHead->_iSetHead == 0)
{
assert(_pMap->_pHead->_iSetTail == 0);
_pMap->update(&_pMap->_pHead->_iSetHead, _iHead);
_pMap->update(&_pMap->_pHead->_iSetTail, _iHead);
}
else
{
//挂在Set链表的头部
if(_pMap->_pHead->_iSetHead == 0)
{
assert(_pMap->_pHead->_iSetTail == 0);
_pMap->update(&_pMap->_pHead->_iSetHead, _iHead);
_pMap->update(&_pMap->_pHead->_iSetTail, _iHead);
}
else
{
assert(_pMap->_pHead->_iSetTail != 0);
_pMap->update(&getBlockHead()->_iSetNext, _pMap->_pHead->_iSetHead);
_pMap->update(&getBlockHead(_pMap->_pHead->_iSetHead)->_iSetPrev, _iHead);
_pMap->update(&_pMap->_pHead->_iSetHead, _iHead);
}
_pMap->update(&getBlockHead()->_iSetNext, _pMap->_pHead->_iSetHead);
_pMap->update(&getBlockHead(_pMap->_pHead->_iSetHead)->_iSetPrev, _iHead);
_pMap->update(&_pMap->_pHead->_iSetHead, _iHead);
}
//挂在Get链表头部
if(_pMap->_pHead->_iGetHead == 0)
{
assert(_pMap->_pHead->_iGetTail == 0);
_pMap->update(&_pMap->_pHead->_iGetHead, _iHead);
_pMap->update(&_pMap->_pHead->_iGetTail, _iHead);
}
else
{
if(_pMap->_pHead->_iGetHead == 0)
{
assert(_pMap->_pHead->_iGetTail == 0);
_pMap->update(&_pMap->_pHead->_iGetHead, _iHead);
_pMap->update(&_pMap->_pHead->_iGetTail, _iHead);
}
else
{
assert(_pMap->_pHead->_iGetTail != 0);
_pMap->update(&getBlockHead()->_iGetNext, _pMap->_pHead->_iGetHead);
_pMap->update(&getBlockHead(_pMap->_pHead->_iGetHead)->_iGetPrev, _iHead);
_pMap->update(&_pMap->_pHead->_iGetHead, _iHead);
}
_pMap->update(&getBlockHead()->_iGetNext, _pMap->_pHead->_iGetHead);
_pMap->update(&getBlockHead(_pMap->_pHead->_iGetHead)->_iGetPrev, _iHead);
_pMap->update(&_pMap->_pHead->_iGetHead, _iHead);
}
//一次写更新操作
_pMap->doUpdate(true);
//一次写更新操作
_pMap->doUpdate(true);
}
void TC_HashMap::Block::erase()
{
//////////////////修改脏数据链表/////////////
if(_pMap->_pHead->_iDirtyTail == _iHead)
{
_pMap->update(&_pMap->_pHead->_iDirtyTail, getBlockHead()->_iSetPrev);
}
//////////////////修改脏数据链表/////////////
if(_pMap->_pHead->_iDirtyTail == _iHead)
{
_pMap->update(&_pMap->_pHead->_iDirtyTail, getBlockHead()->_iSetPrev);
}
//////////////////修改回写数据链表/////////////
if(_pMap->_pHead->_iSyncTail == _iHead)
@ -384,120 +387,120 @@ void TC_HashMap::Block::erase()
_pMap->update(&_pMap->_pHead->_iBackupTail, getBlockHead()->_iGetPrev);
}
////////////////////修改Set链表的数据//////////
////////////////////修改Set链表的数据//////////
{
bool bHead = (_pMap->_pHead->_iSetHead == _iHead);
bool bTail = (_pMap->_pHead->_iSetTail == _iHead);
bool bHead = (_pMap->_pHead->_iSetHead == _iHead);
bool bTail = (_pMap->_pHead->_iSetTail == _iHead);
if(!bHead)
{
if(bTail)
{
assert(getBlockHead()->_iSetNext == 0);
//是尾部, 尾部指针指向上一个元素
_pMap->update(&_pMap->_pHead->_iSetTail, getBlockHead()->_iSetPrev);
_pMap->update(&getBlockHead(getBlockHead()->_iSetPrev)->_iSetNext, (size_t)0);
}
else
{
//不是头部也不是尾部
assert(getBlockHead()->_iSetNext != 0);
_pMap->update(&getBlockHead(getBlockHead()->_iSetPrev)->_iSetNext, getBlockHead()->_iSetNext);
_pMap->update(&getBlockHead(getBlockHead()->_iSetNext)->_iSetPrev, getBlockHead()->_iSetPrev);
}
}
else
{
if(bTail)
{
assert(getBlockHead()->_iSetNext == 0);
assert(getBlockHead()->_iSetPrev == 0);
//头部也是尾部, 指针都设置为0
_pMap->update(&_pMap->_pHead->_iSetHead, (size_t)0);
_pMap->update(&_pMap->_pHead->_iSetTail, (size_t)0);
}
else
{
//头部不是尾部, 头部指针指向下一个元素
assert(getBlockHead()->_iSetNext != 0);
_pMap->update(&_pMap->_pHead->_iSetHead, getBlockHead()->_iSetNext);
if(!bHead)
{
if(bTail)
{
assert(getBlockHead()->_iSetNext == 0);
//是尾部, 尾部指针指向上一个元素
_pMap->update(&_pMap->_pHead->_iSetTail, getBlockHead()->_iSetPrev);
_pMap->update(&getBlockHead(getBlockHead()->_iSetPrev)->_iSetNext, (size_t)0);
}
else
{
//不是头部也不是尾部
assert(getBlockHead()->_iSetNext != 0);
_pMap->update(&getBlockHead(getBlockHead()->_iSetPrev)->_iSetNext, getBlockHead()->_iSetNext);
_pMap->update(&getBlockHead(getBlockHead()->_iSetNext)->_iSetPrev, getBlockHead()->_iSetPrev);
}
}
else
{
if(bTail)
{
assert(getBlockHead()->_iSetNext == 0);
assert(getBlockHead()->_iSetPrev == 0);
//头部也是尾部, 指针都设置为0
_pMap->update(&_pMap->_pHead->_iSetHead, (size_t)0);
_pMap->update(&_pMap->_pHead->_iSetTail, (size_t)0);
}
else
{
//头部不是尾部, 头部指针指向下一个元素
assert(getBlockHead()->_iSetNext != 0);
_pMap->update(&_pMap->_pHead->_iSetHead, getBlockHead()->_iSetNext);
//下一个元素上指针为0
_pMap->update(&getBlockHead(getBlockHead()->_iSetNext)->_iSetPrev, (size_t)0);
}
}
}
}
}
////////////////////修改Get链表的数据//////////
//
////////////////////修改Get链表的数据//////////
//
{
bool bHead = (_pMap->_pHead->_iGetHead == _iHead);
bool bTail = (_pMap->_pHead->_iGetTail == _iHead);
bool bHead = (_pMap->_pHead->_iGetHead == _iHead);
bool bTail = (_pMap->_pHead->_iGetTail == _iHead);
if(!bHead)
{
if(bTail)
{
assert(getBlockHead()->_iGetNext == 0);
//是尾部, 尾部指针指向上一个元素
_pMap->update(&_pMap->_pHead->_iGetTail, getBlockHead()->_iGetPrev);
_pMap->update(&getBlockHead(getBlockHead()->_iGetPrev)->_iGetNext, (size_t)0);
}
else
{
//不是头部也不是尾部
assert(getBlockHead()->_iGetNext != 0);
_pMap->update(&getBlockHead(getBlockHead()->_iGetPrev)->_iGetNext, getBlockHead()->_iGetNext);
_pMap->update(&getBlockHead(getBlockHead()->_iGetNext)->_iGetPrev, getBlockHead()->_iGetPrev);
}
}
else
{
if(bTail)
{
assert(getBlockHead()->_iGetNext == 0);
assert(getBlockHead()->_iGetPrev == 0);
//头部也是尾部, 指针都设置为0
_pMap->update(&_pMap->_pHead->_iGetHead, (size_t)0);
_pMap->update(&_pMap->_pHead->_iGetTail, (size_t)0);
}
else
{
//头部不是尾部, 头部指针指向下一个元素
assert(getBlockHead()->_iGetNext != 0);
_pMap->update(&_pMap->_pHead->_iGetHead, getBlockHead()->_iGetNext);
if(!bHead)
{
if(bTail)
{
assert(getBlockHead()->_iGetNext == 0);
//是尾部, 尾部指针指向上一个元素
_pMap->update(&_pMap->_pHead->_iGetTail, getBlockHead()->_iGetPrev);
_pMap->update(&getBlockHead(getBlockHead()->_iGetPrev)->_iGetNext, (size_t)0);
}
else
{
//不是头部也不是尾部
assert(getBlockHead()->_iGetNext != 0);
_pMap->update(&getBlockHead(getBlockHead()->_iGetPrev)->_iGetNext, getBlockHead()->_iGetNext);
_pMap->update(&getBlockHead(getBlockHead()->_iGetNext)->_iGetPrev, getBlockHead()->_iGetPrev);
}
}
else
{
if(bTail)
{
assert(getBlockHead()->_iGetNext == 0);
assert(getBlockHead()->_iGetPrev == 0);
//头部也是尾部, 指针都设置为0
_pMap->update(&_pMap->_pHead->_iGetHead, (size_t)0);
_pMap->update(&_pMap->_pHead->_iGetTail, (size_t)0);
}
else
{
//头部不是尾部, 头部指针指向下一个元素
assert(getBlockHead()->_iGetNext != 0);
_pMap->update(&_pMap->_pHead->_iGetHead, getBlockHead()->_iGetNext);
//下一个元素上指针为0
_pMap->update(&getBlockHead(getBlockHead()->_iGetNext)->_iGetPrev, (size_t)0);
}
}
}
}
}
///////////////////从block链表中去掉///////////
//
//上一个block指向下一个block
if(getBlockHead()->_iBlockPrev != 0)
{
_pMap->update(&getBlockHead(getBlockHead()->_iBlockPrev)->_iBlockNext, getBlockHead()->_iBlockNext);
}
///////////////////从block链表中去掉///////////
//
//上一个block指向下一个block
if(getBlockHead()->_iBlockPrev != 0)
{
_pMap->update(&getBlockHead(getBlockHead()->_iBlockPrev)->_iBlockNext, getBlockHead()->_iBlockNext);
}
//下一个block指向上一个
if(getBlockHead()->_iBlockNext != 0)
{
_pMap->update(&getBlockHead(getBlockHead()->_iBlockNext)->_iBlockPrev, getBlockHead()->_iBlockPrev);
}
//下一个block指向上一个
if(getBlockHead()->_iBlockNext != 0)
{
_pMap->update(&getBlockHead(getBlockHead()->_iBlockNext)->_iBlockPrev, getBlockHead()->_iBlockPrev);
}
//////////////////如果是hash头部, 需要修改hash索引数据指针//////
//
_pMap->delListCount(getBlockHead()->_iIndex);
if(getBlockHead()->_iBlockPrev == 0)
{
//如果是hash桶的头部, 则还需要处理
TC_HashMap::tagHashItem *pItem = _pMap->item(getBlockHead()->_iIndex);
assert(pItem->_iBlockAddr == _iHead);
if(pItem->_iBlockAddr == _iHead)
{
_pMap->update(&pItem->_iBlockAddr, getBlockHead()->_iBlockNext);
}
}
//////////////////如果是hash头部, 需要修改hash索引数据指针//////
//
_pMap->delListCount(getBlockHead()->_iIndex);
if(getBlockHead()->_iBlockPrev == 0)
{
//如果是hash桶的头部, 则还需要处理
TC_HashMap::tagHashItem *pItem = _pMap->item(getBlockHead()->_iIndex);
assert(pItem->_iBlockAddr == _iHead);
if(pItem->_iBlockAddr == _iHead)
{
_pMap->update(&pItem->_iBlockAddr, getBlockHead()->_iBlockNext);
}
}
//////////////////脏数据///////////////////
//
@ -801,7 +804,7 @@ int TC_HashMap::Block::allocateChunk(size_t fn, vector<size_t> &chunks, vector<T
}
//设置分配的数据块的大小
getChunkHead(t)->_iSize = iAllocSize;
getChunkHead(t)->_iSize = (uint32_t)iAllocSize;
chunks.push_back(t);
@ -2515,7 +2518,7 @@ void TC_HashMap::doUpdate(bool bUpdate)
{
*(size_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = _pstModifyHead->_stModifyData[i]._iModifyValue;
}
#if __WORDSIZE == 6|| defined _WIN644
#if __WORDSIZE == 64 || defined _WIN64
else if(_pstModifyHead->_stModifyData[i]._cBytes == sizeof(uint32_t))
{
*(uint32_t*)((char*)_pHead + _pstModifyHead->_stModifyData[i]._iModifyAddr) = (uint32_t)_pstModifyHead->_stModifyData[i]._iModifyValue;

View File

@ -119,7 +119,7 @@ int TC_HashMapCompact::Block::get(void *pData, uint32_t &iDataLen)
uint32_t iCopyLen = min(iUseSize, iLeftLen);
//copy当前的chunk
memcpy((char*)pData + iHasLen, pChunk->_cData, iCopyLen);
if (iLeftLen <= iUseSize)
if (iLeftLen < iUseSize)
{
iDataLen = iHasLen + iCopyLen;
return TC_HashMapCompact::RT_NOTALL_ERR; //copy不完全
@ -2675,17 +2675,17 @@ void TC_HashMapCompact::doUpdate()
void TC_HashMapCompact::doUpdate2()
{
if(_pstModifyHead->_cModifyStatus == 1)
{
for(size_t i=_pstModifyHead->_iNowIndex-1; i>=0; --i)
{
if(_pstModifyHead->_stModifyData[i]._cBytes == 0)
{
_pstModifyHead->_iNowIndex = i+1;
return;
}
}
}
if(_pstModifyHead->_cModifyStatus == 1)
{
for(int i=_pstModifyHead->_iNowIndex-1; i>=0; --i)
{
if(_pstModifyHead->_stModifyData[i]._cBytes == 0)
{
_pstModifyHead->_iNowIndex = i+1;
return;
}
}
}
}
void TC_HashMapCompact::doUpdate3()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -106,8 +106,8 @@ namespace tars
int next_size = 0;
for (size_t c = 1; c < kNumClasses; c++)
{
const int max_size_in_class = class_to_size_[c];
for (int s = next_size; s <= max_size_in_class; s += kAlignment)
const size_t max_size_in_class = class_to_size_[c];
for (size_t s = next_size; s <= max_size_in_class; s += kAlignment)
{
class_array_[ClassIndex(s)] = c;
}

Some files were not shown because too many files have changed in this diff Show More