BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Scripts / Packet.cs

Packet.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
Packet.cs 1.4 KiB Download Raw
1namespace Oculus.Platform
2{
3 using System;
4 using System.Runtime.InteropServices;
5
6 public sealed class Packet : IDisposable
7 {
8 private readonly ulong size;
9 private readonly IntPtr packetHandle;
10
11 public Packet(IntPtr packetHandle)
12 {
13 this.packetHandle = packetHandle;
14 size = (ulong) CAPI.ovr_Packet_GetSize(packetHandle);
15 }
16
17 /**
18 * Copies all the bytes in the payload into byte[] destination. ex:
19 * Package package ...
20 * byte[] destination = new byte[package.Size];
21 * package.ReadBytes(destination);
22 */
23 public ulong ReadBytes(byte[] destination)
24 {
25 if ((ulong) destination.LongLength < size)
26 {
27 throw new System.ArgumentException(String.Format("Destination array was not big enough to hold {0} bytes", size));
28 }
29 Marshal.Copy(CAPI.ovr_Packet_GetBytes(packetHandle), destination, 0, (int) size);
30 return size;
31 }
32
33 public UInt64 SenderID
34 {
35 get { return CAPI.ovr_Packet_GetSenderID(packetHandle); }
36 }
37
38 public ulong Size
39 {
40 get { return size; }
41 }
42
43 public SendPolicy Policy
44 {
45 get { return (SendPolicy)CAPI.ovr_Packet_GetSendPolicy(packetHandle); }
46 }
47
48 #region IDisposable
49
50 ~Packet()
51 {
52 Dispose();
53 }
54
55 public void Dispose()
56 {
57 CAPI.ovr_Packet_Free(packetHandle);
58 GC.SuppressFinalize(this);
59 }
60
61 #endregion
62 }
63}