-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPipeProto.cs
53 lines (43 loc) · 1.5 KB
/
PipeProto.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CefUnityLib
{
public static class PipeProto
{
public const byte PACKET_START_MARKER = 2;
public const byte LENGTH_MARKER_SIZE = 4;
public const byte OPCODE_FRAME = 1;
public const byte OPCODE_RESIZE = 2;
public const byte OPCODE_NAVIGATE = 3;
public const byte OPCODE_SCRIPT = 4;
public const byte OPCODE_PING = 5;
public const byte OPCODE_MOUSE_EVENT = 6;
public const byte OPCODE_KEY_EVENT = 7;
public const byte OPCODE_SHUTDOWN = 8;
public const byte OPCODE_MOUSE_WHEEL_EVENT = 9;
public static byte[] BytesToProtoMessage(byte[] input, byte opcode)
{
if (input == null)
{
input = new byte[0];
}
// [START BYTE] + [OPCODE] + [LENGTH MARKER] + [PAYLOAD]
byte[] output = new byte[1 + 1 + LENGTH_MARKER_SIZE + input.Length];
var idx = 0;
// Start marker and opcode
output[idx++] = PACKET_START_MARKER;
output[idx++] = opcode;
// Length marker
var lengthMarkerBytes = BitConverter.GetBytes((UInt32)input.Length);
lengthMarkerBytes.CopyTo(output, idx);
idx += LENGTH_MARKER_SIZE;
// Payload
input.CopyTo(output, idx);
// All done
return output;
}
}
}