Advertisement

11.29.2007 at 01:03PM PST, ID: 22991741
[x]
Attachment Details

Indy and TidTCPServer

Asked by alpires in Delphi Programming

Tags: tidtcpserver, delphi, indy

Hi Guys, please I need some help here.

I am doing a program that uses indy with delphi 7. I had a technical question and sent my question to the support of the indy component:

My question was:

//////////////////////////////////////////// My Question to indy support
Hi,

I am using delphi 7 with indy 10.2.3 and TidTCPServer

I did this in my program (only a test):


type
TMyContext = class(TIdContext)
 public
     user_id: integer
     procedure SendMsg(const ID: Integer);
 end;


In OnConnect event of TidTCPServer i did this:


        with TMyContext(AContext) do
        begin
            user_id:=strtoint(Connection.IOHandler.ReadLn) ;
            Connection.IOHandler.WriteLn('User ' + inttostr(user_id) +
'connected');
        end;


In OnExecute i did this:


var
    touser:integer
begin
    touser:=strtoint(AContext.Connection.IOHandler.ReadLn);
    TMyContext(AContext).SendMsg(touser);
end


And in SendMsg procedure a did this:



procedure TMyContext.SendMsg(const ID: Integer);
    var
        List: TList;
        Context: TMyContext;
        I: Integer;
    begin

        List := FContextList.LockList;
        try
            for I := 0 to List.Count-1 do
            begin
                Context := TMyContext(List[I]);
                if Context.user_id = ID then
                begin
                    try
                        Context.Connection.IOHandler.WriteLn('Hi');
                    except
                    end;
                    Exit;
                end;
            end;
        finally
            FContextList.UnlockList;
        end;
    end;


Well, when a client send the number of another client, this client receives
the message 'HI'.
Now i need read a table in my data base and get the user_id, and then send
to the right connection. I dont know how do this outside of OnExecute event.
//////////////////////////////////////////////////////////////////////////// End Of my question

Well, the answer was:

///////////// The answer

In order for that to work properly, you need to implement a locking
mechanism in your Context class so that multiple threads cannot write to the
same client at the same time (locking the ContextList is not enough).  The
alternative is to implement a FIFO queue in the Context class that SendMsg()
simply appends to, and then have the OnExecute handler write out the
contexts of the queue peridoically.

> Now i need read a table in my data base and get the user_id, and
> then send to the right connection. I dont know how do this outside
> of OnExecute event.

Yes, you do, because your SendMsg() method is already doing it.  The
TIdContext.FContextList member points to the same list that the
TIdTCPServer.Contexts property points to, and that property is declared as
public, so any code you have that can reach the TIdTCPServer object can
access the Contexts list.

///////////////////// End of answer

Please Guys, I need help to develop  this locking mechanism in my Context class.
Can someone help me?

Thx
Start Free Trial
[+][-]11.29.2007 at 11:57PM PST, ID: 20380429

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.30.2007 at 02:23PM PST, ID: 20385746

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.30.2007 at 02:54PM PST, ID: 20385931

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Delphi Programming
Tags: tidtcpserver, delphi, indy
Sign Up Now!
Solution Provided By: ciuly
Participating Experts: 1
Solution Grade: B
 
 
[+][-]11.30.2007 at 03:31PM PST, ID: 20386111

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628