@@ -4,6 +4,7 @@ namespace StockSharp.Algo.Storages.Binary.Snapshot
4
4
using System . Collections . Generic ;
5
5
using System . Linq ;
6
6
using System . Runtime . InteropServices ;
7
+ using System . Text ;
7
8
8
9
using Ecng . Common ;
9
10
using Ecng . Interop ;
@@ -122,11 +123,14 @@ private struct TransactionConditionParam
122
123
[ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 256 ) ]
123
124
public string ValueType ;
124
125
125
- [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 32 ) ]
126
- public string StringValue ;
127
126
public long ? NumValue ;
128
127
public decimal ? DecimalValue ;
129
128
public bool ? BoolValue ;
129
+
130
+ public int StringValueLen ;
131
+
132
+ //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
133
+ //public string StringValue;
130
134
}
131
135
132
136
Version ISnapshotSerializer < string , ExecutionMessage > . Version { get ; } = new Version ( 2 , 0 ) ;
@@ -209,22 +213,28 @@ byte[] ISnapshotSerializer<string, ExecutionMessage>.Serialize(Version version,
209
213
var paramSize = typeof ( TransactionConditionParam ) . SizeOf ( ) ;
210
214
var snapshotSize = typeof ( TransactionSnapshot ) . SizeOf ( ) ;
211
215
212
- var buffer = new byte [ snapshotSize + snapshot . ConditionParamsCount * paramSize ] ;
216
+ var result = new List < byte > ( ) ;
217
+
218
+ var buffer = new byte [ snapshotSize ] ;
213
219
214
220
var ptr = snapshot . StructToPtr ( ) ;
215
221
Marshal . Copy ( ptr , buffer , 0 , snapshotSize ) ;
216
222
Marshal . FreeHGlobal ( ptr ) ;
217
223
218
- var offset = snapshotSize ;
224
+ result . AddRange ( buffer ) ;
219
225
220
226
foreach ( var conParam in conParams )
221
227
{
228
+ buffer = new byte [ paramSize ] ;
229
+
222
230
var param = new TransactionConditionParam
223
231
{
224
232
Name = conParam . Key ,
225
233
ValueType = conParam . Value . GetType ( ) . GetTypeAsString ( false ) ,
226
234
} ;
227
235
236
+ byte [ ] stringValue = null ;
237
+
228
238
switch ( conParam . Value )
229
239
{
230
240
case byte b :
@@ -275,19 +285,32 @@ byte[] ISnapshotSerializer<string, ExecutionMessage>.Serialize(Version version,
275
285
case Enum e :
276
286
param . NumValue = e . To < long > ( ) ;
277
287
break ;
288
+ case IPersistable p :
289
+ stringValue = new XmlSerializer < SettingsStorage > ( ) . Serialize ( p . Save ( ) ) ;
290
+ break ;
278
291
default :
279
- param . StringValue = conParam . Value . To < string > ( ) ;
292
+ stringValue = Encoding . UTF8 . GetBytes ( conParam . Value . To < string > ( ) ) ;
280
293
break ;
281
294
}
282
295
296
+ if ( stringValue != null )
297
+ {
298
+ param . StringValueLen = stringValue . Length ;
299
+ }
300
+
283
301
var rowPtr = param . StructToPtr ( ) ;
284
- Marshal . Copy ( rowPtr , buffer , offset , paramSize ) ;
302
+ Marshal . Copy ( rowPtr , buffer , 0 , paramSize ) ;
285
303
Marshal . FreeHGlobal ( rowPtr ) ;
286
304
287
- offset += paramSize ;
305
+ result . AddRange ( buffer ) ;
306
+
307
+ if ( stringValue == null )
308
+ continue ;
309
+
310
+ result . AddRange ( stringValue ) ;
288
311
}
289
312
290
- return buffer ;
313
+ return result . ToArray ( ) ;
291
314
}
292
315
293
316
ExecutionMessage ISnapshotSerializer < string , ExecutionMessage > . Deserialize ( Version version , byte [ ] buffer )
@@ -363,13 +386,18 @@ ExecutionMessage ISnapshotSerializer<string, ExecutionMessage>.Deserialize(Versi
363
386
var paramSize = typeof ( TransactionConditionParam ) . SizeOf ( ) ;
364
387
365
388
if ( ! snapshot . ConditionType . IsEmpty ( ) )
389
+ {
366
390
execMsg . Condition = snapshot . ConditionType . To < Type > ( ) . CreateInstance < OrderCondition > ( ) ;
391
+ execMsg . Condition . Parameters . Clear ( ) ; // removing pre-defined values
392
+ }
367
393
368
394
for ( var i = 0 ; i < snapshot . ConditionParamsCount ; i ++ )
369
395
{
370
396
var param = ptr . ToStruct < TransactionConditionParam > ( ) ;
371
397
var paramType = param . ValueType . To < Type > ( ) ;
372
398
399
+ ptr += paramSize ;
400
+
373
401
object value ;
374
402
375
403
if ( param . NumValue != null )
@@ -378,10 +406,26 @@ ExecutionMessage ISnapshotSerializer<string, ExecutionMessage>.Deserialize(Versi
378
406
value = param . DecimalValue . Value ;
379
407
else if ( param . BoolValue != null )
380
408
value = param . BoolValue . Value ;
381
- else if ( paramType == typeof ( Unit ) )
382
- value = param . StringValue . ToUnit ( ) ;
409
+ //else if (paramType == typeof(Unit))
410
+ // value = param.StringValue.ToUnit();
411
+ else if ( param . StringValueLen > 0 )
412
+ {
413
+ var strBuffer = new byte [ param . StringValueLen ] ;
414
+ Marshal . Copy ( ptr , strBuffer , 0 , strBuffer . Length ) ;
415
+
416
+ if ( typeof ( IPersistable ) . IsAssignableFrom ( paramType ) )
417
+ {
418
+ var persistable = paramType . CreateInstance < IPersistable > ( ) ;
419
+ persistable . Load ( new XmlSerializer < SettingsStorage > ( ) . Deserialize ( strBuffer ) ) ;
420
+ value = persistable ;
421
+ }
422
+ else
423
+ value = Encoding . UTF8 . GetString ( strBuffer ) ;
424
+
425
+ ptr += param . StringValueLen ;
426
+ }
383
427
else
384
- value = param . StringValue ;
428
+ value = null ;
385
429
386
430
try
387
431
{
@@ -392,8 +436,6 @@ ExecutionMessage ISnapshotSerializer<string, ExecutionMessage>.Deserialize(Versi
392
436
{
393
437
ex . LogError ( ) ;
394
438
}
395
-
396
- ptr += paramSize ;
397
439
}
398
440
399
441
return execMsg ;
0 commit comments