Recent

Author Topic: [Ararat Synapse] TCP server threading  (Read 206 times)

mewpokemon

  • New Member
  • *
  • Posts: 29
[Ararat Synapse] TCP server threading
« on: November 02, 2019, 07:47:10 pm »
Heyas!

I was searching for examples of a multi-threaded server using Ararat Synapse and found this thread:

https://forum.lazarus.freepascal.org/index.php/topic,21024.msg122614.html#msg122614

It wasn't really useful in the end because the code given had too many problems.

I've now written my own multi-threaded TCP server solution.  It uses hashed buckets for workers so the thread count can stay fairly low.  This is quite unique, I haven't seen any other solutions using this method. 

I'm putting the code in the public domain but I would like to have some attribution if you use it.  Also, if you do use it and improve it somehow, I'd like to hear from you, of course.  I've made the license the same as Ararat Synapse.

The code is part of my M3wP Yahtzee game and requires a unit from there but only to define the message class.  I should really break that out into another unit since it doesn't really require the full detail of the Yahtzee message class.  I've put the definition of the class in comments for you.

It would be easy enough to change the hash function and number of buckets but I think 32 workers is a good limit for most applications, anyhow.

Here is the link:  https://github.com/M3wP/M3wPYahtzee/blob/master/src/TCPServer.pas

I hope you like it!


Daniel.

mewpokemon

  • New Member
  • *
  • Posts: 29
Re: [Ararat Synapse] TCP server threading
« Reply #1 on: November 02, 2019, 08:05:40 pm »
Oh I forgot..  To use it, simply call the code like this:

Code: Pascal  [Select]
  1.    
  2.         TCPServer.TCPServer:= TTCPServer.Create;
  3.         TCPServer.TCPServer.OnConnect:= DoConnect;
  4.         TCPServer.TCPServer.OnDisconnect:= DoDisconnect;
  5.         TCPServer.TCPServer.OnReject:= DoReject;
  6.         TCPServer.TCPServer.OnReadData:= DoReadData;
  7.  
  8.         TCPListener:= TTCPListener.Create(aPort);
  9.  

When you're done, call:

Code: Pascal  [Select]
  1.         TCPListener.Terminate;
  2.         TCPListener.WaitFor;  
  3.  
  4. //Optional
  5.         TCPServer.Free;
  6.         TCPServer:= nil;
  7.  

Enjoy!


Daniel.