-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCefController.cs
240 lines (203 loc) · 6.2 KB
/
CefController.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using CefUnityLib.Helpers;
using CefUnityLib.Messages;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CefUnityLib
{
public class CefController
{
protected string pipeName;
protected NamedPipeClientStream stream;
protected StreamReader reader;
protected StreamWriter writer;
public EventHandler<PipeProtoMessage> MessageReceived;
public EventHandler<bool> ConnectionStateChanged;
protected ulong statMsgsReceived = 0;
protected ulong statMsgsSent = 0;
public ulong MessagesReceivedCount
{
get
{
return statMsgsReceived;
}
}
public ulong MessagesSentCount
{
get
{
return statMsgsSent;
}
}
public string PipeName
{
get
{
return pipeName;
}
}
public bool Connected
{
get
{
return stream != null && stream.IsConnected;
}
}
public CefController(string pipeName = "default")
{
// Auto prefix pipe name
if (!pipeName.StartsWith(Consts.NAMED_PIPE_PREFIX))
{
pipeName = Consts.NAMED_PIPE_PREFIX + pipeName;
}
this.pipeName = pipeName;
}
public bool SendMessage(PipeProtoMessage msg)
{
if (Connected)
{
try
{
msg.WriteToClientStream(stream, true);
statMsgsSent++;
}
catch (Exception ex)
{
Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString());
if (Debugger.IsAttached)
{
Debugger.Break();
}
return false;
}
return true;
}
return false;
}
public void SendMouseEvent(byte eventType, Int32 coordX, Int32 coordY, MouseButtons mouseButtons)
{
SendMessage(new MouseEventPipeMessage(eventType, coordX, coordY, mouseButtons));
}
public void SendMouseWheelEvent(int x, int y, int delta)
{
SendMessage(new MouseWheelEventPipeMessage(x, y, delta));
}
public void SendKeyEvent(byte eventType, Keys keys, Keys modifiers)
{
SendMessage(new KeyEventPipeMessage(eventType, keys, modifiers));
}
public void SendKeyCharEvent(int keyCharCode, Keys modifiers = Keys.None)
{
SendMessage(new KeyEventPipeMessage(KeyEventPipeMessage.TYPE_KEY_CHAR, keyCharCode, modifiers));
}
public void SendShutdownMessage()
{
SendMessage(new PipeProtoMessage
{
Opcode = PipeProto.OPCODE_SHUTDOWN
});
}
public void SendRepaintRequest()
{
SendMessage(new PipeProtoMessage
{
Opcode = PipeProto.OPCODE_FRAME
});
}
public void Disconnect()
{
if (stream != null)
{
try
{
stream.Dispose();
}
catch (Exception) { }
}
stream = null;
if (ConnectionStateChanged != null)
{
ConnectionStateChanged.Invoke(this, Connected);
}
}
#pragma warning disable CS4014
public void Connect()
{
if (Connected)
{
return;
}
stream = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
try
{
stream.Connect(3000);
}
catch (Exception ex)
{
Disconnect();
if (ConnectionStateChanged != null)
{
ConnectionStateChanged.Invoke(this, Connected);
}
throw ex;
}
Task.Run(new Action(() =>
{
if (ConnectionStateChanged != null)
{
ConnectionStateChanged.Invoke(this, Connected);
}
// Send ping message
var pingMessage = new PipeProtoMessage
{
Opcode = PipeProto.OPCODE_PING
};
try
{
pingMessage.WriteToClientStream(stream, true);
statMsgsSent++;
}
catch (Exception ex)
{
Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString());
if (Debugger.IsAttached)
{
Debugger.Break();
}
Disconnect();
}
// Ask for a repaint
SendRepaintRequest();
while (Connected)
{
// Read next message
PipeProtoMessage incomingMessage = null;
try
{
incomingMessage = PipeProtoMessage.ReadFromStream(stream);
}
catch (Exception) { }
if (incomingMessage != null)
{
try
{
if (MessageReceived != null)
{
MessageReceived.Invoke(this, incomingMessage);
statMsgsReceived++;
}
}
catch (Exception) { }
}
}
}));
}
#pragma warning restore CS4014
}
}