1
- using JT1078 . Protocol . Enums ;
2
- using JT1078 . Protocol . Extensions ;
3
- using JT1078 . Protocol . MessagePack ;
4
- using System ;
1
+ using System ;
5
2
using System . Collections . Concurrent ;
6
- using System . Collections . Generic ;
7
3
using System . IO ;
8
4
using System . Linq ;
9
5
using System . Text ;
10
6
using System . Text . Json ;
7
+ using JT1078 . Protocol . Enums ;
8
+ using JT1078 . Protocol . Extensions ;
9
+ using JT1078 . Protocol . MessagePack ;
11
10
12
11
namespace JT1078 . Protocol
13
12
{
14
13
public static class JT1078Serializer
15
14
{
16
- private readonly static ConcurrentDictionary < string , JT1078Package > JT1078PackageGroupDict = new ConcurrentDictionary < string , JT1078Package > ( StringComparer . OrdinalIgnoreCase ) ;
15
+ private readonly static ConcurrentDictionary < string , JT1078Package > livePackageGroup = new ( StringComparer . OrdinalIgnoreCase ) ;
16
+ private readonly static ConcurrentDictionary < string , JT1078Package > historyPackageGroup = new ( StringComparer . OrdinalIgnoreCase ) ;
17
17
public static byte [ ] Serialize ( JT1078Package package , int minBufferSize = 4096 )
18
18
{
19
19
byte [ ] buffer = JT1078ArrayPool . Rent ( minBufferSize ) ;
@@ -72,18 +72,33 @@ public static JT1078Package Deserialize(ReadOnlySpan<byte> bytes)
72
72
jT1078Package . Bodies = jT1078MessagePackReader . ReadRemainArray ( ) . ToArray ( ) ;
73
73
return jT1078Package ;
74
74
}
75
- public static JT1078Package Merge ( JT1078Package jT1078Package )
75
+
76
+ /// <summary>
77
+ /// merge package
78
+ /// <para>due to the agreement cannot distinguish the Stream type, so need clear Stream type at the time of merger, avoid confusion</para>
79
+ /// </summary>
80
+ /// <param name="jT1078Package">package of jt1078</param>
81
+ /// <param name="channelType">package type is live or history</param>
82
+ /// <returns></returns>
83
+ /// <exception cref="Exception"></exception>
84
+ public static JT1078Package Merge ( JT1078Package jT1078Package , JT808ChannelType channelType )
76
85
{
77
86
string cacheKey = jT1078Package . GetKey ( ) ;
87
+ var packageGroup = channelType switch
88
+ {
89
+ JT808ChannelType . Live => livePackageGroup ,
90
+ JT808ChannelType . History => historyPackageGroup ,
91
+ _=> throw new Exception ( "channel type error" )
92
+ } ;
78
93
if ( jT1078Package . Label3 . SubpackageType == JT1078SubPackageType . 分包处理时的第一个包 )
79
94
{
80
- JT1078PackageGroupDict . TryRemove ( cacheKey , out _ ) ;
81
- JT1078PackageGroupDict . TryAdd ( cacheKey , jT1078Package ) ;
95
+ packageGroup . TryRemove ( cacheKey , out _ ) ;
96
+ packageGroup . TryAdd ( cacheKey , jT1078Package ) ;
82
97
return default ;
83
98
}
84
99
else if ( jT1078Package . Label3 . SubpackageType == JT1078SubPackageType . 分包处理时的中间包 )
85
100
{
86
- if ( JT1078PackageGroupDict . TryGetValue ( cacheKey , out var tmpPackage ) )
101
+ if ( packageGroup . TryGetValue ( cacheKey , out var tmpPackage ) )
87
102
{
88
103
var totalLength = tmpPackage . Bodies . Length + jT1078Package . Bodies . Length ;
89
104
byte [ ] poolBytes = JT1078ArrayPool . Rent ( totalLength ) ;
@@ -92,13 +107,13 @@ public static JT1078Package Merge(JT1078Package jT1078Package)
92
107
jT1078Package . Bodies . CopyTo ( tmpSpan . Slice ( tmpPackage . Bodies . Length ) ) ;
93
108
tmpPackage . Bodies = tmpSpan . Slice ( 0 , totalLength ) . ToArray ( ) ;
94
109
JT1078ArrayPool . Return ( poolBytes ) ;
95
- JT1078PackageGroupDict [ cacheKey ] = tmpPackage ;
110
+ packageGroup [ cacheKey ] = tmpPackage ;
96
111
}
97
112
return default ;
98
113
}
99
114
else if ( jT1078Package . Label3 . SubpackageType == JT1078SubPackageType . 分包处理时的最后一个包 )
100
115
{
101
- if ( JT1078PackageGroupDict . TryRemove ( cacheKey , out var tmpPackage ) )
116
+ if ( packageGroup . TryRemove ( cacheKey , out var tmpPackage ) )
102
117
{
103
118
var totalLength = tmpPackage . Bodies . Length + jT1078Package . Bodies . Length ;
104
119
byte [ ] poolBytes = JT1078ArrayPool . Rent ( totalLength ) ;
0 commit comments