forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeserializeSession.cs
More file actions
102 lines (87 loc) · 3.02 KB
/
Copy pathDeserializeSession.cs
File metadata and controls
102 lines (87 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// -----------------------------------------------------------------------
// <copyright file="DeserializeSession.cs" company="Asynkron HB">
// Copyright (C) 2015-2017 Asynkron HB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using Wire.Internal;
using IntToObjectLookup = System.Collections.Generic.List<object>;
using IntToTypeLookup = System.Collections.Generic.List<System.Type>;
using TypeToVersionInfoLookup = System.Collections.Generic.Dictionary<System.Type, Wire.TypeVersionInfo>;
namespace Wire
{
public class TypeVersionInfo
{}
public class
DeserializerSession
{
public const int MinBufferSize = 9;
private readonly IntToObjectLookup _objectById;
private readonly int _offset;
private readonly TypeToVersionInfoLookup _versionInfoByType;
public readonly Serializer Serializer;
private byte[] _buffer;
private IntToTypeLookup _identifierToType;
public DeserializerSession([NotNull] Serializer serializer)
{
Serializer = serializer;
_buffer = new byte[MinBufferSize];
if (serializer.Options.PreserveObjectReferences)
{
_objectById = new IntToObjectLookup(1);
}
if (serializer.Options.VersionTolerance)
{
_versionInfoByType = new TypeToVersionInfoLookup();
}
_offset = serializer.Options.KnownTypes.Length;
}
public byte[] GetBuffer(int length)
{
if (length <= _buffer.Length)
{
return _buffer;
}
length = Math.Max(length, _buffer.Length*2);
_buffer = new byte[length];
return _buffer;
}
public void TrackDeserializedObject([NotNull] object obj)
{
_objectById.Add(obj);
}
public object GetDeserializedObject(int id)
{
return _objectById[id];
}
public void TrackDeserializedType([NotNull] Type type)
{
if (_identifierToType == null)
{
_identifierToType = new IntToTypeLookup(1);
}
_identifierToType.Add(type);
}
public Type GetTypeFromTypeId(int typeId)
{
if (typeId < _offset)
{
return Serializer.Options.KnownTypes[typeId];
}
if (_identifierToType == null)
{
throw new ArgumentException(nameof(typeId));
}
return _identifierToType[typeId - _offset];
}
public void TrackDeserializedTypeWithVersion([NotNull] Type type, [NotNull] TypeVersionInfo versionInfo)
{
TrackDeserializedType(type);
_versionInfoByType.Add(type, versionInfo);
}
public TypeVersionInfo GetVersionInfo([NotNull] Type type)
{
return _versionInfoByType[type];
}
}
}