Recent

Author Topic: Synapse abort timeout  (Read 3157 times)

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse abort timeout
« Reply #75 on: June 05, 2020, 04:52:30 am »
And Why is that send the message twice if I click on the button although the Enter on edit is works fine?
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4159
Re: Synapse abort timeout
« Reply #76 on: June 05, 2020, 09:41:47 am »
I wrote this:
Code: Pascal  [Select][+][-]
  1. procedure TCustomThread.MonitorHook(Sender: TObject; Writing: boolean; const Buffer: Pointer; Len: Integer);
  2. begin
  3.   inc(ProgressRefreshCooldown,1);
  4.   if ProgressRefreshCooldown>=ProgressRefreshRate then begin
  5.   Synchronize(@ShowProgress);
  6.   ProgressRefreshCooldown:=0;
  7.   end;
  8. end;    
  9.  
And I think thats good.
But I have a qusetion:
how fast it repeats? millisecons or faster?
It might be better to check GetTickCount64 instead of relying on a counter. With GetTickCount64 you can specify the exact timespan on which you want to execute the ShowProgres.

Something like
Code: Pascal  [Select][+][-]
  1. procedure TCustomThread.MonitorHook(Sender: TObject; Writing: boolean; const Buffer: Pointer; Len: Integer);
  2. begin
  3.   if GetTockCount64>=NextTickCountCheck then
  4.   begin
  5.     Synchronize(@ShowProgress);
  6.     NextTickCountCheck:=GetTickCount64 + 500; // 500ms but could also be 1000ms depending on what you want
  7.   end;
  8. end;

rvk

  • Hero Member
  • *****
  • Posts: 4159
Re: Synapse abort timeout
« Reply #77 on: June 05, 2020, 10:01:05 am »
And Why is that send the message twice if I click on the button although the Enter on edit is works fine?
That has nothing to do with the communication.
If you set a Log.Lines.Add('a') at the top of the buttonclick you will see it is executed twice.

I could also not figure out why until I removed the button and placed a new button (with other name).

It then gave me an error on
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MessageEditEditingDone(Sender: TObject);
  2. begin
  3.   SendMessageButton.Click;
  4. end;        

There you Simulate the SendMessageButton.Click.
But THAT event is also triggered when you click the button AND when you are done editing.
So you end up with two buttonclicks (and two sends).

The TEdit is smart enhough not to trigger MessageEditEditingDone() when you didn't change anything in the TEdit. That's why the second time you do a click, only one message gets send.

So make a separate procedure SendMessage and call then individual from both MessageEditEditingDone and SendMessageButtonClick.

BTW, Don't use LineEnding but use CRLF in this case. Synapse really expects CRLF and LineEnding is platform specific.

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse abort timeout
« Reply #78 on: June 05, 2020, 10:11:42 am »
Thanks.
But I'll do it with:

Code: Pascal  [Select][+][-]
  1.   if connected then begin
  2.     if MessageEdit.Text<>'' then
  3.     Connection.SendString(MessageEdit.Text+LineEnding);
  4.    MessagEdit.Text:='';
  5.   end;  
  6.  
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4159
Re: Synapse abort timeout
« Reply #79 on: June 05, 2020, 10:20:11 am »
Thanks.
But I'll do it with:
That's also possible. But the Buttonclick still gets executed twice (which isn't really what should happen).
Setting the TEdit to '' just so the send doesn't happen on the second ButtonClick is kind of a 'hack'  ;)

Jake012345

  • Full Member
  • ***
  • Posts: 243
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Synapse abort timeout
« Reply #80 on: June 05, 2020, 12:14:47 pm »
Okay. I think, I'm done with my App.
Here is:

Link

(The file was too large)
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4159
Re: Synapse abort timeout
« Reply #81 on: June 09, 2020, 01:52:54 pm »
(The file was too large)
You can usually omit the .res file. The publish function also doesn't copy that file.

I have some small suggestions (if I may).
*) The Sleep(2000) in the Execute isn't needed anymore. Because the server has a separate socket, and will listen (and wait) until that socket is connected, the sleep can be removed.
*) Also... the repeat until loop is not needed. The Listen function waits until the connection is made. You can do one CanRead on that socket to make sure the connection is there. So no need for the loop.
*) You had a final Form1.Progress = 100% in the Execute. It would be easier (and safer) to call ShowProgress at the end of the sending and receiving. Because the pointers ALWAYS are on the end of the file, the 100% progress is automatically reported.

*) One extra thing. Code is easier to read if you do proper code indentation. You can do that automatically. In Package > Install Package you can install jcfidelazarus 2.0. If installed (and IDE restarted) you can press Ctrl+D to automatically indent your code correctly.
The only setting I usually change is Tools > Options > JCF Format Settings > Clarify > Line Breaking to Never.
(I like to break my lines manually if needed)

You get something like this.
Code: Pascal  [Select][+][-]
  1. procedure TCustomThread.Execute;
  2. begin
  3.   if connected then
  4.   begin
  5.     if mode = 'filesend' then
  6.     begin
  7.       FileServer := TTCPBlockSocket.Create;
  8.       FileServer.CreateSocket;
  9.       FileServer.Bind(Form1.IpEdit.Text, IntToStr(StrToInt(Form1.PortEdit.Text) + 1));
  10.       FileServer.Listen; // will wait for connection
  11.       if FileServer.CanRead(60000) then // is connection made?
  12.       begin
  13.         FileConnection := TTCPBlockSocket.Create;
  14.         FileConnection.OnMonitor := @MonitorHook;
  15.         FileConnection.CreateSocket;
  16.         FileConnection.Socket := FileServer.Accept;
  17.         MainStream := TFileStream.Create(Form1.OpenFileDialog.FileName, fmOpenRead);
  18.         FileConnection.SendStream(MainStream);
  19.         Synchronize(@ShowProgress);
  20.         StatString := '>:Transfer Done!';
  21.         Synchronize(@ShowStat);
  22.       end;
  23.     end;
  24.     if mode = 'fileaccept' then
  25.     begin
  26.       FileConnection := TTCPBlockSocket.Create;
  27.       FileConnection.OnMonitor := @MonitorHook;
  28.       FileConnection.CreateSocket;
  29.       FileConnection.Connect(Form1.IpEdit.Text, IntToStr(StrToInt(Form1.PortEdit.Text) + 1));
  30.       if FileConnection.LastError = 0 then
  31.       begin
  32.         MainStream := TFileStream.Create(infile_name, fmCreate);
  33.         FileConnection.RecvStream(MainStream, 60000);
  34.         Synchronize(@ShowProgress);
  35.         if MainStream.Size = StrToInt(infile_size) then
  36.           StatString := '>:Transfer Done!'
  37.         else
  38.           StatString := '>:Transfer FAILED!';
  39.         Synchronize(@ShowStat);
  40.       end;
  41.     end;
  42.     mode := 'none';
  43.     MainStream.Free;
  44.     FileServer.Free;
  45.     FileConnection.Free;
  46.   end;
  47. end;

 

TinyPortal © 2005-2018