forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpDebugServer.cs
More file actions
160 lines (138 loc) · 5.02 KB
/
TcpDebugServer.cs
File metadata and controls
160 lines (138 loc) · 5.02 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
/*----------------------------------------------------------
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 System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using OneScript.DebugProtocol.Abstractions;
namespace OneScript.DebugServices
{
public class TcpDebugServer : IDebugServer
{
private readonly int _port;
private volatile bool _isListening;
private TcpListener _listener;
public TcpDebugServer(int port)
{
_port = port;
}
public enum LogLevel
{
Debug,
Info,
Error
}
public int ActualPort() => (_listener.LocalEndpoint as IPEndPoint)?.Port ?? 0;
public delegate void LogHandler(LogLevel level, string message);
public event LogHandler OnLogEvent;
public void Listen()
{
if (_isListening)
return;
_listener = TcpListener.Create(_port);
_listener.Start();
_isListening = true;
StartListenerThread();
}
private void StartListenerThread()
{
new Thread(() =>
{
OnLogEvent?.Invoke(LogLevel.Info, "Starting listener thread");
while (_isListening)
{
try
{
var client = _listener.AcceptTcpClient();
OnLogEvent?.Invoke(LogLevel.Debug, "Client connected");
OnClientConnected?.Invoke(this, new TcpDebuggerClient(client));
}
catch (ObjectDisposedException)
{
_isListening = false;
OnLogEvent?.Invoke(LogLevel.Debug, "Listener interrupted");
break;
}
catch (SocketException e)
{
if (_isListening == false)
{
// Сервер находится в процессе остановки внешним потоком
// и ошибка сокета не является ошибкой.
break;
}
OnLogEvent?.Invoke(LogLevel.Error, $"Socket exception: {e}");
if (OnListenException == null)
{
Stop();
break;
}
var args = new ListenerErrorEventArgs(e);
try
{
OnLogEvent?.Invoke(LogLevel.Debug, $"Calling exception handler");
OnListenException?.Invoke(this, args);
if (args.StopServer)
{
Stop();
}
}
catch (Exception handlerException)
{
OnLogEvent?.Invoke(LogLevel.Error, $"Exception handler raised exception {handlerException}");
Stop();
throw;
}
}
}
OnLogEvent?.Invoke(LogLevel.Info, "Listener thread stopped");
})
{
IsBackground = true,
Name = "debug-server-listener",
}.Start();
}
private class TcpDebuggerClient : IDebuggerClient
{
private TcpClient Client { get; }
public TcpDebuggerClient(TcpClient client)
{
Client = client;
}
public void Dispose()
{
Client.Dispose();
}
public Stream GetDataStream()
{
return Client.GetStream();
}
public bool Connected => Client.Connected;
}
public void Stop()
{
OnLogEvent?.Invoke(LogLevel.Debug, "Stopping listener");
_isListening = false;
try
{
_listener?.Stop();
}
catch (Exception e)
{
// тут нечего делать
OnLogEvent?.Invoke(LogLevel.Error, $"Exception while stopping listener: {e}");
}
finally
{
_listener = null;
}
}
public event EventHandler<IDebuggerClient> OnClientConnected;
public event EventHandler<ListenerErrorEventArgs> OnListenException;
}
}