I have data affixed with a time-stamp coming from my client and passed to my server via web sockets.
Sometimes the order will be wrong so that means the timestamps will be in the wrong order. So, I created a SortedDictionary collection object:
public SortedDictionary<Int64, string> Manifest = new SortedDictionary<Int64, string>();
This data is coming thick and fast (as quickly as possible) and is real-time with as much as 10 data 'blocks' every second.
But, also, sometimes it can take up to 10 seconds for the next data 'block' to come through.
The other client that should recieve the correct order of this data stream needs to be real-time as well.
But, this real-time demand cannot be done as far as I can see so I thought about creating a cache of say 10 seconds worth of data.
Now, you would think that this would be easy but I have had errors such as:
'entry already added'
'cannot find key'
These errors seem to be derived from the fact you cannot modify this type of collection at the same time as reading it off.
My code is this so far:
In my receive event in my web socket I have this:
Manifest.Add(timestamp, 'data');
and in, say my timer (running every 100ms):
var pts = Manifest.ElementAt(0);
var key = res.TS;
//my 10 second cache
if (Convert.ToInt64(DateTime.Now.ToString("yyyyMMddHHmmssfff")) - key >= 10000)
{
//send the data
SendDataToClients(key .data);
//remove send data from queue
Manifest.Remove(res);
}
Now, I am sure people will say the problem is that I am adding to this collection at the same time as I am reading it and a way to solve this is to use a lock.
But I am loathed to use a lock because it can create an overhead and as I am dealing in real-time( albeit with a second lag) I cannot afford ANY overhead.
Are they any other approaches? I have working on this all day and have tried many many things.
The above was my best approach.