-1

此代码是使用 Indy 9 在 Borland C++Builder 6 中编写的:

void __fastcall TfrmMain::ServerConnect(TIdPeerThread *AThread)
{
     BKUK_PACKET Pkt;
----------(Omission)---------------------------------------

//AThread->Connection->WriteBuffer(&Pkt,sizeof(BKUK_PACKET),1);

----------(Omission)---------------------------------------
}

WriteBuffer()在 Indy 10 中找不到命名的函数。有等效的函数吗?

BKUK_PACKET是一个大约1200字节的结构。

typedef struct _BKUK_PACKET_
{
    BYTE head[4];
    WORD PayLoad;
    WORD Length;
    BYTE Data[1200];
    WORD Ver;
    BYTE tail[2];
}BKUK_PACKET;

TIdIOHandler.Write(TIdBytes)我在查看 Indy 10 的说明手册时找到了该方法。

我提到了我之前告诉你的代码:

Indy 10 中是否有相当于 Indy 9 的 ReadBuffer() 的功能?

template<typename T>
void __fastcall PopulateWriteBuffer(T& obj,TIdIOHandler* ioh) {
    System::Byte* p = (System::Byte*) &obj;
    for(unsigned count=0; count<sizeof(T); ++count, ++p)
        ioh->Write(*p);

----------(Omission)---------------------------------------

Populate02(&Pkt,AContext->Connection->IOHandler);
}

但是当我尝试按上述方式编程时,出现错误:

[bcc32c 错误] Main.cpp(608):没有匹配函数调用“Populate02”

Main.cpp(478):候选函数 [with T = _BKUK_PACKET_ *] 不可行:第一个参数没有从 '_PACKET *'(又名 '_BKUK_PACKET_ *')到 '_BKUK_PACKET_ *&' 的已知转换

请告诉我如何修复此代码。

4

1 回答 1

0

您根本没有调用PopulateWriteBuffer(),而是调用了其他一些名为的函数Populate02()。假设这只是一个错字,而您的真正意思是PopulateWriteBuffer(),您将向它传递一个指向aBKUK_PACKET的指针,但它需要一个对a的引用BKUK_PACKET

改变

Populate02(&Pkt, AContext->Connection->IOHandler);

PopulateWriteBuffer(Pkt, AContext->Connection->IOHandler);

话虽如此,该TIdIOHandler::Write(TIdBytes)方法可以正常工作,您只需先将BKUK_PACKET变量复制到中间TIdBytes变量中,例如使用 Indy 的RawToBytes()函数,例如:

void __fastcall TfrmMain::ServerConnect(TIdContext *AContext)
{
    BKUK_PACKET Pkt;
    // populate Pkt as needed...

    AContext->Connection->IOHandler->Write(RawToBytes(&Pkt, sizeof(BKUK_PACKET)));
}

或者,您可以使用TIdIOHandler::Write(TStream*)带有 a 的方法TIdMemoryBufferStream直接从您的BKUK_PACKET变量发送,而无需先复制其数据,类似于 Indy 9's WriteBuffer(),例如:

#include <memory>

void __fastcall TfrmMain::ServerConnect(TIdContext *AContext)
{
    BKUK_PACKET Pkt;
    // populate Pkt as needed...

    std::unique_ptr<TIdMemoryBufferStream> strm(new TIdMemoryBufferStream(&Pkt, sizeof(BKUK_PACKET)));
    // or std::auto_ptr prior to C++11...

    AContext->Connection->IOHandler->Write(strm.get());
}
于 2019-12-02T05:12:57.513 回答