indy tcp server synchronize inside server execute

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Kullanıcı avatarı
mia
Üye
Mesajlar: 239
Kayıt: 17 Nis 2015 02:18

indy tcp server synchronize inside server execute

Mesaj gönderen mia »

hi , iam asking about synchronize inside tcpserver execute is this right way to do ?

Kod: Tümünü seç

function TServobj.UserExists(const Username, Password: String): Boolean;
var
  Exists: Boolean;
begin
 TThread.Synchronize(nil,
    procedure
    begin
connectionmo.userslq.Close;
connectionmo.userslq.SQL.Clear;
connectionmo.userslq.SQL.Add('SELECT * FROM `users` WHERE `username` = "'+ trim(Username) +'"  AND `password` = "' + trim(Password) + '"');
connectionmo.userslq.Open;
try
Exists := not connectionmo.userslq.IsEmpty;
finally
connectionmo.userslq.Close;
end;
end);
Result := Exists;
end;

procedure TServobj.TcpServerExecute(AContext: TIdContext);
var
Command: String;
....................
..................
begin

Command := AContext.Connection.Socket.ReadLn; //read command

 if Command = 'LOGIN' then
  begin

  usrnm := Params[1];
  passwd := params[2];

if not userexists(usrnm, passwd) then// i want to synchronize this 
begin
 AContext.Connection.Socket.WriteLn('INVALIDPASSWORD');
 end;

end;

is this correct ?
بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِ
in god i trust with every movement i do
graduated student and looking for knowledge
Kullanıcı avatarı
mrmarman
Üye
Mesajlar: 4741
Kayıt: 09 Ara 2003 08:13
Konum: İstanbul
İletişim:

Re: indy tcp server synchronize inside server execute

Mesaj gönderen mrmarman »

Hi.
Embarcadero says in this link yes you can.
An example of when you would want to use Synchronize is when you want to interact with either a VCL or a FireMonkey component. Use an in-place anonymous method to solve the problem of passing variables to the method you want to sychronize: yazdı:

Kod: Tümünü seç

Synchronize(
  procedure
  begin
    Form1.Memo1.Lines.Add(‘Begin Execution’);
  end);
- IMHO do not try to access that AContext unless it is not a member of Thread.
- Only use a procedure for Synchronize which triggers any event.

- Trigger may be an VCL.TEXT that that triggers an event (you write a text to TEdit in procedure and TEdit's OnChange event fires automatically) or a Global Variable which a timer or boolean controlled infinite loop waits for it.
Resim
Resim ....Resim
Kullanıcı avatarı
mia
Üye
Mesajlar: 239
Kayıt: 17 Nis 2015 02:18

Re: indy tcp server synchronize inside server execute

Mesaj gönderen mia »

so should i derive a thread class and get it work with server execute ? if yes there is sample of that or better way
بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِ
in god i trust with every movement i do
graduated student and looking for knowledge
Kullanıcı avatarı
mrmarman
Üye
Mesajlar: 4741
Kayıt: 09 Ara 2003 08:13
Konum: İstanbul
İletişim:

Re: indy tcp server synchronize inside server execute

Mesaj gönderen mrmarman »

Yes it looks you can.
Try it and inform us with your experience please.

One more other point some issues i stucked with threads before.
When i create a panel or some thing on the fly and make it's parent to the main thread's any win control, finally could not free it.
So be careful like this inter-thread VCL based creations.
Resim
Resim ....Resim
Kullanıcı avatarı
mia
Üye
Mesajlar: 239
Kayıt: 17 Nis 2015 02:18

Re: indy tcp server synchronize inside server execute

Mesaj gönderen mia »

i tried to synchronize using tidnotifay but i have bad feeling about it , when i try to use TThread i got complex in my mind and dont know where to start

here is the server project with trying of using Tidnotify
Dosya ekleri
Server.rar
(60.43 KiB) 74 kere indirildi
بِسْمِ اللهِ الرَّحْمنِ الرَّحِيمِ
in god i trust with every movement i do
graduated student and looking for knowledge
Kullanıcı avatarı
mrmarman
Üye
Mesajlar: 4741
Kayıt: 09 Ara 2003 08:13
Konum: İstanbul
İletişim:

Re: indy tcp server synchronize inside server execute

Mesaj gönderen mrmarman »

Start from looking at here
Good explaination on that link about TIdSync, TThread.Synchronize() and TThread.Queue()

And then, look at this link for TeamB's answer
Take your decision after that.
Resim
Resim ....Resim
Cevapla