Uniqueness of Guid.NewGuid ?
I noticed that Guid.NewGuid is used for each peer, I know that most likely, it will be a unique Guid, but is it guaranteed?
Uniqueness is guaranteed for a host which is important to keep server sessions in dictionary by guid Id. For different peers probability is dramatically low and is not so important. For example we need client Id only for logging.
I think it’s important to guarantee uniqueness because this is just a potential source of problems
In TcpServer.cs, RegisterSession uses Sessions.TryAdd but doesn’t handle a false return value (Aka duplicate guid)
https://stackoverflow.com/questions/8642858/what-are-the-chances-to-get-a-guid-newguid-duplicate
In next version I'll add virtual Guid CreateGuid() method, which you can override in your code if you need more paranoid UUID generation. For example create a cryptographically secure random GUID in .NET
I’ve seen that stackoverflow post before, but I don’t see any hard evidence. After researching:
From dotnet runtime: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Guid.Windows.cs
Function being used: https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateguid
To a very high degree of certainty, this function returns a unique value
I stand corrected! Sorry!
https://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two
After reading this I feel less confident…
Instead of using a Guid identifier, what about using Interlocked.Increment ?
Instead of using a Guid identifier, what about using Interlocked.Increment ?
i like this idea
https://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two
After reading this I feel less confident…
You're mistaking randomness and uniqueness. GUIDs do not guarantee any useful pseudorandomness, they are not built for that purpose. GUIDs are however perfect for uniqueness, as a relevant comment from one of your linked stackoverflow posts demonstrates:
Statistically speaking, you would be more likely to win the lottery 30,382 times, consecutively, than to run into a single pair of GUIDs within a sample of 1,000,000,000,000 GUIDs.
If you get a collision... go buy a ticket!
Instead of using a Guid identifier, what about using Interlocked.Increment ?
This only makes things worse in terms of uniqueness, considering that this library is built for networked applications, and Interlocked only guarantees uniqueness within a single process.