c# - How to distribute collection of values to dynamic list? -
I am developing a TCP client-server console application. I have the number of customers connected to the server. The record needs to be shared and every record should be sent to the customer only once. Each record should only be received by 1 customer. For example records number of N = 4 and number of subscribers should be M = 10 and result:
record1 â ???? Client1 for Client1 Record2 Client 3 for Record 3 Client4 in record 4 in Client4 record 5 for Client1 record6 ???? Client 5 for Client3 Record 7 Client3 for record 9 Client 5 for record 10 Client 5 The problem is that the number of record M is fixed, but the number client N is not fixed (Sometimes N = 3, sometimes N = 5, etc.)
Can you please suggest a solution to adjust the flow control of this type?
I will try to implement something like this:
-
You will need a list or all customers will be gathered to connect. You will need random access, i.e. you have to add the client front or back and you will have to be able to remove an element at any time. Bonus points if the list is sorted by the number of records assigned (ascending).
-
There is a new record for the process (or when you iterate over a list with them):
- Send the record in front of
- Remove the customer from the list and add it back (unless the list is sorted
-
when a new customer is connected It is added in front of your list.
- If a client disconnects, it is removed from the list and the record is read for redistribution.
If I do not make any mistake while thinking about it, then it should be ensured that At least the number of tasks will get a new record, as long as many clients do not drop mid-processing. In that case, a sorted list will have an advantage.
Of course, the place for optimization And this is not the best strategy if the number of your customers does not change during processing (and only before doing this). In this case, you can easily set that customer. n code> i = n% number_of_clients is in the process of record.
Comments
Post a Comment