Example #1
0
void HandleClient(ConnectionPeer clientPeer, ConnectionPeer serverPeer, std::mutex &outputMutex)
{
    bool close = false;
    size_t messageLength = 0;

    while (!close) {
        while ((messageLength = MessageLength(clientPeer.peek())) == 0)
            std::this_thread::yield();

        std::chrono::system_clock::time_point before = std::chrono::system_clock::now();

        std::string message = clientPeer.read(messageLength);
        serverPeer.write(message);

        close = (DecodeQuery(message).size() == 0);

        outputMutex.lock();
        std::cout << "S_LB "
                  << std::chrono::duration_cast<std::chrono::microseconds>(
                          std::chrono::system_clock::now() - before).count()
                  << '\n';
        outputMutex.unlock();

        if (!close) {
            while ((messageLength = MessageLength(serverPeer.peek())) == 0)
                std::this_thread::yield();

            before = std::chrono::system_clock::now();

            message = serverPeer.read(messageLength);
            clientPeer.write(message);

            outputMutex.lock();
            std::cout << "S_LB "
                      << std::chrono::duration_cast<std::chrono::microseconds>(
                              std::chrono::system_clock::now() - before).count()
                      << '\n';
            outputMutex.unlock();
        }
    }

    clientPeer.close();
    serverPeer.close();
}
// -----------------------------------------------------------------------------
// CNATFWUNSAFMessageFactory::DecodeL
// Mandatory UNSAF attributes must be understood. Optional attributes are
// ignored when not understood.
// -----------------------------------------------------------------------------
//
EXPORT_C CNATFWUNSAFMessage* CNATFWUNSAFMessageFactory::DecodeL(
    const TDesC8& aNATFWUNSAFMessage) const
    {
    __TEST_INVARIANT;

    CNATFWUNSAFMessage* msg = DecodeHeaderLC(aNATFWUNSAFMessage);

    NATFWUNSAF_BYTESTREAMLOG("CNATFWUNSAFMessageFactory::DecodeL data",
        aNATFWUNSAFMessage);

    TInt msgLengthExcludingHeader = MessageLength(aNATFWUNSAFMessage);
    TInt pos(0);

    while (pos < msgLengthExcludingHeader)
        {
        TPtrC8 msgBody = aNATFWUNSAFMessage.Mid(
            CNATFWUNSAFMessage::EHeaderSize + pos);

        //Can't use the enum, as attribute type could have an unrecognized
        //value
        TUint16 attrType(0);
        TUint16 attrTotalLength(0); //includes type, length, value

        CNATFWUNSAFAttribute::ParseTypeAndLengthL(msgBody, attrType,
            attrTotalLength);
        TDecodeFnPtr decodeFn = iAttributeLookupTable->Find(attrType);

        if (decodeFn)
            {
            //After detecting this is a known attribute, check it is allowed to
            //be present in this msgType, if not, ignore it w/o decoding.
            if (msg->IsAllowed(attrType))
                {
                CNATFWUNSAFAttribute* attr = (*decodeFn)(msgBody);
                CleanupStack::PushL(attr);
                msg->AddAttributeL(attr);
                CleanupStack::Pop(attr);
                }
            }
        else
            {
            if (CNATFWUNSAFAttribute::IsMandatory(attrType))
                {
                msg->UnknownMandatoryAttributeFound();
                }
            }

        pos += attrTotalLength;
        }

    CleanupStack::Pop(msg);
    return msg;
    }