Exemplo n.º 1
0
bool FNFSMessageHeader::ReceivePayload(FArrayReader& OutPayload, const FSimpleAbstractSocket& Socket)
{
    // make room to receive a header
    TArray<uint8> HeaderBytes;
    int32 Size = sizeof(FNFSMessageHeader);
    HeaderBytes.AddZeroed(Size);

    if (!Socket.Receive(HeaderBytes.GetData(), Size))
    {
        UE_LOG(LogSockets, Error, TEXT("Unable to read full header"));
        return false;
    }

    // parse it as a header (doing any byte swapping as needed)
    FMemoryReader Reader(HeaderBytes);
    FNFSMessageHeader Header(Socket);
    Reader << Header;

    // make sure it's valid
    if (Header.Magic != Socket.GetMagic())
    {
        UE_LOG(LogSockets, Error, TEXT("Bad network header magic"));
        return false;
    }
    if (!Header.PayloadSize)
    {
        UE_LOG(LogSockets, Error, TEXT("Empty payload"));
        return false;
    }

    // was the header byteswapped? If so, make the archive byteswapped
    OutPayload.SetByteSwapping(Reader.ForceByteSwapping());

    // we are going to append to the payload, so note how much data is in it now
    int32 PayloadOffset = OutPayload.AddUninitialized(Header.PayloadSize);

    // put the read head at the start of the new data
    OutPayload.Seek(PayloadOffset);

    // receive the payload
    if (!Socket.Receive(OutPayload.GetData() + PayloadOffset, Header.PayloadSize))
    {
        UE_LOG(LogSockets, Error, TEXT("Unable to read full payload"));
        return false;
    }

    // make sure it's valid
    uint32 ActualPayloadCrc = FCrc::MemCrc_DEPRECATED(OutPayload.GetData() + PayloadOffset, Header.PayloadSize);
    if (Header.PayloadCrc != ActualPayloadCrc)
    {
        UE_LOG(LogSockets, Error, TEXT("Payload Crc failure."));
        return false;
    }

    // success!
    return true;
}