-
Notifications
You must be signed in to change notification settings - Fork 8
/
SpRpc.cs
192 lines (152 loc) · 5.11 KB
/
SpRpc.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
public enum SpRpcOp {
Request,
Response,
Unknown,
}
public class SpRpcResult {
public int Session;
public SpProtocol Protocol;
public SpRpcOp Op;
public SpObject Arg;
public SpRpcResult () {
Session = 0;
Protocol = null;
Op = SpRpcOp.Unknown;
Arg = null;
}
public SpRpcResult (int s, SpProtocol p, SpRpcOp o, SpObject a) {
Session = s;
Protocol = p;
Op = o;
Arg = a;
}
}
public class SpRpc {
private SpTypeManager mHostTypeManager;
private SpTypeManager mAttachTypeManager;
private SpType mHeaderType;
private Dictionary<int, SpProtocol> mSessions = new Dictionary<int, SpProtocol> ();
public SpRpc (SpTypeManager tm, SpType t) {
mHostTypeManager = tm;
mHeaderType = t;
}
public void Attach (string proto) {
Attach (SpTypeManager.Import (proto));
}
public void Attach (SpTypeManager tm) {
mAttachTypeManager = tm;
}
public SpStream Request (string proto) {
return Request (proto, null);
}
public SpStream Request (string proto, SpObject args) {
return Request (proto, args, 0);
}
public SpStream Request (string proto, SpObject args, int session) {
SpStream encode_stream = EncodeRequest (proto, args, session);
encode_stream.Position = 0;
return SpPacker.Pack (encode_stream);
}
public bool Request (string proto, SpObject args, int session, SpStream stream) {
SpStream encode_stream = EncodeRequest (proto, args, session);
encode_stream.Position = 0;
return SpPacker.Pack (encode_stream, stream);
}
private SpStream EncodeRequest (string proto, SpObject args, int session) {
if (mAttachTypeManager == null || mHostTypeManager == null || mHeaderType == null)
return null;
SpProtocol protocol = mAttachTypeManager.GetProtocolByName (proto);
if (protocol == null)
return null;
SpObject header = new SpObject ();
header.Insert ("type", protocol.Tag);
if (session != 0)
header.Insert ("session", session);
SpStream stream = mHostTypeManager.Codec.Encode (mHeaderType, header);
if (stream == null)
return null;
if (args != null) {
if (mAttachTypeManager.Codec.Encode (protocol.Request, args, stream) == false) {
if (stream.IsOverflow ()) {
if (stream.Position > SpCodec.MAX_SIZE)
return null;
int size = stream.Position;
size = ((size + 7) / 8) * 8;
stream = new SpStream (size);
if (mAttachTypeManager.Codec.Encode (protocol.Request, args, stream) == false)
return null;
}
else {
return null;
}
}
}
if (session != 0) {
mSessions[session] = protocol;
}
return stream;
}
public SpStream Response (int session, SpObject args) {
SpObject header = new SpObject ();
header.Insert ("session", session);
SpStream encode_stream = new SpStream ();
mHostTypeManager.Codec.Encode (mHeaderType, header, encode_stream);
if (session != 0 && mSessions.ContainsKey (session)) {
mHostTypeManager.Codec.Encode (mSessions[session].Response, args, encode_stream);
}
SpStream pack_stream = new SpStream ();
encode_stream.Position = 0;
SpPacker.Pack (encode_stream, pack_stream);
pack_stream.Position = 0;
return pack_stream;
}
public SpRpcResult Dispatch (SpStream stream) {
SpStream unpack_stream = SpPacker.Unpack (stream);
unpack_stream.Position = 0;
SpObject header = mHostTypeManager.Codec.Decode (mHeaderType, unpack_stream);
int session = 0;
if (header["session"] != null)
session = header["session"].AsInt ();
if (header["type"] != null) {
// handle request
SpProtocol protocol = mHostTypeManager.GetProtocolByTag (header["type"].AsInt ());
SpObject obj = mHostTypeManager.Codec.Decode (protocol.Request, unpack_stream);
if (session != 0)
mSessions[session] = protocol;
return new SpRpcResult (session, protocol, SpRpcOp.Request, obj);
}
else {
// handle response
if (mSessions.ContainsKey (session) == false)
return new SpRpcResult ();
SpProtocol protocol = mSessions[session];
mSessions.Remove (session);
if (protocol == null)
return new SpRpcResult ();
if (protocol.Response == null)
return new SpRpcResult (session, protocol, SpRpcOp.Response, null);
SpObject obj = mAttachTypeManager.Codec.Decode (protocol.Response, unpack_stream);
return new SpRpcResult (session, protocol, SpRpcOp.Response, obj);
}
}
public static SpRpc Create (Stream proto, string package) {
return Create (SpTypeManager.Import (proto), package);
}
public static SpRpc Create (string proto, string package) {
return Create (SpTypeManager.Import (proto), package);
}
public static SpRpc Create (SpTypeManager tm, string package) {
if (tm == null)
return null;
SpType t = tm.GetType (package);
if (t == null)
return null;
SpRpc rpc = new SpRpc (tm, t);
return rpc;
}
}