0

I'm working on a multi-threaded tcp game server (using ICS). I'm looking at potentially hundreds of different possible commands coming from the clients. For example the command 'get_skills' will tell the server to stuff the db table Skills into a JSON and send it back to the client. Other incoming commands will have a data component like, 'train_skill:1'

Where I'm having major brain hemorrhages:

With so many possible commands coming in from the client, what will be the best way to route these commands to the proper function/procedure? Something like a lookup table would be nice so that I don't have to hard-code all these commands.

I could do a case statement but that could end up being huge and I'd have to convert the incoming command from a string to an integer and deal with that mess.

I could do a massive if-then-else block but that would be even more nightmarish.

I looked at the options listed here: Delphi: Call a function whose name is stored in a string

The only one of those solutions that even looked viable was the superobject and after downloading it and screwing around for a couple of hours trying to get it to work, I gave up on that.

I looked at the dispatchtable solution and could not figure out how to pass parameters to the procedures. Apparently TProc, TProcedure and procedure are 3 different things that don't play nice with each other.

Other than that, none of them seemed to have the ability or option to pass data to a procedure which would be essential.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
uPrompt
  • 159
  • 1
  • 10
  • Right, I did get that working with And it would call the procedure. But that didn't allow me the ability to pass parameters to it and I couldn't figure out the syntax to allow that. – uPrompt Feb 08 '22 at 15:09
  • Do as @DavidHeffernan said : don't use a TProc but a procedure, function or method with the parameters you need to pass. Something like TMyProcType = procedure (Arg1 : Integer; Arg2:String) of object; and then TDictionary. – fpiette Feb 08 '22 at 15:31
  • Just an FYI, depending on the actual format of the commands on the wire, Indy's `TIdCmdTCPServer` may or may not be able to handle them without you having to setup the lookup table in code at all, it can be setup at design-time instead, with an event handler assigned to each command that receives parsed parameters. – Remy Lebeau Feb 08 '22 at 16:33
  • Thanks François. I get this running and I definitely.... DEFINITELY need to send you a post card! – uPrompt Feb 08 '22 at 16:33
  • Remy... oh, wow. I'd never played with that before. And Indy tcp is multi-threaded? I don't need a time-critical server but one that can handle a few hundred clients doing remote mysql. – uPrompt Feb 08 '22 at 16:49

0 Answers0