-
Notifications
You must be signed in to change notification settings - Fork 1
/
PipeHandler.cs
100 lines (88 loc) · 3.44 KB
/
PipeHandler.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
namespace Handlers {
public class PipeHandler {
public static NamedPipeServerStream PipeServer;
public delegate void TransmitHandler(object Object);
public static event TransmitHandler TransmitEvent;
public static void Transmit(string Pipe, string Message, bool WaitForConnection = false) {
using (PipeServer = new NamedPipeServerStream(Pipe, PipeDirection.Out)) {
StreamWriter PipeWriter = new StreamWriter(PipeServer);
if (WaitForConnection == true) { PipeServer.WaitForConnection(); }
PipeWriter.WriteLine(Message);
PipeWriter.Flush();
TransmitEvent?.Invoke(null);
}
}
public delegate void ReceiveHandler(object Object, PipeArgs ReceiveArgs);
public static event ReceiveHandler DataInEvent;
public static NamedPipeClientStream PipeClient;
public static System.Threading.Thread PipeThread;
public static string Name;
public static bool Enable = true;
public static void PipeConnection(string name) {
Name = name;
PipeThread = new Thread(PipeListen) {
IsBackground = true
};
PipeThread.Start();
}
private static void PipeListen() {
while (Enable) {
using (PipeClient = new NamedPipeClientStream(".", Name, PipeDirection.In)) {
if (!PipeClient.IsConnected) {
PipeClient.Connect();
}
using (StreamReader PipeReader = new StreamReader(PipeClient)) {
while (PipeReader.Peek() > -1) {
PipeArgs Pe = new PipeArgs();
Debug.Print(PipeReader.ReadLine());
DataInEvent?.Invoke(null, Pe);
}
}
}
}
}
}
public class PipeConnection {
public delegate void ReceiveHandler(object Object, PipeArgs ReceiveArgs);
public event ReceiveHandler DataInEvent;
public NamedPipeClientStream PipeClient;
public System.Threading.Thread PipeThread;
public string Name;
public bool Enable = true;
public PipeConnection(string name) {
Name = name;
PipeThread = new Thread(PipeListen) {
IsBackground = true
};
PipeThread.Start();
}
public void PipeListen() {
while (Enable) {
using (PipeClient = new NamedPipeClientStream(".", Name, PipeDirection.In)) {
if (!PipeClient.IsConnected) {
PipeClient.Connect();
}
using (StreamReader PipeReader = new StreamReader(PipeClient)) {
while (PipeReader.Peek() > -1) {
PipeArgs Pe = new PipeArgs();
Debug.Print(PipeReader.ReadLine());
DataInEvent?.Invoke(null, Pe);
}
}
}
}
}
}
public class PipeArgs : EventArgs {
public string Value { get; set; }
}
}