int pyllbc_Service::Post(PyObject *callable) { if (!PyCallable_Check(callable)) { const LLBC_String objDesc = pyllbc_ObjUtil::GetObjStr(callable); pyllbc_SetError(LLBC_String().format("frame callable object not callable: %s", objDesc.c_str())); return LLBC_RTN_FAILED; } if (_handlingBeforeFrameCallables && _handlingAfterFrameCallables) { pyllbc_SetError("could not push callable object to service, internal error!"); return LLBC_RTN_FAILED; } if (_beforeFrameCallables.find(callable) != _beforeFrameCallables.end() || _afterFrameCallables.find(callable) != _afterFrameCallables.end()) { const LLBC_String objDesc = pyllbc_ObjUtil::GetObjStr(callable); pyllbc_SetError(LLBC_String().format( "repeat to add callable to service, callable: %s", objDesc.c_str())); return LLBC_RTN_FAILED; } Py_INCREF(callable); if (_handlingBeforeFrameCallables) { _afterFrameCallables.insert(callable); } else { if (!_handledBeforeFrameCallables) { _beforeFrameCallables.insert(callable); } else { if (!_handlingAfterFrameCallables) _afterFrameCallables.insert(callable); else _beforeFrameCallables.insert(callable); } } return LLBC_RTN_OK; }
LLBC_String LLBC_ExtensionName(const LLBC_String &path) { LLBC_String basename = LLBC_BaseName(path); if (UNLIKELY(basename.empty())) { return LLBC_String(); } LLBC_String::size_type pos = basename.rfind("."); if (pos == LLBC_String::npos) { return LLBC_String(); } return basename.substr(pos + 1); }
LLBC_String LLBC_DirName(const LLBC_String &path) { if (UNLIKELY(path.empty())) { return LLBC_String(); } #if LLBC_TARGET_PLATFORM_NON_WIN32 char *buf = reinterpret_cast<char *>(::malloc(path.size() + 1)); ::memcpy(buf, path.data(), path.size()); buf[path.size()] = '\0'; ::dirname(buf); LLBC_String dirName = buf; ::free(buf); return dirName; #else if (path[path.length() - 1] == ':') { return path; } LLBC_String::size_type slashPos = path.rfind(LLBC_SLASH_A); LLBC_String::size_type backlashPos = path.rfind(LLBC_BACKLASH_A); if (slashPos == LLBC_String::npos) { if (backlashPos == LLBC_String::npos) { return LLBC_String(); } return path.substr(0, backlashPos); } else { if (backlashPos == LLBC_String::npos) { return path.substr(0, slashPos); } } return path.substr(0, MAX(slashPos, backlashPos)); #endif }
LLBC_String LLBC_TrimLeft(const LLBC_String &str, char target) { if (UNLIKELY(str.empty())) { return LLBC_String(); } const LLBC_String::size_type length = str.size(); register LLBC_String::size_type leftPos = 0; for (; str[leftPos] == target && leftPos < length; leftPos ++); if (leftPos >= length) { return LLBC_String(); } return str.substr(leftPos, LLBC_String::npos); }
LLBC_String LLBC_Trim(const LLBC_String &str, const char *targets) { if (UNLIKELY(str.empty())) { return LLBC_String(); } return LLBC_TrimRight(LLBC_TrimLeft(str, targets), targets); }
LLBC_String LLBC_Trim(const LLBC_String &str) { if (UNLIKELY(str.empty())) { return LLBC_String(); } return LLBC_TrimRight(LLBC_TrimLeft(str)); }
LLBC_String LLBC_TrimRight(const LLBC_String &str, char target) { if (UNLIKELY(str.empty())) { return LLBC_String(); } const LLBC_String::size_type length = str.size(); register LLBC_String::size_type rightPos = length - 1; for (; str[rightPos] == target && rightPos != 0; rightPos --); return str.substr(0, rightPos + 1); }
LLBC_String LLBC_BaseName(const LLBC_String &path, bool incExtension) { if (UNLIKELY(path.empty())) { return LLBC_String(); } LLBC_String baseName; #if LLBC_TARGET_PLATFORM_NON_WIN32 baseName = ::basename(const_cast<char *>(path.c_str())); #else LLBC_String::size_type slashPos = path.rfind(LLBC_SLASH_A); LLBC_String::size_type backlashPos = path.rfind(LLBC_BACKLASH_A); if (slashPos == LLBC_String::npos) { if (backlashPos == LLBC_String::npos) { baseName = path; } else { baseName = path.substr(backlashPos + 1); } } else { if (backlashPos == LLBC_String::npos) { baseName = path.substr(slashPos + 1); } else { baseName = path.substr(MAX(slashPos, backlashPos) + 1); } } #endif if (!incExtension) { LLBC_String::size_type dotPos = baseName.rfind('.'); if (dotPos != LLBC_String::npos && dotPos != 0) { baseName.erase(dotPos); } } return baseName; }
int pyllbc_Service::Subscribe(int opcode, PyObject *handler, int flags) { if (_started) { pyllbc_SetError("service already started", LLBC_ERROR_INITED); return LLBC_RTN_FAILED; } else if (_llbcSvcType == LLBC_IService::Raw && opcode != 0) { pyllbc_SetError(LLBC_String().format( "RAW type service could not subscribe opcode[%d] != 0's packet", opcode), LLBC_ERROR_INVALID); return LLBC_RTN_FAILED; } _PacketHandlers::const_iterator it = _handlers.find(opcode); if (it != _handlers.end()) { const LLBC_String handlerDesc = pyllbc_ObjUtil::GetObjStr(handler); LLBC_String err; err.append_format("repeat to subscribeopcode: %d:%s, ", opcode, handlerDesc.c_str()); err.append_format("the opcode already subscribed by "); err.append_format("%s", it->second->ToString().c_str()); pyllbc_SetError(err, LLBC_ERROR_REPEAT); return LLBC_RTN_FAILED; } pyllbc_PacketHandler *wrapHandler = LLBC_New1(pyllbc_PacketHandler, opcode); if (wrapHandler->SetHandler(handler) != LLBC_RTN_OK) { LLBC_Delete(wrapHandler); return LLBC_RTN_FAILED; } _handlers.insert(std::make_pair(opcode, wrapHandler)); _llbcSvc->Subscribe(opcode, _cppFacade, &pyllbc_Facade::OnDataReceived); return LLBC_RTN_OK; }
void pyllbc_Service::HandleFrameCallables(pyllbc_Service::_FrameCallables &callables, bool &usingFlag) { usingFlag = true; for (_FrameCallables::iterator it = callables.begin(); it != callables.end(); it++) { PyObject *callable = *it; PyObject *ret = PyObject_CallFunctionObjArgs(callable, _pySvc, NULL); if (ret) { Py_DECREF(ret); } else { const LLBC_String objDesc = pyllbc_ObjUtil::GetObjStr(callable); pyllbc_TransferPyError(LLBC_String().format("When call frame-callable: %s", objDesc.c_str())); break; } } this->DestroyFrameCallables(callables, usingFlag); }
int TestCase_Comm_Svc::Run(int argc, char *argv[]) { LLBC_PrintLine("Server/Client test:"); if (argc < 5) { LLBC_PrintLine("argument error, eg: ./a [client/server] [normal/raw] ip port"); return -1; } // Parse arguments. const char *ip = argv[3]; const int port = LLBC_Str2Int32(argv[4]); const bool asClient = LLBC_String(argv[1]) == "client" ? true : false; LLBC_IService::Type svcType = LLBC_String(argv[2]) == "normal" ? LLBC_IService::Normal : LLBC_IService::Raw; LLBC_PrintLine("Will start %s type service, service type: %s", asClient ? "CLIENT" : "SERVER", svcType == LLBC_IService::Normal ? "Normal" : "Raw"); // Create service LLBC_IService *svc = LLBC_IService::Create(svcType); TestFacade *facade = LLBC_New(TestFacade); svc->RegisterFacade(facade); svc->Subscribe(OPCODE, facade, &TestFacade::OnDataArrival); svc->SuppressCoderNotFoundWarning(); svc->Start(2); // Connect to server / Create listen session to wait client connect. int sessionId; if (!asClient) { LLBC_PrintLine("Will listening in %s:%d", ip, port); if ((sessionId = svc->Listen(ip, port)) == 0) { LLBC_PrintLine("Create session failed, reason: %s", LLBC_FormatLastError()); LLBC_Delete(svc); return -1; } } else { // Client service, we create some clients to test service. int clientCount; const int pollerType = LLBC_PollerType::Str2Type(LLBC_CFG_COMM_POLLER_MODEL); if (pollerType == LLBC_PollerType::SelectPoller) clientCount = 50; else clientCount = 1024; LLBC_PrintLine("Create %d clients to test", clientCount); for (int i = 0; i < clientCount; i++) { const int sessionId = svc->Connect(ip, port); const int dataSize = 4096; char *data = LLBC_Malloc(char, dataSize); ::memset(data, 1, dataSize); LLBC_Packet *packet = LLBC_New(LLBC_Packet); packet->SetHeader(sessionId, OPCODE, 0); packet->Write(data, dataSize); LLBC_Free(data); svc->Send(packet); // Test unhandled packet(unsubscribe opcode). LLBC_Packet *unhandledPacket = LLBC_New(LLBC_Packet); unhandledPacket->SetHeader(sessionId, OPCODE + 10000, 0); unhandledPacket->Write("Hello World", 12); svc->Send(unhandledPacket); } } LLBC_PrintLine("Press any key to continue..."); getchar(); return 0; }
LLBC_String LLBC_Directory::HomeDir() { #if LLBC_TARGET_PLATFORM_WIN32 size_t requiredSize = 0; if (getenv_s(&requiredSize, NULL, 0, "HOMEPATH") != 0) { LLBC_SetLastError(LLBC_ERROR_CLIB); return LLBC_String(); } else if (requiredSize == 0) { LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); return LLBC_String(); } char *envVal = LLBC_Malloc(char, requiredSize); if (getenv_s(&requiredSize, envVal, requiredSize, "HOMEPATH") != 0) { LLBC_Free(envVal); LLBC_SetLastError(LLBC_ERROR_CLIB); return LLBC_String(); } LLBC_String path(envVal); LLBC_Free(envVal); #else // Non-Win32 char *envVal = getenv("HOME"); if (!envVal) { LLBC_SetLastError(LLBC_ERROR_NOT_FOUND); return ""; } const LLBC_String path(envVal); #endif // Win32 #if LLBC_TARGET_PLATFORM_WIN32 const size_t pathLen = path.length(); if (path[pathLen - 1] == LLBC_SLASH_A || path[pathLen - 1] == LLBC_BACKLASH_A) { if (pathLen == 3 && LLBC_String::isalpha(path[0]) && path[1] == ':') return path; else return path.substr(0, pathLen - 1); } else { return path; } #else // Non-Win32 const size_t pathLen = path.length(); if (path[pathLen - 1] == LLBC_SLASH_A) { if (pathLen == 1) return path; else return path.substr(0, path.length() -1); } else { return path; } #endif // Win32 }