Recent

Author Topic: Synapse abort timeout  (Read 9273 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Synapse abort timeout
« on: May 31, 2020, 01:11:41 pm »
Hello!

I want to abort a timeout when I know if I don't need the listening, but I don't want ot close the connection. How can i? There is some command?
« Last Edit: May 31, 2020, 01:14:04 pm by Jake012345 »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #1 on: May 31, 2020, 05:28:52 pm »
You can't abort a timeout. Once given, the connection will wait until that timeout.

You can choose a smaller timeout and build a loop around the communication. In that case you can jump out the loop when you want or when multiple number of smaller timeouts have occurred.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #2 on: May 31, 2020, 06:20:43 pm »
Okay.

BTW
I could make my application without freezing, just with Application.ProcessMessages is 'blcksock' unit. That's the final repair, because when the transfer done, the application freezing for timeout interval.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #3 on: May 31, 2020, 06:29:46 pm »
I could make my application without freezing, just with Application.ProcessMessages is 'blcksock' unit. That's the final repair, because when the transfer done, the application freezing for timeout interval.
After recvstreamraw it should just continue, not freeze.
So I'm but sure what's in your code that does the freezing.

And I don't think you can put processmessages in blcksock. Synapse is a blocking library so during transport your calling thread (which might be your main thread) will freeze. The only solution to that would be to implement communication in separate threads.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #4 on: May 31, 2020, 06:50:23 pm »
do I submit the project?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #5 on: May 31, 2020, 06:58:22 pm »
do I submit the project?
In lazarus you can choose project>publish
Give a clean directory and publish.
You can zip that directory and attach it to a post.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #6 on: May 31, 2020, 08:41:30 pm »
Thanks!

that's useful.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #7 on: May 31, 2020, 08:43:51 pm »
OR

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #8 on: May 31, 2020, 10:07:09 pm »
Ok. I had a look at the non-modified version.
It seems the RecvStreamRaw() version always waits until the timeout to know that it has the end of the file. It might be better to use RecvStream() (of RecvStreamIndy). With RecvStream(), the size of the file is first transferred and the client knows exactly how much data it should receive. So there is no need for the timeout.

So change Connection.SendStreamRaw to Connection.SendStream
and Connection.RecvStreamRaw to Connection.RecvStream
After that, you don't have a freeze anymore.

Next... if the target file already exists I would add " (1)" to the filename. If that already exists I would add " (2)" etc.

And finally the Unable to create file "".
Somehow... after the filetransfer is done... the GetTimerTimer is triggered again while mode is still fileaccept. That triggers a second TFileStream.Create on an empty filename (which gives the error).

I tried to put a GetTimer.Enabled := false; at the top and a GetTimer.Enabled := true; at the bottom of the timer event but that doesn't work. Although I definitely would add those lines anyway.

For now you could make the line this:
  if (mode='fileaccept') and (infile_name <> '') then begin
In that case at least it won't try to create the file

Anyway... The second project with the modified blcksock.pas isn't needed.
I also noticed you had a loop around RecvStreamRaw in that modified project.
That's also not correct because RecvStreamRaw should receive the entire stream and you don't want a loop there.
And the loop on FileSize() isn't even useful because the stream might not have had the time to write it's buffer to the file so the filesize if only definite when .free is called.

Anyway... I would rake your first project and see if you can figure out why the second trigger on the timer is called with infile_name=''.

Edit: I think I found out why the GetTimerTimer gives the unable to create "" error.
You send on the server side a string with Connection.SendString('>StartSendFile'
But DIRECTLY afterwards you send the stream.
It's better to build in a delay before sending so the client has time to set up the receiving end.
(That's why I in the beginning suggested a separate socket for filetransfer might be better)

Code: Pascal  [Select][+][-]
  1. Sleep(1000);
  2. MainStream := TFileStream.Create(OpenFileDialog.FileName, fmOpenRead);
  3. Connection.SendStream(MainStream);
  4. MainStream.Free;

This should be much better.

O, one final note... During transfer your application does freeze. But that's because the library is 'blocking'. You could hack the synapse code. But if you really want to fix that you'll need threads.
« Last Edit: May 31, 2020, 10:29:28 pm by rvk »

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #9 on: June 01, 2020, 01:14:08 pm »
Okay.

I had some errors when I tried build in, the treading so I want to test it, in a new app.
But what is the most simple way to overload one thread?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #10 on: June 01, 2020, 01:45:54 pm »
But what is the most simple way to overload one thread?
What do you mean by 'overload one thread'.

You can begin to move the sending and receiving of files to a separate thread. But in that case I would also implement a separate socket/port for the binary raw data. Because you can't use a socket in two thrwads at the same time. And in that case you also separate string from binary.

On sending side you can implement a small loop to wait for a new connection (you can do this in the main thread). When the connection is established, move the socket to a thread to do the sendstream. On the receiving end you can directly establish the connection and move it to a thread for recvstream.

There is a simple example for tcp communication and threads somewhere in the wiki or forum (can't look right now, about to head out for some golf 🌞).

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #11 on: June 01, 2020, 03:26:19 pm »
I did it, but I have Access Violation and SIGSEGV errors.

EDIT:
I forgot to create the threads.
But now it works like without threads...
« Last Edit: June 01, 2020, 06:11:03 pm by Jake012345 »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #12 on: June 01, 2020, 10:58:32 pm »
I take it the version attached to your post is where you didn't create the connectionthread.

There are some more remarks.
*) you can't call logging directly in your thread. It might work now but tmemo isn't thread-safe and you can't write directly to a variable (so tmemo) from the mainthread. You must use synchronize for that. If you do it like you have now you can have unexpected freezes.
*) you have the filesocket.free now in the thread. In that case I would also create it in the execute (or create) or the thread.
*) you have a lot of global variabele. You actually should have no global variabele whatsoever but they should all be moved somewhere else. It does work now but it will only become more complicated.

For now it should work fine (seeing the code without executing it) but I would move some things around (which I can't go into right now typing on a small mobile but especially those global variables). For example, while one file is transfering you should be able to keep typing text and even start a second file-transfer. That doesn't seem possible now.

(For multiple filetransfers you could create a tlistbox (aligned at bottom) of current file transfers and show a progressbar besides them)

But that all depends on how sophisticated you want to made it.



Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Synapse abort timeout
« Reply #13 on: June 02, 2020, 01:33:53 pm »
I think now it shoud work, but I got RunError (Unable to create file)
in the accept file section in TCustomThread.Execute procedure.
Code: Pascal  [Select][+][-]
  1.  MainStream:=TFileStream.Create(infile_name,fmCreate);
  2.  
And sometimes SIGSEGV unable to open file error in the sender.


« Last Edit: June 02, 2020, 01:51:44 pm by Jake012345 »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Synapse abort timeout
« Reply #14 on: June 02, 2020, 01:44:18 pm »
I think now it shoud work, but I got RunError (Unable to create file)
Your infile_name is probably empty.

You should pass the infile_name to the create function of the thread.
There you should put it in a local thread variable and then you can use that in the execute procedure.

Also... the error in your picture is because you have a strtoint somewhere, where the string is empty (and you can't convert an empty string into a integer).

Show the complete code of the thread again and the snippet of how you create and call the thread.
(I can make the create constructor of the thread if you can't)

 

TinyPortal © 2005-2018