forked from MasterDevs/ChromeDevTools
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChromeSession.cs
410 lines (359 loc) · 14.3 KB
/
ChromeSession.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#if !NETSTANDARD1_5
using MasterDevs.ChromeDevTools.Serialization;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WebSocket4Net;
namespace MasterDevs.ChromeDevTools
{
public class ChromeSession : IChromeSession
{
private readonly string _endpoint;
private readonly ConcurrentDictionary<string, ConcurrentBag<Action<object>>> _handlers = new ConcurrentDictionary<string, ConcurrentBag<Action<object>>>();
private ICommandFactory _commandFactory;
private IEventFactory _eventFactory;
private ManualResetEvent _openEvent = new ManualResetEvent(false);
private ManualResetEvent _publishEvent = new ManualResetEvent(false);
private ConcurrentDictionary<long, ManualResetEventSlim> _requestWaitHandles = new ConcurrentDictionary<long, ManualResetEventSlim>();
private ICommandResponseFactory _responseFactory;
private ConcurrentDictionary<long, ICommandResponse> _responses = new ConcurrentDictionary<long, ICommandResponse>();
private WebSocket _webSocket;
private static object _Lock = new object();
public ChromeSession(string endpoint, ICommandFactory commandFactory, ICommandResponseFactory responseFactory, IEventFactory eventFactory)
{
_endpoint = endpoint;
_commandFactory = commandFactory;
_responseFactory = responseFactory;
_eventFactory = eventFactory;
}
public void Dispose()
{
if (null == _webSocket) return;
if (_webSocket.State == WebSocketState.Open)
{
_webSocket.Close();
}
_webSocket.Dispose();
}
private void EnsureInit()
{
if (null == _webSocket)
{
lock (_Lock)
{
if (null == _webSocket)
{
Init().Wait();
}
}
}
}
private Task Init()
{
_openEvent.Reset();
_webSocket = new WebSocket(_endpoint);
_webSocket.EnableAutoSendPing = false;
_webSocket.Opened += WebSocket_Opened;
_webSocket.MessageReceived += WebSocket_MessageReceived;
_webSocket.Error += WebSocket_Error;
_webSocket.Closed += WebSocket_Closed;
_webSocket.DataReceived += WebSocket_DataReceived;
_webSocket.Open();
return Task.Run(() =>
{
_openEvent.WaitOne();
});
}
public Task<ICommandResponse> SendAsync<T>(CancellationToken cancellationToken)
{
var command = _commandFactory.Create<T>();
return SendCommand(command, cancellationToken);
}
public Task<CommandResponse<T>> SendAsync<T>(ICommand<T> parameter, CancellationToken cancellationToken)
{
var command = _commandFactory.Create(parameter);
var task = SendCommand(command, cancellationToken);
return CastTaskResult<ICommandResponse, CommandResponse<T>>(task);
}
private Task<TDerived> CastTaskResult<TBase, TDerived>(Task<TBase> task) where TDerived : TBase
{
TaskCompletionSource<TDerived> tcs = new TaskCompletionSource<TDerived>();
task.ContinueWith(
// t => tcs.SetResult((TDerived)t.Result)
delegate (Task<TBase> t)
{
try
{
TDerived res = (TDerived)t.Result;
tcs.SetResult(res);
}
catch (System.Exception ex)
{
ErrorResponse error = t.Result as ErrorResponse;
if (error != null)
{
string errorMessage = "";
try
{
errorMessage += "Id " + System.Convert.ToString(error.Id, System.Globalization.CultureInfo.InvariantCulture) + System.Environment.NewLine;
}
catch
{ }
try
{
errorMessage += "Error " + System.Convert.ToString(error.Error.Code, System.Globalization.CultureInfo.InvariantCulture) + System.Environment.NewLine;
}
catch
{ }
try
{
errorMessage += error.Error.Message + System.Environment.NewLine;
}
catch
{ }
try
{
errorMessage += error.Method;
}
catch
{ }
tcs.SetException(new Exception(errorMessage, ex));
}
else
tcs.SetException(ex);
}
}, TaskContinuationOptions.OnlyOnRanToCompletion
);
task.ContinueWith(
// t => tcs.SetException(t.Exception.InnerExceptions)
delegate (Task<TBase> t)
{
try
{
tcs.SetException(t.Exception.InnerExceptions);
}
catch (System.Exception ex)
{
tcs.SetException(ex);
}
}
, TaskContinuationOptions.OnlyOnFaulted
);
task.ContinueWith(
// t => tcs.SetCanceled()
delegate (Task<TBase> t)
{
try
{
tcs.SetCanceled();
}
catch (System.Exception ex)
{
tcs.SetException(ex);
}
}
, TaskContinuationOptions.OnlyOnCanceled
);
return tcs.Task;
}
public void Subscribe<T>(Action<T> handler) where T : class
{
var handlerType = typeof(T);
var handlerForBag = new Action<object>(obj => handler((T)obj));
_handlers.AddOrUpdate(handlerType.FullName,
(m) => new ConcurrentBag<Action<object>>(new [] { handlerForBag }),
(m, currentBag) =>
{
currentBag.Add(handlerForBag);
return currentBag;
});
}
private void HandleEvent(IEvent evnt)
{
if (null == evnt
|| null == evnt)
{
return;
}
var type = evnt.GetType().GetGenericArguments().FirstOrDefault();
if (null == type)
{
return;
}
var handlerKey = type.FullName;
ConcurrentBag<Action<object>> handlers = null;
if (_handlers.TryGetValue(handlerKey, out handlers))
{
var localHandlers = handlers.ToArray();
foreach (var handler in localHandlers)
{
ExecuteHandler(handler, evnt);
}
}
}
private void ExecuteHandler(Action<object> handler, dynamic evnt)
{
if (evnt.GetType().GetGenericTypeDefinition() == typeof(Event<>))
{
handler(evnt.Params);
} else
{
handler(evnt);
}
}
private void HandleResponse(ICommandResponse response)
{
if (null == response) return;
ManualResetEventSlim requestMre;
if (_requestWaitHandles.TryGetValue(response.Id, out requestMre))
{
_responses.AddOrUpdate(response.Id, id => response, (key, value) => response);
requestMre.Set();
}
else
{
// in the case of an error, we don't always get the request Id back :(
// if there is only one pending requests, we know what to do ... otherwise
if (1 == _requestWaitHandles.Count)
{
var requestId = _requestWaitHandles.Keys.First();
_requestWaitHandles.TryGetValue(requestId, out requestMre);
_responses.AddOrUpdate(requestId, id => response, (key, value) => response);
requestMre.Set();
}
}
}
private Task<ICommandResponse> SendCommand(Command command, CancellationToken cancellationToken)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new MessageContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
};
var requestString = JsonConvert.SerializeObject(command, settings);
var requestResetEvent = new ManualResetEventSlim(false);
_requestWaitHandles.AddOrUpdate(command.Id, requestResetEvent, (id, r) => requestResetEvent);
return Task.Run(() =>
{
EnsureInit();
_webSocket.Send(requestString);
requestResetEvent.Wait(cancellationToken);
ICommandResponse response = null;
_responses.TryRemove(command.Id, out response);
_requestWaitHandles.TryRemove(command.Id, out requestResetEvent);
return response;
});
}
private bool TryGetCommandResponse(byte[] data, out ICommandResponse response)
{
response = _responseFactory.Create(data);
return null != response;
}
private bool TryGetCommandResponse(string message, out ICommandResponse response)
{
response = _responseFactory.Create(message);
return null != response;
}
private bool TryGetEvent(byte[] data, out IEvent evnt)
{
evnt = _eventFactory.Create(data);
return null != evnt;
}
private bool TryGetEvent(string message, out IEvent evnt)
{
evnt = _eventFactory.Create(message);
return null != evnt;
}
private void WebSocket_Closed(object sender, EventArgs e)
{
}
private void WebSocket_DataReceived(object sender, DataReceivedEventArgs e)
{
ICommandResponse response;
if (TryGetCommandResponse(e.Data, out response))
{
HandleResponse(response);
return;
}
IEvent evnt;
if (TryGetEvent(e.Data, out evnt))
{
HandleEvent(evnt);
return;
}
throw new Exception("Don't know what to do with response: " + e.Data);
}
private void WebSocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
{
// This causes IIS to crash ?
// throw e.Exception;
try
{
KillChrome();
}
catch (System.Exception)
{ }
try
{
string logfile = System.AppDomain.CurrentDomain.BaseDirectory;
logfile = System.IO.Path.Combine(logfile, "Exceptions.txt");
System.IO.File.AppendAllText(logfile, "\r\n\r\n\r\n\r\n", System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, e.Exception.Message, System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, "\r\n\r\n", System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, e.Exception.StackTrace, System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, "\r\n\r\n", System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, e.Exception.ToString(), System.Text.Encoding.UTF8);
System.IO.File.AppendAllText(logfile, "\r\n\r\n", System.Text.Encoding.UTF8);
}
catch (System.Exception)
{ }
}
public string KillChrome()
{
// Return System.Web.HttpContext.Current.Server.MapPath("~")
// Return GetType(Converter).Assembly.Location
string s = System.Environment.MachineName + ": ";
s += "taskkill /F /IM chrome.exe";
// taskkill /F /IM chrome.exe
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "taskkill.exe";
startInfo.Arguments = "/F /IM chrome.exe";
using (System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(startInfo))
{
s += exeProcess.StandardOutput.ReadToEnd();
s += exeProcess.StandardError.ReadToEnd();
exeProcess.WaitForExit();
}
return s;
}
private void WebSocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
ICommandResponse response;
if (TryGetCommandResponse(e.Message, out response))
{
HandleResponse(response);
return;
}
IEvent evnt;
if (TryGetEvent(e.Message, out evnt))
{
HandleEvent(evnt);
return;
}
throw new Exception("Don't know what to do with response: " + e.Message);
}
private void WebSocket_Opened(object sender, EventArgs e)
{
_openEvent.Set();
}
}
}
#endif