-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogCat.cs
75 lines (70 loc) · 2.61 KB
/
LogCat.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BalatroMessager
{
public class LogCat
{
private readonly TcpListener _listener;
public LogCat(int port)
{
_listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, port));
}
public async Task Listen(CancellationToken token)
{
try
{
_listener.Start();
var buffer = new byte[1024];
while (!token.IsCancellationRequested)
{
Console.WriteLine($"[LogCat] {_listener.LocalEndpoint} Listening...");
while (!_listener.Pending())
await Task.Delay(100, token);
using (var client = _listener.AcceptTcpClient())
using (var stream = client.GetStream())
{
Console.WriteLine("[LogCat] Connected. Receiving...");
try
{
while (!token.IsCancellationRequested)
{
var len = await stream.ReadAsync(buffer, 0, buffer.Length, token);
if (len == 0)
{
Console.WriteLine("[LogCat] Disconnected.");
break;
}
Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, len));
}
}
catch (SocketException ex)
{
Console.WriteLine("[LogCat] Socket Error occurred while listening. Code: {0}\n{1}", ex.ErrorCode, ex);
}
catch (Exception ex)
{
Console.WriteLine("[LogCat] {0} Error occurred while listening: {1}", ex.GetType().Name, ex.Message);
}
try
{
client.Close();
}
catch (Exception ex)
{
Console.WriteLine("[LogCat] close throw exception: {0}", ex);
}
}
}
}
finally
{
_listener.Stop();
Console.WriteLine("[LogCat] Stopped.");
}
}
}
}