-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWSNet2Serializer.cs
More file actions
170 lines (151 loc) · 4.35 KB
/
WSNet2Serializer.cs
File metadata and controls
170 lines (151 loc) · 4.35 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
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
168
169
170
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace WSNet2
{
/// <summary>
/// 型を保存するシリアライザ/デシリアライザ
/// </summary>
/// <remarks>
/// <para>
/// 独自型の登録はこのクラスのstaticメソッドで行う。
/// </para>
/// </remarks>
public class WSNet2Serializer
{
public delegate object ReadFunc(SerialReader reader, object recycle);
const int WRITER_BUFSIZE = 1024;
static Hashtable registeredTypes = new Hashtable();
static ReadFunc[] readFuncs = new ReadFunc[256];
static SerialWriter writer;
/// <summary>
/// SerialWriter新規作成
/// </summary>
/// <remarks>
/// <para>
/// 使い回されることはない。
/// </para>
/// </remarks>
public static SerialWriter NewWriter(int size = WRITER_BUFSIZE)
{
return new SerialWriter(size, registeredTypes);
}
/// <summary>
/// 使い回し用のSerialWriterを取得
/// </summary>
/// <remarks>
/// <para>
/// lockを取って使うこと。
/// </para>
/// </remarks>
public static SerialWriter GetWriter()
{
if (writer == null)
{
writer = new SerialWriter(WRITER_BUFSIZE, registeredTypes);
}
return writer;
}
/// <summary>
/// SerialReader
/// </summary>
public static SerialReader NewReader(byte[] buf)
{
return NewReader(new ArraySegment<byte>(buf));
}
/// <summary>
/// SerialReader
/// </summary>
public static SerialReader NewReader(ArraySegment<byte> buf)
{
return new SerialReader(buf, registeredTypes, readFuncs);
}
/// <summary>
/// カスタム型を登録する
/// </summary>
/// <typeparam name="T">登録する型(IWSNet2Serializable)</typeparam>
/// <param name="classID">クラス識別子</param>
public static void Register<T>(byte classID) where T : class, IWSNet2Serializable, new()
{
var t = typeof(T);
if (registeredTypes.ContainsKey(t))
{
var msg = string.Format("Type '{0}' is aleady registered as {1}", t, classID);
throw new ArgumentException(msg);
}
if (readFuncs[classID] != null)
{
var msg = string.Format("ClassID '{0}' is aleady used for {1}", classID, t);
throw new ArgumentException(msg);
}
registeredTypes[t] = classID;
readFuncs[classID] = (reader, obj) => reader.ReadObject<T>(obj as T);
}
}
/// <summary>
/// Websocketで送受信するカスタム型はこのインターフェイスを実装する
/// </summary>
public interface IWSNet2Serializable
{
/// <summary>
/// Serializeする.
/// </summary>
/// <param name="writer">writer</param>
void Serialize(SerialWriter writer);
/// <summary>
/// Deserializeする.
/// </summary>
/// <param name="reader">reader</param>
/// <param name="size">readerから利用可能なデータ長</param>
void Deserialize(SerialReader reader, int size);
}
enum Type : byte
{
Null = 0,
False,
True,
SByte,
Byte,
Char,
Short,
UShort,
Int,
UInt,
Long,
ULong,
Float,
Double,
Decimal,
Str8,
Str16,
Obj,
List,
Dict,
Bools,
SBytes,
Bytes,
Chars,
Shorts,
UShorts,
Ints,
UInts,
Longs,
ULongs,
Floats,
Doubles,
Decimals,
}
[Serializable()]
public class WSNet2SerializerException : Exception
{
public WSNet2SerializerException() : base()
{
}
public WSNet2SerializerException(string message) : base(message)
{
}
public WSNet2SerializerException(string message, Exception innerException) : base(message, innerException)
{
}
}
}