Ejemplo n.º 1
0
void TestCase_Comm_SendBytes::FetchArgs(int argc, char *argv[])
{
    _asClient = LLBC_ToLower(argv[1]) == "client" ? true : false;
    _svcType = LLBC_ToLower(argv[2]) == "normal" ? LLBC_IService::Normal : LLBC_IService::Raw;

    _runIp = argv[3];
    _runPort = LLBC_Str2Int32(argv[4]);
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
uint32 LLBC_Str2UInt32(const char *str)
{
    return static_cast<uint32>(LLBC_Str2Int32(str));
}
Ejemplo n.º 4
0
int TestCase_Core_Utils_Text::Run(int argc, char *argv[])
{
    LLBC_PrintLine("core/utils/Util_Text test:");

    // Split string test.
    LLBC_String str = "hello world hello world";
    LLBC_PrintLine("split string: %s", str.c_str());
    std::vector<LLBC_String> strs;
    LLBC_SplitString(str, " ", strs);
    LLBC_PrintLine("result(sub string count: %lu):", strs.size());
    for(size_t i = 0; i < strs.size(); i ++)
    {
        LLBC_PrintLine("\t%s", strs[i].c_str());
    }
    LLBC_PrintLine("");

    // Filter-out test.
    LLBC_PrintLine("filter out string: %s", str.c_str());
    LLBC_PrintLine("result: %s", LLBC_FilterOutString(str, " ").c_str());
    LLBC_PrintLine("");

    // Upper<->Lower test.
    str = "AaBbCcDd eEfFgG";
    LLBC_PrintLine("string [%s] to Upper: %s", str.c_str(), LLBC_ToUpper( str.c_str()).c_str());
    LLBC_PrintLine("string [%s] to Lower: %s", str.c_str(), LLBC_ToLower( str.c_str()).c_str());
    LLBC_PrintLine("");

    // Trim test(include left, right).
    str = "     Hello World     \t\t\t";
    LLBC_PrintLine("trim test, string: %s", str.c_str());
    LLBC_PrintLine("after trim left(len: %lu): %s", 
        LLBC_TrimLeft(str).length(), LLBC_TrimLeft(str).c_str());
    LLBC_PrintLine("after trim right(len: %lu): %s",
        LLBC_TrimRight(str).length(), LLBC_TrimRight(str).c_str());
    LLBC_PrintLine("after trim(left and right)(len: %lu): %s",
        LLBC_Trim(str).length(), LLBC_Trim(str).c_str());
    LLBC_PrintLine("");

    // String -> Number test.
    str = "-30";
    LLBC_PrintLine("string [%s] to number(Str2Int32): %d", 
        str.c_str(), LLBC_Str2Int32( str.c_str()));
    LLBC_PrintLine("string [%s] to number(Str2Long): %ld", 
        str.c_str(), LLBC_Str2Long( str.c_str()));
    LLBC_PrintLine("string [%s] to number(Str2Int64): %lld", 
        str.c_str(), LLBC_Str2Int64( str.c_str()));
    LLBC_PrintLine("string [%s] to number(Str2UInt32): %u", 
        str.c_str(), LLBC_Str2UInt32( str.c_str()));
    LLBC_PrintLine("string [%s] to number(Str2ULong): %lu", 
        str.c_str(), LLBC_Str2ULong( str.c_str()));
    LLBC_PrintLine("string [%s] to pointer(Str2Ptr): %p",
        "30", LLBC_Str2Ptr("30"));
    LLBC_PrintLine("string [%s] to pointer(Str2Ptr): %p",
        "0xcdcdcdcd", LLBC_Str2Ptr("0xcdcdcdcd"));
    LLBC_PrintLine("string [%s] to number(Str2UInt64): %llu", 
        str.c_str(), LLBC_Str2UInt64( str.c_str()));
    LLBC_PrintLine("string [%s] to number(Str2Double): %f", 
        str.c_str(), LLBC_Str2Double( str.c_str()));
    LLBC_PrintLine("");

    // Number -> String test.
    {
        sint8 sint8Val = 'a';
        LLBC_PrintLine("LLBC_Num2Str<sint8>()[%c] -> string(base:10): %s",
            sint8Val, LLBC_Num2Str(sint8Val).c_str());
        uint8 uint8Val = 97;
        LLBC_PrintLine("LLBC_Num2Str<uint8>()[%d] -> string(base:10): %s",
            uint8Val, LLBC_Num2Str(uint8Val).c_str());

        sint16 sint16Val = -16;
        LLBC_PrintLine("LLBC_Num2Str<sint16>()[%d] -> string(base:16): %s",
            sint16Val, LLBC_Num2Str(sint16Val, 16).c_str());
        uint16 uint16Val = 16;
        LLBC_PrintLine("LLBC_Num2Str<uint16>()[%u] -> string(base:8): %s",
            uint16Val, LLBC_Num2Str(uint16Val, 8).c_str());

        sint32 sint32Val = -32;
        LLBC_PrintLine("LLBC_Num2Str<sint32>()[%d] -> string(base:10): %s",
            sint32Val, LLBC_Num2Str(sint32Val).c_str());
        uint32 uint32Val = 32;
        LLBC_PrintLine("LLBC_Num2Str<uint32>()[%u] -> string(base:10): %s",
            uint32Val, LLBC_Num2Str(uint32Val).c_str());

        long longVal = -1;
        LLBC_PrintLine("LLBC_Num2Str<long>()[%ld] -> string(base:10): %s",
            longVal, LLBC_Num2Str(longVal).c_str());
        llbc::ulong ulongVal = -1;
        LLBC_PrintLine("LLBC_Num2Str<ulong>()[%lu] -> string(base:10): %s",
            ulongVal, LLBC_Num2Str(ulongVal).c_str());

        sint64 sint64Val = -64;
        LLBC_PrintLine("LLBC_Num2Str<sint64>()[%lld] -> string(base:10): %s",
            sint64Val, LLBC_Num2Str(sint64Val).c_str());
        uint64 uint64Val = 64;
        LLBC_PrintLine("LLBC_Num2Str<uint64>()[%llu] -> string(base:10): %s",
            uint64Val, LLBC_Num2Str(uint64Val).c_str());

        float fVal = 1.0f;
        LLBC_PrintLine("LLBC_Num2Str<float>()[%f] -> string: %s",
            fVal, LLBC_Num2Str(fVal).c_str());
        double doubleVal = -1.0f;
        LLBC_PrintLine("LLBC_Num2Str<double>()[%f] -> string: %s",
            doubleVal, LLBC_Num2Str(doubleVal).c_str());

        int *intPtr = reinterpret_cast<int *>(0xcdcdcdcd);
        LLBC_PrintLine("LLBC_Num2Str<int *>()[%p] -> string: %s",
            intPtr, LLBC_Num2Str(intPtr).c_str());
        const void *voidPtr = reinterpret_cast<const void *>(0xfffffffe);
        LLBC_PrintLine("LLBC_Num2Str<void *>()[%p] -> string: %s",
            voidPtr, LLBC_Num2Str(voidPtr).c_str());
    }

    LLBC_PrintLine("");

    // DirName, BaseName, ExtensionName test.
#if LLBC_TARGET_PLATFORM_NON_WIN32
    LLBC_String path = "/usr/tmp/a.txt";
#else
    LLBC_String path = "c:\\Windows\\a.txt";
#endif
    LLBC_PrintLine("path: %s", path.c_str());
    LLBC_PrintLine("\tdirname: %s", LLBC_DirName(path).c_str());
    LLBC_PrintLine("\tbasename(include extension): %s", LLBC_BaseName(path).c_str());
    LLBC_PrintLine("\tbasename(not-include extension): %s", LLBC_BaseName(path, false).c_str());
    LLBC_PrintLine("\textension: %s", LLBC_ExtensionName(path).c_str());

    LLBC_PrintLine("Press any key to continue ...");
    getchar();

    return 0;
}
Ejemplo n.º 5
0
int LLBC_LogTokenChain::Build(const LLBC_String &pattern)
{
    if (_head)
    {
        LLBC_SetLastError(LLBC_ERROR_REENTRY);
        return LLBC_RTN_FAILED;
    }

    char ch = '\0';
    const char *curPattern = NULL;
    LLBC_String::size_type patternLength = 0;
    int state = LLBC_INTERNAL_NS __g_literal_state;

    LLBC_ILogToken *token = NULL;
    LLBC_LogFormattingInfo *formatter = NULL;

    LLBC_String buf;
    if (pattern.empty())
    {
        curPattern = LLBC_INTERNAL_NS __g_default_pattern;
        patternLength = LLBC_StrLenA(LLBC_INTERNAL_NS __g_default_pattern);
    }
    else
    {
        curPattern = pattern.data();
        patternLength = pattern.size();
    }

    for (size_t i = 0; i < patternLength;)
    {
        ch = curPattern[i ++];
        switch(state)
        {
        case LLBC_INTERNAL_NS __g_literal_state:
            if (i == patternLength)
            {
                buf.append(1, ch);
                break;
            }

            if (ch == LLBC_LogTokenType::EscapeToken)
            {
                if (curPattern[i] == LLBC_LogTokenType::EscapeToken)
                {
                    buf.append(1, ch);
                    i ++;
                }
                else
                {
                    if (!buf.empty())
                    {
                        token = LLBC_LogTokenBuilderSingleton->BuildLogToken(LLBC_LogTokenType::StrToken);
                        token->Initialize(formatter, buf);
                        this->AppendToken(token);

                        buf.clear();
                        formatter = NULL;
                    }

                    buf.append(1, ch);
                    LLBC_XDelete(formatter);
                    state = LLBC_INTERNAL_NS __g_converter_state;
                }
            }
            else
            {
                buf.append(1, ch);
            }

            break;

        case LLBC_INTERNAL_NS __g_converter_state:
            buf.append(1, ch);
            
            if ((ch >= 0x30 && ch <= 0x39) || ch == '-')
            {
                state = LLBC_INTERNAL_NS __g_number_state;
                break;
            }

            buf.erase(buf.rfind(LLBC_LogTokenType::EscapeToken));
            token = LLBC_LogTokenBuilderSingleton->BuildLogToken(ch);
            if (!formatter)
            {
                formatter = new LLBC_LogFormattingInfo;
            }

            token->Initialize(formatter, "");
            this->AppendToken(token);

            formatter = NULL;
            state = LLBC_INTERNAL_NS __g_literal_state;

            break;

        case LLBC_INTERNAL_NS __g_number_state:
            if ((ch < 0x30 || ch > 0x39) && ch != '-')
            {
                int minLength = LLBC_Str2Int32(&buf[buf.rfind(LLBC_LogTokenType::EscapeToken) + 1]);
                if (!formatter)
                {
                    formatter = new LLBC_LogFormattingInfo(minLength < 0 ? true : false, ::abs(minLength), INT_MAX);
                }
                else
                {
                    formatter->SetLeftAligh(minLength < 0 ? true : false);
                    formatter->SetMinLen(minLength);
                    formatter->SetMaxLen(INT_MAX);
                }

                i --;
                state = LLBC_INTERNAL_NS __g_converter_state;

                break;
            }

            buf.append(1, ch);
            break;

        default:
            LLBC_XDelete(formatter);
            this->Cleanup();

            LLBC_SetLastError(LLBC_ERROR_FORMAT);
            return LLBC_RTN_FAILED;

            break;
        }
    }

    if (!buf.empty())
    {
        token = LLBC_LogTokenBuilderSingleton->BuildLogToken(LLBC_LogTokenType::StrToken);
        token->Initialize(NULL, buf);
        this->AppendToken(token);
    }

    return LLBC_RTN_OK;
}