Board index » delphi » Re: Indy demos about the clients
Remy Lebeau (TeamB)
![]() Delphi Developer |
Remy Lebeau (TeamB)
![]() Delphi Developer |
Re: Indy demos about the clients2004-11-10 06:41:56 AM delphi74 "Gollup" <XXXX@XXXXX.COM>writes QuoteCan you provide a little code sample about it ? QuoteShould i set the ReadTimeOut then check for the |
Gollup
![]() Delphi Developer |
2004-11-12 05:09:33 AM
Re: Indy demos about the clients
Should we care about the same situation in server threads too ? Do we need
to do something special like we do for clients for to read and write into the OnExecute of TIdTCPServer ? Regards, "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes Quote
|
Gollup
![]() Delphi Developer |
2004-11-12 05:15:48 AM
Re: Indy demos about the clients
I mean do we need to handle read and write separately ?
"Gollup" <XXXX@XXXXX.COM>writes QuoteShould we care about the same situation in server threads too ? Do we need |
Gollup
![]() Delphi Developer |
2004-11-12 05:44:30 AM
Re: Indy demos about the clients
My OnExecute code is like this :
ReadRecord; if ReadedRecord = recMsg then TriggerMessageReceive; if ThereIsAMesageToSend then SendMessage; When i send a message from the client to server it works fine. But when i try to send a message from server to client, it just send after the server receives a message from the client. I mean like that : Client ->Message("Hello") ->Server Server gets "Hello" Server ->Message("Hello Reply") ->Client Client gets nothing Client ->Message("Hello Again") ->Server Server gets "Hello Again" Client gets "Hello Reply" I checked this code with breakpoints and i saw that OnExecute triggers and gets the available message. Then check if there is a message. At the first time there isn't any message so nothing is sending from server to client. And when i send an another message from client to server, server receives the message without to have any problem and see that there is a message to send this time and send the avilable message from server to client. OnExecute just running when it is receive a message. I couldn't understand what is going on. Do you have any idea about it ? "Gollup" <XXXX@XXXXX.COM>writes QuoteI mean do we need to handle read and write separately ? |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2004-11-12 06:12:46 AM
Re: Indy demos about the clients
"Gollup" <XXXX@XXXXX.COM>writes
QuoteMy OnExecute code is like this : timely fashion. However, as has already been mentioned earlier, if you have a thread that wants to send data to a socket, there is nothing stopping you from simply writing to that socket directly immediately. Just make sure that you are providing adequate synchronization to that socket so that multiple packets being written will not overlap each other. QuoteWhen i send a message from the client to server it works fine. asking, you need to make use of actual timeouts so that the reading stops reading periodically so that the writing can then proceed when needed. Gambit |
Gollup
![]() Delphi Developer |
2004-11-12 06:17:39 AM
Re: Indy demos about the clients
Thanks for your reply. Sorry Remy but i really get confused about it :( When
i set ReadTimeOut from something else than "0" it rises a EIdReadTimeOut exception. But could you please show me how to make it work. At least with pseudo code. My code is like that : ReadRecord; if ReadedRecord = recMsg then TriggerMessageReceive; if ThereIsAMesageToSend then SendMessage; Could you please show me how to make it work on my actual code ? At least as a pseudo code. Thank you, "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes Quote
|
Gollup
![]() Delphi Developer |
2004-11-12 06:20:11 AM
Re: Indy demos about the clients
Do i just need to set the ReadTimeOut for clients ?
"Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes QuoteThat is because you are probably reading with infinite timeouts so that |
Gollup
![]() Delphi Developer |
2004-11-12 06:27:59 AM
Re: Indy demos about the clients
When the server receives a new connection i do :
...(AThread: TIdPeerThread); begin AThread.Connection.ReadTimeOut := 10; end; It makes EIdReadTimeOut to rise. What else i should do into the OnExecute ? "Gollup" <XXXX@XXXXX.COM>writes QuoteDo i just need to set the ReadTimeOut for clients ? |
Gollup
![]() Delphi Developer |
2004-11-12 06:31:59 AM
Re: Indy demos about the clients
try
ReadRecord; if ReadedRecord = recMsg then TriggerMessageReceive; except on EIdReadTimeOut do begin if ThereIsAMesageToSend then SendMessage; end; end; Something like that ? At that time i couldn't handle this EIdReadTimeOut too.. "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes Quote
|
Remy Lebeau (TeamB)
![]() Delphi Developer |
2004-11-12 07:10:29 AM
Re: Indy demos about the clients
"Gollup" <XXXX@XXXXX.COM>writes
QuoteWhen i set ReadTimeOut from something else than "0" QuoteBut could you please show me how to make it work. begin try bRecordReadOk := ReadRecord; except On E: Exception do begin if not (E is EIdReadTimeOut) then raise; bRecordReadOk := False; end; end; if (bRecordReadOk = True) and (ReadedRecord = recMsg) then begin TriggerMessageReceive; end; if ThereIsAMesageToSend then begin SendMessage; end; end; Gambit |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2004-11-12 07:11:20 AM
Re: Indy demos about the clients
"Gollup" <XXXX@XXXXX.COM>writes
QuoteDo i just need to set the ReadTimeOut for clients ? for their own local reading/writing, and servers set their own timeout for their own local reading/writing. Gambit |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2004-11-12 07:11:45 AM
Re: Indy demos about the clients
"Gollup" <XXXX@XXXXX.COM>writes
QuoteWhen the server receives a new connection i do : |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2004-11-12 07:18:09 AM
Re: Indy demos about the clients
"Gollup" <XXXX@XXXXX.COM>writes
QuoteSomething like that ? short time, your writing is delayed again. It should be more like this: try ReadRecord; if ReadedRecord = recMsg then TriggerMessageReceive; if ThereIsAMesageToSend then SendMessage; except on E: Exception do begin if E is EIdReadTimeOut then begin if ThereIsAMesageToSend then SendMessage; end else raise; end; end; Which can then become this to remove duplicated code: begin try ReadRecord; if ReadedRecord = recMsg then TriggerMessageReceive; except on E: Exception do begin // do not ignore other Indy exceptions, let them through if not (E is EIdReadTimeOut) then raise; end; end; if ThereIsAMesageToSend then SendMessage; end; You could then move your try..except into the reading function so that the OnExecute code is not so clutered: function ReadRecord(var Record: TRecordType); begin Result := False; try // read the data here... Result := True; except on E: Exception do begin // do not ignore other Indy exceptions, let them through if not (E is EIdReadTimeOut) then raise; end; end; end; var Record: TRecordType; begin if ReadRecord(Record) then begin if Record = recMsg then TriggerMessageReceive; end; if ThereIsAMesageToSend then SendMessage; end; Gambit |
Gollup
![]() Delphi Developer |
2004-11-12 07:29:15 AM
Re: Indy demos about the clients
Thanks Remy, i really don't know how to show my happiness :)). That was the
last problem and now it works pretty fine. Thanks a lot. with my best regards, Goran, "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes Quote
|