About netstring() functions on Indy 10.

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

About netstring() functions on Indy 10.

Postby Munfaqqiha » Mon Jul 06, 2015 1:51 pm

Howdy, All

I'm sorry that a lot to ask, it is because I am new in this regard :) .

So, I've read the Remy's answers in a Stackverflow's post about an Indy 10's function.

Code: Select all
String __fastcall netstring(TIdMappedPortContext* AContext)
{
    return BytesToStringRaw(AContext->NetData);
}

I've tried to apply it in Indy 10.6.2.5263 [a default version of C ++ Builder X8 Update-1], but an error always occur due to the AContext on that function is a member of TIdMappedPortContext. Meanwhile, this function is used in TIdMappedPortTCP which has the same member: AContext, CMIIW.

Code: Select all
[bcc32 Error] Unit1.cpp(173): E2034 Cannot convert 'TIdContext *' to 'TIdMappedPortContext *'
  Full parser context
    Unit1.cpp(164): parsing: void _fastcall TMunfaqqiha::TCPOutboundData(TIdContext *)

[bcc32 Error] Unit1.cpp(173): E2342 Type mismatch in parameter 'AContext' (wanted 'TIdMappedPortContext *', got 'TIdContext *')
  Full parser context
    Unit1.cpp(164): parsing: void _fastcall TMunfaqqiha::TCPOutboundData(TIdContext *)

I've been attempting to change the function's code, but the error persists. My goal to use the function is to get more concise and flexible codes

So how is that netsring function can work perfectly when used in TIdMappedPortTCP?

Greetz,
M
User avatar
Munfaqqiha
 
Posts: 4
Joined: Sun Jul 05, 2015 10:04 am
Location: Lombok, Indonesia

Re: About netstring() functions on Indy 10.

Postby rlebeau » Mon Jul 06, 2015 2:42 pm

Munfaqqiha wrote:So, I've read the Remy's answers in a Stackverflow's post about an Indy 10's function.

Code: Select all
String __fastcall netstring(TIdMappedPortContext* AContext)
{
    return BytesToStringRaw(AContext->NetData);
}



netstring() is not an Indy function. That is a C++ translation of some end-user Delphi code that was posted on a StackOverflow question, in an effort to migrate some Indy 9 code to Indy 10 where NetData had been changed from String to TIdBytes.

Munfaqqiha wrote:So how is that netsring function can work perfectly when used in TIdMappedPortTCP?


You have to use a type-cast (which I am pretty sure I would have shown in my SO answer when I posted that translation):

Code: Select all
void _fastcall TMunfaqqiha::TCPOutboundData(TIdContext *AContext)
{
    ...
    String s = netstring(static_cast<TIdMappedPortContext*>(AContext));
    ...
}


The original code took a TIdContext as an input parameter to netstring() and performed the type-cast internally. That was not the right design choice, so I changed it when I posted the translation. NetData is a member of TIdMappedPortContext specifically, not TIdContext generically, so netstring() should take a TIdMappedPortContext as input to avoid using netstring() in any code outside of TIdMappedPortTCP.
Last edited by rlebeau on Tue Jul 07, 2015 2:10 pm, edited 2 times in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1648
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: About netstring() functions on Indy 10.

Postby Munfaqqiha » Mon Jul 06, 2015 3:02 pm

rlebeau wrote:You have to use a type-cast (which I am pretty sure I would have shown in my SO answer when I posted that translation):

Code: Select all
void _fastcall TMunfaqqiha::TCPOutboundData(TIdContext *AContext)
{
    ...
    String s = netstring(static_cast<TIdMappedPortContext*>(AContext));
    ...
}

Yup, I knew that is not an Indy function. I made a mistake with my post, I am sorry ! :D.

So, what I mean is that the above error appear when using the syntax, i.e : String NetData = netstring(AContext);, while your syntax above, does not cause any error.

rlebeau wrote:The original code took a TIdContext as an input parameter to netstring() and performed the type-cast internally. That was not the right design choice, so I changed it when I posted the translation. NetData is a member of TIdMappedPortContext specifically, not TIdContext generically, so netstring() should take a TIdMappedPortContext as input to avoid using netstring() in any code outside of TIdMappedPortTCP

It means, I can not use the netstring(AContext); inside of TIdMappedPortTCP, directly. Right ?

Thank you so much for help, I GREATLY appreciate it, really..! :wink:

Greetz,
M
User avatar
Munfaqqiha
 
Posts: 4
Joined: Sun Jul 05, 2015 10:04 am
Location: Lombok, Indonesia

Re: About netstring() functions on Indy 10.

Postby rlebeau » Tue Jul 07, 2015 2:14 pm

Munfaqqiha wrote:So, what I mean is that the above error appear when using the syntax, i.e : String NetData = netstring(AContext);, while your syntax above, does not cause any error.


Correct. You cannot pass a TIdContext* pointer where a TIdMappedPortContext* is expected. However, in TIdMappedPortTCP, all context objects are TIdMappedPortContext (unless specified differently the user by setting the ContextClass property, but any user-defined class must be derived from TIdMappedPortContext), so the cast is necessary but safe to do.

Munfaqqiha wrote:It means, I can not use the netstring(AContext); inside of TIdMappedPortTCP, directly. Right ?


No. You must cast AContext to TIdMappedPortContext*.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1648
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 13 guests

cron