forked from QL-Win/QuickLook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeServerManager.cs
More file actions
172 lines (151 loc) · 6.22 KB
/
PipeServerManager.cs
File metadata and controls
172 lines (151 loc) · 6.22 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
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using QuickLook.NativeMethods;
namespace QuickLook
{
internal static class PipeMessages
{
public const string RunAndClose = "QuickLook.App.PipeMessages.RunAndClose";
public const string Switch = "QuickLook.App.PipeMessages.Switch";
public const string Invoke = "QuickLook.App.PipeMessages.Invoke";
public const string Toggle = "QuickLook.App.PipeMessages.Toggle";
public const string Forget = "QuickLook.App.PipeMessages.Forget";
public const string Close = "QuickLook.App.PipeMessages.Close";
public const string Quit = "QuickLook.App.PipeMessages.Quit";
}
internal class PipeServerManager : IDisposable
{
private static readonly string PipeName = "QuickLook.App.Pipe." + WindowsIdentity.GetCurrent().User?.Value;
private static PipeServerManager _instance;
private DispatcherOperation _lastOperation;
private NamedPipeServerStream _server;
public PipeServerManager()
{
_server = new NamedPipeServerStream(PipeName, PipeDirection.In);
new Task(() =>
{
using (var reader = new StreamReader(_server))
{
Debug.WriteLine("PipeManager: Ready");
while (true)
{
_server.WaitForConnection();
var msg = reader.ReadLine();
Debug.WriteLine($"PipeManager: {msg}");
// dispatch message
if (MessageReceived(msg))
return;
_server.Disconnect();
}
}
}).Start();
}
public void Dispose()
{
GC.SuppressFinalize(this);
if (_server != null)
SendMessage(PipeMessages.Quit);
_server?.Dispose();
_server = null;
}
public static void SendMessage(string pipeMessage, string path = null)
{
if (path == null)
path = "";
try
{
using (var client = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
{
client.Connect();
using (var writer = new StreamWriter(client))
{
writer.WriteLine($"{pipeMessage}|{path}");
writer.Flush();
}
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
private bool MessageReceived(string msg)
{
var split = msg.IndexOf('|');
if (split == -1)
return false;
if (_lastOperation != null && _lastOperation.Status == DispatcherOperationStatus.Pending)
{
_lastOperation.Abort();
Debug.WriteLine("Dispatcher task canceled");
}
var wParam = msg.Substring(0, split);
var lParam = msg.Substring(split + 1, msg.Length - split - 1);
switch (wParam)
{
case PipeMessages.RunAndClose:
Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().RunAndClosePreview()),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Invoke:
_lastOperation = Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().InvokePreview(lParam)),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Switch:
_lastOperation = Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().SwitchPreview(lParam)),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Toggle:
_lastOperation = Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().TogglePreview(lParam)),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Forget:
Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().ForgetCurrentWindow()),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Close:
Application.Current.Dispatcher.BeginInvoke(
new Action(() => ViewWindowManager.GetInstance().ClosePreview()),
DispatcherPriority.ApplicationIdle);
return false;
case PipeMessages.Quit:
return true;
default:
return false;
}
}
public static PipeServerManager GetInstance()
{
return _instance ?? (_instance = new PipeServerManager());
}
}
}