Indy AContext->Data

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Indy AContext->Data

Postby Lena » Thu Mar 19, 2015 5:48 am

Is it correct way to use AContext->Data?
Code: Select all
void __fastcall TForm1Main::IdTCPServer1Execute(TIdContext *AContext)
{
 //other code and now:

  ParseJSONValue(Sdata, ConnectServerIP, AContext);
}


Code: Select all
void ParseJSONValue(String JsonData, String ServerPascalIP, TIdContext *AContext)
{
//***** other code and now
if(AContext->Data == NULL)
{
 JsonMemberNameID = JsonMemberValue;
 AContext->Data = (TObject*)new String(JsonMemberNameID);
}
  //****
   ParseJsonArraySens(CoderjimoeJsonArraySens, *(String*)AContext->Data);
}


Code: Select all
void ParseJsonArraySens(String CoderjimoeJsonArraySens, String sServerID)
{
 //use sServerID
}

void __fastcall TForm1Main::IdTCPServer1Disconnect(TIdContext *AContext)
{
   if(AContext->Data)
       {
        delete (String*)AContext->Data;
        AContext->Data = NULL;
       }
}
Lena
BCBJ Master
BCBJ Master
 
Posts: 571
Joined: Sun Feb 06, 2011 1:28 pm

Re: Indy AContext->Data

Postby rlebeau » Thu Mar 19, 2015 1:33 pm

Lena wrote:Is it correct way to use AContext->Data?


I would not suggest doing it that way. The Data property expects a TObject* pointer, so you should define a real TObject-derived class to hold the String:

Code: Select all
class TMyData : public TObject
{
public:
    String Value;
    __fastcall TMyData(const String &aValue) : TObject(), Value(aValue) {}
};

void ParseJSONValue(String JsonData, String ServerPascalIP, TIdContext *AContext)
{
    //***** other code and now
    If (AContext->Data == NULL)
    {
        JsonMemberNameID = JsonMemberValue;
        AContext->Data = new TMyData(JsonMemberNameID);
    }
    //****
    ParseJsonArraySens(CoderjimoeJsonArraySens, static_cast<TMyData*>(AContext->Data)->Value);
}

void __fastcall TForm1Main::IdTCPServer1Disconnect(TIdContext *AContext)
{
    delete AContext->Data;
    AContext->Data = NULL;
}


Personally, I would take it a step further, by deriving a class from TIdServerContext instead and then set the TIdTCPServer::ContextClass property:

Code: Select all
class TMyContext : public TIdServerContext
{
public:
    String Value;
};

__fastcall TForm1Main::TForm1Main(TComponent *Owner)
    : TForm(Owner)
{
    IdTCPServer1->ContextClass = __classid(TMyContext);
}

void ParseJSONValue(String JsonData, String ServerPascalIP, TIdContext *AContext)
{
    TMyContext *Ctx = static_cast<TMyContext*>(AContext);

    //****

    If (Ctx->Value == "")
    {
        JsonMemberNameID = JsonMemberValue;
        Ctx->Value = JsonMemberNameID;
    }

    //****

    ParseJsonArraySens(CoderjimoeJsonArraySens, Ctx->Value);
}


No need to use the OnDisconnect event to free anything, as TIdContext is freed by TIdTCPServer automatically.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1533
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Indy AContext->Data

Postby Lena » Fri Mar 20, 2015 1:12 am

Thank you very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 571
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 10 guests

cron