簡體   English   中英

Delphi C ++ Indy UDP服務器BytesToString沒有收到正確的字符串?

[英]Delphi C++ Indy UDP Server BytesToString does not receive correct string?

我正在嘗試從TUIO多模擬器接收UDP數據包。 但是缺少一些數據。

https://www.tuio.org/?specification

void __fastcall TMain::UDPServerUDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
    TIdSocketHandle *ABinding)
{
    UDPData->Lines->Add(BytesToString(AData));

    for (int i = 0; i < AData.Length; i++)
        UDPData->Lines->Add((char)AData[i]);
}

當我使用BytesToString() ,我只將單詞"#bundle"作為字符串,但是當使用第二個循環時,我得到了:

#捆綁

/ tuio / 2 D cur,sialive 4 / tuio / 2 D cur,sifffffset嗎?

ホ 9>

/ tuio / 2 D cur,sifseq#束

/ tuio / 2 D obj,實際

/ tuio / 2 D obj,sifseq#捆綁包

/ tuio / 2 D曲線

/ tuio / 2 D cur,sifseq

當然,每個字符都放在單獨的行上。

因此,當我不知道編碼時如何獲得正確的數據? 我在這里迷路了,需要真正的幫助。

另外,如何使用TIdUDPClient發送類似的捆綁數據?

您顯示的UDP數據主要是二進制數據,其中包含一些文本元素。 因此,你不應該將整個 AData到一個字符串BytesToString()因為它是不完全的文本開始(嗯,你可以使用BytesToStringRaw()BytesToString()IndyTextEncoding_8bit()作為編碼,但這樣做並不能真正幫助您)。

您需要根據鏈接到的協議規范將數據分解成各個組成部分(依次基於Open Sound Control協議)。 只有這樣,您才能根據需要處理組件。 這意味着您需要按協議逐個字節地遍歷AData ,解析上下文中的每個字節。

例如(到目前為止,這還不是一個完整的實現,但是應該可以讓您了解所涉及的邏輯類型):

struct OSCArgument
{
    char Type;
    Variant Data;
}

struct OSCBundleElement
{
    virtual ~OSCBundleElement() {}
};

struct OSCMessage : OSCBundleElement
{
    String AddressPattern;
    DynamicArray<OSCArgument> Arguments;
};

struct OSCBundle : OSCBundleElement
{
    TDateTime TimeTag;
    DynamicArray<OSCBundleElement*> Elements;

    ~OSCBundle()
    {
        int len = Elements.Length;
        for (int i = 0; i < len; ++i)
            delete Elements[i];
    }
};

int32_t readInt32(const TIdBytes &Bytes, int &offset)
{
    uint32_t ret = GStack->NetworkToHost(BytesToUInt32(Bytes, offset));
    offet += 4;
    return reinterpret_cast<int32_t&>(ret);
}

TDateTime readOSCTimeTag(const TIdBytes &Bytes, int &offset)
{
    int32_t secondsSinceEpoch = readInt32(Bytes, offset); // since January 1 1900 00:00:00
    int32_t fractionalSeconds = readInt32(Bytes, offset); // precision of about 200 picoseconds
    // TODO: convert seconds to TDateTime...
    TDateTime ret = ...;
    return ret;
}

float readFloat32(const TIdBytes &Bytes, int &offset)
{
    uint32_t ret = BytesToUInt32(Bytes, offset);
    offet += 4;
    return reinterpret_cast<float&>(ret);
}

String readOSCString(const TIdBytes &Bytes, int &offset)
{
    int found = ByteIndex(0x00, Bytes, offset);
    if (found == -1) throw ...; // error!
    int len = found - offset;
    String ret = BytesToString(Bytes, offset, len, IndyTextEncoding_ASCII());
    len = ((len + 3) & ~3)); // round up to even multiple of 32 bits
    offset += len;
    return ret;
}

TIdBytes readOSCBlob(const TIdBytes &Bytes, int &offset)
{
    int32_t size = readInt32(Bytes, offset);
    TIdBytes ret;
    ret.Length = size;
    CopyTIdBytes(Bytes, offset, ret, 0, size);
    size = ((size + 3) & ~3)); // round up to even multiple of 32 bits
    offset += size;
    return ret;
}

OSCMessage* readOSCMessage(const TIdBytes &Bytes, int &offset);
OSCBundle* readOSCBundle(const TIdBytes &Bytes, int &offset);

OSCBundleElement* readOSCBundleElement(const TIdBytes &Bytes, int &offset)
{
    TIdBytes data = readOSCBlob(Bytes, offset);
    int dataOffset = 0;

    switch (data[0])
    {
        case '/': return readOSCMessage(data, dataOffset); 
        case '#': return readOSCBundle(data, dataOffset);
    }

    throw ...; // unknown data!
}

Variant readOSCArgumentData(char ArgType, const TIdBytes &Bytes, int &offset)
{
    switch (ArgType)
    {
        case 'i': return readInt32(Bytes, offset);
        case 'f': return readFloat32(Bytes, offset);
        case 's': return readOSCString(Bytes, offset);
        case 'b': return readOSCBlob(Bytes, offset);
        // other types as needed ...
    }

    throw ...; // unknown data!
}

OSCMessage* readOSCMessage(const TIdBytes &Bytes, int &offset)
{
    OSCMessage* ret = new OSCMessage;
    try
    {
        ret->AddressPattern = readOSCString(Bytes, offset);

        String ArgumentTypes = readOSCString(Bytes, offset);
        if (ArgumentTypes[1] != ',') throw ...; // error!

        for (int i = 2; i <= ret->ArgumentTypes.Length(); ++i)
        {
            OSCArgument arg;
            arg.Type = ArgumentTypes[i];
            arg.Data = readOSCArgumentData(arg.Type, Bytes, offset);

            ret.Arguments.Length = ret.Arguments.Length + 1;
            ret.Arguments[ret.Arguments.High] = arg;
        }
    }
    catch (...)
    {
        delete ret;
        throw;
    }

    return ret;
}

OSCBundle* readOSCBundle(const TIdBytes &Bytes, int &offset)
{
    if (readOSCString(Bytes, offset) != "#bundle")
        throw ...; // error!

    OSCBundle *ret = new OSCBundle;
    try
    {
        ret->TimeTag = readOSCTimeTag(Bytes, offset);

        int len = Bytes.Length;
        while (offset < len)
        {
            OSCBundleElement *element = readOSCBundleElement(Bytes, offset);
            try
            {
                ret->Elements.Length = ret->Elements.Length + 1;
                ret->Elements[ret->Elements.High] = element;
            }
            catch (...)
            {
                delete element;
                throw;
            }
        }
    }
    catch (...)
    {
        delete ret;
        throw;
    }

    return ret;
}

void __fastcall TMain::UDPServerUDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
    TIdSocketHandle *ABinding)
{
    int offset = 0;

    if (AData[0] == '/')
    {
        OSCMessage *msg = readOSCMessage(AData, offset);
        // process msg as needed...
        delete msg;
    }
    else if (AData[0] == '#')
    {
        OSCBundle *bundle = readOSCBundle(AData, offset); 
        // process bundle as needed...
        delete bundle;
    }
    else
    {
        // unknown data!
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM