Indy and TidTCPServer
Posted on 2007-11-29
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
I am using delphi 7 with indy 10.2.3 and TidTCPServer
I did this in my program (only a test):
TMyContext = class(TIdContext)
procedure SendMsg(const ID: Integer);
In OnConnect event of TidTCPServer i did this:
with TMyContext(AContext) do
Connection.IOHandler.WriteLn('User ' + inttostr(user_id) +
In OnExecute i did this:
And in SendMsg procedure a did this:
procedure TMyContext.SendMsg(const ID: Integer);
List := FContextList.LockList;
for I := 0 to List.Count-1 do
Context := TMyContext(List[I]);
if Context.user_id = ID then
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?