I have an application that is sending messages over a socket from different threads. I create my own wrapper for the socket that enables me to extend functionality.
Example - on new SocketConnection() it returns only one instance based on IP and port. So if some other thread tries to create instance of IP 184.108.40.206 and port 1000 every time it gets the same instance if it already exists. (Single instance for one device.)
- Auto connect
- State machine for connection etc.
Every unique instance has a send message queue (from higher app) and has a Task/Thread that is running in a loop and checking if something is in the queue to be sent, then processes socket.send (if queue is not empty). When the send is successful or fails then respond using a callback to the process that added the message to the queue.
I'm not sure if this is a good solution to have for each unique instance a Task/Thread running in a loop. Imagine I have 100 endpoint devices (machines) and my app is from different threads sending data to these devices.
If I have 100 devices to communicate with, than I have 100 unique instances with 100 threads/Tasks running in loop and processing queues.
App generates to one of these devices (for example 50 requests to send from different upper threads) to destination device with IP 220.127.116.11 port 70. My unique class for this IP, adds this to messages queue and thread-loop of this instance, processes these messages one by one. Upper level just gets callback if socket.send() was OK or failed.
Is this good way how to process many connection? The same way I'm receiving responses from devices. (The Device is a server.)
What I know now is that using create new thread is faster than using Task for loops in each instance. Is it better to use timer interrupt? Or what's the best way to process incoming communications?
Are there any patterns how to do that?