Exemple #1
0
 public static void HandleClientHello(LobbySession session, ClientHello hello)
 {
     session.CalculateNetworkKey(hello.Time, hello.Seed);
     session.SessionHash = XxHash.CalculateHash(Guid.NewGuid().ToByteArray());
     session.Send(new ServerHello
     {
         SessionHash = session.SessionHash
     });
 }
Exemple #2
0
        public unsafe void CRCBenchmark()
        {
            var lens = new int[] { 1, 2, 3, 4, 7, 8, 9, 15, 16, 31, 32, 63, 64, 255, 256, 511, 512, 1023, 1024 };

            foreach (var len in lens)
            {
                var arr    = new byte[len];
                var memory = (Memory <byte>)arr;
                var handle = memory.Pin();
                var ptr    = (byte *)handle.Pointer;
                var rng    = new Random(42);
                rng.NextBytes(arr);
                var count = 1_00_000_000;
                var cnt   = count / len;
                var sum   = 0UL;

                {
                    uint digest = 0;

                    using (Benchmark.Run("xxHash", count, true))
                    {
                        for (int i = 0; i < cnt; i++)
                        {
                            digest = XxHash.CalculateHash(ptr, len, digest);
                        }
                    }
                }

                uint crc0, crc1, crc2;

                {
                    uint digest = 0;
                    using (Benchmark.Run("CRC32 Intrinsic", count, true))
                    {
                        for (int i = 0; i < cnt; i++)
                        {
                            digest = Crc32C.CalculateCrc32C(ptr, len, digest);
                        }
                    }
                    crc0 = digest;
                }

                {
                    uint digest = 0;
                    using (Benchmark.Run("CRC32 Managed", count, true))
                    {
                        for (int i = 0; i < cnt; i++)
                        {
                            digest = Crc32C.CalculateCrc32CManaged(ptr, len, digest);
                        }
                    }
                    crc1 = digest;
                }

                {
                    var  crc32C = new Ralph.Crc32C.Crc32C();
                    uint digest = 0;
                    using (Benchmark.Run("CRC32 Ralph", count, true))
                    {
                        for (int i = 0; i < cnt; i++)
                        {
                            crc32C.Update(arr, 0, arr.Length);
                            digest = crc32C.GetIntValue();
                        }
                    }

                    crc2 = digest;
                }

                Assert.AreEqual(crc0, crc1);
                Assert.AreEqual(crc0, crc2);

                Benchmark.Dump("LEN: " + len);
            }
        }