-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClientHandler.cs
More file actions
194 lines (169 loc) · 6.34 KB
/
ClientHandler.cs
File metadata and controls
194 lines (169 loc) · 6.34 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using SkylinesPythonShared;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace SkylinesRemotePython
{
public class ClientHandler : TcpConversation
{
private PythonEngine engine;
private Dictionary<long, CallbackHandle> callbackDict = new Dictionary<long, CallbackHandle>();
private long counter = 1;
public bool AsynchronousMode { get; set; }
[ThreadStatic]
public static ClientHandler Instance;
public static void Accept(Socket listener, Socket handler)
{
ClientHandler client = new ClientHandler(handler);
client.HandleClient();
}
protected ClientHandler(Socket client) : base(client)
{
Instance = this;
}
private void HandleClient()
{
try {
engine = new PythonEngine(this);
while (true) {
try {
GetMessage(out long _1, out object _2, out string _3);
}
catch (AbortScriptException) {
SendMessage(null, "c_ready");
}
}
} catch(Exception ex) {
Console.WriteLine(ex);
SkylinesRemotePythonDotnet.exitEvent.Set();
}
}
private void GetMessage(out long requestId, out object result, out string error)
{
requestId = 0;
result = null;
error = null;
MessageHeader msg = AwaitMessage();
#if DEBUG
Console.WriteLine("In: " + msg.messageType);
#endif
if (msg.messageType == "s_script_run") {
engine.RunScript(msg.payload);
return;
}
if (msg.messageType == "s_script_abort") {
Console.WriteLine("Abort script");
throw new AbortScriptException();
}
if (msg.requestId != 0) {
var callback = callbackDict[msg.requestId];
callbackDict.Remove(msg.requestId);
requestId = msg.requestId;
callback.Resolved = true;
if(msg.messageType != "s_exception") {
result = callback.Callback.Invoke(msg.payload, null);
} else {
error = (string)msg.payload;
callback.Callback.Invoke(null, error);
if(!AsynchronousMode) {
throw new Exception(error);
} else {
Print("Exception: " + error + "\n");
}
}
return;
}
if (msg.messageType == "s_exception") {
string text = (string)msg.payload;
Console.WriteLine("Exception: " + text);
throw new Exception(text);
}
throw new Exception("Received unknown message: '" + msg.messageType + "'");
}
public CallbackHandle RemoteCall(Contract contract, object param, Func<object, string, object> callback)
{
var handle = RemoteCallInternal(contract, param, callback);
if (!AsynchronousMode && !contract.IsAsyncByDefault && contract.RetType != null) {
WaitOnHandle(handle);
}
return handle;
}
private CallbackHandle RemoteCallInternal(Contract contract, object param, Func<object, string, object> callback)
{
long requestId = counter;
counter++;
var handle = new CallbackHandle(requestId, callback);
callbackDict.Add(requestId, handle);
SendMessage(param, "c_callfunc_" + contract.FuncName, requestId);
return handle;
}
public T SynchronousCall<T>(Contract contract, object param)
{
var handle = RemoteCallInternal(contract, param, (ret, error) => {
return ret;
});
return contract.RetType != null ?(T)WaitOnHandle(handle) : default(T);
}
public object WaitOnHandle(CallbackHandle handle)
{
if(handle.Resolved) {
return null;
}
if (!callbackDict.ContainsKey(handle.HandleId)) {
throw new Exception("Engine error (report to developers). Cannot wait on invalid handle");
}
#if DEBUG
var callerMethod = (new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod();
var callerName = (new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name;
if(callerName == "SynchronousCall") {
callerMethod = (new System.Diagnostics.StackTrace()).GetFrame(2).GetMethod();
}
Console.WriteLine($"Waiting on handle {handle.HandleId}. Caller: {callerMethod.DeclaringType.Name}.{callerMethod.Name}");
#endif
while (true) {
GetMessage(out long requestId, out object result, out string error);
if(requestId == handle.HandleId) {
return result;
}
// feature - add infinite loop check
}
}
public void WaitForNextMessage()
{
GetMessage(out long requestId, out object result, out string error);
}
public void Print(string text)
{
SendMessage(text, "c_output_message", 0);
}
public void SendMsg(object obj, string type)
{
SendMessage(obj, type);
}
protected override void SendMessage(object obj, string type, long requestId = 0, bool ignoreReturnValue = false)
{
base.SendMessage(obj, type, requestId, ignoreReturnValue);
#if DEBUG
Console.WriteLine("Out: " + type);
#endif
}
}
public class CallbackHandle
{
public CallbackHandle(long handleId, Func<object, string, object> callback)
{
HandleId = handleId;
Callback = callback;
}
public long HandleId { get; private set; }
public Func<object, string, object> Callback { get; private set; }
public bool Resolved { get; set; }
}
public class AbortScriptException : Exception
{
}
}