Delphi C++ Indy UDP Server BytesToString не получает правильную строку?

Я пытаюсь получать пакеты UDP от мультисимулятора TUIO. Но часть данных отсутствует.

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" в виде строки, но при использовании второго цикла я получаю следующее:

# b u n d l e

/ t u i o / 2 D c u r , s i a l i v e 4 / t u i o / 2 D c u r , s i f f f f f s e t ?

ホ 9 >  ᅳ

/ t u i o / 2 D c u r , s i f s e q # b u n d l e

/ t u i o / 2 D o b j , s a l i v e

/ t u i o / 2 D o b j , s i f s e q # b u n d l e

/ t u i o / 2 D c u r , s a l i v e

/ t u i o / 2 D c u r , s i f s e q

С каждым символом в отдельной строке, разумеется.

Итак, как я могу получить правильные данные, если я не знаю кодировку? Я потерялся здесь, и мне действительно нужна помощь.

Кроме того, как я могу отправить аналогичные данные пакета с TIdUDPClient?


person Ahmed Sayed    schedule 12.06.2018    source источник
comment
Итак, в какой кодировке отправляются данные? И почему вы обрабатываете его по одному байту за раз? А как узнать, что пришла вся посылка?   -  person David Heffernan    schedule 13.06.2018
comment
@DavidHeffernan откуда вы знаете, что весь пакет прибыл? - UDP ориентирован на сообщения, а не на потоки, как TCP. TIdUDPServer получает только целые сообщения, а не части сообщений.   -  person Remy Lebeau    schedule 13.06.2018


Ответы (1)


Данные 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!
    }
}
person Remy Lebeau    schedule 12.06.2018