Example #1
0
Protocol* Protocol::decode(const char *bytes, int32_t len, bool decrypt)
{
    int type;
    memcpy(&type, bytes, sizeof(type));
    bytes += sizeof(type);
    len -= 4;
    
    char *temp = new char[len];
    if (decrypt)
    {
        rc4_state state;
        rc4_init(&state, (const u_char *)INKEY.c_str(), INKEY.size());
        rc4_crypt(&state, (const u_char *)bytes, (u_char *)temp, len);
    }
    else
    {
        memcpy(temp, bytes, len);
    }
    std::string plain(temp, len);
    delete[] temp;
    CCLOG("==== Client received:%s ====", plain.c_str());
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(plain, root))
        return NULL;
    //int type = root["type"].asInt();
    Protocol *p = Protocol::create(type);
    if (p != NULL)
    {
        JsonStream stream(root);
        p->decode(stream);
    }
    return p;
}