【问题标题】:Is there an equivalent of Indy 9's WriteBuffer() in Indy 10?Indy 10 中是否有相当于 Indy 9 的 WriteBuffer() 的功能?
【发布时间】:2019-12-02 01:58:21
【问题描述】:

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

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

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

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

在 Indy 10 中找不到名为 WriteBuffer() 的函数。是否有等效函数?

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

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

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

我参考了之前告诉你的代码:

Is there an equivalent of Indy 9's ReadBuffer() in Indy 10?

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_ *&' 的已知转换

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

【问题讨论】:

    标签: c++ indy rad-studio


    【解决方案1】:

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

    改变

    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)));
    }
    

    或者,您可以使用带有TIdMemoryBufferStreamTIdIOHandler::Write(TStream*) 方法直接从您的BKUK_PACKET 变量发送,而无需先复制其数据,类似于Indy 9 的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());
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多