diff --git a/.gitignore b/.gitignore index d792ba5..789e45d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,17 @@ DerivedData -libcorpc_mac.xcworkspace/xcuserdata +test +libcorpc_test.xcworkspace +libcorpc.xcworkspace/xcuserdata co/co.xcodeproj/xcuserdata corpc/corpc.xcodeproj/xcuserdata corpc_mysql/corpc_mysql.xcodeproj/xcuserdata +corpc_memcached/corpc_memcached.xcodeproj/xcuserdata example/example_interRpc/example_interRpc.xcodeproj/xcuserdata example/example_innerRpc/example_innerRpc.xcodeproj/xcuserdata example/example_echoTcp/example_echoTcp.xcodeproj/xcuserdata example/example_echoUdp/example_echoUdp.xcodeproj/xcuserdata example/example_mysql/example_mysql.xcodeproj/xcuserdata -example/test* +example/example_memcached/example_memcached.xcodeproj/xcuserdata tutorial/tutorial1/tutorial1.xcodeproj/xcuserdata tutorial/tutorial2/tutorial2.xcodeproj/xcuserdata tutorial/tutorial3/tutorial3.xcodeproj/xcuserdata diff --git a/corpc/Makefile b/corpc/Makefile index e09bacd..c8890ce 100644 --- a/corpc/Makefile +++ b/corpc/Makefile @@ -22,7 +22,7 @@ DIR_OBJ = ./${v}/obj/ DIR_STATICLIB = ./lib/ DIR_DYNAMICLIB = ./solib/ -OBJS = corpc_option.pb.o corpc_utils.o corpc_routine_env.o corpc_io.o corpc_rpc_server.o corpc_rpc_client.o corpc_inner_rpc.o corpc_message_server.o +OBJS = corpc_option.pb.o corpc_thirdparty.pb.o corpc_utils.o corpc_routine_env.o corpc_io.o corpc_rpc_server.o corpc_rpc_client.o corpc_inner_rpc.o corpc_message_server.o OBJS_WITH_PATH = ${foreach n,${OBJS},${DIR_OBJ}${n}} empty = diff --git a/corpc/corpc.xcodeproj/project.pbxproj b/corpc/corpc.xcodeproj/project.pbxproj index e26660c..2712a7c 100644 --- a/corpc/corpc.xcodeproj/project.pbxproj +++ b/corpc/corpc.xcodeproj/project.pbxproj @@ -26,6 +26,8 @@ 2DEA99B9208DAE3A0095C62F /* corpc_message_server.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA99B7208DAE3A0095C62F /* corpc_message_server.h */; }; 2DEA9A8C20A2E7C90095C62F /* corpc_option.pb.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA9A8A20A2E7C90095C62F /* corpc_option.pb.h */; }; 2DEA9A8D20A2E7C90095C62F /* corpc_option.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 2DEA9A8B20A2E7C90095C62F /* corpc_option.pb.cc */; }; + 2DEA9A9420A98F540095C62F /* corpc_thirdparty.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 2DEA9A9220A98F530095C62F /* corpc_thirdparty.pb.cc */; }; + 2DEA9A9520A98F540095C62F /* corpc_thirdparty.pb.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA9A9320A98F530095C62F /* corpc_thirdparty.pb.h */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -49,6 +51,8 @@ 2DEA99B7208DAE3A0095C62F /* corpc_message_server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = corpc_message_server.h; sourceTree = ""; }; 2DEA9A8A20A2E7C90095C62F /* corpc_option.pb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = corpc_option.pb.h; path = ../proto/corpc_option.pb.h; sourceTree = ""; }; 2DEA9A8B20A2E7C90095C62F /* corpc_option.pb.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = corpc_option.pb.cc; path = ../proto/corpc_option.pb.cc; sourceTree = ""; }; + 2DEA9A9220A98F530095C62F /* corpc_thirdparty.pb.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = corpc_thirdparty.pb.cc; path = ../proto/corpc_thirdparty.pb.cc; sourceTree = ""; }; + 2DEA9A9320A98F530095C62F /* corpc_thirdparty.pb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = corpc_thirdparty.pb.h; path = ../proto/corpc_thirdparty.pb.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -83,6 +87,8 @@ 2DB2B2CE2049298900114392 /* src */ = { isa = PBXGroup; children = ( + 2DEA9A9220A98F530095C62F /* corpc_thirdparty.pb.cc */, + 2DEA9A9320A98F530095C62F /* corpc_thirdparty.pb.h */, 2DEA9A8B20A2E7C90095C62F /* corpc_option.pb.cc */, 2DEA9A8A20A2E7C90095C62F /* corpc_option.pb.h */, 2DB2B2F3204931A300114392 /* corpc_rpc_client.cpp */, @@ -130,6 +136,7 @@ 2DB2B30D204931A500114392 /* corpc_inner_rpc.h in Headers */, 2D161BEC2058CAF10062BA35 /* corpc_utils.h in Headers */, 2DEA997C208480940095C62F /* corpc_rpc_client.h in Headers */, + 2DEA9A9520A98F540095C62F /* corpc_thirdparty.pb.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -197,6 +204,7 @@ 2DB2B30A204931A500114392 /* corpc_rpc_server.cpp in Sources */, 2DEA9A8D20A2E7C90095C62F /* corpc_option.pb.cc in Sources */, 2DB2B30E204931A500114392 /* corpc_routine_env.cpp in Sources */, + 2DEA9A9420A98F540095C62F /* corpc_thirdparty.pb.cc in Sources */, 2DB2B306204931A500114392 /* corpc_io.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/corpc_memcached/corpc_memcached.cpp b/corpc_memcached/corpc_memcached.cpp new file mode 100644 index 0000000..a4d6c8d --- /dev/null +++ b/corpc_memcached/corpc_memcached.cpp @@ -0,0 +1,197 @@ +/* + * Created by Xianke Liu on 2018/5/14. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 "corpc_memcached.h" + +using namespace corpc; + +MemcachedConnectPool::Proxy::Proxy(MemcachedConnectPool *pool) { + InnerRpcClient *client = InnerRpcClient::instance(); + + InnerRpcClient::Channel *channel = new InnerRpcClient::Channel(client, pool->_server); + + _stub = new thirdparty::ThirdPartyService::Stub(channel, thirdparty::ThirdPartyService::STUB_OWNS_CHANNEL); +} + +MemcachedConnectPool::Proxy::~Proxy() { + delete _stub; +} + +void MemcachedConnectPool::Proxy::callDoneHandle(::google::protobuf::Message *request, corpc::Controller *controller) { + delete controller; + delete request; +} + +memcached_st* MemcachedConnectPool::Proxy::take() { + Void *request = new Void(); + thirdparty::TakeResponse *response = new thirdparty::TakeResponse(); + Controller *controller = new Controller(); + + _stub->take(controller, request, response, NULL); + + if (controller->Failed()) { + fprintf(stderr, "Rpc Call Failed : %s\n", controller->ErrorText().c_str()); + return NULL; + } + + memcached_st* memc = (memcached_st*)response->handle(); + + delete controller; + delete response; + delete request; + + return memc; +} + +void MemcachedConnectPool::Proxy::put(memcached_st* memc, bool error) { + thirdparty::PutRequest *request = new thirdparty::PutRequest(); + Controller *controller = new Controller(); + + request->set_handle((intptr_t)memc); + if (error) { + request->set_error(error); + } + + _stub->put(controller, request, NULL, google::protobuf::NewCallback<::google::protobuf::Message *>(&callDoneHandle, request, controller)); +} + +MemcachedConnectPool::MemcachedConnectPool(memcached_server_st *memcServers, uint32_t maxConnectNum, uint32_t maxIdleNum): _memcServers(memcServers), _maxConnectNum(maxConnectNum), _maxIdleNum(maxIdleNum), _realConnectCount(0) { + +} + +void MemcachedConnectPool::take(::google::protobuf::RpcController* controller, + const Void* request, + thirdparty::TakeResponse* response, + ::google::protobuf::Closure* done) { + if (_idleList.size() > 0) { + intptr_t handle = (intptr_t)_idleList.front(); + _idleList.pop_front(); + + response->set_handle(handle); + } else if (_realConnectCount < _maxConnectNum) { + // 建立新连接 + memcached_st *memc = memcached_create(NULL); + memcached_return rc = memcached_server_push(memc, _memcServers); + + if (rc == MEMCACHED_SUCCESS) { + response->set_handle((intptr_t)memc); + _realConnectCount++; + } else { + fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(memc, rc)); + memcached_free(memc); + // MYSQL对象创建失败 + controller->SetFailed("Couldn't add memcached server"); + } + } else { + // 等待空闲连接 + _waitingList.push_back(co_self()); + co_yield_ct(); + + if (_idleList.size() == 0) { + controller->SetFailed("can't connect to memcached server"); + } else { + intptr_t handle = (intptr_t)_idleList.front(); + _idleList.pop_front(); + + response->set_handle(handle); + } + } +} + +void MemcachedConnectPool::put(::google::protobuf::RpcController* controller, + const thirdparty::PutRequest* request, + Void* response, + ::google::protobuf::Closure* done) { + memcached_st *memc = (memcached_st *)request->handle(); + + if (_idleList.size() < _maxIdleNum) { + if (request->error()) { + _realConnectCount--; + memcached_free(memc); + + // 若有等待协程,尝试重连 + if (_waitingList.size() > 0) { + assert(_idleList.size() == 0); + + if (_realConnectCount < _maxConnectNum) { + memcached_st *memc = memcached_create(NULL); + memcached_return rc = memcached_server_push(memc, _memcServers); + + if (rc == MEMCACHED_SUCCESS) { + _realConnectCount++; + _idleList.push_back(memc); + + stCoRoutine_t *co = _waitingList.front(); + _waitingList.pop_front(); + + co_resume(co); + } else { + memcached_free(memc); + + // 唤醒当前所有等待协程 + while (!_waitingList.empty()) { + stCoRoutine_t *co = _waitingList.front(); + _waitingList.pop_front(); + + co_resume(co); + } + } + } + } + } else { + _idleList.push_back(memc); + + if (_waitingList.size() > 0) { + assert(_idleList.size() == 1); + stCoRoutine_t *co = _waitingList.front(); + _waitingList.pop_front(); + + co_resume(co); + } + } + } else { + assert(_waitingList.size() == 0); + _realConnectCount--; + memcached_free(memc); + } +} + +MemcachedConnectPool* MemcachedConnectPool::create(memcached_server_st *memcServers, uint32_t maxConnectNum, uint32_t maxIdleNum) { + MemcachedConnectPool *pool = new MemcachedConnectPool(memcServers, maxConnectNum, maxIdleNum); + pool->init(); + + return pool; +} + +void MemcachedConnectPool::init() { + _server = InnerRpcServer::create(); + _server->registerService(this); +} + +MemcachedConnectPool::Proxy* MemcachedConnectPool::getProxy() { + pid_t pid = GetPid(); + + auto iter = _threadProxyMap.find(pid); + if (iter != _threadProxyMap.end()) { + return iter->second; + } + + // 为当前线程创建proxy + Proxy *proxy = new Proxy(this); + _threadProxyMap.insert(std::make_pair(pid, proxy)); + + return proxy; +} diff --git a/corpc_memcached/corpc_memcached.h b/corpc_memcached/corpc_memcached.h new file mode 100644 index 0000000..bc55519 --- /dev/null +++ b/corpc_memcached/corpc_memcached.h @@ -0,0 +1,84 @@ +/* + * Created by Xianke Liu on 2018/5/14. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 corpc_memcached_h +#define corpc_memcached_h + +#include "corpc_routine_env.h" +#include "corpc_controller.h" +#include "corpc_inner_rpc.h" + +#include "corpc_thirdparty.pb.h" + +#include + +using namespace corpc; + +class MemcachedConnectPool : public thirdparty::ThirdPartyService { +public: + class Proxy { + public: + memcached_st* take(); + void put(memcached_st* memc, bool error); + + private: + Proxy(MemcachedConnectPool *pool); + ~Proxy(); + + static void callDoneHandle(::google::protobuf::Message *request, corpc::Controller *controller); + + private: + thirdparty::ThirdPartyService::Stub *_stub; + + public: + friend class MemcachedConnectPool; + }; + +public: + virtual void take(::google::protobuf::RpcController* controller, + const Void* request, + thirdparty::TakeResponse* response, + ::google::protobuf::Closure* done); + virtual void put(::google::protobuf::RpcController* controller, + const thirdparty::PutRequest* request, + Void* response, + ::google::protobuf::Closure* done); + +public: + static MemcachedConnectPool* create(memcached_server_st *memcServers, uint32_t maxConnectNum, uint32_t maxIdleNum); + + Proxy* getProxy(); + +private: + MemcachedConnectPool(memcached_server_st *memcServers, uint32_t maxConnectNum, uint32_t maxIdleNum); + ~MemcachedConnectPool() {} + + void init(); +private: + memcached_server_st *_memcServers; // memcached服务器列表 + + uint32_t _maxConnectNum; // 与mysql数据库最多建立的连接数 + uint32_t _maxIdleNum; // 最大空闲连接数量 + uint32_t _realConnectCount; // 当前实际建立连接的数量 + + std::list _idleList; // 空闲连接表 + std::list _waitingList; // 等待队列:当连接数量达到最大时,新的请求需要等待 + + InnerRpcServer *_server; + std::map _threadProxyMap; // 线程相关代理 +}; + +#endif /* corpc_memcached_h */ diff --git a/corpc_memcached/corpc_memcached.xcodeproj/project.pbxproj b/corpc_memcached/corpc_memcached.xcodeproj/project.pbxproj new file mode 100644 index 0000000..f33ceab --- /dev/null +++ b/corpc_memcached/corpc_memcached.xcodeproj/project.pbxproj @@ -0,0 +1,318 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + 2DE7AA0620AA7B0800C16383 /* libco.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DE7AA0820AA7B0800C16383 /* libco.dylib */; }; + 2DE7AA0720AA7B0800C16383 /* libcorpc.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DE7AA0920AA7B0800C16383 /* libcorpc.dylib */; }; + 2DEA9AA920A992090095C62F /* corpc_memcached.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2DEA9AA720A992090095C62F /* corpc_memcached.cpp */; }; + 2DEA9AAA20A992090095C62F /* corpc_memcached.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA9AA820A992090095C62F /* corpc_memcached.h */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 2DE7AA0820AA7B0800C16383 /* libco.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libco.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DE7AA0920AA7B0800C16383 /* libcorpc.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libcorpc.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DEA9A9F20A991AA0095C62F /* libcorpc_memcached.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libcorpc_memcached.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DEA9AA720A992090095C62F /* corpc_memcached.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = corpc_memcached.cpp; sourceTree = ""; }; + 2DEA9AA820A992090095C62F /* corpc_memcached.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = corpc_memcached.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 2DEA9A9C20A991AA0095C62F /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DE7AA0620AA7B0800C16383 /* libco.dylib in Frameworks */, + 2DE7AA0720AA7B0800C16383 /* libcorpc.dylib in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 2DE7AA0520AA7B0800C16383 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 2DE7AA0820AA7B0800C16383 /* libco.dylib */, + 2DE7AA0920AA7B0800C16383 /* libcorpc.dylib */, + ); + name = Frameworks; + sourceTree = ""; + }; + 2DEA9A9620A991AA0095C62F = { + isa = PBXGroup; + children = ( + 2DEA9AA620A991CD0095C62F /* src */, + 2DEA9AA020A991AA0095C62F /* Products */, + 2DE7AA0520AA7B0800C16383 /* Frameworks */, + ); + sourceTree = ""; + }; + 2DEA9AA020A991AA0095C62F /* Products */ = { + isa = PBXGroup; + children = ( + 2DEA9A9F20A991AA0095C62F /* libcorpc_memcached.dylib */, + ); + name = Products; + sourceTree = ""; + }; + 2DEA9AA620A991CD0095C62F /* src */ = { + isa = PBXGroup; + children = ( + 2DEA9AA720A992090095C62F /* corpc_memcached.cpp */, + 2DEA9AA820A992090095C62F /* corpc_memcached.h */, + ); + name = src; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 2DEA9A9D20A991AA0095C62F /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DEA9AAA20A992090095C62F /* corpc_memcached.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 2DEA9A9E20A991AA0095C62F /* corpc_memcached */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2DEA9AA320A991AA0095C62F /* Build configuration list for PBXNativeTarget "corpc_memcached" */; + buildPhases = ( + 2DEA9A9B20A991AA0095C62F /* Sources */, + 2DEA9A9C20A991AA0095C62F /* Frameworks */, + 2DEA9A9D20A991AA0095C62F /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = corpc_memcached; + productName = corpc_memcached; + productReference = 2DEA9A9F20A991AA0095C62F /* libcorpc_memcached.dylib */; + productType = "com.apple.product-type.library.dynamic"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 2DEA9A9720A991AA0095C62F /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0920; + ORGANIZATIONNAME = Dena; + TargetAttributes = { + 2DEA9A9E20A991AA0095C62F = { + CreatedOnToolsVersion = 9.2; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = 2DEA9A9A20A991AA0095C62F /* Build configuration list for PBXProject "corpc_memcached" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 2DEA9A9620A991AA0095C62F; + productRefGroup = 2DEA9AA020A991AA0095C62F /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 2DEA9A9E20A991AA0095C62F /* corpc_memcached */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 2DEA9A9B20A991AA0095C62F /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DEA9AA920A992090095C62F /* corpc_memcached.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 2DEA9AA120A991AA0095C62F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 2DEA9AA220A991AA0095C62F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 2DEA9AA420A991AA0095C62F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + EXECUTABLE_PREFIX = lib; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + ../co, + ../corpc, + ../proto, + ); + LIBRARY_SEARCH_PATHS = /usr/local/lib; + OTHER_LDFLAGS = ( + "-lprotobuf", + "-lmemcached", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 2DEA9AA520A991AA0095C62F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + EXECUTABLE_PREFIX = lib; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + ../co, + ../corpc, + ../proto, + ); + LIBRARY_SEARCH_PATHS = /usr/local/lib; + OTHER_LDFLAGS = ( + "-lprotobuf", + "-lmemcached", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 2DEA9A9A20A991AA0095C62F /* Build configuration list for PBXProject "corpc_memcached" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2DEA9AA120A991AA0095C62F /* Debug */, + 2DEA9AA220A991AA0095C62F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 2DEA9AA320A991AA0095C62F /* Build configuration list for PBXNativeTarget "corpc_memcached" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2DEA9AA420A991AA0095C62F /* Debug */, + 2DEA9AA520A991AA0095C62F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 2DEA9A9720A991AA0095C62F /* Project object */; +} diff --git a/corpc_mysql/Makefile b/corpc_mysql/Makefile index fc14a7b..76e7b56 100644 --- a/corpc_mysql/Makefile +++ b/corpc_mysql/Makefile @@ -22,7 +22,7 @@ DIR_OBJ = ./${v}/obj/ DIR_STATICLIB = ./lib/ DIR_DYNAMICLIB = ./solib/ -OBJS = corpc_thirdparty.pb.o corpc_mysql.o +OBJS = corpc_mysql.o OBJS_WITH_PATH = ${foreach n,${OBJS},${DIR_OBJ}${n}} empty = diff --git a/corpc_mysql/corpc_mysql.cpp b/corpc_mysql/corpc_mysql.cpp index 2717070..6591c33 100644 --- a/corpc_mysql/corpc_mysql.cpp +++ b/corpc_mysql/corpc_mysql.cpp @@ -1,10 +1,18 @@ -// -// corpc_mysql.cpp -// corpc_mysql -// -// Created by Xianke Liu on 2018/5/8. -//Copyright © 2018年 Dena. All rights reserved. -// +/* + * Created by Xianke Liu on 2018/5/8. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 "corpc_mysql.h" @@ -90,7 +98,6 @@ void MysqlConnectPool::take(::google::protobuf::RpcController* controller, // MYSQL对象创建失败 controller->SetFailed("can't create MYSQL object probably because not enough memory"); } - } else { // 等待空闲连接 _waitingList.push_back(co_self()); diff --git a/corpc_mysql/corpc_mysql.h b/corpc_mysql/corpc_mysql.h index e52b954..18a41e7 100644 --- a/corpc_mysql/corpc_mysql.h +++ b/corpc_mysql/corpc_mysql.h @@ -1,10 +1,18 @@ -// -// corpc_mysql.h -// corpc_mysql -// -// Created by Xianke Liu on 2018/5/8. -//Copyright © 2018年 Dena. All rights reserved. -// +/* + * Created by Xianke Liu on 2018/5/8. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 corpc_mysql_h #define corpc_mysql_h diff --git a/corpc_mysql/corpc_mysql.xcodeproj/project.pbxproj b/corpc_mysql/corpc_mysql.xcodeproj/project.pbxproj index aa774a3..ee51422 100644 --- a/corpc_mysql/corpc_mysql.xcodeproj/project.pbxproj +++ b/corpc_mysql/corpc_mysql.xcodeproj/project.pbxproj @@ -11,8 +11,6 @@ 2DEA9A5D20A143140095C62F /* libcorpc.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DEA9A5F20A143140095C62F /* libcorpc.dylib */; }; 2DEA9A6320A145A60095C62F /* corpc_mysql.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2DEA9A6120A145A60095C62F /* corpc_mysql.cpp */; }; 2DEA9A6420A145A60095C62F /* corpc_mysql.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA9A6220A145A60095C62F /* corpc_mysql.h */; }; - 2DEA9A9020A2EA9A0095C62F /* corpc_thirdparty.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 2DEA9A8E20A2EA9A0095C62F /* corpc_thirdparty.pb.cc */; }; - 2DEA9A9120A2EA9A0095C62F /* corpc_thirdparty.pb.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DEA9A8F20A2EA9A0095C62F /* corpc_thirdparty.pb.h */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -21,8 +19,6 @@ 2DEA9A5F20A143140095C62F /* libcorpc.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libcorpc.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; 2DEA9A6120A145A60095C62F /* corpc_mysql.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = corpc_mysql.cpp; sourceTree = ""; }; 2DEA9A6220A145A60095C62F /* corpc_mysql.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = corpc_mysql.h; sourceTree = ""; }; - 2DEA9A8E20A2EA9A0095C62F /* corpc_thirdparty.pb.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = corpc_thirdparty.pb.cc; path = ../proto/corpc_thirdparty.pb.cc; sourceTree = ""; }; - 2DEA9A8F20A2EA9A0095C62F /* corpc_thirdparty.pb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = corpc_thirdparty.pb.h; path = ../proto/corpc_thirdparty.pb.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -67,8 +63,6 @@ 2DEA9A6020A145670095C62F /* src */ = { isa = PBXGroup; children = ( - 2DEA9A8E20A2EA9A0095C62F /* corpc_thirdparty.pb.cc */, - 2DEA9A8F20A2EA9A0095C62F /* corpc_thirdparty.pb.h */, 2DEA9A6120A145A60095C62F /* corpc_mysql.cpp */, 2DEA9A6220A145A60095C62F /* corpc_mysql.h */, ); @@ -82,7 +76,6 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - 2DEA9A9120A2EA9A0095C62F /* corpc_thirdparty.pb.h in Headers */, 2DEA9A6420A145A60095C62F /* corpc_mysql.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -145,7 +138,6 @@ buildActionMask = 2147483647; files = ( 2DEA9A6320A145A60095C62F /* corpc_mysql.cpp in Sources */, - 2DEA9A9020A2EA9A0095C62F /* corpc_thirdparty.pb.cc in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -267,6 +259,7 @@ /usr/local/include, ../co, ../corpc, + ../proto, ); LIBRARY_SEARCH_PATHS = ( /usr/local/mysql/lib, @@ -292,6 +285,7 @@ /usr/local/include, ../co, ../corpc, + ../proto, ); LIBRARY_SEARCH_PATHS = ( /usr/local/mysql/lib, diff --git a/example/example_echoTcp/example_echoTcp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist b/example/example_echoTcp/example_echoTcp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist index ea3f21a..c6a80ce 100644 --- a/example/example_echoTcp/example_echoTcp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/example/example_echoTcp/example_echoTcp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ echoTcpclt.xcscheme orderHint - 8 + 9 echoTcpsvr.xcscheme orderHint - 7 + 8 SuppressBuildableAutocreation diff --git a/example/example_echoUdp/example_echoUdp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist b/example/example_echoUdp/example_echoUdp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist index 18918c5..2ea0d35 100644 --- a/example/example_echoUdp/example_echoUdp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/example/example_echoUdp/example_echoUdp.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ echoUdpclt.xcscheme orderHint - 10 + 11 echoUdpsvr.xcscheme orderHint - 9 + 10 SuppressBuildableAutocreation diff --git a/example/example_innerRpc/example_innerRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist b/example/example_innerRpc/example_innerRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist index ca56365..d88b80b 100644 --- a/example/example_innerRpc/example_innerRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/example/example_innerRpc/example_innerRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ innerRpc.xcscheme orderHint - 6 + 7 SuppressBuildableAutocreation diff --git a/example/example_interRpc/example_interRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist b/example/example_interRpc/example_interRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist index c4e3c7a..b7471a4 100644 --- a/example/example_interRpc/example_interRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/example/example_interRpc/example_interRpc.xcodeproj/xcuserdata/xianke.liu.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ interRpcClt.xcscheme orderHint - 5 + 6 interRpcSvr.xcscheme orderHint - 4 + 5 SuppressBuildableAutocreation diff --git a/example/example_memcached/example_memcached.xcodeproj/project.pbxproj b/example/example_memcached/example_memcached.xcodeproj/project.pbxproj new file mode 100644 index 0000000..1a84475 --- /dev/null +++ b/example/example_memcached/example_memcached.xcodeproj/project.pbxproj @@ -0,0 +1,316 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + 2DE7AA1F20AA7C2A00C16383 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2DE7AA1E20AA7C2A00C16383 /* main.cpp */; }; + 2DE7AA2120AA800C00C16383 /* libco.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DE7AA2420AA800C00C16383 /* libco.dylib */; }; + 2DE7AA2220AA800C00C16383 /* libcorpc_memcached.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DE7AA2520AA800C00C16383 /* libcorpc_memcached.dylib */; }; + 2DE7AA2320AA800C00C16383 /* libcorpc.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DE7AA2620AA800C00C16383 /* libcorpc.dylib */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 2DE7AA1120AA7BCC00C16383 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 2DE7AA1320AA7BCC00C16383 /* example_memcached */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = example_memcached; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DE7AA1E20AA7C2A00C16383 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; + 2DE7AA2420AA800C00C16383 /* libco.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libco.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DE7AA2520AA800C00C16383 /* libcorpc_memcached.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libcorpc_memcached.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; + 2DE7AA2620AA800C00C16383 /* libcorpc.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libcorpc.dylib; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 2DE7AA1020AA7BCC00C16383 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DE7AA2120AA800C00C16383 /* libco.dylib in Frameworks */, + 2DE7AA2320AA800C00C16383 /* libcorpc.dylib in Frameworks */, + 2DE7AA2220AA800C00C16383 /* libcorpc_memcached.dylib in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 2DE7AA0A20AA7BCC00C16383 = { + isa = PBXGroup; + children = ( + 2DE7AA1D20AA7BFB00C16383 /* src */, + 2DE7AA1420AA7BCC00C16383 /* Products */, + 2DE7AA2020AA800C00C16383 /* Frameworks */, + ); + sourceTree = ""; + }; + 2DE7AA1420AA7BCC00C16383 /* Products */ = { + isa = PBXGroup; + children = ( + 2DE7AA1320AA7BCC00C16383 /* example_memcached */, + ); + name = Products; + sourceTree = ""; + }; + 2DE7AA1D20AA7BFB00C16383 /* src */ = { + isa = PBXGroup; + children = ( + 2DE7AA1E20AA7C2A00C16383 /* main.cpp */, + ); + name = src; + sourceTree = ""; + }; + 2DE7AA2020AA800C00C16383 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 2DE7AA2420AA800C00C16383 /* libco.dylib */, + 2DE7AA2520AA800C00C16383 /* libcorpc_memcached.dylib */, + 2DE7AA2620AA800C00C16383 /* libcorpc.dylib */, + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 2DE7AA1220AA7BCC00C16383 /* example_memcached */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2DE7AA1A20AA7BCC00C16383 /* Build configuration list for PBXNativeTarget "example_memcached" */; + buildPhases = ( + 2DE7AA0F20AA7BCC00C16383 /* Sources */, + 2DE7AA1020AA7BCC00C16383 /* Frameworks */, + 2DE7AA1120AA7BCC00C16383 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = example_memcached; + productName = example_memcached; + productReference = 2DE7AA1320AA7BCC00C16383 /* example_memcached */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 2DE7AA0B20AA7BCC00C16383 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0920; + ORGANIZATIONNAME = Dena; + TargetAttributes = { + 2DE7AA1220AA7BCC00C16383 = { + CreatedOnToolsVersion = 9.2; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = 2DE7AA0E20AA7BCC00C16383 /* Build configuration list for PBXProject "example_memcached" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 2DE7AA0A20AA7BCC00C16383; + productRefGroup = 2DE7AA1420AA7BCC00C16383 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 2DE7AA1220AA7BCC00C16383 /* example_memcached */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 2DE7AA0F20AA7BCC00C16383 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2DE7AA1F20AA7C2A00C16383 /* main.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 2DE7AA1820AA7BCC00C16383 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 2DE7AA1920AA7BCC00C16383 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 2DE7AA1B20AA7BCC00C16383 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + ../../co, + ../../corpc, + ../../proto, + ../../corpc_memcached, + ); + LIBRARY_SEARCH_PATHS = /usr/local/lib; + OTHER_LDFLAGS = ( + "-lprotobuf", + "-lmemcached", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 2DE7AA1C20AA7BCC00C16383 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + HEADER_SEARCH_PATHS = ( + /usr/local/include, + ../../co, + ../../corpc, + ../../proto, + ../../corpc_memcached, + ); + LIBRARY_SEARCH_PATHS = /usr/local/lib; + OTHER_LDFLAGS = ( + "-lprotobuf", + "-lmemcached", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 2DE7AA0E20AA7BCC00C16383 /* Build configuration list for PBXProject "example_memcached" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2DE7AA1820AA7BCC00C16383 /* Debug */, + 2DE7AA1920AA7BCC00C16383 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 2DE7AA1A20AA7BCC00C16383 /* Build configuration list for PBXNativeTarget "example_memcached" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2DE7AA1B20AA7BCC00C16383 /* Debug */, + 2DE7AA1C20AA7BCC00C16383 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 2DE7AA0B20AA7BCC00C16383 /* Project object */; +} diff --git a/example/example_memcached/main.cpp b/example/example_memcached/main.cpp new file mode 100644 index 0000000..0652299 --- /dev/null +++ b/example/example_memcached/main.cpp @@ -0,0 +1,154 @@ +// +// main.cpp +// example_mysql +// +// Created by Xianke Liu on 2018/5/15. +// Copyright © 2018年 Dena. All rights reserved. +// + +#include "corpc_memcached.h" +#include "corpc_utils.h" + +#include + +using namespace corpc; + +static std::atomic g_cnt(0); + +static void *log_routine( void *arg ) +{ + co_enable_hook_sys(); + + int total = 0; + int average = 0; + + time_t startAt = time(NULL); + + while (true) { + sleep(1); + + total += g_cnt; + + if (total == 0) { + startAt = time(NULL); + continue; + } + + time_t now = time(NULL); + + time_t difTime = now - startAt; + if (difTime > 0) { + average = total / difTime; + } else { + average = total; + } + + printf("time %ld seconds, cnt: %d, average: %d, total: %d\n", difTime, int(g_cnt), average, total); + + g_cnt = 0; + } + + return NULL; +} + +static void *memc_routine( void *arg ) +{ + co_enable_hook_sys(); + + MemcachedConnectPool *memcPool = (MemcachedConnectPool*)arg; + MemcachedConnectPool::Proxy* proxy = memcPool->getProxy(); + + memcached_return rc; + char *key = "mykey"; + char *value = "myvalue"; + size_t value_length; + uint32_t flags; + + while (1) + { + // 获取连接 + memcached_st *memc = proxy->take(); + + if (!memc) { + fprintf(stderr, "cant take memcached handle\n"); + return NULL; + } + + rc = memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0); + + if (rc == MEMCACHED_SUCCESS) { + //fprintf(stderr, "Key stored successfully\n"); + } else { + fprintf(stderr, "Couldn't store key: %s\n", memcached_strerror(memc, rc)); + proxy->put(memc, true); + return NULL; + } + + char *retrieved_value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc); + + if (rc == MEMCACHED_SUCCESS) { + //printf("The key '%s' returned value '%s'.\n", key, retrieved_value); + free(retrieved_value); + } + else { + fprintf(stderr, "Couldn't retrieve key: %s\n", memcached_strerror(memc, rc)); + proxy->put(memc, true); + return NULL; + } + + g_cnt++; + + // 归还连接 + proxy->put(memc, false); + } + + return NULL; +} + +void *timerTask(void * arg) { + co_enable_hook_sys(); + + while (1) { + sleep(1); + + printf("======================\n"); + } + + return NULL; +} + +void clientThread(MemcachedConnectPool *memcPool) { + // 开多个个协程 + for (int i=0; i<2; i++) { + RoutineEnvironment::startCoroutine(memc_routine, memcPool); + } + + printf("running...\n"); + + corpc::RoutineEnvironment::startCoroutine(timerTask, NULL); + //corpc::RoutineEnvironment::startCoroutine(log_routine, NULL); + + RoutineEnvironment::runEventLoop(100); +} + + +int main(int argc, const char * argv[]) { + co_start_hook(); + + memcached_return rc; + memcached_server_st *servers = NULL; + servers = memcached_server_list_append(servers, "localhost", 11211, &rc); + + MemcachedConnectPool *memcPool = MemcachedConnectPool::create(servers, 4, 2); + + // 开两个线程进行多线程访问 + std::thread t1 = std::thread(clientThread, memcPool); + std::thread t2 = std::thread(clientThread, memcPool); + + corpc::RoutineEnvironment::startCoroutine(log_routine, NULL); + + RoutineEnvironment::runEventLoop(); + + return 0; +} + diff --git a/example/example_mysql/main.cpp b/example/example_mysql/main.cpp index 1e81cd1..5367e9b 100644 --- a/example/example_mysql/main.cpp +++ b/example/example_mysql/main.cpp @@ -99,12 +99,13 @@ static void *mysql_routine( void *arg ) return NULL; } - g_cnt++; //printf("data length %lu \n", lengths[0]); } mysql_free_result(result); + g_cnt++; + // 归还连接 proxy->put(con, false); } diff --git a/libcorpc_mac.xcworkspace/contents.xcworkspacedata b/libcorpc.xcworkspace/contents.xcworkspacedata similarity index 72% rename from libcorpc_mac.xcworkspace/contents.xcworkspacedata rename to libcorpc.xcworkspace/contents.xcworkspacedata index e71c7f8..aee459b 100644 --- a/libcorpc_mac.xcworkspace/contents.xcworkspacedata +++ b/libcorpc.xcworkspace/contents.xcworkspacedata @@ -2,16 +2,16 @@ + location = "group:co/co.xcodeproj"> + location = "group:corpc/corpc.xcodeproj"> + location = "group:corpc_mysql/corpc_mysql.xcodeproj"> + location = "group:corpc_memcached/corpc_memcached.xcodeproj"> @@ -25,6 +25,12 @@ + + + + @@ -40,22 +46,4 @@ - - - - - - - - - - - - diff --git a/libcorpc_mac.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/libcorpc.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings similarity index 100% rename from libcorpc_mac.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings rename to libcorpc.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings