From 0b2175e5dc5aeec5b471651a39348d5523741977 Mon Sep 17 00:00:00 2001 From: jarodruan Date: Mon, 27 Jan 2020 20:13:21 +0800 Subject: [PATCH] util & tools compiler succ --- CMakeLists.txt | 85 ++++- servant/tup/tup.h | 24 +- tools/tars2android/tars2android.cpp | 8 +- tools/tars2c/tars2c.cpp | 14 +- tools/tars2cs/tars2cs.cpp | 16 +- tools/tars2node/gen_js.cpp | 2 +- tools/tars2node/gen_js_dts.cpp | 2 +- tools/tars2node/gen_proxy.cpp | 2 +- tools/tars2node/gen_proxy_dts.cpp | 2 +- tools/tars2node/gen_server.cpp | 2 +- tools/tars2node/gen_server_dts.cpp | 2 +- tools/tars2node/gen_server_imp.cpp | 2 +- tools/tars2oc/tars2oc.cpp | 48 +-- tools/tars2php/tars2php.cpp | 12 +- tools/tars2python/tars2python.cpp | 22 +- util/CMakeLists.txt | 8 +- util/include/util/tc_dyn_object.h | 2 +- util/include/util/tc_encoder.h | 100 +++--- util/include/util/tc_epoll_server.h | 72 +++-- util/include/util/tc_ex.h | 3 - util/include/util/tc_hashmap.h | 4 +- util/include/util/tc_hashmap_compact.h | 4 +- util/include/util/tc_http_async.h | 394 +++++++++++------------ util/include/util/tc_logger.h | 4 +- util/include/util/tc_mmap.h | 2 +- util/include/util/tc_monitor.h | 2 +- util/include/util/tc_multi_hashmap.h | 4 +- util/include/util/tc_pack.h | 3 +- util/include/util/tc_rbtree.h | 2 +- util/include/util/tc_shm.h | 2 +- util/include/util/tc_socket.h | 10 +- util/include/util/tc_spin_lock.h | 2 +- util/include/util/tc_thread_pool.h | 2 +- util/include/util/tc_timeprovider.h | 4 +- util/src/CMakeLists.txt | 2 +- util/src/tc_encoder.cpp | 352 +++++++++++++------- util/src/tc_epoll_server.cpp | 77 +++-- util/src/tc_epoller.cpp | 2 +- util/src/tc_http_async.cpp | 424 +++++++++++++++---------- util/src/tc_mmap.cpp | 3 +- util/src/tc_pack.cpp | 132 +++++--- util/src/tc_sha.cpp | 12 +- util/src/tc_shm.cpp | 2 + util/src/tc_socket.cpp | 14 +- util/src/tc_thread.cpp | 1 + 45 files changed, 1096 insertions(+), 792 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f6a0a8a..c3f610d 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.8) project(tars_cpp) - +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/") set(CMAKE_VERBOSE_MAKEFILE off) set(MYSQL_DIR_INC "/usr/local/mysql/include") @@ -16,6 +16,18 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall -Wno-deprecated") #set(CMAKE_BUILD_TYPE "Debug") +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "set build type to release default") +IF (CMAKE_BUILD_TYPE STREQUAL "") + set(CMAKE_BUILD_TYPE "Release") +ENDIF() + +message("CMAKE_BUILD_TYPE TYPE = " ${CMAKE_BUILD_TYPE}) + +#编译的可执行程序输出目录 +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + set(TARS_VERSION "1.4.0") add_definitions(-DTARS_VERSION="${TARS_VERSION}") set(TARS_SSL 0) @@ -29,14 +41,83 @@ set(OPENTRACKING_INC "/usr/local/include") add_definitions(-D_USE_OPENTRACKING=${_USE_OPENTRACKING}) endif() + +IF (UNIX) +#set(LIB_MYSQL "mysqlclient") set(INSTALL_PREFIX "/usr/local/tars/cpp") +ELSE() +set(INSTALL_PREFIX "c:\\taf\\cpp") +set(LIB_MYSQL "libmysql") +ENDIF() set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX}) +IF (WIN32) +set(MYSQL_PATH "${CMAKE_SOURCE_DIR}/thirdparty/win_64") +include_directories("${MYSQL_PATH}/include/mysql") +link_directories("${MYSQL_PATH}/lib") + +ELSE() + +find_package(MySQL REQUIRED) +add_definitions(-DMySQL) +include_directories(${MYSQL_INCLUDE_DIR}) +list(APPEND LIB_MYSQL ${MYSQL_LIBRARIES}) +link_libraries(${LIB_MYSQL}) +link_libraries(pthread dl) + +ENDIF() + + +IF (APPLE) +link_libraries(iconv) +ENDIF(APPLE) + + +IF (UNIX) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11 -Wno-deprecated -fno-strict-aliasing -Wno-overloaded-virtual") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-builtin-macro-redefined -D__FILE__='\"$(notdir $(abspath $<))\"'") + + set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -Wall -g") + set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O2 -Wall -fno-strict-aliasing") + + IF(APPLE) + message("mac.....") + SET(CMAKE_C_ARCHIVE_CREATE " Scr ") + SET(CMAKE_CXX_ARCHIVE_CREATE " Scr ") + SET(CMAKE_C_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") + SET(CMAKE_CXX_ARCHIVE_FINISH " -no_warning_for_no_symbols -c ") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmismatched-tags -Wself-assign") + ENDIF(APPLE) + +ELSEIF (WIN32) + message("window.....") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4101 /wd4244 /wd4996 /wd4091 /wd4503 /wd4819 /wd4200 /wd4800") + # set(BUILD_USE_64BITS on) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj " ) + #set(CMAKE_CXX_FLAGS_RELEASE "/GL" ) + +ELSE () + MESSAGE(STATUS "================ ERROR: This platform is unsupported!!! ================") +ENDIF (UNIX) + + +IF(WIN32) +set(TARS2CPP "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/tars2cpp.exe") +ELSE() +set(TARS2CPP "${CMAKE_BINARY_DIR}/bin/tars2cpp") +ENDIF() + + +IF(WIN32) +include_directories(${CMAKE_SOURCE_DIR}/util/src/epoll_windows) +ENDIF() + add_subdirectory(util) add_subdirectory(tools) -set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp") +# set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp") add_subdirectory(servant) diff --git a/servant/tup/tup.h b/servant/tup/tup.h index a7866e8..5eb1c39 100644 --- a/servant/tup/tup.h +++ b/servant/tup/tup.h @@ -30,11 +30,11 @@ #include "tup/RequestF.h" #endif -#ifdef __GNUC__ -# if __GNUC__ >3 || __GNUC_MINOR__ > 3 -# include -# endif -#endif +// #ifdef __GNUC__ +// # if __GNUC__ >3 || __GNUC_MINOR__ > 3 +// # include +// # endif +// #endif using namespace std; using namespace tars; @@ -673,13 +673,13 @@ public: }; -#ifdef __GNUC__ -# if __GNUC__ >3 || __GNUC_MINOR__ > 3 - typedef UniAttribute UniAttrPoolAlloc; - typedef UniPacket UniPacketPoolAlloc; - typedef TarsUniPacket TarsUniPacketPoolAlloc; -# endif -#endif +// #ifdef __GNUC__ +// # if __GNUC__ >3 || __GNUC_MINOR__ > 3 +// typedef UniAttribute UniAttrPoolAlloc; +// typedef UniPacket UniPacketPoolAlloc; +// typedef TarsUniPacket TarsUniPacketPoolAlloc; +// # endif +// #endif } //////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tools/tars2android/tars2android.cpp b/tools/tars2android/tars2android.cpp index 12be527..fef7e1a 100644 --- a/tools/tars2android/tars2android.cpp +++ b/tools/tars2android/tars2android.cpp @@ -1117,7 +1117,7 @@ string Tars2Java::generateJava(const StructPtr& pPtr, const NamespacePtr& nPtr) string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId())); tars::TC_File::save2file(fileJava, s.str()); return s.str(); @@ -1288,7 +1288,7 @@ string Tars2Java::generateJava(const EnumPtr& pPtr, const NamespacePtr& nPtr) co s << TAB << "}" << endl; string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId())); tars::TC_File::save2file(fileJava, s.str()); @@ -1331,7 +1331,7 @@ void Tars2Java::generateJava(const ConstPtr& pPtr, const NamespacePtr& nPtr) con s << TAB << "}" << endl; string fileJava = getFilePath(nPtr->getId()) + "/cnst/" + pPtr->getTypeIdPtr()->getId() + ".java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId() + "/cnst/"), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId() + "/cnst/")); tars::TC_File::save2file(fileJava, s.str()); return; @@ -2011,7 +2011,7 @@ string Tars2Java::generateAndroidStub(const InterfacePtr &pPtr, const NamespaceP string fileJava = getFilePath(nPtr->getId()) + pPtr->getId() + "Agent.java"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId())); tars::TC_File::save2file(fileJava, s.str()); return s.str(); diff --git a/tools/tars2c/tars2c.cpp b/tools/tars2c/tars2c.cpp index 1a7a235..3548853 100644 --- a/tools/tars2c/tars2c.cpp +++ b/tools/tars2c/tars2c.cpp @@ -343,7 +343,7 @@ string Tars2C::generateH(const StructPtr &pPtr, const string& namespaceId) const s << TAB << "Int32 (*writeTo)( const "<& member = pPtr->getAllMemberPtr(); for (size_t j = 0; j < member.size(); j++) { @@ -374,7 +374,7 @@ string Tars2C::generateC(const StructPtr &pPtr, const string& namespaceId) const ostringstream s; string sStructName = namespaceId + "_" + pPtr->getId(); - //Ա + //�����Ա���� vector& member = pPtr->getAllMemberPtr(); //////////////////////////////////////////////////////////// @@ -463,7 +463,7 @@ string Tars2C::generateC(const StructPtr &pPtr, const string& namespaceId) const for (size_t j = 0; j < member.size(); j++) { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //stringֵҪת + //stringֵҪת�� if (bPtr && bPtr->kind() == Builtin::KindString) { s << TAB << "this->" << member[j]->getId() << " = JString_new();" << endl; @@ -537,7 +537,7 @@ string Tars2C::generateC(const StructPtr &pPtr, const string& namespaceId) const for (size_t j = 0; j < member.size(); j++) { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(member[j]->getTypePtr()); - //stringֵҪת + //stringֵҪת�� if (bPtr && bPtr->kind() == Builtin::KindString) { if (member[j]->hasDefault()) @@ -704,7 +704,7 @@ void Tars2C::generateH(const ContextPtr &pPtr) const s << endl; s << "#endif" << endl; - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); + tars::TC_File::makeDirRecursive(m_sBaseDir); tars::TC_File::save2file(fileH, s.str()); } @@ -731,7 +731,7 @@ void Tars2C::generateC(const ContextPtr &pPtr) const s << endl; - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); + tars::TC_File::makeDirRecursive(m_sBaseDir); tars::TC_File::save2file(fileC, s.str()); } @@ -754,7 +754,7 @@ StructPtr Tars2C::findStruct(const ContextPtr &pPtr,const string &id) { string sid = id; - //ڵǰnamespaceв + //�ڵ�ǰnamespace�в��� vector namespaces = pPtr->getNamespaces(); for (size_t i = 0; i < namespaces.size(); i++) { diff --git a/tools/tars2cs/tars2cs.cpp b/tools/tars2cs/tars2cs.cpp index 62e059e..306f5ce 100644 --- a/tools/tars2cs/tars2cs.cpp +++ b/tools/tars2cs/tars2cs.cpp @@ -49,7 +49,7 @@ string Tars2Cs::toTypeInit(const TypePtr &pPtr) const VectorPtr vPtr = VectorPtr::dynamicCast(pPtr); if (vPtr) { - //⴦ + //�������⴦�� string sType; size_t iPosBegin, iPosEnd; sType = tostr(vPtr->getTypePtr()); @@ -57,7 +57,7 @@ string Tars2Cs::toTypeInit(const TypePtr &pPtr) const { sType = sType.substr(0, iPosBegin) + sType.substr(iPosEnd+1); } - //[] ()Ϊ[1] + //[] (����)�������Ϊ[1] sType = tars::TC_Common::replace(sType, "[]" , "[1]"); return "(" + tostr(vPtr->getTypePtr()) + "[]) new " + sType + "[1];";; } @@ -186,7 +186,7 @@ string Tars2Cs::generateCs(const StructPtr &pPtr, const NamespacePtr &nPtr) cons s << TAB << "{" << endl; INC_TAB; - //Աset;get + //�����Ա����set;get���� for (size_t i = 0; i < member.size(); i++) { string sDefault; @@ -281,7 +281,7 @@ string Tars2Cs::generateCs(const StructPtr &pPtr, const NamespacePtr &nPtr) cons s << endl; string fileCs = getFilePath(nPtr->getId()) + pPtr->getId() + ".cs"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId())); tars::TC_File::save2file(fileCs, s.str()); return s.str(); @@ -307,7 +307,6 @@ void Tars2Cs::generateCs(const vector &es,const vector &cs,co s << TAB << "class Const " << endl; s << TAB << "{" << endl; INC_TAB; - //-----------------constͿʼ------------------------------------ for (size_t i = 0; i < cs.size(); i++) { if (cs[i]->getConstGrammarPtr()->t == ConstGrammar::STRING) @@ -330,10 +329,8 @@ void Tars2Cs::generateCs(const vector &es,const vector &cs,co DEL_TAB; s << TAB << "}" << endl; } - //-----------------constͽ-------------------------------- if (es.size()>0) { - //-----------------öͿʼ--------------------------------- for (size_t i = 0; i < es.size(); i++) { s << TAB << "public enum "<getId()< &es,const vector &cs,co s<< TAB <<"}"<getId()) + nPtr->getId()+"_const.cs"; - tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId()), 0755); + tars::TC_File::makeDirRecursive(getFilePath(nPtr->getId())); tars::TC_File::save2file(fileCs, s.str()); return; @@ -377,7 +373,7 @@ void Tars2Cs::generateCs(const NamespacePtr &pPtr) const generateCs(ss[i], pPtr); } - generateCs(es,cs,pPtr);//c#ö١constŵһ + generateCs(es,cs,pPtr); return; } diff --git a/tools/tars2node/gen_js.cpp b/tools/tars2node/gen_js.cpp index b4b0739..d6d1e27 100644 --- a/tools/tars2node/gen_js.cpp +++ b/tools/tars2node/gen_js.cpp @@ -381,7 +381,7 @@ bool CodeGenerator::generateJS(const ContextPtr &pPtr) sstr << estr.str() << endl; string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + IDL_TYPE + ".js"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, sstr.str()); return true; diff --git a/tools/tars2node/gen_js_dts.cpp b/tools/tars2node/gen_js_dts.cpp index 5a29f96..415a0e9 100644 --- a/tools/tars2node/gen_js_dts.cpp +++ b/tools/tars2node/gen_js_dts.cpp @@ -226,6 +226,6 @@ void CodeGenerator::generateDTS(const ContextPtr &pPtr) string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + IDL_TYPE + ".d.ts"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, sstr.str()); } \ No newline at end of file diff --git a/tools/tars2node/gen_proxy.cpp b/tools/tars2node/gen_proxy.cpp index 2454b86..0c2932b 100644 --- a/tools/tars2node/gen_proxy.cpp +++ b/tools/tars2node/gen_proxy.cpp @@ -376,7 +376,7 @@ bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr) string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Proxy.js"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, sstr.str()); return true; diff --git a/tools/tars2node/gen_proxy_dts.cpp b/tools/tars2node/gen_proxy_dts.cpp index 72fef55..70ca702 100644 --- a/tools/tars2node/gen_proxy_dts.cpp +++ b/tools/tars2node/gen_proxy_dts.cpp @@ -168,6 +168,6 @@ void CodeGenerator::generateDTSProxy(const ContextPtr &cPtr) sstr << estr.str() << endl; string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Proxy.d.ts"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, sstr.str()); } \ No newline at end of file diff --git a/tools/tars2node/gen_server.cpp b/tools/tars2node/gen_server.cpp index 4501df4..aab593c 100644 --- a/tools/tars2node/gen_server.cpp +++ b/tools/tars2node/gen_server.cpp @@ -374,7 +374,7 @@ bool CodeGenerator::generateJSServer(const ContextPtr &pPtr) string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".js"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, str.str()); return true; diff --git a/tools/tars2node/gen_server_dts.cpp b/tools/tars2node/gen_server_dts.cpp index 49803cb..1d6479e 100644 --- a/tools/tars2node/gen_server_dts.cpp +++ b/tools/tars2node/gen_server_dts.cpp @@ -144,6 +144,6 @@ void CodeGenerator::generateDTSServer(const ContextPtr &pPtr) string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".d.ts"; - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, str.str()); } \ No newline at end of file diff --git a/tools/tars2node/gen_server_imp.cpp b/tools/tars2node/gen_server_imp.cpp index a73b61b..44f9168 100644 --- a/tools/tars2node/gen_server_imp.cpp +++ b/tools/tars2node/gen_server_imp.cpp @@ -118,6 +118,6 @@ void CodeGenerator::generateJSServerImp(const ContextPtr &cPtr) str << generateJSServerImp(cPtr, namespaces[i]); } - TC_File::makeDirRecursive(_sToPath, 0755); + TC_File::makeDirRecursive(_sToPath); makeUTF8File(sFileName, str.str()); } \ No newline at end of file diff --git a/tools/tars2oc/tars2oc.cpp b/tools/tars2oc/tars2oc.cpp index c7dc771..bf57566 100644 --- a/tools/tars2oc/tars2oc.cpp +++ b/tools/tars2oc/tars2oc.cpp @@ -99,7 +99,7 @@ string Tars2OC::toAddtionalClassName(const TypePtr &pPtr) const if (vPtr) { string s = ""; - //vectorӸϢ + //vector�������Ӹ�����Ϣ���� if (toClassName(vPtr) == "list") { s = "ONSData"; @@ -121,7 +121,7 @@ string Tars2OC::toAddtionalClassName(const TypePtr &pPtr) const ostringstream osKeyLen; { int iLen = sKey.length(); - //涨ܳ99 + //�涨���ܳ���99 assert(iLen <= 99 && iLen >=0); char sLen[3]; snprintf(&sLen[0],3,"%.2d",iLen); @@ -133,14 +133,14 @@ string Tars2OC::toAddtionalClassName(const TypePtr &pPtr) const StructPtr sPtr = StructPtr::dynamicCast(pPtr); if (sPtr) { - //structObject + //struct����Object���� return "O" + tostrStruct(sPtr);//TC_Common::replace(sPtr->getSid(), "::", ""); } EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); if (ePtr) { - //EnumObject + //Enum����Object���� return "ONSNumber"; } @@ -302,14 +302,14 @@ string Tars2OC::toTarsV2Procstr(const TypeIdPtr &pPtr) const if (bPtr) { - //ͨʹJV2_PROP_NM + //��ͨ����ʹ��JV2_PROP_NM s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() <<","<< pPtr->getId() << ")"; } VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); if (vPtr) { - //vectorӸϢ + //vector�������Ӹ�����Ϣ���� if (toClassName(vPtr) == "list") { s << "JV2_PROP_NM(" << getReqOptionType(pPtr) << "," << pPtr->getTag() << "," << pPtr->getId() << ")"; @@ -369,7 +369,7 @@ string Tars2OC::tostrEnum(const EnumPtr &pPtr) const void Tars2OC::toIncludeName(const TypePtr &pPtr,map& mReference) const { - //ûṹ + //��������û�����������ṹ BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); if (bPtr) { @@ -420,7 +420,7 @@ map Tars2OC::getReferences(const StructPtr &pPtr, const string& name map mTemp; vector& vMember = pPtr->getAllMemberPtr(); - //鿴ԱǷĽṹ + //�鿴��Ա�����Ƿ����������Ľṹ�� for (size_t j = 0; j < vMember.size(); j++) { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vMember[j]->getTypePtr()); @@ -432,7 +432,7 @@ map Tars2OC::getReferences(const StructPtr &pPtr, const string& name VectorPtr vPtr = VectorPtr::dynamicCast(vMember[j]->getTypePtr()); if (vPtr) { - //vectorӸϢ + //vector�������Ӹ�����Ϣ���� if (toClassName(vPtr) == "list") { continue; @@ -467,7 +467,7 @@ map Tars2OC::getReferences(const StructPtr &pPtr, const string& name } bool Tars2OC::IsRetainType(const TypePtr &pPtr) const { - //retain,enum is NSInteger + //����������retain,enum is NSInteger BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); EnumPtr ePtr = EnumPtr::dynamicCast(pPtr); if((bPtr && bPtr->kind() != Builtin::KindString) || ePtr) @@ -539,7 +539,7 @@ string Tars2OC::generateH(const StructPtr &pPtr, const string& namespaceId) cons s << "@interface " << sStructName << " : TarsObjectV2"<< endl; s << endl; - //Ա + //�����Ա���� vector& vMember = pPtr->getAllMemberPtr(); for (size_t j = 0; j < vMember.size(); j++) { @@ -552,7 +552,7 @@ string Tars2OC::generateH(const StructPtr &pPtr, const string& namespaceId) cons s << endl; s << "@end" << endl; - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); + tars::TC_File::makeDirRecursive(m_sBaseDir); tars::TC_File::save2file(fileH, s.str()); return ""; @@ -570,7 +570,7 @@ string Tars2OC::writeInit(const vector& vMember) const INC_TAB; for (size_t j = 0; j < vMember.size(); j++) { - //ͳʼ,Ĭֵ˵stringͲųʼ + //�������ͳ�ʼ��,��Ĭ��ֵ����˵string���Ͳų�ʼ�� BuiltinPtr bPtr = BuiltinPtr::dynamicCast(vMember[j]->getTypePtr()); if(bPtr) { @@ -595,10 +595,10 @@ string Tars2OC::writeInit(const vector& vMember) const { s <getId()) << " = " << vMember[j]->def() << ";" << endl; } - //ĻͲóʼϵͳṩĬֵ + //��������Ļ������Ͳ��ó�ʼ����ϵͳ�ṩĬ��ֵ } - //Ƿrequireֶ,retain + //�Ƿ�require�ֶ�,����retain���� if(vMember[j]->isRequire() && IsRetainType(vMember[j]->getTypePtr())) { VectorPtr vPtr = VectorPtr::dynamicCast(vMember[j]->getTypePtr()); @@ -644,7 +644,7 @@ string Tars2OC::writedealloc(const vector& vMember) const INC_TAB; for (size_t j = 0; j < vMember.size(); j++) { - //ͲҪ=nil,NSStringǶ + //�������Ͳ���Ҫ=nil,NSString�Ƕ������� if(IsRetainType(vMember[j]->getTypePtr())) { s <getId()) << " = nil;" <printHeaderRemark(); - //Ա + //�����Ա���� vector& member = pPtr->getAllMemberPtr(); //////////////////////////////////////////////////////////// @@ -832,7 +832,7 @@ string Tars2OC::generateM(const StructPtr &pPtr, const string& namespaceId) cons s << "@end" << endl; - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); + tars::TC_File::makeDirRecursive(m_sBaseDir); tars::TC_File::save2file(fileM, s.str()); return ""; } @@ -899,7 +899,7 @@ string Tars2OC::generateH(const EnumPtr &pPtr, const string& namespaceId) const s << "@end" <&ss = pPtr->getAllStructPtr(); vector&es = pPtr->getAllEnumPtr(); - //ȱö,ļ + //�ȱ���ö������,���������ļ� for (size_t i = 0; i < es.size(); i++) { generateH(es[i], pPtr->getId()); generateM(es[i], pPtr->getId()); } - //ṹ,ļ + //�����ṹ������,���������ļ� for (size_t i = 0; i < ss.size(); i++) { generateH(ss[i], pPtr->getId()); diff --git a/tools/tars2php/tars2php.cpp b/tools/tars2php/tars2php.cpp index 01a7809..6411662 100644 --- a/tools/tars2php/tars2php.cpp +++ b/tools/tars2php/tars2php.cpp @@ -45,7 +45,7 @@ string Tars2Php::readFrom(const TypeIdPtr &pPtr, bool bIsRequire) const } -/*******************************ȡ********************************/ +/*******************************��ȡ������������********************************/ int Tars2Php::getSuffix(const TypeIdPtr &pPtr) const { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); @@ -63,7 +63,7 @@ int Tars2Php::getSuffix(const TypeIdPtr &pPtr) const return -1; } -/***************************************************************/ +/*******************************������������********************************/ string Tars2Php::toStrSuffix(const TypeIdPtr &pPtr) const { @@ -180,7 +180,7 @@ string Tars2Php::generatePHP(const StructPtr &pPtr, const string& namespaceId) c s << TAB << "class " << pPtr->getId() << " extends c_struct" << endl; s << TAB << "{" << endl; INC_TAB; - // + //�������� for(size_t k = 0;k < member.size();k++) { s<< TAB << "public $" <getId()<<";"<"<"; - tars::TC_File::makeDirRecursive(m_sBaseDir, 0755); + tars::TC_File::makeDirRecursive(m_sBaseDir); tars::TC_File::save2file(fileH, s.str()); } @@ -336,7 +336,7 @@ void Tars2Php::createFile(const string &file, const vector &vsCoder) { if(file == contexts[i]->getFileName()) { - //tup汾 + //tup�汾�� generatePHP(contexts[i]); } } diff --git a/tools/tars2python/tars2python.cpp b/tools/tars2python/tars2python.cpp index 3b49908..0c60129 100644 --- a/tools/tars2python/tars2python.cpp +++ b/tools/tars2python/tars2python.cpp @@ -34,7 +34,7 @@ string Tars2Python::generatePython(const EnumPtr &pPtr, const std::string &sName s << TAB << "class " << pPtr->getId() << ":" << endl; INC_TAB; - //Ա + //��Ա���� int nenum = -1; vector& member = pPtr->getAllMemberPtr(); for (size_t i = 0; i < member.size(); i++) @@ -233,7 +233,7 @@ string Tars2Python::generatePython(const StructPtr & pPtr, const NamespacePtr & s << TAB << "class " << pPtr->getId() << "(tarscore.struct):"<< endl; INC_TAB; - //STEP01 ʼҪ + //STEP01 ��ʼ��������Ҫ������ s << TAB << "__tars_class__ = \"" << nPtr->getId() << "." << pPtr->getId() << "\";" << endl; for (size_t i = 0, len = member.size(); i < len; i++) { @@ -255,7 +255,7 @@ string Tars2Python::generatePython(const StructPtr & pPtr, const NamespacePtr & } } - //STEP02 ݳԱ + //STEP02 �������ݳ�Ա s << TAB << endl; s << TAB << "def __init__(self):" << endl; INC_TAB; @@ -265,7 +265,7 @@ string Tars2Python::generatePython(const StructPtr & pPtr, const NamespacePtr & } DEL_TAB; - //STEP03 ʼwriteTo + //STEP03 ��ʼ����writeTo���� s << TAB << endl; s << TAB << "@staticmethod" << endl; s << TAB << "def writeTo(oos, value):" << endl; @@ -277,7 +277,7 @@ string Tars2Python::generatePython(const StructPtr & pPtr, const NamespacePtr & } DEL_TAB; - //STEP04 ʼreadFrom + //STEP04 ��ʼ����readFrom���� s << TAB << endl; s << TAB << "@staticmethod" << endl; s << TAB << "def readFrom(ios):" << endl; @@ -424,7 +424,7 @@ string Tars2Python::generatePython(const InterfacePtr &pPtr) vector& vOperation = pPtr->getAllOperationPtr(); for(size_t i = 0; i < vOperation.size(); i++) { - //ÿӿڵı + //����ÿ���ӿڵı���� str << TAB << "def " << vOperation[i]->getId() << "(self, " << makeParams(vOperation[i]) << "):" << endl; INC_TAB; str << makeOperations(vOperation[i]) << endl; @@ -441,7 +441,7 @@ string Tars2Python::generatePython(const NamespacePtr &pPtr) std::ostringstream str; /* - //STEP01 ʼɳ + //STEP01 ��ʼ���ɳ��� vector &cs = pPtr->getAllConstPtr(); for (size_t i = 0; i < cs.size(); i++) { @@ -450,21 +450,21 @@ string Tars2Python::generatePython(const NamespacePtr &pPtr) str << endl; */ - //STEP02 ʼöֵ + //STEP02 ��ʼ����ö��ֵ vector & es = pPtr->getAllEnumPtr(); for (size_t i = 0; i < es.size(); i++) { str << generatePython(es[i], pPtr->getId()) << endl; } - //STEP03 ʼɽṹ뺯 + //STEP03 ��ʼ���ɽṹ�����뺯�� vector & ss = pPtr->getAllStructPtr(); for (size_t i = 0; i < ss.size(); i++) { str << generatePython(ss[i], pPtr) << endl; } - //STEP04 ʼProxy + //STEP04 ��ʼ����Proxy���� vector & is = pPtr->getAllInterfacePtr(); for(size_t i = 0; i < is.size(); i++) { @@ -500,7 +500,7 @@ void Tars2Python::generatePython(const ContextPtr &pPtr) string sPath = getFilePath() + "/" + namespaces[i]->getId() + "/"; string sFileName = sPath + tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName())) + ".py"; - tars::TC_File::makeDirRecursive(sPath, 0755); + tars::TC_File::makeDirRecursive(sPath); tars::TC_File::save2file(sFileName, str.str()); } diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index ecad534..16450e2 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -4,7 +4,13 @@ project(util) include_directories(${MYSQL_DIR_INC}) link_directories(${MYSQL_DIR_LIB}) -enable_language(ASM) +if(CMAKE_ASM_COMPILER) + enable_language(ASM) +else() + message(STATUS "No ASM support") +endif() + +# enable_language(ASM) add_subdirectory(src) add_subdirectory(include) diff --git a/util/include/util/tc_dyn_object.h b/util/include/util/tc_dyn_object.h index cf28874..c074754 100644 --- a/util/include/util/tc_dyn_object.h +++ b/util/include/util/tc_dyn_object.h @@ -33,7 +33,7 @@ namespace tars * @brief 动态生成类 */ class TC_DYN_Object; -class TC_DYN_RuntimeClass; +struct TC_DYN_RuntimeClass; struct TC_DYN_RuntimeClass { diff --git a/util/include/util/tc_encoder.h b/util/include/util/tc_encoder.h index a365790..d32d5e4 100644 --- a/util/include/util/tc_encoder.h +++ b/util/include/util/tc_encoder.h @@ -54,21 +54,10 @@ struct TC_Encoder_Exception : public TC_Exception class TC_Encoder { public: - /** - * @brief gbk 转换到 utf8. - * - * @param sOut 输出buffer - * @param iMaxOutLen 输出buffer最大的长度/sOut的长度 - * @param sIn 输入buffer - * @param iInLen 输入buffer长度 - * @throws TC_Encoder_Exception - * @return - */ - static void gbk2utf8(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen); /** - * @brief gbk 转换到 utf8. - * + * @brief gbk 转换到 utf8. + * * @param sIn 输入buffer* * @throws TC_Encoder_Exception * @return 转换后的utf8编码 @@ -76,8 +65,8 @@ public: static string gbk2utf8(const string &sIn); /** - * @brief gbk 转换到 utf8. - * + * @brief gbk 转换到 utf8. + * * @param sIn 输入buffer * @param vtStr 输出gbk的vector * @throws TC_Encoder_Exception @@ -85,9 +74,49 @@ public: */ static void gbk2utf8(const string &sIn, vector &vtStr); + /** - * @brief utf8 转换到 gbk. - * + * @brief utf8 转换到 gbk. + * + * @param sIn 输入buffer + * @throws TC_Encoder_Exception + * @return 转换后的gbk编码 + */ + static string utf82gbk(const string &sIn); + + /** + * @brief 将string的\n替换掉,转义字符串中的某个字符 + * + * 缺省:\n 转换为 \r\0; \r转换为\, + * + * 主要用于将string记录在一行,通常用于写bin-log的地方; + * @param str 待转换字符串 + * @param f 需要转义的字符 + * @param t 转义后的字符 + * @param u 借用的转义符 + * @return str 转换后的字符串 + */ + static string transTo(const string& str, char f = '\n', char t = '\r', char u = '\0'); + + /** + * @brief 从替换的数据恢复源数据,将 transTo 的字符串还原, + * + * 缺省:\r\0 还原为\n,\r\r还原为, + * + * 主要用于将string记录在一行,通常用于写bin-log的地方 + * @param str 待还原的字符串(必须是transTo后得到的字符串) + * @param f 被转义的字符 + * @param t 转义后的字符 + * @param u 借用的转义符 + * @return str 还原后的字符串 + */ + static string transFrom(const string& str, char f = '\n', char t = '\r', char u = '\0'); + +protected: + + /** + * @brief utf8 转换到 gbk. + * * @param sOut 输出buffer * @param iMaxOutLen 输出buffer最大的长度/sOut的长度 * @param sIn 输入buffer @@ -96,43 +125,6 @@ public: * @return */ static void utf82gbk(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen); - - /** - * @brief utf8 转换到 gbk. - * - * @param sIn 输入buffer - * @throws TC_Encoder_Exception - * @return 转换后的gbk编码 - */ - static string utf82gbk(const string &sIn); - - /** - * @brief 将string的\n替换掉,转义字符串中的某个字符 - * - * 缺省:\n 转换为 \r\0; \r转换为\, - * - * 主要用于将string记录在一行,通常用于写bin-log的地方; - * @param str 待转换字符串 - * @param f 需要转义的字符 - * @param t 转义后的字符 - * @param u 借用的转义符 - * @return str 转换后的字符串 - */ - static string transTo(const string& str, char f = '\n', char t = '\r', char u = '\0'); - - /** - * @brief 从替换的数据恢复源数据,将 transTo 的字符串还原, - * - * 缺省:\r\0 还原为\n,\r\r还原为, - * - * 主要用于将string记录在一行,通常用于写bin-log的地方 - * @param str 待还原的字符串(必须是transTo后得到的字符串) - * @param f 被转义的字符 - * @param t 转义后的字符 - * @param u 借用的转义符 - * @return str 还原后的字符串 - */ - static string transFrom(const string& str, char f = '\n', char t = '\r', char u = '\0'); }; } diff --git a/util/include/util/tc_epoll_server.h b/util/include/util/tc_epoll_server.h index 134845b..60ae218 100644 --- a/util/include/util/tc_epoll_server.h +++ b/util/include/util/tc_epoll_server.h @@ -35,8 +35,8 @@ // #include "util/tc_mmap.h" // #include "util/tc_fifo.h" #include "util/tc_network_buffer.h" -#include "util/tc_buffer.h" -#include "util/tc_buffer_pool.h" +// #include "util/tc_buffer.h" +// #include "util/tc_buffer_pool.h" #include "util/tc_cas_queue.h" using namespace std; @@ -123,8 +123,8 @@ public: class Handle; typedef TC_AutoPtr HandlePtr; - class HandleGroup; - typedef TC_AutoPtr HandleGroupPtr; + // class HandleGroup; + // typedef TC_AutoPtr HandleGroupPtr; // //////////////////////////////////////////////////////////////////////////// @@ -249,13 +249,13 @@ public: * 每组handle处理一个或多个Adapter消息 * 每个handle对象一个线程 */ - struct HandleGroup : public TC_HandleBase - { - string name; - TC_ThreadLock monitor; - vector handles; - map adapters; - }; + // struct HandleGroup : public TC_HandleBase + // { + // string name; + // TC_ThreadLock monitor; + // vector handles; + // map adapters; + // }; //////////////////////////////////////////////////////////////////////////// /** * @brief 定义服务逻辑处理的接口 @@ -360,6 +360,11 @@ public: */ virtual void notifyFilter(); + /** + * 心跳(每处理完一个请求或者等待请求超时都会调用一次) + */ + virtual void heartbeat() {} + protected: /** * 具体的处理逻辑 @@ -439,11 +444,6 @@ public: */ virtual void handleCustomMessage(bool bExpectIdle = false) {} - /** - * 心跳(每处理完一个请求或者等待请求超时都会调用一次) - */ - virtual void heartbeat() {} - /** * 线程已经启动, 进入具体处理前调用 */ @@ -467,11 +467,11 @@ public: virtual bool allFilterIsEmpty(); - /** - * 设置服务 - * @param pEpollServer - */ - void setEpollServer(TC_EpollServer *pEpollServer); + // /** + // * 设置服务 + // * @param pEpollServer + // */ + // void setEpollServer(TC_EpollServer *pEpollServer); /** * 设置Adapter @@ -535,7 +535,7 @@ public: }; using close_functor = std::function; - using auth_process_wrapper_functor = std::function; + using auth_process_wrapper_functor = std::function& )>; //////////////////////////////////////////////////////////////////////////// // 服务端口管理,监听socket信息 @@ -1350,7 +1350,7 @@ public: */ EnumConnectionType getType() const { return _enType; } - bool IsEmptyConn() const {return _bEmptyConn;} + bool isEmptyConn() const {return _bEmptyConn;} /** * Init Auth State; @@ -1798,13 +1798,13 @@ public: // */ // void send(unsigned int uid, const string &s, const string &ip, uint16_t port); - /** - * 获取某一监听端口的连接数 - * @param lfd - * - * @return vector - */ - vector getConnStatus(int lfd); + // /** + // * 获取某一监听端口的连接数 + // * @param lfd + // * + // * @return vector + // */ + // vector getConnStatus(int lfd); /** * 获取连接数 @@ -1954,7 +1954,7 @@ public: /** *是否空连接检测 */ - bool IsEmptyConnCheck() const; + bool isEmptyConnCheck() const; friend class BindAdapter; friend class ConnectionList; @@ -2226,6 +2226,12 @@ public: */ BindAdapterPtr getBindAdapter(const string &sName); + /** + * 获取所有adatapters + * @return + */ + vector getBindAdapters(); + /** * 向网络线程添加连接 */ @@ -2297,7 +2303,7 @@ public: * INFO日志 * @param s */ - void tars(const string &s); + void info(const string &s); /** * 记录错误日志 @@ -2323,7 +2329,7 @@ public: /** * 获取所有业务线程的数目 */ - unsigned int getLogicThreadNum(); + size_t getLogicThreadNum(); //回调给应用服务 typedef std::function application_callback_functor; diff --git a/util/include/util/tc_ex.h b/util/include/util/tc_ex.h index 684cb20..e494eba 100644 --- a/util/include/util/tc_ex.h +++ b/util/include/util/tc_ex.h @@ -31,9 +31,6 @@ namespace tars */ ///////////////////////////////////////////////// -/** -* @brief 异常类. -*/ class TC_Exception : public exception { public: diff --git a/util/include/util/tc_hashmap.h b/util/include/util/tc_hashmap.h index 5f99170..c241d79 100644 --- a/util/include/util/tc_hashmap.h +++ b/util/include/util/tc_hashmap.h @@ -1668,9 +1668,9 @@ protected: friend class Block; friend class BlockAllocator; - friend class HashMapIterator; + friend struct HashMapIterator; friend class HashMapItem; - friend class HashMapLockIterator; + friend struct HashMapLockIterator; friend class HashMapLockItem; //禁止copy构造 diff --git a/util/include/util/tc_hashmap_compact.h b/util/include/util/tc_hashmap_compact.h index 690db72..ab9d49c 100644 --- a/util/include/util/tc_hashmap_compact.h +++ b/util/include/util/tc_hashmap_compact.h @@ -1757,9 +1757,9 @@ protected: friend class Block; friend class BlockAllocator; - friend class HashMapIterator; + friend struct HashMapIterator; friend class HashMapItem; - friend class HashMapLockIterator; + friend struct HashMapLockIterator; friend class HashMapLockItem; /** diff --git a/util/include/util/tc_http_async.h b/util/include/util/tc_http_async.h index ebd9a53..2a1d6fb 100644 --- a/util/include/util/tc_http_async.h +++ b/util/include/util/tc_http_async.h @@ -18,6 +18,7 @@ #define __TC_HTTP_ASYNC_H_ #include +#include "util/tc_platform.h" #include "util/tc_thread_pool.h" #include "util/tc_http.h" #include "util/tc_autoptr.h" @@ -27,188 +28,87 @@ namespace tars { ///////////////////////////////////////////////// -/** -* @file tc_http_async.h -* @brief http异步调用类. -* -* http同步调用使用TC_HttpRequest::doRequest就可以了 -*/ +/** +* @file tc_http_async.h +* @brief http异步调用类. +* +* http同步调用使用TC_HttpRequest::doRequest就可以了 +* 代码示例请参考example_http_async.cpp +* 说明: +* 1 背后会启动唯一的网络线程 +* 2 目前只支持http短连接 +* 3 RequestCallback回调里面, onSucc和onFailed是对应的, 每次异步请求, onSucc/onFailed其中之一会被唯一响应 +* @author jarodruan@tencent.com +*/ ///////////////////////////////////////////////// -/** -* @brief 线程异常 -*/ -struct TC_HttpAsync_Exception : public TC_Exception -{ - TC_HttpAsync_Exception(const string &buffer) : TC_Exception(buffer){}; - TC_HttpAsync_Exception(const string &buffer, int err) : TC_Exception(buffer, err){}; - ~TC_HttpAsync_Exception() throw(){}; -}; +///** +//* @brief 线程异常 +//*/ +//struct TC_HttpAsync_Exception : public TC_Exception +//{ +// TC_HttpAsync_Exception(const string &buffer) : TC_Exception(buffer) {}; +// TC_HttpAsync_Exception(const string &buffer, int err) : TC_Exception(buffer, err) {}; +// ~TC_HttpAsync_Exception() throw() {}; +//}; /** - * @brief 异步线程处理类. - * - * 异步HTTP请求 ,使用方式示例如下: - * - * 实现异步回调对象 - * - * 异步对象回调执行的时候是在TC_HttpAsync中线程执行的 - * - * 用智能指针new出来, 不用管生命周期 - * - * class AsyncHttpCallback : public - * - * TC_HttpAsync::RequestCallback - * - * { - * - * public: - * - * AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl) - * - * { - * - * } - * - * virtual void onException(const string &ex) - * - * { - * - * cout << "onException:" << _sUrl << ":" << ex << endl; - * - * } - * - * //请求回来的时候onResponse被调用 - * - * //bClose表示服务端关闭了连接 ,从而认为收到了一个完整的http响应 - * - * virtual void onResponse(bool bClose, TC_HttpResponse - * - * &stHttpResponse) - * - * { - * - * cout << "onResponse:" << _sUrl << ":" << - * - * TC_Common::tostr(stHttpResponse.getHeaders()) << - * - * endl; - * } - * - * virtual void onTimeout() - * - * { - * - * cout << "onTimeout:" << _sUrl << endl; - * - * } - * - * - * //连接被关闭时调用 - * - * virtual void onClose() - * - * { - * - * cout << "onClose:" << _sUrl << endl; - * - * } - * - * protected: - * - * string _sUrl; - * - * }; - * - * //封装一个函数, 根据实际情况处理 - * - * int addAsyncRequest(TC_HttpAsync &ast, const string &sUrl) { - * - * TC_HttpRequest stHttpReq; stHttpReq.setGetRequest(sUrl); - * - * - * //new出来一个异步回调对象 - * - * TC_HttpAsync::RequestCallbackPtr p = new - * - * AsyncHttpCallback(sUrl); - * - * return ast.doAsyncRequest(stHttpReq, p); - * - * } - * - * //具体使用的示例代码如下: - * - * TC_HttpAsync ast; - * - * ast.setTimeout(1000); //设置异步请求超时时间 - * - * ast.start(); - * - * //真正的代码需要判断返回值,返回值=0才表示请求已经发送出去了 - * - * int ret = addAsyncRequest(ast, "www.baidu.com"); - * - * addAsyncRequest(ast, "www.qq.com"); - * - * addAsyncRequest(ast, "www.google.com"); - * - * addAsyncRequest(ast, "http://news.qq.com/a/20100108/002269.htm"); - * - * addAsyncRequest(ast, "http://news.qq.com/zt/2010/mtjunshou/"); - * - * addAsyncRequest(ast,"http://news.qq.com/a/20100108/000884.htm"); - * - * addAsyncRequest(ast,"http://news.qq.com/a/20100108/000884.htm"); - * - * addAsyncRequest(ast,"http://tech.qq.com/zt/2009/renovate/index.htm"); - * - * ast.waitForAllDone(); - * - * ast.terminate(); + * @brief 异步线程处理类. */ class TC_HttpAsync : public TC_Thread, public TC_ThreadLock { public: /** * @brief 异步请求回调对象 + * onSucc, 收到成功回包时响应 + * onFailed, 失败时响应 + * onSucc和onFailed是成对出现的, 且一次请求, 只会一个被响应, 且只响应一次 + * onFailed被调用时, 链接就会被关闭掉 */ class RequestCallback : public TC_HandleBase { public: /** - * @brief 完整的响应回来了. - * - * @param bClose 因为远程服务器关闭连接认为http完整了 + * 错误码 + */ + enum FAILED_CODE + { + Failed_Net = 0x01, //网络出错 + Failed_Connect = 0x02, //连接服务器出错 + Failed_Timeout = 0x03, //超时 + Failed_Interrupt = 0x04, //中断接收数据 + Failed_Close = 0x05, //服务器主动关闭了链接 + Failed_ConnectTimeout = 0x06, //链接超时 + }; + + /** + * @brief 每次收到数据且http头收全了都会调用, + * stHttpResponse的数据可能不是完全的http响应数据 ,只有部分body数据 + * @param stHttpResponse 收到的http数据 + * @return true:继续收取数据, false:不收取数据了(会触发onFailed, Failed_Interrupt) + */ + virtual bool onContinue(TC_HttpResponse &stHttpResponse) { return true; } + + /** + * @brief 完整的响应回来了. + * * @param stHttpResponse http响应包 */ - virtual void onResponse(bool bClose, TC_HttpResponse &stHttpResponse) = 0; + virtual void onSucc(TC_HttpResponse &stHttpResponse) = 0; /** - * @brief 每次收到数据且http头收全了都会调用, - * stHttpResponse的数据可能不是完全的http响应数据 ,只有部分body数据 - * @param stHttpResponse 收到的http数据 - * @return true:继续收取数据, false:不收取数据了 + * @brief 异常, 发生异常时, onClose也会被调用, 连接会被关闭掉 + * + * @param ret, 错误码 + * @param info 异常原因 */ - virtual bool onReceive(TC_HttpResponse &stHttpResponse) { return true;}; - - /** - * @brief 异常. - * - * @param ex 异常原因 - */ - virtual void onException(const string &ex) = 0; - - /** - * @brief 超时没有响应 - */ - virtual void onTimeout() = 0; + virtual void onFailed(FAILED_CODE ret, const string &info) = 0; /** * @brief 连接被关闭 */ - virtual void onClose() = 0; + virtual void onClose() {}; }; typedef TC_AutoPtr RequestCallbackPtr; @@ -221,12 +121,12 @@ protected: { public: /** - * @brief 构造. - * + * @brief 构造. + * * @param stHttpRequest * @param callbackPtr */ - AsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr); + AsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy); /** * @brief 析构 @@ -235,30 +135,32 @@ protected: /** * @brief 获取句柄 - * + * * @return int */ - int getfd() { return _fd.getfd(); } + int getfd() const { return _fd.getfd(); } + + /** + * 获取通知fd + */ + // int getNotifyfd() const { return _notify.getfd(); } /** * @brief 发起建立连接. - * - * @return int + * */ - int doConnect(); + void doConnect(); /** - * @brief 发起建立到addr的连接,不用DNS解析. - * - * @param addr 可用于直接连接透明代理或者连接通过DNS解析后的地址 - * @return int + * @brief 获取系统错误提示 + * @return */ - int doConnect(struct sockaddr* addr); + string getError(const char* sDefault) const; /** - * @brief 发生异常 - */ - void doException(); + * @brief 发生异常 + */ + void doException(RequestCallback::FAILED_CODE ret, const string &e); /** * @brief 发送请求 @@ -281,50 +183,78 @@ protected: void timeout(); /** - * @brief 设置唯一ID. - * + * @brief 设置唯一ID. + * * @param uniqId */ void setUniqId(uint32_t uniqId) { _iUniqId = uniqId;} /** * @brief 获取唯一ID. - * + * * @return uint32_t */ uint32_t getUniqId() const { return _iUniqId; } - + /** * @brief 设置处理请求的http异步线程. - * + * * @param pHttpAsync :异步线程处理对象 */ void setHttpAsync(TC_HttpAsync *pHttpAsync) { _pHttpAsync = pHttpAsync; } /** - * @brief 设置发网络请求时绑定的ip地址. - * + * @brief 设置发网络请求时绑定的ip地址. + * * @param addr */ void setBindAddr(const struct sockaddr* addr); + /** + * @brief 链接是否有效 + */ + bool isValid() const { return _fd.isValid(); } + + /** + * @brief 是否链接上 + * @return [description] + */ + bool hasConnected() const { return _isConnected; } + + /** + * @brief 设置链接状态 + * @param isConnected [description] + */ + void setConnected(bool isConnected) { _isConnected = isConnected; } + + /** + * 处理网络事件 + * @param events [description] + */ + void processNet(const epoll_event &ev); + + /** + * 处理通知事件 + */ + void processNotify(); + protected: /** - * @brief 接收请求. - * + * @brief 接收请求. + * * @param buf * @param len - * @param flag + * @param flag * @return int */ int recv(void* buf, uint32_t len, uint32_t flag); /** - * @brief 发送请求. - * + * @brief 发送请求. + * * @param buf 发送内容 * @param len 发送长度 - * @param flag + * @param flag * @return int */ int send(const void* buf, uint32_t len, uint32_t flag); @@ -341,6 +271,8 @@ protected: RequestCallbackPtr _callbackPtr; bool _bindAddrSet; struct sockaddr _bindAddr; + bool _bUseProxy; + bool _isConnected; }; typedef TC_AutoPtr AsyncRequestPtr; @@ -360,63 +292,66 @@ public: ~TC_HttpAsync(); /** - * @brief 异步发起请求. - * + * @brief 异步发起请求. + * * @param stHttpRequest * @param httpCallbackPtr * @param bUseProxy,是否使用代理方式连接 - * @param addr, bUseProxy为false 直接连接指定的地址 - * @return int, <0:发起连接失败, 可以通过strerror(返回值) - * =0:成功 */ - int doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy=false, struct sockaddr* addr=NULL); + void doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy = false); /** * @brief 设置proxy地址 - * + * */ - int setProxyAddr(const char* Host,uint16_t Port); + int setProxyAddr(const char* Host, uint16_t Port); /** - * @brief 设置代理的地址. - * - * 不通过域名解析发送,直接发送到代理服务器的ip地址) + * @brief 设置代理的地址. + * + * 不通过域名解析发送,直接发送到代理服务器的ip地址) * @param sProxyAddr 格式 192.168.1.2:2345 或者 sslproxy.qq.com:2345 */ int setProxyAddr(const char* sProxyAddr); /** - * @brief 设置绑定的地址. - * + * @brief 设置绑定的地址. + * * @param sProxyAddr 格式 192.168.1.2 */ int setBindAddr(const char* sBindAddr); /** - * @brief 设置绑定的地址. - * + * @brief 设置绑定的地址. + * * @param addr 直接用 addr 赋值 */ void setProxyAddr(const struct sockaddr* addr); /** - * @brief 启动异步处理. - * + * @brief 获取代理地址, 设置代理地址后才有效 + * @return [description] + */ + const struct sockaddr* getProxyAddr() const { return &_proxyAddr; } + + /** + * @brief 启动异步处理. + * * 参数已经无效(网络层有且只有一个线程) * @param num, 异步处理的线程数 */ - void start(int iThreadNum = 1); + void start(); /** - * @brief 设置超时(所有请求都只能用一种超时时间). - * + * @brief 设置超时(所有请求都只能用一种超时时间). + * * @param timeout: 毫秒, 但是具体的超时精度只能在s左右 */ void setTimeout(int millsecond) { _data->setTimeout(millsecond); } /** - * @brief 等待请求全部结束(等待毫秒精度在100ms左右). - * + * @brief 等待请求全部结束(等待毫秒精度在100ms左右). + * * @param millsecond, 毫秒 -1表示永远等待 */ void waitForAllDone(int millsecond = -1); @@ -428,11 +363,12 @@ public: protected: - using async_process_type = std::function; + // typedef TC_Functor::Result> async_process_type; + typedef std::function async_process_type; /** - * @brief 超时处理. - * + * @brief 超时处理. + * * @param ptr */ static void timeout(AsyncRequestPtr& ptr); @@ -440,7 +376,13 @@ protected: /** * @brief 具体网络处理 */ - static void process(AsyncRequestPtr &p, int events); + // static void process(AsyncRequestPtr &p, int events); + + /** + * @brief 确保线程 + * @param _threadId [description] + */ + void assertThreadId() { assert(_threadId == std::this_thread::get_id()); } /** * @brief 具体的网络处理逻辑 @@ -450,17 +392,40 @@ protected: /** * @brief 删除异步请求对象 */ - void erase(uint32_t uniqId); + void erase(uint32_t uniqId); + + /** + * @brief 监控链接 + * @param fd [description] + * @param uniqId [description] + * @param events [description] + */ + void addConnection(int fd, uint32_t uniqId, uint32_t events); + + /** + * @brief 删除链接 + * @param fd [description] + * @param events [description] + */ + void delConnection(int fd); friend class AsyncRequest; protected: + std::thread::id _threadId; + TC_ThreadPool _tpool; http_queue_type *_data; - + TC_Epoller _epoller; + TC_Epoller::NotifyInfo _notify; + + std::mutex _mutex; + + deque _events; + bool _terminate; struct sockaddr _proxyAddr; @@ -469,7 +434,6 @@ protected: bool _bindAddrSet; }; - } #endif diff --git a/util/include/util/tc_logger.h b/util/include/util/tc_logger.h index 6545667..ebff605 100644 --- a/util/include/util/tc_logger.h +++ b/util/include/util/tc_logger.h @@ -573,7 +573,7 @@ namespace tars //写错误,警告,调试log DEBUG_LOG_LEVEL = 5, //给TAF框架打日志用 - TAF_LOG_LEVEL = 6, + TARS_LOG_LEVEL = 6, }; /** @@ -850,7 +850,7 @@ namespace tars case WARN_LOG_LEVEL: case DEBUG_LOG_LEVEL: case INFO_LOG_LEVEL: - case TAF_LOG_LEVEL: + case TARS_LOG_LEVEL: return true; break; } diff --git a/util/include/util/tc_mmap.h b/util/include/util/tc_mmap.h index 8a25c98..78c8904 100644 --- a/util/include/util/tc_mmap.h +++ b/util/include/util/tc_mmap.h @@ -27,7 +27,7 @@ #include "util/tc_ex.h" using namespace std; -namespace taf +namespace tars { ///////////////////////////////////////////////// /** diff --git a/util/include/util/tc_monitor.h b/util/include/util/tc_monitor.h index 318fd9b..6db770c 100644 --- a/util/include/util/tc_monitor.h +++ b/util/include/util/tc_monitor.h @@ -20,7 +20,7 @@ #include "util/tc_thread_mutex.h" #include "util/tc_thread_cond.h" -namespace taf +namespace tars { ///////////////////////////////////////////////// /** diff --git a/util/include/util/tc_multi_hashmap.h b/util/include/util/tc_multi_hashmap.h index 3e5bd2e..9a589b6 100644 --- a/util/include/util/tc_multi_hashmap.h +++ b/util/include/util/tc_multi_hashmap.h @@ -2248,9 +2248,9 @@ protected: friend class Block; friend class BlockAllocator; - friend class HashMapIterator; + friend struct HashMapIterator; friend class HashMapItem; - friend class HashMapLockIterator; + friend struct HashMapLockIterator; friend class HashMapLockItem; /** diff --git a/util/include/util/tc_pack.h b/util/include/util/tc_pack.h index f52bbc6..255e10f 100644 --- a/util/include/util/tc_pack.h +++ b/util/include/util/tc_pack.h @@ -17,11 +17,10 @@ #ifndef __TC_PACK_H_ #define __TC_PACK_H_ -#include +#include "util/tc_platform.h" #include #include #include -#include #include "util/tc_ex.h" namespace tars diff --git a/util/include/util/tc_rbtree.h b/util/include/util/tc_rbtree.h index 626bfb6..888b93c 100644 --- a/util/include/util/tc_rbtree.h +++ b/util/include/util/tc_rbtree.h @@ -1743,7 +1743,7 @@ protected: friend class Block; friend class BlockAllocator; - friend class RBTreeLockIterator; + friend struct RBTreeLockIterator; friend class RBTreeLockItem; //禁止copy构造 diff --git a/util/include/util/tc_shm.h b/util/include/util/tc_shm.h index 85110eb..0026726 100644 --- a/util/include/util/tc_shm.h +++ b/util/include/util/tc_shm.h @@ -25,7 +25,7 @@ #endif #include "util/tc_ex.h" -namespace taf +namespace tars { ///////////////////////////////////////////////// /** diff --git a/util/include/util/tc_socket.h b/util/include/util/tc_socket.h index ee74893..77649e2 100644 --- a/util/include/util/tc_socket.h +++ b/util/include/util/tc_socket.h @@ -216,7 +216,7 @@ public: * @throws TC_Socket_Exception * @return */ - void getSockName(string &sSockAddress, uint16_t &iSockPort); + void getSockName(string &sSockAddress, uint16_t &iSockPort) const; /** * @brief 修改socket选项. @@ -289,7 +289,7 @@ public: * 其他错误还是通过异常返回(例如),例如地址错误 * @return int */ - int connectNoThrow(struct sockaddr* addr); + int connectNoThrow(const struct sockaddr* addr); /** * @brief 在socket上监听. @@ -585,7 +585,7 @@ protected: * @param serverLen pstServerAddr指向的结构的长度 * @return int */ - int connect(struct sockaddr *pstServerAddr, socklen_t serverLen); + int connect(const struct sockaddr *pstServerAddr, socklen_t serverLen); /** * @brief 获取对点的地址. @@ -595,7 +595,7 @@ protected: * @throws TC_Socket_Exception * @return */ - void getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen); + void getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen) const; /** * @brief 获取自己的的ip和端口. @@ -605,7 +605,7 @@ protected: * @throws TC_Socket_Exception * @return */ - void getSockName(struct sockaddr *pstSockAddr, socklen_t &iSockLen); + void getSockName(struct sockaddr *pstSockAddr, socklen_t &iSockLen) const; private: diff --git a/util/include/util/tc_spin_lock.h b/util/include/util/tc_spin_lock.h index 4f58fb5..d15589a 100755 --- a/util/include/util/tc_spin_lock.h +++ b/util/include/util/tc_spin_lock.h @@ -9,7 +9,7 @@ using namespace std; -namespace taf +namespace tars { /** diff --git a/util/include/util/tc_thread_pool.h b/util/include/util/tc_thread_pool.h index 06de471..b4ff3d7 100644 --- a/util/include/util/tc_thread_pool.h +++ b/util/include/util/tc_thread_pool.h @@ -29,7 +29,7 @@ using namespace std; -namespace taf +namespace tars { ///////////////////////////////////////////////// /** diff --git a/util/include/util/tc_timeprovider.h b/util/include/util/tc_timeprovider.h index 5826abc..539389c 100644 --- a/util/include/util/tc_timeprovider.h +++ b/util/include/util/tc_timeprovider.h @@ -26,8 +26,8 @@ namespace tars { -#define TNOW taf::TC_TimeProvider::getInstance()->getNow() -#define TNOWMS taf::TC_TimeProvider::getInstance()->getNowMs() +#define TNOW TC_TimeProvider::getInstance()->getNow() +#define TNOWMS TC_TimeProvider::getInstance()->getNowMs() ///////////////////////////////////////////////// /** diff --git a/util/src/CMakeLists.txt b/util/src/CMakeLists.txt index e16f716..c0874ae 100644 --- a/util/src/CMakeLists.txt +++ b/util/src/CMakeLists.txt @@ -2,7 +2,7 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) include_directories(${PROJECT_SOURCE_DIR}/include) -# aux_source_directory(. DIR_SRCS) +aux_source_directory(. DIR_SRCS) # if (CMAKE_SIZEOF_VOID_P EQUAL 8) # list(APPEND DIR_SRCS "tc_jump_x86_64_sysv_elf_gas.s") diff --git a/util/src/tc_encoder.cpp b/util/src/tc_encoder.cpp index abfc3af..b55d8fa 100644 --- a/util/src/tc_encoder.cpp +++ b/util/src/tc_encoder.cpp @@ -13,100 +13,170 @@ * 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" +#if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS #include -#include +#endif + #include #include "util/tc_encoder.h" +#include namespace tars { -void TC_Encoder::gbk2utf8(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen) +#if TARGET_PLATFORM_WINDOWS +#include + +void TC_Encoder::gbk2utf8(const string &sIn, vector &vtStr) { - char * pIn = (char*)sIn; - char * pEnd = pIn+iInLen; - char * pOut = sOut; - size_t iLeftLen; - size_t iGbkLen; - iconv_t cd; + string sOut; - if (iInLen > iMaxOutLen) + for(string::size_type pos = 0; pos < sIn.length(); ++pos) { - throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iInLen > iMaxOutLen error : ", errno); - } - - cd = iconv_open("UTF-8","GBK"); - if (cd == (iconv_t)-1) - { - throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error : ", errno); - } - - iLeftLen = iMaxOutLen; - while(pIn < pEnd) - { - if((unsigned char)(*pIn)==0x80) - { - //注意GBK的0x80转换为UTF-8时为E2 82 AC - *pOut = 0xe2; pOut++; iLeftLen--; - *pOut = 0x82; pOut++; iLeftLen--; - *pOut = 0xac; pOut++; iLeftLen--; - pIn++; - } - else if((unsigned char)(*pIn)<0x80) + if((unsigned char)sIn[pos] < 0x80) { //单字节(GBK: 0x00-0x7F) - *pOut = *pIn; - pIn++;pOut++;iLeftLen--; - } + sOut += sIn[pos]; + } else { //双字节 - iGbkLen=2; - int iRet=iconv(cd, &pIn, (size_t *)&iGbkLen, (char **)&pOut, (size_t *)&iLeftLen); - if(iRet < 0) - { - *pOut = ' '; //转换不了替换为空格 - pIn+=2; pOut++; iLeftLen--; - } - } - } + char pIn[128] = "\0"; - iconv_close(cd); - sOut[iMaxOutLen - iLeftLen] = '\0'; - iMaxOutLen = iMaxOutLen - iLeftLen; + strncpy(pIn, sIn.c_str() + pos, 2); + + sOut = gbk2utf8(pIn); + + ++pos; + } + + vtStr.push_back(sOut); + } } +std::string TC_Encoder::gbk2utf8(const std::string &strGbk) +{ + string outUtf8 = ""; + int n = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0); + WCHAR *str1 = new WCHAR[n]; + MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1,str1, n); + n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL); + char *str2 = new char[n]; + WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL); + outUtf8 = str2; + delete[]str1; + str1 = NULL; + delete[]str2; + str2 = NULL; + return outUtf8; +} + +std::string TC_Encoder::utf82gbk(const std::string &strUtf8) +{ + string outGBK = ""; + int n = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0); + WCHAR *str1 = new WCHAR[n]; + MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, str1, n); + n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL); + char *str2 = new char[n]; + WideCharToMultiByte(CP_ACP, 0, str1, -1, str2, n, NULL, NULL); + outGBK = str2; + delete[] str1; + str1 = NULL; + delete[] str2; + str2 = NULL; + return outGBK; +} + +#else +// void TC_Encoder::gbk2utf8(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen) +// { +// char * pIn = (char*)sIn; +// char * pEnd = pIn+iInLen; +// char * pOut = sOut; +// size_t iLeftLen; +// size_t iGbkLen; +// iconv_t cd; + +// if (iInLen > iMaxOutLen) +// { +// throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iInLen > iMaxOutLen error : ", errno); +// } + +// cd = iconv_open("UTF-8","GBK"); +// if (cd == (iconv_t)-1) +// { +// throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error : ", errno); +// } + +// iLeftLen = iMaxOutLen; +// while(pIn < pEnd) +// { +// if((unsigned char)(*pIn)==0x80) +// { +// //注意GBK的0x80转换为UTF-8时为E2 82 AC +// *pOut = 0xe2; pOut++; iLeftLen--; +// *pOut = 0x82; pOut++; iLeftLen--; +// *pOut = 0xac; pOut++; iLeftLen--; +// pIn++; +// } +// else if((unsigned char)(*pIn)<0x80) +// { +// //单字节(GBK: 0x00-0x7F) +// *pOut = *pIn; +// pIn++;pOut++;iLeftLen--; +// } +// else +// { +// //双字节 +// iGbkLen=2; +// int iRet=iconv(cd, &pIn, (size_t *)&iGbkLen, (char **)&pOut, (size_t *)&iLeftLen); +// if(iRet < 0) +// { +// *pOut = ' '; //转换不了替换为空格 +// pIn+=2; pOut++; iLeftLen--; +// } +// } +// } + +// iconv_close(cd); +// sOut[iMaxOutLen - iLeftLen] = '\0'; +// iMaxOutLen = iMaxOutLen - iLeftLen; +// } + string TC_Encoder::gbk2utf8(const string &sIn) { - iconv_t cd; + iconv_t cd; - cd = iconv_open("UTF-8","GBK"); - if (cd == (iconv_t)-1) + cd = iconv_open("UTF-8","GBK"); + if (cd == (iconv_t)-1) { - throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error", errno); + TAF_THROW_EXCEPTION_SYSCODE(TC_Encoder_Exception, "[TC_Encoder::gbk2utf8] iconv_open error"); + + // throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error", TC_Exception::getSystemCode()); } string sOut; for(string::size_type pos = 0; pos < sIn.length(); ++pos) { - if((unsigned char)sIn[pos] == 0x80) + if((unsigned char)sIn[pos] == 0x80) { //注意GBK的0x80转换为UTF-8时为E2 82 AC sOut += 0xe2; sOut += 0x82; sOut += 0xac; - } - else if((unsigned char)sIn[pos] < 0x80) + } + else if((unsigned char)sIn[pos] < 0x80) { //单字节(GBK: 0x00-0x7F) - sOut += sIn[pos]; - } + sOut += sIn[pos]; + } else { //双字节 - size_t sizeGbkLen = 2; + size_t sizeGbkLen = 2; char pIn[128] = "\0"; strncpy(pIn, sIn.c_str() + pos, sizeGbkLen); @@ -116,11 +186,11 @@ string TC_Encoder::gbk2utf8(const string &sIn) char pOut[128] = "\0"; char *o = pOut; int iRet = iconv(cd, &p, &sizeGbkLen, (char **)&o, &sizeLeftLen); - if(iRet < 0) + if(iRet < 0) { //转换不了, 暂时替换为空格 - sOut += ' '; - } + sOut += ' '; + } else { sOut += pOut; @@ -128,22 +198,23 @@ string TC_Encoder::gbk2utf8(const string &sIn) ++pos; - } - } + } + } - iconv_close(cd); - return sOut; + iconv_close(cd); + return sOut; } void TC_Encoder::gbk2utf8(const string &sIn, vector &vtStr) { - iconv_t cd; + iconv_t cd; - cd = iconv_open("UTF-8","GBK"); - if (cd == (iconv_t)-1) + cd = iconv_open("UTF-8","GBK"); + if (cd == (iconv_t)-1) { - throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error", errno); - } + TAF_THROW_EXCEPTION_SYSCODE(TC_Encoder_Exception, "[TC_Encoder::gbk2utf8] iconv_open error"); + // throw TC_Encoder_Exception("[TC_Encoder::gbk2utf8] iconv_open error", TC_Exception::getSystemCode()); + } vtStr.clear(); @@ -151,22 +222,22 @@ void TC_Encoder::gbk2utf8(const string &sIn, vector &vtStr) { string sOut; - if((unsigned char)sIn[pos] == 0x80) + if((unsigned char)sIn[pos] == 0x80) { //注意GBK的0x80转换为UTF-8时为E2 82 AC sOut += 0xe2; sOut += 0x82; sOut += 0xac; - } - else if((unsigned char)sIn[pos] < 0x80) + } + else if((unsigned char)sIn[pos] < 0x80) { //单字节(GBK: 0x00-0x7F) - sOut += sIn[pos]; - } + sOut += sIn[pos]; + } else { //双字节 - size_t iGbkLen = 2; + size_t iGbkLen = 2; char pIn[128] = "\0"; strncpy(pIn, sIn.c_str() + pos, iGbkLen); @@ -176,11 +247,11 @@ void TC_Encoder::gbk2utf8(const string &sIn, vector &vtStr) char pOut[128] = "\0"; char *o = pOut; int iRet = iconv(cd, &p, (size_t *)&iGbkLen, (char **)&o, (size_t *)&iLeftLen); - if(iRet < 0) + if(iRet < 0) { //转换不了, 暂时替换为空格 - sOut += ' '; - } + sOut += ' '; + } else { sOut += pOut; @@ -188,14 +259,61 @@ void TC_Encoder::gbk2utf8(const string &sIn, vector &vtStr) ++pos; - } + } vtStr.push_back(sOut); - } + } - iconv_close(cd); + iconv_close(cd); } +// string TC_Encoder::utf82gbk(const string &sIn) +// { +// if(sIn.length() == 0) +// { +// return ""; +// } + +// iconv_t cd; + +// cd = iconv_open("GBK","UTF-8"); +// if (cd == (iconv_t)-1) +// { +// throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv_open error", errno); +// } + +// size_t sizeLeftLen = sIn.length() * 2 + 1; +// size_t iMaxOutLen = sizeLeftLen; +// // char *pOut = new char[iMaxOutLen]; +// char pOut[255]; +// // memset(pOut, iMaxOutLen, 0x00); +// const char * pIn = sIn.c_str(); +// size_t sizeInLen = sIn.length(); + +// char *out = pOut; +// cout << sizeInLen << ", " << iMaxOutLen << endl; +// size_t ret = iconv(cd, (char**)&pIn, (size_t*)&sizeInLen, (char **)&out, (size_t*)&sizeLeftLen); +// if (ret == (size_t) - 1) +// { +// // delete[] pOut; +// iconv_close(cd); +// throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv error", errno); +// } + +// iconv_close(cd); + +// out[iMaxOutLen - sizeLeftLen] = '\0'; + +// cout << sIn << ", " << iMaxOutLen << "," << sizeLeftLen << "," << out << endl; + +// string sOut; +// sOut.assign(out, (iMaxOutLen - sizeLeftLen)); + +// // delete[] pOut; + +// return sOut; +// } + void TC_Encoder::utf82gbk(char *sOut, int &iMaxOutLen, const char *sIn, int iInLen) { iconv_t cd; @@ -203,12 +321,13 @@ void TC_Encoder::utf82gbk(char *sOut, int &iMaxOutLen, const char *sIn, int iInL cd = iconv_open("GBK","UTF-8"); if (cd == (iconv_t)-1) { - throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv_open error", errno); + TAF_THROW_EXCEPTION_SYSCODE(TC_Encoder_Exception, "[TC_Encoder::utf82gbk] iconv_open error"); + // throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv_open error", TC_Exception::getSystemCode()); } char * pIn = (char*)sIn; - size_t sizeLeftLen = iMaxOutLen; - size_t sizeInLen = iInLen; + size_t sizeLeftLen = iMaxOutLen; + size_t sizeInLen = iInLen; char* pOut = sOut; size_t ret = iconv(cd, &pIn, &sizeInLen, (char **)&sOut, &sizeLeftLen); @@ -216,7 +335,8 @@ void TC_Encoder::utf82gbk(char *sOut, int &iMaxOutLen, const char *sIn, int iInL { iMaxOutLen = 0; iconv_close(cd); - throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv error", errno); + TAF_THROW_EXCEPTION_SYSCODE(TC_Encoder_Exception, "[TC_Encoder::utf82gbk] iconv error"); + // throw TC_Encoder_Exception("[TC_Encoder::utf82gbk] iconv error", TC_Exception::getSystemCode()); return; } @@ -254,31 +374,33 @@ string TC_Encoder::utf82gbk(const string &sIn) delete[] pOut; - return sOut; + return sOut; } +#endif + /** * \n -> \r\0 * \r -> \r\r */ string TC_Encoder::transTo(const string& str, char f /*='\n'*/, char t /*= '\r'*/, char u /*= '\0'*/) { - string ret = str; + string ret = str; - for (size_t i = 0; i < ret.length(); ++i) - { - if (ret[i] == f) - { - ret[i] = t; + for (size_t i = 0; i < ret.length(); ++i) + { + if (ret[i] == f) + { + ret[i] = t; - ret.insert(++i, 1, u); - } - else if (ret[i] == t) - { - ret.insert(++i, 1, t); - } - } - return ret; + ret.insert(++i, 1, u); + } + else if (ret[i] == t) + { + ret.insert(++i, 1, t); + } + } + return ret; } /** @@ -287,24 +409,26 @@ string TC_Encoder::transTo(const string& str, char f /*='\n'*/, char t /*= '\r'* */ string TC_Encoder::transFrom(const string& str, char f /*= '\n'*/, char t /*= '\r'*/, char u /*= '\0'*/) { - string ret = ""; + string ret = ""; - for (string::const_iterator it = str.begin() - ; it != str.end() - ; ++it) - { - ret.append(1, *it); + for (string::const_iterator it = str.begin() + ; it != str.end() + ; ++it) + { + ret.append(1, *it); - if (*it == t) - { - if (*(++it) == u) - { - *ret.rbegin() = f; - } - } - } - return ret; + if (*it == t) + { + if (*(++it) == u) + { + *ret.rbegin() = f; + } + } + } + return ret; } } + + diff --git a/util/src/tc_epoll_server.cpp b/util/src/tc_epoll_server.cpp index efad182..ec58d69 100644 --- a/util/src/tc_epoll_server.cpp +++ b/util/src/tc_epoll_server.cpp @@ -894,7 +894,7 @@ TC_EpollServer::header_filter_functor &TC_EpollServer::BindAdapter::getHeaderFil // _iHeaderLen = iHeaderLen; // } -void TC_EpollServer::BindAdapter::setConnProtocol(const TC_EpollServer::conn_protocol_functor& cpf, int iHeaderLen, const TC_EpollServer::header_filter_functor &hf) +void TC_EpollServer::BindAdapter::setConnProtocol(const TC_NetWorkBuffer::protocol_functor& cpf, int iHeaderLen, const TC_EpollServer::header_filter_functor &hf) { _cpf = cpf; @@ -908,10 +908,10 @@ void TC_EpollServer::BindAdapter::setConnProtocol(const TC_EpollServer::conn_pro // return _pf; // } -TC_EpollServer::header_filter_functor& TC_EpollServer::BindAdapter::getHeaderFilterFunctor() -{ - return _hf; -} +// TC_EpollServer::header_filter_functor& TC_EpollServer::BindAdapter::getHeaderFilterFunctor() +// { +// return _hf; +// } int TC_EpollServer::BindAdapter::getHeaderFilterLen() { @@ -1168,17 +1168,18 @@ int TC_EpollServer::Connection::parseProtocol() } #endif - string ro; + // string ro; + vector ro; int b = TC_EpollServer::PACKET_LESS; - if (_pBindAdapter->getConnProtocol()) - { - b = _pBindAdapter->getConnProtocol()(_recvbuffer, ro, this); - } - else - { - b = _pBindAdapter->getProtocol()(_recvbuffer, ro); - } + // if (_pBindAdapter->getConnProtocol()) + // { + // b = _pBindAdapter->getConnProtocol()(_recvbuffer, ro, this); + // } + // else + // { + // b = _pBindAdapter->getProtocol()(_recvbuffer, ro); + // } if(b == TC_EpollServer::PACKET_LESS) { @@ -1187,8 +1188,7 @@ int TC_EpollServer::Connection::parseProtocol() } else if(b == TC_EpollServer::PACKET_FULL) { - if (_pBindAdapter->_authWrapper && - _pBindAdapter->_authWrapper(this, ro)) + if (_pBindAdapter->_authWrapper && _pBindAdapter->_authWrapper(this, ro)) continue; shared_ptr recv = std::make_shared(getId(), _ip, _port, getfd(), _pBindAdapter); @@ -2125,9 +2125,9 @@ void TC_EpollServer::NetThread::debug(const string &s) _epollServer->debug(s); } -void TC_EpollServer::NetThread::tars(const string &s) +void TC_EpollServer::NetThread::info(const string &s) { - _epollServer->tars(s); + _epollServer->info(s); } void TC_EpollServer::NetThread::error(const string &s) @@ -2645,7 +2645,7 @@ void TC_EpollServer::NetThread::processPipe() continue; } - switch(sc->cmd) + switch(sc->cmd()) { case 'c': { @@ -2897,7 +2897,7 @@ TC_EpollServer::TC_EpollServer(unsigned int iNetThreadNum) #if TARGET_PLATFORM_WINDOWS WSADATA wsadata; WSAStartup(MAKEWORD(2, 2), &wsadata); -#endi +#endif if(_netThreadNum < 1) { @@ -3500,6 +3500,11 @@ TC_EpollServer::BindAdapterPtr TC_EpollServer::getBindAdapter(const string &sNam return NULL; } +vector TC_EpollServer::getBindAdapters() +{ + return this->_bindAdapters; +} + // TC_EpollServer::BindAdapterPtr TC_EpollServer::getBindAdapter(const string &sName) // { // for(size_t i = 0; i < _netThreads.size(); ++i) @@ -3551,11 +3556,11 @@ void TC_EpollServer::debug(const string &s) } } -void TC_EpollServer::tars(const string &s) +void TC_EpollServer::info(const string &s) { if(_pLocalLogger) { - _pLocalLogger->tars() << "[TARS]" << s << endl; + _pLocalLogger->info() << "[TARS]" << s << endl; } } @@ -3581,20 +3586,26 @@ vector TC_EpollServer::getConnStatus(int lfd) return vConnStatus; } -map TC_EpollServer::getListenSocketInfo() + +unordered_map TC_EpollServer::getListenSocketInfo() { - map mListen; - for(size_t i = 0; i < _netThreads.size(); ++i) - { - auto tmp = _netThreads[i]->getListenSocketInfo(); - for (const auto& kv : tmp) - { - mListen.insert(kv); - } - } - return mListen; + return _listeners; } +// map TC_EpollServer::getListenSocketInfo() +// { +// map mListen; +// for(size_t i = 0; i < _netThreads.size(); ++i) +// { +// auto tmp = _netThreads[i]->getListenSocketInfo(); +// for (const auto& kv : tmp) +// { +// mListen.insert(kv); +// } +// } +// return mListen; +// } + size_t TC_EpollServer::getConnectionCount() { size_t iConnTotal = 0; diff --git a/util/src/tc_epoller.cpp b/util/src/tc_epoller.cpp index c781bd0..a88d690 100644 --- a/util/src/tc_epoller.cpp +++ b/util/src/tc_epoller.cpp @@ -20,7 +20,7 @@ #include #endif -namespace taf +namespace tars { TC_Epoller::NotifyInfo::NotifyInfo() : _ep(NULL) diff --git a/util/src/tc_http_async.cpp b/util/src/tc_http_async.cpp index 556d542..f8bf1e2 100644 --- a/util/src/tc_http_async.cpp +++ b/util/src/tc_http_async.cpp @@ -16,32 +16,46 @@ #include "util/tc_http_async.h" #include "util/tc_common.h" +#include "util/tc_timeprovider.h" namespace tars { -TC_HttpAsync::AsyncRequest::AsyncRequest(TC_HttpRequest &stHttpRequest, TC_HttpAsync::RequestCallbackPtr &callbackPtr) - : _pHttpAsync(NULL),_iUniqId(0), _callbackPtr(callbackPtr) +TC_HttpAsync::AsyncRequest::AsyncRequest(TC_HttpRequest &stHttpRequest, TC_HttpAsync::RequestCallbackPtr &callbackPtr, bool bUseProxy) + : _pHttpAsync(NULL), _iUniqId(0), _callbackPtr(callbackPtr), _bUseProxy(bUseProxy), _isConnected(false) { - memset(&_bindAddr,0,sizeof(struct sockaddr)); + memset(&_bindAddr, 0, sizeof(struct sockaddr)); _bindAddrSet = false; _sReq = stHttpRequest.encode(); stHttpRequest.getHostPort(_sHost, _iPort); + + // _notify.createSocket(); } TC_HttpAsync::AsyncRequest::~AsyncRequest() { doClose(); + + // if (_pHttpAsync) + // _pHttpAsync->delConnection(_notify.getfd()); + + // _notify.close(); } void TC_HttpAsync::AsyncRequest::doClose() { - if(_fd.isValid()) + if (_pHttpAsync) _pHttpAsync->assertThreadId(); + + if (_fd.isValid()) { - if(_callbackPtr) _callbackPtr->onClose(); - if(_pHttpAsync) _pHttpAsync->erase(_iUniqId); + try { if (_callbackPtr) _callbackPtr->onClose(); } catch (...) {} + if (_pHttpAsync) + { + _pHttpAsync->delConnection(_fd.getfd()); + _pHttpAsync->erase(_iUniqId); + } _fd.close(); } } @@ -53,82 +67,58 @@ void TC_HttpAsync::AsyncRequest::setBindAddr(const struct sockaddr* addr) _bindAddrSet = true; } -int TC_HttpAsync::AsyncRequest::doConnect() +void TC_HttpAsync::AsyncRequest::doConnect() { + if (_pHttpAsync) + _pHttpAsync->assertThreadId(); _fd.createSocket(); _fd.setblock(); - - //不产生TimeWait状态 + _fd.setTcpNoDelay(); _fd.setNoCloseWait(); try { - if(_bindAddrSet) + if (_bindAddrSet) { - _fd.bind(&_bindAddr,sizeof(_bindAddr)); + _fd.bind(&_bindAddr, sizeof(_bindAddr)); } - int ret = _fd.connectNoThrow(_sHost, _iPort); + int ret = 0; - if(ret < 0 && errno != EINPROGRESS) + if (_bUseProxy) + ret = _fd.connectNoThrow(_pHttpAsync->getProxyAddr()); + else + ret = _fd.connectNoThrow(_sHost, _iPort); + + if (ret < 0 && !TC_Socket::isInProgress()) { - _fd.close(); - return ret; + doException(RequestCallback::Failed_Connect, getError("connect server error.")); + return; } + + _pHttpAsync->addConnection(_fd.getfd(), _iUniqId, EPOLLIN | EPOLLOUT); + } - catch(exception &ex) + catch (exception &ex) { - _fd.close(); - return -1; + doException(RequestCallback::Failed_Connect, ex.what()); } - - return 0; } -int TC_HttpAsync::AsyncRequest::doConnect(struct sockaddr* proxyAddr) -{ - _fd.createSocket(); - _fd.setblock(); - - //不产生TimeWait状态 - _fd.setNoCloseWait(); - - try - { - if(_bindAddrSet) - { - _fd.bind(&_bindAddr,sizeof(_bindAddr)); - } - - int ret = _fd.connectNoThrow(proxyAddr); - if(ret < 0 && errno != EINPROGRESS) - { - _fd.close(); - return ret; - } - } - catch(exception &ex) - { - _fd.close(); - return -1; - } - - return 0; -} int TC_HttpAsync::AsyncRequest::recv(void* buf, uint32_t len, uint32_t flag) { - int ret = ::recv(_fd.getfd(), buf, len, flag); + int ret = ::recv(_fd.getfd(), (char*)buf, len, flag); if (ret == 0) { return 0; } - else if(ret < 0 && errno == EAGAIN) + else if (ret < 0 && TC_Socket::isPending()) { return -1; } - else if(ret < 0) + else if (ret < 0) { //其他网络异常 return -2; @@ -140,9 +130,9 @@ int TC_HttpAsync::AsyncRequest::recv(void* buf, uint32_t len, uint32_t flag) int TC_HttpAsync::AsyncRequest::send(const void* buf, uint32_t len, uint32_t flag) { - int ret = ::send(_fd.getfd(), buf, len, flag); + int ret = ::send(_fd.getfd(), (char*)buf, len, flag); - if (ret < 0 && errno == EAGAIN) + if (ret < 0 && TC_Socket::isPending()) { return -1; } @@ -155,45 +145,67 @@ int TC_HttpAsync::AsyncRequest::send(const void* buf, uint32_t len, uint32_t fla void TC_HttpAsync::AsyncRequest::timeout() { - try - { - doClose(); + if (_pHttpAsync) _pHttpAsync->assertThreadId(); + + if (hasConnected()) + doException(RequestCallback::Failed_Timeout, "timeout error."); + else + doException(RequestCallback::Failed_ConnectTimeout, "connect timeout error."); - if(_callbackPtr) _callbackPtr->onTimeout(); - } - catch(exception &ex) - { - if(_callbackPtr) _callbackPtr->onException(ex.what()); - } - catch(...) - { - if(_callbackPtr) _callbackPtr->onException("unknown error."); - } } -void TC_HttpAsync::AsyncRequest::doException() +string TC_HttpAsync::AsyncRequest::getError(const char* sDefault) const { - string err("unknown error."); - - if(_fd.isValid()) + int ret = TC_Exception::getSystemCode(); + if(ret!= 0) { - int error; - socklen_t len = sizeof(error); - _fd.getSockOpt(SO_ERROR, (void*)&error, len, SOL_SOCKET); - err = strerror(error); - - doClose(); + return TC_Exception::parseError(ret); } - try { if(_callbackPtr) _callbackPtr->onException(err); } catch(...) { } + return sDefault; +} +// string TC_HttpAsync::AsyncRequest::getError(const string &sDefault) const +// { +// string err; + +// if (_fd.isValid()) +// { +// int error; +// SOCKET_LEN_TYPE len = sizeof(error); +// _fd.getSockOpt(SO_ERROR, (void*)&error, len, SOL_SOCKET); +// if (error != 0) +// { +// err = strerror(error); +// } +// } + +// if (err.empty() && errno != 0) +// { +// err = strerror(errno); +// } + +// if (err.empty()) +// err = sDefault; + +// return err; +// } + +void TC_HttpAsync::AsyncRequest::doException(RequestCallback::FAILED_CODE ret, const string &e) +{ + doClose(); + + try { if (_callbackPtr) _callbackPtr->onFailed(ret, e); } catch (...) { } } void TC_HttpAsync::AsyncRequest::doRequest() { - if(!_fd.isValid()) return; + if (_pHttpAsync) _pHttpAsync->assertThreadId(); + if (!_fd.isValid()) return; int ret = -1; + setConnected(true); + do { ret = -1; @@ -202,21 +214,22 @@ void TC_HttpAsync::AsyncRequest::doRequest() { if ((ret = this->send(_sReq.c_str(), _sReq.length(), 0)) > 0) { - _sReq = _sReq.substr(ret); + _sReq = _sReq.substr(ret); } } - }while(ret > 0); + } while (ret > 0); //网络异常 - if(ret == -2) + if (ret == -2) { - doException(); + doException(RequestCallback::Failed_Net, getError("send error.")); } } void TC_HttpAsync::AsyncRequest::doReceive() { - if(!_fd.isValid()) return; + if (_pHttpAsync) _pHttpAsync->assertThreadId(); + if (!_fd.isValid()) return; int recv = 0; @@ -231,62 +244,102 @@ void TC_HttpAsync::AsyncRequest::doReceive() } while (recv > 0); - if(recv == -2) + if (recv == -2) { - doException(); + doException(RequestCallback::Failed_Net, getError("recv error.")); } else { - try + //增量decode + bool ret = _stHttpResp.incrementDecode(_sRsp); + + //有头部数据了 + if (_callbackPtr && !_stHttpResp.getHeaders().empty()) { - //增量decode - bool ret = _stHttpResp.incrementDecode(_sRsp); - - //有头部数据了 - if(_callbackPtr && !_stHttpResp.getHeaders().empty()) + bool bContinue = _callbackPtr->onContinue(_stHttpResp); + if (!bContinue) { - bool bContinue = _callbackPtr->onReceive(_stHttpResp); - if(!bContinue) - { - doClose(); - return; - } + doException(RequestCallback::Failed_Interrupt, getError("receive interrupt.")); + return; } + } + //数据接收完毕 + if (ret) + { + //只支持短连接, 数据收取完毕, 关闭链接 + doClose(); + try { if (_callbackPtr) _callbackPtr->onSucc(_stHttpResp); } catch (...) { } + return; + } + else + { //服务器关闭了连接 bool bClose = (recv == 0); - //如果远程关闭连接或者增量decode完毕 - if(bClose || ret) + if (bClose) { doClose(); - if(_callbackPtr) _callbackPtr->onResponse(bClose, _stHttpResp); + try { if (_callbackPtr) _callbackPtr->onSucc(_stHttpResp); } catch (...) { } + + // doException(RequestCallback::Failed_Close, getError("close by server.")); } } - catch(exception &ex) - { - if(_callbackPtr) _callbackPtr->onException(ex.what()); - } - catch(...) - { - if(_callbackPtr) _callbackPtr->onException("unknown error."); - } + } +} + +void TC_HttpAsync::AsyncRequest::processNet(const epoll_event &ev) +{ + if (TC_Epoller::readEvent(ev))//events & EPOLLIN) + { + // cout << "readEvent" << endl; + doReceive(); + } + + if (TC_Epoller::writeEvent(ev)) + { + // cout << "writeEvent" << endl; + doRequest(); + } +} + +void TC_HttpAsync::AsyncRequest::processNotify() +{ + //没有建立连接, 发起连接 + if (!isValid()) + { + // cout << "doConnect" << endl; + doConnect(); + } + else + { + // cout << "doRequest" << endl; + doRequest(); } } /////////////////////////////////////////////////////////////////////////// +#define H64(x) (((uint64_t)x) << 32) TC_HttpAsync::TC_HttpAsync() : _terminate(false) { - memset(&_proxyAddr,0,sizeof(struct sockaddr)); - memset(&_bindAddr,0,sizeof(struct sockaddr)); + memset(&_proxyAddr, 0, sizeof(struct sockaddr)); + memset(&_bindAddr, 0, sizeof(struct sockaddr)); - _bindAddrSet=false; + _bindAddrSet = false; _data = new http_queue_type(10000); _epoller.create(1024); + + // _notify.createSocket(); + _notify.init(&_epoller); + + uint64_t data = H64(_notify.notifyFd()) | 0; + _notify.add(data); + + // _epoller.add(_notify.getfd(), data, EPOLLIN); } TC_HttpAsync::~TC_HttpAsync() @@ -294,13 +347,21 @@ TC_HttpAsync::~TC_HttpAsync() terminate(); delete _data; + + // _notify.close(); + _notify.release(); + _epoller.close(); + } -void TC_HttpAsync::start(int iThreadNum) +void TC_HttpAsync::start() { _tpool.init(1); _tpool.start(); + // TC_Functor cmd(this, &TC_HttpAsync::run); + // TC_Functor::wrapper_type wt(cmd); + _tpool.exec(std::bind(&TC_HttpAsync::run, this)); } @@ -308,9 +369,9 @@ void TC_HttpAsync::waitForAllDone(int millsecond) { time_t now = TNOW; - while(_data->size() > 0) + while (_data->size() > 0) { - if(millsecond < 0) + if (millsecond < 0) { TC_ThreadLock::Lock lock(*this); timedWait(100); @@ -323,7 +384,7 @@ void TC_HttpAsync::waitForAllDone(int millsecond) timedWait(100); } - if((TNOW - now) >= (millsecond/1000)) + if ((TNOW - now) >= (millsecond / 1000)) break; } @@ -341,6 +402,9 @@ void TC_HttpAsync::erase(uint32_t uniqId) void TC_HttpAsync::terminate() { _terminate = true; + + _notify.notify(); + _tpool.waitForAllDone(); } @@ -349,33 +413,15 @@ void TC_HttpAsync::timeout(AsyncRequestPtr& ptr) ptr->timeout(); } -int TC_HttpAsync::doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy,struct sockaddr* addr) +void TC_HttpAsync::doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackPtr &callbackPtr, bool bUseProxy) { - int ret; + AsyncRequest * req = new AsyncRequest(stHttpRequest, callbackPtr, bUseProxy); - AsyncRequestPtr req = new AsyncRequest(stHttpRequest, callbackPtr); - - if(_bindAddrSet) + if (_bindAddrSet) { req->setBindAddr(&_bindAddr); } - //发起异步连接请求 - if(bUseProxy) - { - ret = req->doConnect(&_proxyAddr); - } - else if(NULL != addr) - { - ret = req->doConnect(addr); - } - else - { - ret = req->doConnect(); - } - - if(ret < 0) return -1; - uint32_t uniqId = _data->generateId(); req->setUniqId(uniqId); @@ -384,14 +430,29 @@ int TC_HttpAsync::doAsyncRequest(TC_HttpRequest &stHttpRequest, RequestCallbackP _data->push(req, uniqId); - _epoller.add(req->getfd(), uniqId, EPOLLIN | EPOLLOUT); + { + std::lock_guard lock(_mutex); + _events.push_back(H64(_notify.notifyFd()) | uniqId); + } + _notify.notify(); + // addConnection(req->getNotifyfd(), uniqId, EPOLLIN); +} - return 0; +void TC_HttpAsync::addConnection(int fd, uint32_t uniqId, uint32_t events) +{ + uint64_t data = H64(fd) | uniqId; + + _epoller.add(fd, data, events); +} + +void TC_HttpAsync::delConnection(int fd) +{ + _epoller.del(fd, 0, 0); } int TC_HttpAsync::setBindAddr(const char* sBindAddr) { - bzero(&_bindAddr,sizeof(_bindAddr)); + memset(&_bindAddr, 0x00, sizeof(_bindAddr)); struct sockaddr_in* p = (struct sockaddr_in *)&_bindAddr; @@ -399,7 +460,7 @@ int TC_HttpAsync::setBindAddr(const char* sBindAddr) { TC_Socket::parseAddr(sBindAddr, p->sin_addr); } - catch(exception &ex) + catch (exception &ex) { return -1; } @@ -412,12 +473,11 @@ int TC_HttpAsync::setBindAddr(const char* sBindAddr) return 0; } - int TC_HttpAsync::setProxyAddr(const char* sProxyAddr) { vector v = TC_Common::sepstr(sProxyAddr, ":"); - if(v.size() < 2) + if (v.size() < 2) return -1; return setProxyAddr(v[0].c_str(), TC_Common::strto(v[1])); @@ -425,7 +485,7 @@ int TC_HttpAsync::setProxyAddr(const char* sProxyAddr) int TC_HttpAsync::setProxyAddr(const char* sHost, uint16_t iPort) { - bzero(&_proxyAddr,sizeof(_proxyAddr)); + memset(&_proxyAddr, 0x00, sizeof(_proxyAddr)); struct sockaddr_in *p = (struct sockaddr_in *)&_proxyAddr; @@ -433,7 +493,7 @@ int TC_HttpAsync::setProxyAddr(const char* sHost, uint16_t iPort) { TC_Socket::parseAddr(sHost, p->sin_addr); } - catch(exception &ex) + catch (exception &ex) { return -1; } @@ -449,34 +509,15 @@ void TC_HttpAsync::setProxyAddr(const struct sockaddr* addr) memcpy(&_proxyAddr, addr, sizeof(struct sockaddr)); } -void TC_HttpAsync::process(AsyncRequestPtr& p, int events) -{ - if (events & (EPOLLERR | EPOLLHUP)) - { - p->doException(); - return; - } - - if(events & EPOLLIN) - { - p->doReceive(); - } - - if(events & EPOLLOUT) - { - p->doRequest(); - } -} - void TC_HttpAsync::run() { - TC_TimeoutQueue::data_functor df(&TC_HttpAsync::timeout); + _threadId = std::this_thread::get_id(); - async_process_type apt(&TC_HttpAsync::process); + TC_TimeoutQueue::data_functor df(&TC_HttpAsync::timeout); int64_t lastDealTimeout = 0; - while(!_terminate) + while (!_terminate) { try { @@ -493,19 +534,52 @@ void TC_HttpAsync::run() { epoll_event ev = _epoller.get(i); - uint32_t uniqId = (uint32_t)ev.data.u64; + if (_terminate) + break; - AsyncRequestPtr p = _data->get(uniqId, false); + uint32_t fd = TC_Epoller::getU32(ev, true); - if(!p) continue; + if ((int)fd == _notify.notifyFd()) + { + // cout << "notify" << endl; + deque events; -// async_process_type::wrapper_type w(apt, p, ev.events); + { + std::lock_guard lock(_mutex); -// _npool[uniqId%_npool.size()]->exec(w); - process(p, ev.events); + _events.swap(events); + } + + for(auto data : events) + { + uint32_t uniqId = (uint32_t)data; + + // cout << "socket uniqId:" << uniqId << endl; + AsyncRequestPtr ptr = _data->getAndRefresh(uniqId); + if (!ptr) + continue; + + ptr->processNotify(); + } + + } + else + { + + uint32_t uniqId = TC_Epoller::getU32(ev, false); + + // cout << "http socket uniqId:" << uniqId << endl; + + AsyncRequestPtr ptr = _data->getAndRefresh(uniqId); + if (!ptr) + continue; + + // assert(fd == ptr->getfd()); + ptr->processNet(ev); + } } } - catch(exception &ex) + catch (exception &ex) { cerr << "[TC_HttpAsync::run] error:" << ex.what() << endl; } diff --git a/util/src/tc_mmap.cpp b/util/src/tc_mmap.cpp index 76e43a2..65852fb 100644 --- a/util/src/tc_mmap.cpp +++ b/util/src/tc_mmap.cpp @@ -27,7 +27,8 @@ #include #include #include "util/tc_file.h" -namespace taf + +namespace tars { TC_Mmap::TC_Mmap(bool bOwner) diff --git a/util/src/tc_pack.cpp b/util/src/tc_pack.cpp index e4bc619..32f5b73 100644 --- a/util/src/tc_pack.cpp +++ b/util/src/tc_pack.cpp @@ -1,26 +1,84 @@ -/** - * 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. - */ - +#include "util/tc_platform.h" #include "util/tc_pack.h" + #include #include +#if TARGET_PLATFORM_LINUX +#include +#endif namespace tars { +#if TARGET_PLATFORM_WINDOWS +#pragma comment(lib,"ws2_32.lib") +#endif + +#if TARGET_PLATFORM_IOS || TARGET_PLATFORM_WINDOWS +# ifndef __LITTLE_ENDIAN +# define __LITTLE_ENDIAN 1234 +# endif +# ifndef __BIG_ENDIAN +# define __BIG_ENDIAN 4321 +# endif +# ifndef __BYTE_ORDER +# define __BYTE_ORDER __LITTLE_ENDIAN +# endif +#endif + +#if __BYTE_ORDER == __BIG_ENDIAN +# define pack_ntohll(x) (x) +# define pack_htonll(x) (x) +# define pack_ntohf(x) (x) +# define pack_htonf(x) (x) +# define pack_ntohd(x) (x) +# define pack_htond(x) (x) +#else +# if __BYTE_ORDER == __LITTLE_ENDIAN +# define pack_ntohll(x) pack_htonll(x) +union pack_bswap_helper +{ + int64_t i64; + int32_t i32[2]; +}; +inline int64_t pack_htonll(int64_t x) +{ + pack_bswap_helper h; + h.i64 = x; + int64_t tmp = htonl(h.i32[1]); + h.i32[1] = htonl(h.i32[0]); + h.i32[0] = tmp; + return h.i64; +} +inline float pack_ntohf(float x) +{ + union { + float f; + int32_t i32; + } helper; + + helper.f = x; + helper.i32 = htonl( helper.i32 ); + + return helper.f; +} +# define pack_htonf(x) pack_ntohf(x) +inline double pack_ntohd(double x) +{ + union { + double d; + int64_t i64; + } helper; + + helper.d = x; + helper.i64 = pack_htonll( helper.i64 ); + + return helper.d; +} +# define pack_htond(x) pack_ntohd(x) +# endif +#endif + TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (bool t) { _buffer.append(sizeof(bool), (char)t); @@ -71,9 +129,7 @@ TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (long t) { if(sizeof(long) == 8) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_htonll(t); } else { @@ -88,9 +144,7 @@ TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long { if(sizeof(unsigned long) == 8) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_htonll(t); } else { @@ -103,9 +157,7 @@ TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (long long t) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_htonll(t); _buffer.append((const char *)&t, sizeof(long long)); return *this; @@ -113,9 +165,7 @@ TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (long long t) TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long long t) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_htonll(t); _buffer.append((const char *)&t, sizeof(unsigned long long)); @@ -124,12 +174,16 @@ TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (float t) { + t = pack_htonf(t); + _buffer.append((const char *)&t, sizeof(float)); return *this; } TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (double t) { + t = pack_htond(t); + _buffer.append((const char *)&t, sizeof(double)); return *this; } @@ -297,9 +351,7 @@ TC_PackOut& TC_PackOut::operator >> (long &t) if(sizeof(unsigned long) == 8) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_ntohll(t); } else { @@ -323,9 +375,7 @@ TC_PackOut& TC_PackOut::operator >> (unsigned long &t) if(sizeof(unsigned long) == 8) { - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_ntohll(t); } else { @@ -347,9 +397,7 @@ TC_PackOut& TC_PackOut::operator >> (long long &t) memcpy(&t, _pbuffer + _pos, len); - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_ntohll(t); _pos += len; @@ -366,9 +414,7 @@ TC_PackOut& TC_PackOut::operator >> (unsigned long long &t) memcpy(&t, _pbuffer + _pos, len); - #if __BYTE_ORDER == __LITTLE_ENDIAN - t = __bswap_64(t); - #endif + t = pack_ntohll(t); _pos += len; @@ -383,6 +429,9 @@ TC_PackOut& TC_PackOut::operator >> (float &f) } memcpy(&f, _pbuffer + _pos, sizeof(float)); + + f = pack_ntohf(f); + _pos += sizeof(float); return *this; @@ -396,6 +445,9 @@ TC_PackOut& TC_PackOut::operator >> (double &f) } memcpy(&f, _pbuffer + _pos, sizeof(double)); + + f = pack_ntohd(f); + _pos += sizeof(double); return *this; diff --git a/util/src/tc_sha.cpp b/util/src/tc_sha.cpp index 7ec4388..804d05c 100644 --- a/util/src/tc_sha.cpp +++ b/util/src/tc_sha.cpp @@ -13,12 +13,11 @@ * 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_sha.h" #include "util/tc_common.h" #include #include -#include +// #include #include namespace tars @@ -667,7 +666,7 @@ string TC_SHA::sha1file(const string &fileName) size_t n; detail_sha1::sha1_ctx cx[1]; if(( f = fopen( fileName.c_str(), "rb" )) == NULL ) - throw TC_SHA_Exception("[TC_SHA::sha1file] fopen '" + fileName + "', error.", errno); + throw TC_SHA_Exception("[TC_SHA::sha1file] fopen '" + fileName + "', error.", true); detail_sha1::sha1_begin(cx); while((n = fread( buf, 1, sizeof( buf ),f)) > 0 ) @@ -709,7 +708,7 @@ string TC_SHA::sha256file(const string &fileName) size_t n; detail_sha2::sha256_ctx cx[1]; if(( f = fopen( fileName.c_str(), "rb" )) == NULL ) - throw TC_SHA_Exception("[TC_SHA::sha256file] fopen '" + fileName + "', error.", errno); + throw TC_SHA_Exception("[TC_SHA::sha256file] fopen '" + fileName + "', error.", true); detail_sha2::sha256_begin(cx); while((n = fread( buf, 1, sizeof( buf ),f)) > 0 ) @@ -751,7 +750,7 @@ string TC_SHA::sha384file(const string &fileName) size_t n; detail_sha2::sha384_ctx cx[1]; if(( f = fopen( fileName.c_str(), "rb" )) == NULL ) - throw TC_SHA_Exception("[TC_SHA::sha384file] fopen '" + fileName + "', error.", errno); + throw TC_SHA_Exception("[TC_SHA::sha384file] fopen '" + fileName + "', error.", true); detail_sha2::sha384_begin(cx); while((n = fread( buf, 1, sizeof( buf ),f)) > 0 ) @@ -793,7 +792,7 @@ string TC_SHA::sha512file(const string &fileName) size_t n; detail_sha2::sha512_ctx cx[1]; if(( f = fopen( fileName.c_str(), "rb" )) == NULL ) - throw TC_SHA_Exception("[TC_SHA::sha512file] fopen '" + fileName + "', error.", errno); + throw TC_SHA_Exception("[TC_SHA::sha512file] fopen '" + fileName + "', error.", true); detail_sha2::sha512_begin(cx); while((n = fread( buf, 1, sizeof( buf ),f)) > 0 ) @@ -807,4 +806,3 @@ string TC_SHA::sha512file(const string &fileName) } } - diff --git a/util/src/tc_shm.cpp b/util/src/tc_shm.cpp index 1615dff..8fc0116 100644 --- a/util/src/tc_shm.cpp +++ b/util/src/tc_shm.cpp @@ -127,3 +127,5 @@ int TC_Shm::del() #endif } + +} diff --git a/util/src/tc_socket.cpp b/util/src/tc_socket.cpp index 1c2c252..a1fbdb8 100755 --- a/util/src/tc_socket.cpp +++ b/util/src/tc_socket.cpp @@ -109,7 +109,7 @@ void TC_Socket::getPeerName(string &sPeerAddress, uint16_t &iPeerPort) #if TARGET_PLATFORM_LINUX||TARGET_PLATFORM_IOS -void TC_Socket::getPeerName(string &sPathName) +void TC_Socket::getPeerName(string &sPathName) const { assert(_iDomain == AF_LOCAL); @@ -121,7 +121,7 @@ void TC_Socket::getPeerName(string &sPathName) sPathName = stSock.sun_path; } -void TC_Socket::getSockName(string &sPathName) +void TC_Socket::getSockName(string &sPathName) const { assert(_iDomain == AF_LOCAL); @@ -170,7 +170,7 @@ int TC_Socket::connectNoThrow(const char *sPathName) #endif -void TC_Socket::getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen) +void TC_Socket::getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen) const { if(getpeername(_sock, pstPeerAddr, &iPeerLen) < 0) { @@ -178,7 +178,7 @@ void TC_Socket::getPeerName(struct sockaddr *pstPeerAddr, socklen_t &iPeerLen) } } -void TC_Socket::getSockName(string &sSockAddress, uint16_t &iSockPort) +void TC_Socket::getSockName(string &sSockAddress, uint16_t &iSockPort) const { assert(_iDomain == AF_INET || _iDomain == AF_INET6); @@ -195,7 +195,7 @@ void TC_Socket::getSockName(string &sSockAddress, uint16_t &iSockPort) iSockPort = (AF_INET6 == _iDomain) ? ntohs(in6.sin6_port) : ntohs(in4.sin_port); } -void TC_Socket::getSockName(struct sockaddr *pstSockAddr, socklen_t &iSockLen) +void TC_Socket::getSockName(struct sockaddr *pstSockAddr, socklen_t &iSockLen) const { if(getsockname(_sock, pstSockAddr, &iSockLen) < 0) { @@ -427,7 +427,7 @@ int TC_Socket::connectNoThrow(const string &sServerAddr, uint16_t port) return connect(serverAddr, len); } -int TC_Socket::connectNoThrow(struct sockaddr* addr) +int TC_Socket::connectNoThrow(const struct sockaddr* addr) { assert(_iDomain == AF_INET || _iDomain == AF_INET6); @@ -445,7 +445,7 @@ void TC_Socket::connect(const string &sServerAddr, uint16_t port) } -int TC_Socket::connect(struct sockaddr *pstServerAddr, socklen_t serverLen) +int TC_Socket::connect(const struct sockaddr *pstServerAddr, socklen_t serverLen) { return ::connect(_sock, pstServerAddr, serverLen); diff --git a/util/src/tc_thread.cpp b/util/src/tc_thread.cpp index ada3f35..64c02d4 100644 --- a/util/src/tc_thread.cpp +++ b/util/src/tc_thread.cpp @@ -15,6 +15,7 @@ */ #include "util/tc_thread.h" +#include #include namespace tars