@@ -122,7 +122,7 @@ public static int GetInt(XmlNode n0, string path)
122
122
/// <returns>renamed string</returns>
123
123
private static string xmlStringMapper ( string xmlString )
124
124
{
125
- switch ( xmlString )
125
+ switch ( xmlString )
126
126
{
127
127
case "no-wait" :
128
128
return "nowait" ;
@@ -445,12 +445,14 @@ public void EmitPrelude()
445
445
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
446
446
//---------------------------------------------------------------------------
447
447
448
+ using System;
448
449
using System.Collections.Generic;
449
450
using System.Text;
450
451
451
452
using RabbitMQ.Client;
452
453
using RabbitMQ.Client.Exceptions;
453
454
using RabbitMQ.Client.Framing.Impl;
455
+ using RabbitMQ.Client.Impl;
454
456
" ;
455
457
EmitLine ( prelude ) ;
456
458
}
@@ -681,6 +683,48 @@ public void EmitClassProperties(AmqpClass c)
681
683
}
682
684
EmitLine ( " }" ) ;
683
685
EmitLine ( "" ) ;
686
+ EmitLine ( " public override int GetRequiredPayloadBufferSize()" ) ;
687
+ EmitLine ( " {" ) ;
688
+ EmitLine ( " int bufferSize = 0;" ) ;
689
+ EmitLine ( " int fieldCount = 0;" ) ;
690
+ foreach ( AmqpField f in c . m_Fields )
691
+ {
692
+ switch ( MapDomain ( f . Domain ) )
693
+ {
694
+ case "byte" :
695
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize++; }} // _{ MangleMethod ( f . Name ) } in bytes") ;
696
+ break ;
697
+ case "string" :
698
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += 1 + Encoding.UTF8.GetByteCount(_{ MangleMethod ( f . Name ) } ); }} // _{ MangleMethod ( f . Name ) } in bytes") ;
699
+ break ;
700
+ case "byte[]" :
701
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += 4 + _{ MangleMethod ( f . Name ) } .Length; }} // _{ MangleMethod ( f . Name ) } in bytes") ;
702
+ break ;
703
+ case "ushort" :
704
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += 2; }} // _{ MangleMethod ( f . Name ) } in bytes") ;
705
+ break ;
706
+ case "uint" :
707
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += 4; }} // _{ MangleMethod ( f . Name ) } in bytes") ;
708
+ break ;
709
+ case "ulong" :
710
+ case "AmqpTimestamp" :
711
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += 8; }} // _{ MangleMethod ( f . Name ) } in bytes") ;
712
+ break ;
713
+ case "bool" :
714
+ // TODO: implement if used, not used anywhere yet
715
+ break ;
716
+ case "IDictionary<string, object>" :
717
+ EmitLine ( $ " if (_{ MangleMethod ( f . Name ) } _present) {{ fieldCount++; bufferSize += WireFormatting.GetTableByteCount(_{ MangleMethod ( f . Name ) } ); }} // _{ MangleMethod ( f . Name ) } in bytes") ;
718
+ break ;
719
+ default :
720
+ throw new ArgumentOutOfRangeException ( $ "Can't handle size calculations for type = { f . Domain } ;") ;
721
+ }
722
+ }
723
+
724
+ EmitLine ( $ " bufferSize += Math.Max((int)Math.Ceiling(fieldCount / 15.0), 1) * 2; // number of presence fields in bytes") ;
725
+ EmitLine ( " return bufferSize;" ) ;
726
+ EmitLine ( " }" ) ;
727
+ EmitLine ( "" ) ;
684
728
EmitLine ( " public override void AppendPropertyDebugStringTo(StringBuilder sb)" ) ;
685
729
EmitLine ( " {" ) ;
686
730
EmitLine ( " sb.Append(\" (\" );" ) ;
@@ -813,6 +857,51 @@ public void EmitClassMethodImplementations(AmqpClass c)
813
857
}
814
858
EmitLine ( " }" ) ;
815
859
EmitLine ( "" ) ;
860
+ EmitLine ( " public override int GetRequiredBufferSize()" ) ;
861
+ EmitLine ( " {" ) ;
862
+ EmitLine ( " int bufferSize = 0;" ) ;
863
+ int bitCount = 0 ;
864
+ foreach ( AmqpField f in m . m_Fields )
865
+ {
866
+ switch ( MapDomain ( f . Domain ) )
867
+ {
868
+ case "byte" :
869
+ EmitLine ( $ " bufferSize++; // _{ MangleMethod ( f . Name ) } in bytes") ;
870
+ break ;
871
+ case "string" :
872
+ EmitLine ( $ " bufferSize += 1 + Encoding.UTF8.GetByteCount(_{ MangleMethod ( f . Name ) } ); // _{ MangleMethod ( f . Name ) } in bytes") ;
873
+ break ;
874
+ case "byte[]" :
875
+ EmitLine ( $ " bufferSize += 4 + _{ MangleMethod ( f . Name ) } .Length; // _{ MangleMethod ( f . Name ) } in bytes") ;
876
+ break ;
877
+ case "ushort" :
878
+ EmitLine ( $ " bufferSize += 2; // _{ MangleMethod ( f . Name ) } in bytes") ;
879
+ break ;
880
+ case "uint" :
881
+ EmitLine ( $ " bufferSize += 4; // _{ MangleMethod ( f . Name ) } in bytes") ;
882
+ break ;
883
+ case "ulong" :
884
+ case "AmqpTimestamp" :
885
+ EmitLine ( $ " bufferSize += 8; // _{ MangleMethod ( f . Name ) } in bytes") ;
886
+ break ;
887
+ case "bool" :
888
+ bitCount ++ ;
889
+ break ;
890
+ case "IDictionary<string, object>" :
891
+ EmitLine ( $ " bufferSize += WireFormatting.GetTableByteCount(_{ MangleMethod ( f . Name ) } ); // _{ MangleMethod ( f . Name ) } in bytes") ;
892
+ break ;
893
+ default :
894
+ throw new ArgumentOutOfRangeException ( $ "Can't handle size calculations for type = { f . Domain } ;") ;
895
+ }
896
+ }
897
+
898
+ if ( bitCount > 0 )
899
+ {
900
+ EmitLine ( $ " bufferSize += { Math . Ceiling ( bitCount / 8.0 ) } ; // number of bit fields in bytes") ;
901
+ }
902
+ EmitLine ( " return bufferSize;" ) ;
903
+ EmitLine ( " }" ) ;
904
+ EmitLine ( "" ) ;
816
905
EmitLine ( " public override void AppendArgumentDebugStringTo(StringBuilder sb)" ) ;
817
906
EmitLine ( " {" ) ;
818
907
EmitLine ( " sb.Append(\" (\" );" ) ;
@@ -840,44 +929,41 @@ public void EmitClassMethodImplementations(AmqpClass c)
840
929
841
930
public void EmitMethodArgumentReader ( )
842
931
{
843
- EmitLine ( " internal override Client.Impl.MethodBase DecodeMethodFrom(Util.NetworkBinaryReader reader )" ) ;
932
+ EmitLine ( " internal override Client.Impl.MethodBase DecodeMethodFrom(ReadOnlyMemory<byte> memory )" ) ;
844
933
EmitLine ( " {" ) ;
845
- EmitLine ( " ushort classId = reader.ReadUInt16();" ) ;
846
- EmitLine ( " ushort methodId = reader.ReadUInt16();" ) ;
847
- EmitLine ( " Client.Impl.MethodBase result = null;" ) ;
934
+ EmitLine ( " ushort classId = Util.NetworkOrderDeserializer.ReadUInt16(memory);" ) ;
935
+ EmitLine ( " ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(memory.Slice(2));" ) ;
936
+ EmitLine ( " Client.Impl.MethodBase result = DecodeMethodFrom(classId, methodId);" ) ;
937
+ EmitLine ( " if(result != null)" ) ;
938
+ EmitLine ( " {" ) ;
939
+ EmitLine ( " result.ReadArgumentsFrom(new Client.Impl.MethodArgumentReader(memory.Slice(4)));" ) ;
940
+ EmitLine ( " return result;" ) ;
941
+ EmitLine ( " }" ) ;
848
942
EmitLine ( "" ) ;
943
+ EmitLine ( " throw new Client.Impl.UnknownClassOrMethodException(classId, methodId);" ) ;
944
+ EmitLine ( " }" ) ;
945
+ EmitLine ( "" ) ;
946
+ EmitLine ( " internal Client.Impl.MethodBase DecodeMethodFrom(ushort classId, ushort methodId)" ) ;
947
+ EmitLine ( " {" ) ;
849
948
EmitLine ( " switch ((classId << 16) | methodId)" ) ;
850
949
EmitLine ( " {" ) ;
851
950
foreach ( AmqpClass c in m_classes )
852
951
{
853
952
foreach ( AmqpMethod m in c . m_Methods )
854
953
{
855
- EmitLine ( $ " case (ClassConstants.{ MangleConstant ( c . Name ) } << 16) | { MangleConstant ( c . Name ) } MethodConstants.{ MangleConstant ( m . Name ) } :") ;
856
- EmitLine ( " {" ) ;
857
- EmitLine ( $ " result = new Impl.{ MangleMethodClass ( c , m ) } ();") ;
858
- EmitLine ( $ " break;") ;
859
- EmitLine ( " }" ) ;
954
+ EmitLine ( $ " case (ClassConstants.{ MangleConstant ( c . Name ) } << 16) | { MangleConstant ( c . Name ) } MethodConstants.{ MangleConstant ( m . Name ) } : return new Impl.{ MangleMethodClass ( c , m ) } ();") ;
860
955
}
861
956
}
862
- EmitLine ( " default: break ;" ) ;
957
+ EmitLine ( " default: return null ;" ) ;
863
958
EmitLine ( " }" ) ;
864
- EmitLine ( "" ) ;
865
- EmitLine ( " if(result != null)" ) ;
866
- EmitLine ( " {" ) ;
867
- EmitLine ( " result.ReadArgumentsFrom(new Client.Impl.MethodArgumentReader(reader));" ) ;
868
- EmitLine ( " return result;" ) ;
869
- EmitLine ( " }" ) ;
870
- EmitLine ( "" ) ;
871
- EmitLine ( " throw new Client.Impl.UnknownClassOrMethodException(classId, methodId);" ) ;
872
959
EmitLine ( " }" ) ;
960
+ EmitLine ( "" ) ;
873
961
}
874
962
875
963
public void EmitContentHeaderReader ( )
876
964
{
877
- EmitLine ( " internal override Client.Impl.ContentHeaderBase DecodeContentHeaderFrom(Util.NetworkBinaryReader reader )" ) ;
965
+ EmitLine ( " internal override Client.Impl.ContentHeaderBase DecodeContentHeaderFrom(ushort classId )" ) ;
878
966
EmitLine ( " {" ) ;
879
- EmitLine ( " ushort classId = reader.ReadUInt16();" ) ;
880
- EmitLine ( "" ) ;
881
967
EmitLine ( " switch (classId)" ) ;
882
968
EmitLine ( " {" ) ;
883
969
foreach ( AmqpClass c in m_classes )
@@ -887,9 +973,8 @@ public void EmitContentHeaderReader()
887
973
EmitLine ( $ " case { c . Index } : return new { MangleClass ( c . Name ) } Properties();") ;
888
974
}
889
975
}
890
- EmitLine ( " default: break ;" ) ;
976
+ EmitLine ( " default: throw new Client.Impl.UnknownClassOrMethodException(classId, 0) ;" ) ;
891
977
EmitLine ( " }" ) ;
892
- EmitLine ( " throw new Client.Impl.UnknownClassOrMethodException(classId, 0);" ) ;
893
978
EmitLine ( " }" ) ;
894
979
}
895
980
0 commit comments