This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathNetworkData.cs
241 lines (199 loc) · 7.96 KB
/
NetworkData.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
241
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
// If you want to add more network message types:
// 1. Add a new entry to the MessageType enum.
// 2. Add a new Serialize and Deserialize method to this class, following the existing method patterns.
// 3. Call your new deserialize method in DeserializeData if the header matches your new message type.
// 4. Update SharingManager.ReceiveMessage to react to your new message type.
// 5. Update RemotePlayerManager or any relevant game state to react to your new message data.
namespace SimpleSharing
{
public class NetworkData
{
public enum MessageType
{
Pose = 0,
AirTap = 1,
AnchorName = 2,
};
int WriteHeader(MessageType type)
{
int headerSize = sizeof(int);
Buffer.BlockCopy(BitConverter.GetBytes((int)type), 0, buffer, 0, headerSize);
return headerSize;
}
public const int bufSize = 128;
public byte[] buffer = new byte[bufSize];
// Data to serialize
#region Pose Properties
public Vector3 Position
{
get; private set;
}
public Quaternion Rotation
{
get; private set;
}
#endregion
#region AirTap Properties
public Vector3 AirTapLocation
{
get; private set;
}
public Vector3 AirTapDirection
{
get; private set;
}
public Vector3 AirTapHitLocation
{
get; private set;
}
#endregion
#region AnchorName Properties
public String AnchorName
{
get; private set;
}
public String AnchorIP
{
get; private set;
}
#endregion
#region Serialize Helpers
private void SerializeVector(Vector3 input, ref int dstOffset)
{
Buffer.BlockCopy(BitConverter.GetBytes(input.x), 0, buffer, dstOffset + 0 * sizeof(float), sizeof(float));
Buffer.BlockCopy(BitConverter.GetBytes(input.y), 0, buffer, dstOffset + 1 * sizeof(float), sizeof(float));
Buffer.BlockCopy(BitConverter.GetBytes(input.z), 0, buffer, dstOffset + 2 * sizeof(float), sizeof(float));
dstOffset += 3 * sizeof(float);
}
private Vector3 DeserializeVector(ref int srcOffset)
{
float x = BitConverter.ToSingle(buffer, srcOffset + 0 * sizeof(float));
float y = BitConverter.ToSingle(buffer, srcOffset + 1 * sizeof(float));
float z = BitConverter.ToSingle(buffer, srcOffset + 2 * sizeof(float));
srcOffset += 3 * sizeof(float);
return new Vector3(x, y, z);
}
private void SerializeQuaternion(Quaternion input, ref int dstOffset)
{
Buffer.BlockCopy(BitConverter.GetBytes(input.x), 0, buffer, dstOffset + 0 * sizeof(float), sizeof(float));
Buffer.BlockCopy(BitConverter.GetBytes(input.y), 0, buffer, dstOffset + 1 * sizeof(float), sizeof(float));
Buffer.BlockCopy(BitConverter.GetBytes(input.z), 0, buffer, dstOffset + 2 * sizeof(float), sizeof(float));
Buffer.BlockCopy(BitConverter.GetBytes(input.w), 0, buffer, dstOffset + 3 * sizeof(float), sizeof(float));
dstOffset += 4 * sizeof(float);
}
private Quaternion DeserializeQuaternion(ref int srcOffset)
{
float x = BitConverter.ToSingle(buffer, srcOffset + 0 * sizeof(float));
float y = BitConverter.ToSingle(buffer, srcOffset + 1 * sizeof(float));
float z = BitConverter.ToSingle(buffer, srcOffset + 2 * sizeof(float));
float w = BitConverter.ToSingle(buffer, srcOffset + 3 * sizeof(float));
srcOffset += 4 * sizeof(float);
return new Quaternion(x, y, z, w);
}
private void SerializeString(String input, ref int dstOffset)
{
byte[] bytes = Encoding.ASCII.GetBytes(input);
// First write string length so we can deserialize.
Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length), 0, buffer, dstOffset, sizeof(Int32));
dstOffset += sizeof(Int32);
Buffer.BlockCopy(bytes, 0, buffer, dstOffset, bytes.Length);
dstOffset += input.Length;
}
private string DeserializeString(ref int srcOffset)
{
Int32 length = BitConverter.ToInt32(buffer, srcOffset);
srcOffset += sizeof(Int32);
byte[] buf = new byte[length];
Buffer.BlockCopy(buffer, srcOffset, buf, 0, length);
srcOffset += length;
return Encoding.ASCII.GetString(buf);
}
#endregion
#region Pose
public int SerializePose(Vector3 position, Quaternion rotation)
{
int bufferOffset = WriteHeader(MessageType.Pose);
// Serialize data to byte array.
SerializeVector(position, ref bufferOffset);
SerializeQuaternion(rotation, ref bufferOffset);
return bufferOffset;
}
private void DeSerializePose()
{
// Deserialize input byte array into local variables.
// Assumes input data has same format as Serialize function.
int bufferOffset = 0;
Position = DeserializeVector(ref bufferOffset);
Rotation = DeserializeQuaternion(ref bufferOffset);
}
#endregion
#region AirTap
public int SerializeAirTap(Vector3 position, Vector3 rotation, Vector3 hitLocation)
{
int bufferOffset = WriteHeader(MessageType.AirTap);
// Serialize data to byte array.
SerializeVector(position, ref bufferOffset);
SerializeVector(rotation, ref bufferOffset);
SerializeVector(hitLocation, ref bufferOffset);
return bufferOffset;
}
private void DeSerializeAirTap()
{
// Deserialize input byte array into local variables.
// Assumes input data has same format as Serialize function.
int bufferOffset = 0;
AirTapLocation = DeserializeVector(ref bufferOffset);
AirTapDirection = DeserializeVector(ref bufferOffset);
AirTapHitLocation = DeserializeVector(ref bufferOffset);
}
#endregion
#region AnchorName
public int SerializeAnchorName(String name, String ip)
{
int bufferOffset = WriteHeader(MessageType.AnchorName);
SerializeString(name, ref bufferOffset);
SerializeString(ip, ref bufferOffset);
return bufferOffset;
}
private void DeSerializeAnchorName()
{
int bufferOffset = 0;
AnchorName = DeserializeString(ref bufferOffset);
AnchorIP = DeserializeString(ref bufferOffset);
}
#endregion
public MessageType DeserializeData(byte[] input, int size)
{
MessageType header = (MessageType)BitConverter.ToInt32(input, 0);
if (size > bufSize)
{
Debug.LogWarning("Message size is too large.");
size = bufSize;
}
// Strip the leading header bytes.
Buffer.BlockCopy(input, sizeof(int), buffer, 0, size);
// Deserialize the network stream to meaningful data.
switch (header)
{
case MessageType.Pose:
DeSerializePose();
break;
case MessageType.AirTap:
DeSerializeAirTap();
break;
case MessageType.AnchorName:
DeSerializeAnchorName();
break;
};
return header;
}
}
}