2 using System
.Collections
.Generic
;
5 using System
.Threading
;
9 public class ServerContext
12 public static int DefaultRefSlotsCap
= 256;
13 public PxpObject
[] refSlots
= new PxpObject
[256];
14 public Dictionary
<String
, Object
> funcMap
= new Dictionary
<String
, Object
>();
15 protected BuiltInFuncList builtIn
;
16 public void init(Stream stream
)
19 builtIn
= new BuiltInFuncList();
20 funcMap
["builtin"] = builtIn
;
22 public bool running
= true;
24 protected void putRefSlots(int addr
, PxpObject r
)
26 if (refSlots
[addr
] != null)
27 refSlots
[addr
].release();
32 private Mutex priv__writeLock
= new Mutex();
33 public Mutex
writeLock()
35 return priv__writeLock
;
37 public void push(PxpRequest r
)
39 putRefSlots(r
.destAddr
, new PxpObject(r
.parameter
));
40 writeLock().WaitOne();
41 this.stream
.Write(r
.session
);
42 writeLock().ReleaseMutex();
45 public void pull(PxpRequest r
)
48 if (refSlots
[r
.srcAddr
] != null)
50 o
= refSlots
[r
.srcAddr
].get();
52 writeLock().WaitOne();
53 this.stream
.Write(r
.session
);
62 byte[] b
= Encoding
.UTF8
.GetBytes((String
)o
);
70 writeLock().ReleaseMutex();
73 public void assign(PxpRequest r
)
75 putRefSlots(r
.destAddr
, this.refSlots
[r
.srcAddr
]);
76 writeLock().WaitOne();
77 this.stream
.Write(r
.session
);
78 writeLock().ReleaseMutex();
81 public void unlink(PxpRequest r
)
83 putRefSlots(r
.destAddr
, null);
84 writeLock().WaitOne();
85 this.stream
.Write(r
.session
);
86 writeLock().ReleaseMutex();
89 public void call(PxpRequest r
)
92 r
.callable
.call(r
, (Object result
) =>
96 putRefSlots(r
.destAddr
, new PxpObject(result
));
98 writeLock().WaitOne();
99 stream
.Write(r
.session
);
100 r
.callable
.writeResult(r
);
101 writeLock().ReleaseMutex();
108 public void getInfo(PxpRequest r
)
110 writeLock().WaitOne();
111 this.stream
.Write(r
.session
);
113 b
= Encoding
.UTF8
.GetBytes(
114 "server name:pxprpc for c#\n" +
116 "reference slots capacity:" + this.refSlots
.Length
+ "\n"
118 writeInt32(b
.Length
);
120 writeLock().ReleaseMutex();
129 PxpRequest r
= new PxpRequest();
131 byte[] session
= new byte[4];
132 Utils
.readf(stream
, session
);
134 r
.opcode
= session
[0];
138 r
.destAddr
= readInt32();
139 int len
= readInt32();
140 byte[] buf
= new byte[len
];
141 Utils
.readf(stream
, buf
);
146 r
.srcAddr
= readInt32();
150 r
.destAddr
= readInt32();
151 r
.srcAddr
= readInt32();
155 r
.destAddr
= readInt32();
159 r
.destAddr
= readInt32();
160 r
.srcAddr
= readInt32();
161 r
.callable
= (PxpCallable
)refSlots
[r
.srcAddr
].get();
162 r
.callable
.readParameter(r
);
166 r
.destAddr
= readInt32();
167 r
.srcAddr
= readInt32();
181 public int readInt32()
183 return Utils
.readInt32(stream
);
186 public long readInt64()
188 return Utils
.readInt64(stream
);
190 public float readFloat32()
192 return Utils
.readFloat32(stream
);
194 public double readFloat64()
196 return Utils
.readFloat64(stream
);
198 public byte[] readNextRaw()
200 int addr
= readInt32();
201 return (byte[])refSlots
[addr
].get();
203 public void getFunc(PxpRequest r
)
205 String name
= getStringAt(r
.srcAddr
);
206 int namespaceDelim
= name
.LastIndexOf(".");
207 String ns
= name
.Substring(0, namespaceDelim
);
208 String func
= name
.Substring(namespaceDelim
+ 1);
209 Object obj
= funcMap
[ns
];
210 PxpCallable found
= null;
213 found
= builtIn
.getBoundMethod(obj
, func
);
215 writeLock().WaitOne();
219 this.stream
.Write(r
.session
);
225 putRefSlots(r
.destAddr
, new PxpObject(found
));
227 this.stream
.Write(r
.session
);
229 writeInt32(r
.destAddr
);
232 writeLock().ReleaseMutex();
236 public String
readNextString()
238 int addr
= readInt32();
239 return getStringAt(addr
);
242 public String
getStringAt(int addr
)
244 if (refSlots
[addr
] == null)
248 Object o
= refSlots
[addr
].get();
251 return Encoding
.UTF8
.GetString((byte[])o
);
259 public void writeInt32(int d
)
261 Utils
.writeInt32(stream
, d
);
263 public void writeInt64(long d
)
265 Utils
.writeInt64(stream
, d
);
267 public void writeFloat32(float d
)
269 Utils
.writeFloat32(stream
, d
);
271 public void writeFloat64(double d
)
273 Utils
.writeFloat64(stream
, d
);
277 public void Dispose()
282 for (int i1
= 0; i1
< refSlots
.Length
; i1
++)
284 if (refSlots
[i1
] != null)
288 refSlots
[i1
].release();
290 catch (Exception e
) { }