I've got a client/server gaming app where I use an Indy TCPServer component (ver 10.6.3.4) for my packet exchange system via websocket. I've hand-coded my own websocket protocol since it's not native to Indy. Currently my OnExecute loop calls a function that sends any outbound packets that are ready in a queue and then reads the next inbound packet like this:
At the bottom of my OnExecute is a Sleep(10) command to keep the CPU from spinning to 100%.
All this has worked fine for many years now but there is a limit to how many connections (a few hundred) this system can support. I'm having Claude Code look at this and it's telling me the Sleep command is the main bottleneck as the system will eventually spend most of its time thread switching rather than running the game. It initially suggested using a blocking read command with a timeout (allowing me to delete the Sleep) and then moving the outgoing packet writes to a separate worker thread. That all sounded good until we got into the details and now it's telling me "OpenSSL isn't safe for truly concurrent SSL read / SSL write on the same connection without locking". So is this true? Will I have to implement critical sections or MREW locks to prevent any chance of a simultaneous read/write? Putting a lock around the blocking read is highly problematic since it may stay for awhile and that kind of defeats the efficiency improvement I'm trying to make. By the way, I know my Indy install is nearly 2 years out of date. Updating is on my to-do list.
Code:
// loop here to send any ready outgoing packets
// now check for incoming packet
if IO.InputBufferIsEmpty and not(IO.CheckForDataOnSource) then Exit;
Opcode := IO.ReadByte; // start of packetAt the bottom of my OnExecute is a Sleep(10) command to keep the CPU from spinning to 100%.
All this has worked fine for many years now but there is a limit to how many connections (a few hundred) this system can support. I'm having Claude Code look at this and it's telling me the Sleep command is the main bottleneck as the system will eventually spend most of its time thread switching rather than running the game. It initially suggested using a blocking read command with a timeout (allowing me to delete the Sleep) and then moving the outgoing packet writes to a separate worker thread. That all sounded good until we got into the details and now it's telling me "OpenSSL isn't safe for truly concurrent SSL read / SSL write on the same connection without locking". So is this true? Will I have to implement critical sections or MREW locks to prevent any chance of a simultaneous read/write? Putting a lock around the blocking read is highly problematic since it may stay for awhile and that kind of defeats the efficiency improvement I'm trying to make. By the way, I know my Indy install is nearly 2 years out of date. Updating is on my to-do list.

