-
Notifications
You must be signed in to change notification settings - Fork 2k
/
SerializationContext.cs
167 lines (144 loc) · 5.99 KB
/
SerializationContext.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Orleans.Serialization
{
public static class SerializationContextExtensions
{
public static void RecordObject(this ISerializationContext context, object original)
{
context.RecordObject(original, context.CurrentOffset);
}
public static ISerializationContext CreateNestedContext(
this ISerializationContext context,
int position,
IBinaryTokenStreamWriter writer)
{
return new SerializationContext.NestedSerializationContext(context, position, writer);
}
public static void SerializeInner<T>(this ISerializationContext @this, T obj)
{
@this.SerializeInner(obj, typeof(T));
}
}
/// <summary>
/// Maintains context information for current thread during serialization operations.
/// </summary>
/// <remarks>
/// DeepCopier functions in Orleans generated code use the RecordObject method to
/// record the mapping of original object to the copied instance of that object
/// so that object identity can be preserved when serializing .NET object graphs.
/// </remarks>
public sealed class SerializationContext : SerializationContextBase, ICopyContext, ISerializationContext
{
private struct Record
{
public readonly object Copy;
public readonly int Offset;
public Record(object copy)
{
Copy = copy;
Offset = 0;
}
public Record(int offset)
{
Copy = null;
Offset = offset;
}
}
/// <summary>
/// Gets the serialization manager.
/// </summary>
public IBinaryTokenStreamWriter StreamWriter { get; set; }
private Dictionary<object, Record> processedObjects;
public SerializationContext(SerializationManager serializationManager) : base(serializationManager)
{
this.Reset();
}
internal void Reset()
{
if (this.processedObjects is null || this.processedObjects.Count > this.MaxSustainedSerializationContextCapacity)
{
processedObjects = new Dictionary<object, Record>(ReferenceEqualsComparer.Instance);
}
else
{
processedObjects.Clear();
}
}
/// <summary>
/// Record an object-to-copy mapping into the current serialization context.
/// Used for maintaining the .NET object graph during serialization operations.
/// Used in generated code.
/// </summary>
/// <param name="original">Original object.</param>
/// <param name="copy">Copy object that will be the serialized form of the original.</param>
public void RecordCopy(object original, object copy)
{
if (!processedObjects.ContainsKey(original))
{
processedObjects[original] = new Record(copy);
}
}
public void RecordObject(object original, int offset)
{
processedObjects[original] = new Record(offset);
}
// Returns an object suitable for insertion if this is a back-reference, or null if it's new
public object CheckObjectWhileCopying(object raw)
{
Record record;
bool found = processedObjects.TryGetValue(raw, out record);
if (found)
{
return record.Copy;
}
return null;
}
// Returns an offset where the first version of this object was seen, or -1 if it's new
public int CheckObjectWhileSerializing(object raw)
{
Record record;
bool found = processedObjects.TryGetValue(raw, out record);
if (found)
{
return record.Offset;
}
return -1;
}
public int CurrentOffset => this.StreamWriter.CurrentOffset;
public override object AdditionalContext => this.SerializationManager.RuntimeClient;
public object DeepCopyInner(object original)
{
return SerializationManager.DeepCopyInner(original, this);
}
public void SerializeInner(object obj, Type expected)
{
SerializationManager.SerializeInner(obj, this, expected);
}
internal class NestedSerializationContext : ISerializationContext
{
private readonly int initialOffset;
private readonly ISerializationContext parentContext;
/// <summary>
/// Creates a new instance of the <see cref="NestedSerializationContext"/> class.
/// </summary>
/// <param name="parent">The parent context.</param>
/// <param name="offset">The absolute offset at which this stream begins.</param>
/// <param name="writer">The writer.</param>
public NestedSerializationContext(ISerializationContext parent, int offset, IBinaryTokenStreamWriter writer)
{
this.parentContext = parent;
this.initialOffset = offset;
this.StreamWriter = writer;
}
public IServiceProvider ServiceProvider => this.parentContext.ServiceProvider;
public object AdditionalContext => this.parentContext.ServiceProvider;
public IBinaryTokenStreamWriter StreamWriter { get; }
public int CurrentOffset => this.initialOffset + this.StreamWriter.CurrentOffset;
public void SerializeInner(object obj, Type expected) => SerializationManager.SerializeInner(obj, this, expected);
public void RecordObject(object original, int offset) => this.parentContext.RecordObject(original, offset);
public int CheckObjectWhileSerializing(object raw) => this.parentContext.CheckObjectWhileSerializing(raw);
}
}
}