Skip to content

Commit 4ef4266

Browse files
author
Stefán J. Sigurðarson
committed
Fix regressions
1 parent 05e0363 commit 4ef4266

File tree

1 file changed

+189
-18
lines changed

1 file changed

+189
-18
lines changed

projects/RabbitMQ.Client/client/impl/WireFormatting.cs

Lines changed: 189 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -515,22 +515,101 @@ public static int WriteBits(Span<byte> span, bool val)
515515
}
516516

517517
[MethodImpl(MethodImplOptions.AggressiveInlining)]
518-
public static int WriteBits(Span<byte> span, params bool[] vals)
518+
public static int WriteBits(Span<byte> span, bool val1, bool val2)
519519
{
520-
if(vals.Length > 8)
520+
byte bits = 0;
521+
if (val1)
522+
{
523+
bits |= 1 << 0;
524+
}
525+
526+
if (val2)
521527
{
522-
throw new ArgumentOutOfRangeException(nameof(vals), "Can only write less than 8 values");
528+
bits |= 1 << 1;
523529
}
530+
span[0] = bits;
531+
return 1;
532+
}
524533

534+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
535+
public static int WriteBits(Span<byte> span, bool val1, bool val2, bool val3)
536+
{
525537
byte bits = 0;
526-
for(int i = 0; i < vals.Length; i++)
538+
if (val1)
527539
{
528-
if (vals[i])
529-
{
530-
bits |= (byte)(1 << i);
531-
}
540+
bits |= 1 << 0;
532541
}
533542

543+
if (val2)
544+
{
545+
bits |= 1 << 1;
546+
}
547+
548+
if (val3)
549+
{
550+
bits |= 1 << 2;
551+
}
552+
553+
span[0] = bits;
554+
return 1;
555+
}
556+
557+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
558+
public static int WriteBits(Span<byte> span, bool val1, bool val2, bool val3, bool val4)
559+
{
560+
byte bits = 0;
561+
if (val1)
562+
{
563+
bits |= 1 << 0;
564+
}
565+
566+
if (val2)
567+
{
568+
bits |= 1 << 1;
569+
}
570+
571+
if (val3)
572+
{
573+
bits |= 1 << 2;
574+
}
575+
576+
if (val4)
577+
{
578+
bits |= 1 << 3;
579+
}
580+
581+
span[0] = bits;
582+
return 1;
583+
}
584+
585+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
586+
public static int WriteBits(Span<byte> span, bool val1, bool val2, bool val3, bool val4, bool val5)
587+
{
588+
byte bits = 0;
589+
if (val1)
590+
{
591+
bits |= 1 << 0;
592+
}
593+
594+
if (val2)
595+
{
596+
bits |= 1 << 1;
597+
}
598+
599+
if (val3)
600+
{
601+
bits |= 1 << 2;
602+
}
603+
604+
if (val4)
605+
{
606+
bits |= 1 << 3;
607+
}
608+
609+
if (val5)
610+
{
611+
bits |= 1 << 4;
612+
}
534613
span[0] = bits;
535614
return 1;
536615
}
@@ -541,8 +620,78 @@ public static int WriteBits(Span<byte> span,
541620
bool val6, bool val7, bool val8, bool val9, bool val10,
542621
bool val11, bool val12, bool val13, bool val14)
543622
{
544-
WriteBits(span, val8, val7, val6, val5, val4, val3, val2, val1);
545-
WriteBits(span.Slice(1), false, false, val14, val13, val12, val11, val10, val9);
623+
byte bits = 0;
624+
if (val1)
625+
{
626+
bits |= 1 << 7;
627+
}
628+
629+
if (val2)
630+
{
631+
bits |= 1 << 6;
632+
}
633+
634+
if (val3)
635+
{
636+
bits |= 1 << 5;
637+
}
638+
639+
if (val4)
640+
{
641+
bits |= 1 << 4;
642+
}
643+
644+
if (val5)
645+
{
646+
bits |= 1 << 3;
647+
}
648+
649+
if (val6)
650+
{
651+
bits |= 1 << 2;
652+
}
653+
654+
if (val7)
655+
{
656+
bits |= 1 << 1;
657+
}
658+
659+
if (val8)
660+
{
661+
bits |= 1 << 0;
662+
}
663+
span[0] = bits;
664+
bits = 0;
665+
if (val9)
666+
{
667+
bits |= 1 << 7;
668+
}
669+
670+
if (val10)
671+
{
672+
bits |= 1 << 6;
673+
}
674+
675+
if (val11)
676+
{
677+
bits |= 1 << 5;
678+
}
679+
680+
if (val12)
681+
{
682+
bits |= 1 << 4;
683+
}
684+
685+
if (val13)
686+
{
687+
bits |= 1 << 3;
688+
}
689+
690+
if (val14)
691+
{
692+
bits |= 1 << 2;
693+
}
694+
span[1] = bits;
546695
return 2;
547696
}
548697

@@ -637,7 +786,7 @@ public static int WriteTable(Span<byte> span, IDictionary val)
637786
return 4 + bytesWritten;
638787
}
639788

640-
public static int WriteTable<T>(Span<byte> span, T val) where T : class, IDictionary<string, object>
789+
public static int WriteTable(Span<byte> span, IDictionary<string, object> val)
641790
{
642791
if (val is null || val.Count == 0)
643792
{
@@ -648,10 +797,21 @@ public static int WriteTable<T>(Span<byte> span, T val) where T : class, IDictio
648797
// Let's only write after the length header.
649798
Span<byte> slice = span.Slice(4);
650799
int bytesWritten = 0;
651-
foreach (KeyValuePair<string, object> entry in val)
800+
if (val is Dictionary<string, object> dictVal)
652801
{
653-
bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
654-
bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
802+
foreach (KeyValuePair<string, object> entry in dictVal)
803+
{
804+
bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
805+
bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
806+
}
807+
}
808+
else
809+
{
810+
foreach (KeyValuePair<string, object> entry in val)
811+
{
812+
bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
813+
bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
814+
}
655815
}
656816

657817
NetworkOrderSerializer.WriteUInt32(span, (uint)bytesWritten);
@@ -675,18 +835,29 @@ public static int GetTableByteCount(IDictionary val)
675835
return byteCount;
676836
}
677837

678-
public static int GetTableByteCount<T>(T val) where T : class, IDictionary<string, object>
838+
public static int GetTableByteCount(IDictionary<string, object> val)
679839
{
680840
int byteCount = 4;
681841
if (val is null || val.Count == 0)
682842
{
683843
return byteCount;
684844
}
685845

686-
foreach (KeyValuePair<string, object> entry in val)
846+
if (val is Dictionary<string, object> valDict)
687847
{
688-
byteCount += GetUTF8ByteCount(entry.Key) + 1;
689-
byteCount += GetFieldValueByteCount(entry.Value);
848+
foreach (KeyValuePair<string, object> entry in valDict)
849+
{
850+
byteCount += GetUTF8ByteCount(entry.Key) + 1;
851+
byteCount += GetFieldValueByteCount(entry.Value);
852+
}
853+
}
854+
else
855+
{
856+
foreach (KeyValuePair<string, object> entry in val)
857+
{
858+
byteCount += GetUTF8ByteCount(entry.Key) + 1;
859+
byteCount += GetFieldValueByteCount(entry.Value);
860+
}
690861
}
691862

692863
return byteCount;

0 commit comments

Comments
 (0)