1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
5
4
namespace MLAPI . NetworkingManagerComponents
6
5
{
@@ -37,21 +36,67 @@ public static List<byte[]> GetChunkedMessage(ref byte[] message, int chunkSize)
37
36
return chunks ;
38
37
}
39
38
39
+ public static bool HasMissingParts ( ref List < byte [ ] > chunks , uint expectedChunksCount )
40
+ {
41
+ if ( chunks . Count < expectedChunksCount )
42
+ return true ;
43
+
44
+ HashSet < uint > chunkIndexes = new HashSet < uint > ( ) ;
45
+ uint duplicateCount = 0 ;
46
+ for ( int i = 0 ; i < chunks . Count ; i ++ )
47
+ {
48
+ uint chunkIndex = BitConverter . ToUInt32 ( chunks [ i ] , 0 ) ;
49
+ if ( chunkIndexes . Contains ( chunkIndex ) )
50
+ duplicateCount ++ ;
51
+ else
52
+ chunkIndexes . Add ( chunkIndex ) ;
53
+ }
54
+ return chunks . Count - duplicateCount != expectedChunksCount ;
55
+ }
56
+
57
+ public static bool IsOrdered ( ref List < byte [ ] > chunks )
58
+ {
59
+ uint lastChunkIndex = 0 ;
60
+ for ( int i = 0 ; i < chunks . Count ; i ++ )
61
+ {
62
+ uint chunkIndex = BitConverter . ToUInt32 ( chunks [ i ] , 0 ) ;
63
+ //This can't be right?
64
+ if ( chunkIndex <= lastChunkIndex )
65
+ lastChunkIndex ++ ;
66
+ else
67
+ return false ;
68
+ }
69
+ return true ;
70
+ }
71
+
72
+ public static bool HasDuplicates ( ref List < byte [ ] > chunks , uint expectedChunksCount )
73
+ {
74
+ if ( chunks . Count > expectedChunksCount )
75
+ return true ;
76
+
77
+ HashSet < uint > chunkIndexes = new HashSet < uint > ( ) ;
78
+ for ( int i = 0 ; i < chunks . Count ; i ++ )
79
+ {
80
+ uint chunkIndex = BitConverter . ToUInt32 ( chunks [ i ] , 0 ) ;
81
+ if ( chunkIndexes . Contains ( chunkIndex ) )
82
+ return true ;
83
+ else
84
+ chunkIndexes . Add ( chunkIndex ) ;
85
+ }
86
+ return false ;
87
+ }
88
+
89
+
40
90
public static byte [ ] GetMessageOrdered ( ref List < byte [ ] > chunks , int chunkSize = - 1 )
41
91
{
42
92
if ( chunks . Count == 0 )
43
93
return new byte [ 0 ] ;
44
94
if ( chunkSize == - 1 )
45
95
chunkSize = chunks [ 0 ] . Length - 4 ;
46
96
47
- uint lastIndex = 0 ;
48
97
uint messageSize = 0 ;
49
98
for ( int i = 0 ; i < chunks . Count ; i ++ )
50
99
{
51
- uint chunkIndex = BitConverter . ToUInt32 ( chunks [ i ] , 0 ) ;
52
- if ( chunkIndex <= lastIndex )
53
- throw new ArgumentException ( "Chunks not in order" ) ;
54
- lastIndex = chunkIndex ;
55
100
messageSize += Convert . ToUInt32 ( chunks [ i ] . Length - 4 ) ;
56
101
}
57
102
byte [ ] message = new byte [ messageSize ] ;
@@ -61,5 +106,34 @@ public static byte[] GetMessageOrdered(ref List<byte[]> chunks, int chunkSize =
61
106
}
62
107
return message ;
63
108
}
109
+
110
+ public static byte [ ] GetMessageUnordered ( ref List < byte [ ] > chunks , int chunkSize = - 1 )
111
+ {
112
+ if ( chunks . Count == 0 )
113
+ return new byte [ 0 ] ;
114
+ if ( chunkSize == - 1 )
115
+ chunkSize = chunks [ 0 ] . Length - 4 ;
116
+
117
+ uint messageSize = 0 ;
118
+ for ( int i = 0 ; i < chunks . Count ; i ++ )
119
+ {
120
+ messageSize += Convert . ToUInt32 ( chunks [ i ] . Length - 4 ) ;
121
+ }
122
+ byte [ ] message = new byte [ messageSize ] ;
123
+ uint nextIndex = 0 ;
124
+ //Loop as many times as there are chunks.
125
+ for ( int i = 0 ; i < chunks . Count ; i ++ )
126
+ {
127
+ //For each chunk, find the right chunk
128
+ for ( int j = 0 ; j < chunks . Count ; j ++ )
129
+ {
130
+ if ( BitConverter . ToUInt32 ( chunks [ j ] , 0 ) == nextIndex )
131
+ {
132
+ Array . Copy ( chunks [ j ] , 3 , message , nextIndex * chunkSize , chunkSize ) ;
133
+ }
134
+ }
135
+ }
136
+ return message ;
137
+ }
64
138
}
65
139
}
0 commit comments