Recent

Author Topic: Synapse TCP/IP client and server(Second Edition)  (Read 3787 times)

balazsszekely

  • Guest
Synapse TCP/IP client and server(Second Edition)
« on: March 30, 2023, 02:26:58 pm »
I rewrote a large part of the client/server application from the following thread: https://forum.lazarus.freepascal.org/index.php/topic,48677.0.html
Hopefully I managed to address all the issues mentioned there:
  - dropped RecvPacket(@kupferstecher)
  - thread safety(@kupferstecher and others)
  - the server's thread list now is properly locked with critical sections, each thread waits until the list is unlocked, no more connections are dropped(@piola)
  - server works as a daemon/service too(@datilas), tested only on windows
  - proper variables initialization(@Phoenix)

What's new:
  - each file transfer has been moved to a worker thread, parallel file transfer is now possible
  - newly connected/disconnected clients are highlighted for a custom amount of time
  - added a new project(server_stress_test), server can be tested up to few thousand connections(requires 64 bit, see notes for more details)
  - smaller stack size for each thread
  - many more

Notes:
  - there is a limited amount of threads that a 32 bit process(2 GB address space) can create. For better performance please build synapse client/server with a 64 bit version of Laz/FPC
  - developed with Lazarus_Trunk/FPC_3.2.0(64 bit) on the following OSs: Windows 10(64 bit) and Linux Mint Cinnamon 20.02(64 bit), with synapse trunk(rev. 267)
  - ico and res files are removed(forum attachment size limitation), please re-add the icons
  - I did not have enough time to thoroughly test the projects
« Last Edit: April 02, 2023, 09:25:41 am by GetMem »

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #1 on: March 30, 2023, 05:02:12 pm »
looking at the demo: synapse_daemon and synapse_client
imagining a scenario where synapse_daemon is active
and have 3 synapse_client connected on the server.

I couldn't understand how I could send message
from client1 to client2 or client 3
and I also could not understand how to send messages from any client to all clients

I'm a beginner so I'm asking
is there any demo in this situation?
Sorry for my English I use google translator.

balazsszekely

  • Guest
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #2 on: March 30, 2023, 05:33:23 pm »
@datilas
Quote
looking at the demo: synapse_daemon and synapse_client
imagining a scenario where synapse_daemon is active
and have 3 synapse_client connected on the server.

I couldn't understand how I could send message
from client1 to client2 or client 3
and I also could not understand how to send messages from any client to all clients
You mean like in a chat application? This is not implemented currently, though it shouldn't be too difficult to implement. I mean the messaging part, audio/video streaming is done usually via udp hole punching, much more difficult to implement.

Quote
I'm a beginner so I'm asking is there any demo in this situation?
Unfortunately no.

Quote
Sorry for my English I use google translator.
No problem, I understood everything.

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #3 on: March 30, 2023, 05:35:28 pm »
it would be great if no server exists a method like:
GetClients so clients can see each other

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #4 on: March 30, 2023, 05:38:36 pm »
@datilas
You mean like in a chat application? This is not implemented currently, though it shouldn't be too difficult to implement. I mean the messaging part, audio/video streaming is done usually via udp hole punching, much more difficult to implement.
yes. chat application.
in my situation it would be just text.

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #5 on: March 30, 2023, 08:29:21 pm »
Let me explain my situation:
I have an application that runs on multiple clients and multiple terminals per client.
ex:
client1
terminal1, terminal2, terminal3 ...

client2
terminal1, terminal2, terminal3 ...

I would like to upload synapse_daemon to a VPS
and my application would connect with the synapse_daemon as soon as it runs
and from my office I can send specific messages to each client or terminal
or all terminals of a client.

I could do this control using:
Connection.FUser := client1_terminal1;

in another terminal:
Connection.FUser := client1_terminal2;
...

but i would like to send a message from my office to anyone or everyone
if there was a GetClients method on the server that returns all the necessary parameters to use the "SendMessage(AAC: TAcceptedClient; AMsg: String; AParams: TStringArray)" from the client
I could filter from my application who would be the recipient.

I do not know if i was clear.
but do you have any idea how to do this?
thank you very much.

balazsszekely

  • Guest
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #6 on: March 30, 2023, 09:16:46 pm »
@datilas
I understood what are you after and as I mentioned in one of my previous post is not too difficult to implement it, however it does require time, which I don't have. I think your best shot is to convert a simple chat application coded in Indy. Just do a quick google search, I saw in the past chat demos on the net.

Phoenix

  • Jr. Member
  • **
  • Posts: 87
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #7 on: April 01, 2023, 09:57:18 am »
yes. chat application.
in my situation it would be just text.

as a simple idea, on the server side when you edit the user list you can send an "update" message to all clients. Then the "chat-like" clients that need it, send the server a request for the list (the server will send via stream). Now that both have a synchronized list, you can use the parameter field to determine the client (for example for a private message) or without a recipient by sending back to all in broadcast from the server.
However in any case the demo of @GetMem gives you all the tools to create what you want.
Note: my English is also bad and I use google translate a lot so I hope you understand enough of the generic idea.

@GetMem
Thanks for this great improvement! There is also the stress test.  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #8 on: April 01, 2023, 11:06:34 am »
udp hole punching, much more difficult to implement.
Well it is actually just a pair of sockets. It is a bit cumbersome, but it is not really difficult for you, although it IS difficult for beginners.
I will add some code later, have to look it up. ((and translate it from fcl-web and fcl-net back to synapse)
Note that hole punching is something that can be added to both and may be is a good idea to add an example. Will do my best.
« Last Edit: April 01, 2023, 11:12:54 am by Thaddy »
Specialize a type, not a var.

balazsszekely

  • Guest
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #9 on: April 02, 2023, 09:34:56 am »
@Thaddy
Quote
Well it is actually just a pair of sockets. It is a bit cumbersome, but it is not really difficult for you, although it IS difficult for beginners.
In order to cover all the NAT traversal mechanism you have to do a lot of work in my opinion.

balazsszekely

  • Guest
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #10 on: April 02, 2023, 09:41:37 am »
@datilas

I attach a chat like demo application where connected clients can communicate with each other(only messages, no file transfer or streaming). Start server or daemon, connect a few clients then click "Remote clients" as in the attached image.

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #11 on: April 03, 2023, 03:19:49 pm »
Thank you very much.
it helped me a lot.
it's exactly what i need.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #12 on: April 03, 2023, 03:46:47 pm »
  - added a new project(server_stress_test), server can be tested up to few thousand connections(requires 64 bit, see notes for more details)
On a side note for high throughput/heavy load servers it is unwise to scale with threads. By default Linux only allows around 65k processes, which includes all threads of all programms running. If you spawn up a thread for each incomming connection with thousands of connections per second, you are going to take a substantial chunk of the PID space. Also threads are quite expensive, so it's not just that PID limit, but also the resources available.

Therefore what is usually done is to have one thread serve multiple (up to a few thousnad) connections, and serve them asynchronously through select/(e)poll (or WaitForMultipleObjects on Windows) together with non-blocking operations (as context changes for blocking operations are quite expensive).

I'm not so familiar with synapse, I ususally use the "raw" berkley style sockets for raw TCP or UDP networking, so I don't know how good this is implementable with synapse, but might also be interesting to study for stress tests

balazsszekely

  • Guest
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #13 on: April 03, 2023, 07:31:29 pm »
@Warfley
Synapse uses blocking sockets, with very limited support for non-blocking mode, so one thread per connection is required. This model has some advantages, the most important one it's easy to code because of the sequential processing. You already mentioned the cons: threads are expensive, there is a limited number of threads that can be created, etc...and yes I agree it's not scalable, but then not everybody needs high performance servers, capable of handling 100,000+ connections, it depends on use case.

datilas

  • New Member
  • *
  • Posts: 14
Re: Synapse TCP/IP client and server(Second Edition)
« Reply #14 on: April 04, 2023, 05:09:14 pm »
a TimeOut property on the client would be very good for networks with high latency on the server and low connection on the client
so we could control the connection timeout.

 

TinyPortal © 2005-2018