-
Notifications
You must be signed in to change notification settings - Fork 83
/
MessageProcessor.cs
62 lines (58 loc) · 2.09 KB
/
MessageProcessor.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Threading;
using GestureSign.Common.Gestures;
using GestureSign.Common.InterProcessCommunication;
using Point = System.Drawing.Point;
namespace GestureSign.ControlPanel
{
class MessageProcessor : IMessageProcessor
{
public static event EventHandler<PointPattern[]> GotNewPattern;
public bool ProcessMessages(NamedPipeServerStream server)
{
try
{
BinaryFormatter binForm = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
server.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
object data = binForm.Deserialize(memoryStream);
Application.Current?.Dispatcher.InvokeAsync(() =>
{
string message = data as string;
if (message != null)
{
switch (message)
{
case "Exit":
{
Application.Current.Shutdown();
break;
}
}
}
else
{
var newGesture = data as List<List<List<Point>>>;
if (newGesture == null) return;
GotNewPattern?.Invoke(this, newGesture.Select(list => new PointPattern(list)).ToArray());
}
}, DispatcherPriority.Input);
}
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
}
}