private void SendImpl(IEnumerator <IGrouping <string, Message> > enumerator, TaskCompletionSource <object> taskCompletionSource)
        {
send:

            if (!enumerator.MoveNext())
            {
                taskCompletionSource.TrySetResult(null);
            }
            else
            {
                IGrouping <string, Message> group = enumerator.Current;

                // Get the channel index we're going to use for this message
                int index = (int)((uint)_sipHashBasedComparer.GetHashCode(group.Key) % StreamCount);

                Debug.Assert(index >= 0, "Hash function resulted in an index < 0.");

                Task sendTask = StreamManager.Send(index, group.ToArray()).Catch(_logger);

                if (sendTask.IsCompleted)
                {
                    try
                    {
                        sendTask.Wait();

                        goto send;
                    }
                    catch (Exception ex)
                    {
                        taskCompletionSource.SetUnwrappedException(ex);
                    }
                }
                else
                {
                    sendTask.Then((enumer, tcs) => SendImpl(enumer, tcs), enumerator, taskCompletionSource)
                    .ContinueWithNotComplete(taskCompletionSource);
                }
            }
        }
Exemple #2
0
        public unsafe void TestVectors()
        {
            if (!BitConverter.IsLittleEndian)
            {
                return; // this test is only valid on little-endian platforms
            }

            // Arrange
            byte *keyBytes = stackalloc byte[16];

            for (int i = 0; i < 16; i++)
            {
                keyBytes[i] = (byte)i;
            }
            ulong k0 = *(ulong *)&keyBytes[0];
            ulong k1 = *(ulong *)&keyBytes[8];

            const int INPUT_SIZE = 63;
            byte *    pbInput    = stackalloc byte[INPUT_SIZE];

            for (int i = 0; i < INPUT_SIZE; i++)
            {
                pbInput[i] = (byte)i;
            }

            var comparer = new SipHashBasedStringEqualityComparer(k0, k1);

            // Act & assert
            for (int i = 0; i < INPUT_SIZE; i++)
            {
                int hashCode = comparer.GetHashCode(pbInput, (uint)i);
                fixed(byte *pVector = &vectors[i, 0])
                {
                    int v = *(int *)pVector;

                    Assert.Equal(hashCode, v);
                }
            }
        }
        public unsafe void TestVectors()
        {
            if (!BitConverter.IsLittleEndian)
            {
                return; // this test is only valid on little-endian platforms
            }

            // Arrange
            byte* keyBytes = stackalloc byte[16];
            for (int i = 0; i < 16; i++)
            {
                keyBytes[i] = (byte)i;
            }
            ulong k0 = *(ulong*)&keyBytes[0];
            ulong k1 = *(ulong*)&keyBytes[8];

            const int INPUT_SIZE = 63;
            byte* pbInput = stackalloc byte[INPUT_SIZE];
            for (int i = 0; i < INPUT_SIZE; i++)
            {
                pbInput[i] = (byte)i;
            }

            var comparer = new SipHashBasedStringEqualityComparer(k0, k1);

            // Act & assert
            for (int i = 0; i < INPUT_SIZE; i++)
            {
                int hashCode = comparer.GetHashCode(pbInput, (uint)i);
                fixed (byte* pVector = &vectors[i, 0])
                {
                    int v = *(int*)pVector;
                    Assert.Equal(hashCode, v);
                }
            }
        }
Exemple #4
0
        public void NullInputReturns0()
        {
            var comparer = new SipHashBasedStringEqualityComparer();

            Assert.Equal(0, comparer.GetHashCode(null));
        }
 public void NullInputReturns0()
 {
     var comparer = new SipHashBasedStringEqualityComparer();
     Assert.Equal(0, comparer.GetHashCode(null));
 }