-
Notifications
You must be signed in to change notification settings - Fork 1
/
PipeServer.cs
75 lines (62 loc) · 2.15 KB
/
PipeServer.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.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace MultiClip
{
public class PipeServer
{
private static Thread serverThread;
private static bool run;
public static void Start()
{
run = true;
serverThread = new Thread(ExecuteThread);
serverThread.Name = "PipeServerWatcher";
serverThread.Start();
}
public static void Stop()
{
run = false;
try
{
serverThread.Abort();
}
catch (Exception) { }
}
private static void ExecuteThread()
{
Debug.WriteLine("\n*** Named pipe server stream starting (" + Program.PROCESS_PIPE_NAME + ") ***\n");
var pipeServer = new NamedPipeServerStream(Program.PROCESS_PIPE_NAME, PipeDirection.InOut, 1);
int threadId = Thread.CurrentThread.ManagedThreadId;
while (run)
{
pipeServer.WaitForConnection();
try
{
StreamString ss = new StreamString(pipeServer);
var incoming = ss.ReadString();
Debug.WriteLine("Incoming command from pipe: " + incoming);
ss.WriteString("OK");
pipeServer.Disconnect();
if (incoming.Length <= 0)
{
continue;
}
string[] commandParts = incoming.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
CommandProcessor.Process(commandParts);
}
catch (IOException e)
{
Debug.WriteLine("\n*** ERROR - Named pipe server stream failed, exception details follow [will try to continue] (" + Program.PROCESS_PIPE_NAME + ") ***\n\n {0} \n\n", e.Message);
try
{
pipeServer.Disconnect();
}
catch (Exception) { }
}
}
}
}
}