-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathDefaultDebugger.cs
More file actions
116 lines (100 loc) · 3.85 KB
/
DefaultDebugger.cs
File metadata and controls
116 lines (100 loc) · 3.85 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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using OneScript.DebugProtocol.Abstractions;
using OneScript.DebugProtocol.TcpServer;
using OneScript.DebugServices.Internal;
using ScriptEngine.Machine.Debugger;
namespace OneScript.DebugServices
{
/// <summary>
/// Простой отладчик, поддерживающий одновременно только одну сессию отладки.
/// </summary>
public class DefaultDebugger : IDebugger
{
// NB! должен быть согласован с перечислением TransportProtocols в адаптере
private const short JSON_FORMAT_MARKER = 2;
// NB! должен быть согласован с файлом ProtocolVersions в адаптере
private const short SUPPORTED_FORMAT_VERSION = 4;
private readonly IDebugServer _transport;
private IDebugSession _session;
private readonly object _sessionRecycleLock = new object();
public DefaultDebugger(IDebugServer transport)
{
_transport = transport;
}
public bool IsEnabled => true;
public bool AttachMode { get; set; }
public void Start()
{
_transport.OnClientConnected += TransportOnOnClientConnected;
_transport.OnListenException += ListenerException;
_transport.Listen();
}
private void ListenerException(object sender, ListenerErrorEventArgs e)
{
e.StopServer = true;
}
private void TransportOnOnClientConnected(object sender, IDebuggerClient debuggerClient)
{
lock (_sessionRecycleLock)
{
if (_session?.IsActive == true)
{
// Если есть активная сессия, то не начинаем новую
debuggerClient.Dispose();
return;
}
}
var dataStream = debuggerClient.GetDataStream();
if (FormatReconcileUtils.CheckReconcileRequest(dataStream))
{
// Да, это наш фейковый заголовок
FormatReconcileUtils.WriteReconcileResponse(dataStream, JSON_FORMAT_MARKER, SUPPORTED_FORMAT_VERSION);
}
else
{
// Не поддерживаем старый формат протокола отладки
debuggerClient.Dispose();
return;
}
lock (_sessionRecycleLock)
{
var session = new DebugSession(debuggerClient, AttachMode);
session.OnClose += OnSessionClose;
if (_session is ConnectableSessionProxy proxy)
{
proxy.Connect(session);
}
else
{
_session = session;
}
}
}
private void OnSessionClose(DebugSession session)
{
lock (_sessionRecycleLock)
{
session.OnClose -= OnSessionClose;
_session = null;
}
}
public IDebugSession GetSession()
{
lock (_sessionRecycleLock)
{
return _session ??= new ConnectableSessionProxy(AttachMode);
}
}
public void NotifyProcessExit(int exitCode)
{
_transport.OnClientConnected-=TransportOnOnClientConnected;
_transport.Stop();
_session?.Dispose();
}
}
}