Tim Mowrer
5 年前
当前提交
a1c2fe9b
共有 13 个文件被更改,包括 447 次插入 和 64 次删除
-
151Assets/Scenes/ARCollaborationData/TCPConnection.cs
-
4Assets/Scenes/ARCollaborationData/IMessage.cs
-
11Assets/Scenes/ARCollaborationData/IMessage.cs.meta
-
23Assets/Scenes/ARCollaborationData/MessageHeader.cs
-
11Assets/Scenes/ARCollaborationData/MessageHeader.cs.meta
-
6Assets/Scenes/ARCollaborationData/MessageType.cs
-
11Assets/Scenes/ARCollaborationData/MessageType.cs.meta
-
102Assets/Scenes/ARCollaborationData/NetworkBuffer.cs
-
11Assets/Scenes/ARCollaborationData/NetworkBuffer.cs.meta
-
84Assets/Scenes/ARCollaborationData/NetworkDataDecoder.cs
-
11Assets/Scenes/ARCollaborationData/NetworkDataDecoder.cs.meta
-
75Assets/Scenes/ARCollaborationData/NetworkDataEncoder.cs
-
11Assets/Scenes/ARCollaborationData/NetworkDataEncoder.cs.meta
|
|||
public interface IMessage |
|||
{ |
|||
int EncodeTo(byte[] bytes); |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 48716d5221a70425aa3409ef5a24a10d |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
public struct MessageHeader : IMessage |
|||
{ |
|||
public int messageSize; |
|||
|
|||
public MessageType messageType; |
|||
|
|||
public int EncodeTo(byte[] bytes) |
|||
{ |
|||
var encoder = new NetworkDataEncoder(bytes); |
|||
encoder.Encode(messageSize); |
|||
encoder.Encode((byte)messageType); |
|||
return encoder.length; |
|||
} |
|||
|
|||
public const int k_EncodedSize = sizeof(int) + sizeof(byte); |
|||
|
|||
public MessageHeader(byte[] bytes, int size) |
|||
{ |
|||
var decoder = new NetworkDataDecoder(bytes, size); |
|||
messageSize = decoder.DecodeInt(); |
|||
messageType = (MessageType)decoder.DecodeByte(); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 392beb324d6f3400ab0b5ffe1ae43b70 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
public enum MessageType : byte |
|||
{ |
|||
None, |
|||
|
|||
CollaborationData |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b16d841d0e5dd48d0a70c79beff77499 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Net.Sockets; |
|||
using UnityEngine; |
|||
using Unity.Collections; |
|||
using Unity.Collections.LowLevel.Unsafe; |
|||
|
|||
public struct NetworkBuffer |
|||
{ |
|||
byte[] m_Buffer; |
|||
|
|||
public NetworkBuffer(int size) |
|||
{ |
|||
m_Buffer = new byte[size]; |
|||
} |
|||
|
|||
public byte[] buffer => m_Buffer; |
|||
|
|||
public int Read(NetworkStream stream, int offset, int size) |
|||
{ |
|||
ValidateAndThrow(stream); |
|||
|
|||
if (offset < 0) |
|||
throw new ArgumentOutOfRangeException(nameof(offset), offset, $"{nameof(offset)} must be greater than or equal to zero."); |
|||
|
|||
if (size < 0) |
|||
throw new ArgumentOutOfRangeException(nameof(size), size, $"{nameof(size)} must be greater than or equal to zero."); |
|||
|
|||
if (offset + size > m_Buffer.Length) |
|||
throw new InvalidOperationException($"Reading {size} bytes starting at offset {offset} would read past the end of the buffer (buffer length = {m_Buffer.Length})."); |
|||
|
|||
int bytesRemaining = size; |
|||
while (bytesRemaining > 0) |
|||
{ |
|||
int bytesRead = stream.Read(m_Buffer, offset, size); |
|||
CollaborationNetworkingIndicator.NotifyIncomingDataReceived(); |
|||
offset += bytesRead; |
|||
bytesRemaining -= bytesRead; |
|||
} |
|||
|
|||
return size; |
|||
} |
|||
|
|||
public void Send(NetworkStream stream, int offset, int size) |
|||
{ |
|||
ValidateAndThrow(stream); |
|||
|
|||
if (offset + size > m_Buffer.Length) |
|||
throw new InvalidOperationException($"Writing {size} bytes starting at offset {offset} would write past the end of the buffer (buffer length = {m_Buffer.Length})."); |
|||
|
|||
try |
|||
{ |
|||
stream.Write(m_Buffer, offset, size); |
|||
CollaborationNetworkingIndicator.NotifyOutgoingDataSent(); |
|||
} |
|||
catch (SocketException socketException) |
|||
{ |
|||
Logger.Log($"Socket exception: {socketException}"); |
|||
} |
|||
} |
|||
|
|||
public unsafe void Send(NetworkStream stream, NativeSlice<byte> bytes) |
|||
{ |
|||
ValidateAndThrow(stream); |
|||
|
|||
var basePtr = new IntPtr(bytes.GetUnsafeReadOnlyPtr()); |
|||
int bytesRemaining = bytes.Length; |
|||
int offset = 0; |
|||
|
|||
while (bytesRemaining > 0) |
|||
{ |
|||
// Memcpy next chunk into destinationBuffer
|
|||
int size = Mathf.Min(m_Buffer.Length, bytesRemaining); |
|||
fixed(byte* dst = m_Buffer) |
|||
{ |
|||
var src = basePtr + offset; |
|||
UnsafeUtility.MemCpy(dst, (void*)src, size); |
|||
} |
|||
|
|||
bytesRemaining -= size; |
|||
offset += size; |
|||
|
|||
Send(stream, 0, size); |
|||
} |
|||
} |
|||
|
|||
public void Send<T>(NetworkStream stream, T message) where T : struct, IMessage |
|||
{ |
|||
ValidateAndThrow(stream); |
|||
|
|||
int size = message.EncodeTo(m_Buffer); |
|||
Send(stream, 0, size); |
|||
} |
|||
|
|||
void ValidateAndThrow(NetworkStream stream) |
|||
{ |
|||
if (stream == null) |
|||
throw new ArgumentNullException(nameof(stream)); |
|||
|
|||
if (m_Buffer == null) |
|||
throw new InvalidOperationException($"{nameof(NetworkBuffer)} has not been initialized."); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 6159b84b308ec4356bdc9ae17d26964b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Net; |
|||
|
|||
public struct NetworkDataDecoder |
|||
{ |
|||
byte[] m_Buffer; |
|||
|
|||
int m_Offset; |
|||
|
|||
int m_Length; |
|||
|
|||
public NetworkDataDecoder(byte[] buffer, int size) |
|||
{ |
|||
if (buffer == null) |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
|
|||
if (size > buffer.Length) |
|||
throw new ArgumentOutOfRangeException(nameof(size), size, $"'{nameof(size)}' is greater than the length of {nameof(buffer)} ({buffer.Length})."); |
|||
|
|||
m_Buffer = buffer; |
|||
m_Offset = 0; |
|||
m_Length = size; |
|||
} |
|||
|
|||
public unsafe float DecodeFloat() |
|||
{ |
|||
var value = DecodeInt(); |
|||
return *(float*)&value; |
|||
} |
|||
|
|||
public unsafe double DecodeDouble() |
|||
{ |
|||
var value = DecodeLong(); |
|||
return *(double*)&value; |
|||
} |
|||
|
|||
public uint DecodeUInt() => (uint)DecodeInt(); |
|||
|
|||
public ulong DecodeULong() => (ulong)DecodeLong(); |
|||
|
|||
public byte DecodeByte() |
|||
{ |
|||
if (m_Offset >= m_Length) |
|||
throw new InvalidOperationException("Buffer is exhausted. Cannot decode more data."); |
|||
|
|||
return m_Buffer[m_Offset++]; |
|||
} |
|||
|
|||
public unsafe short DecodeShort() |
|||
{ |
|||
if (m_Offset + 2 > m_Length) |
|||
throw new InvalidOperationException("Buffer is exhausted. Cannot decode more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
m_Offset += 2; |
|||
return IPAddress.NetworkToHostOrder(*(short*)ptr); |
|||
} |
|||
} |
|||
|
|||
public unsafe int DecodeInt() |
|||
{ |
|||
if (m_Offset + 4 > m_Length) |
|||
throw new InvalidOperationException("Buffer is exhausted. Cannot decode more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
m_Offset += 4; |
|||
return IPAddress.NetworkToHostOrder(*(int*)ptr); |
|||
} |
|||
} |
|||
|
|||
public unsafe long DecodeLong() |
|||
{ |
|||
if (m_Offset + 8 > m_Length) |
|||
throw new InvalidOperationException("Buffer is exhausted. Cannot decode more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
m_Offset += 8; |
|||
return IPAddress.NetworkToHostOrder(*(long*)ptr); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d3331be3e25f44589a8a9895cdb60f02 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Net; |
|||
|
|||
public struct NetworkDataEncoder |
|||
{ |
|||
byte[] m_Buffer; |
|||
|
|||
int m_Offset; |
|||
|
|||
public NetworkDataEncoder(byte[] buffer) |
|||
{ |
|||
if (buffer == null) |
|||
throw new ArgumentNullException(nameof(buffer)); |
|||
|
|||
m_Buffer = buffer; |
|||
m_Offset = 0; |
|||
} |
|||
|
|||
public int length => m_Offset; |
|||
|
|||
public unsafe void Encode(float value) => Encode(*(int*)&value); |
|||
|
|||
public unsafe void Encode(double value) => Encode(*(long*)&value); |
|||
|
|||
public void Encode(uint value) => Encode((int)value); |
|||
|
|||
public void Encode(ulong value) => Encode((long)value); |
|||
|
|||
public void Encode(byte value) |
|||
{ |
|||
if (m_Offset + 1 > m_Buffer.Length) |
|||
throw new InvalidOperationException("Buffer is full. Cannot write more data."); |
|||
|
|||
m_Buffer[m_Offset++] = value; |
|||
} |
|||
|
|||
public unsafe void Encode(short value) |
|||
{ |
|||
int newOffset = m_Offset + 2; |
|||
if (newOffset > m_Buffer.Length) |
|||
throw new InvalidOperationException("Buffer is full. Cannot write more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
*(short*)ptr = IPAddress.HostToNetworkOrder(value); |
|||
} |
|||
m_Offset = newOffset; |
|||
} |
|||
|
|||
public unsafe void Encode(int value) |
|||
{ |
|||
int newOffset = m_Offset + 4; |
|||
if (newOffset > m_Buffer.Length) |
|||
throw new InvalidOperationException("Buffer is full. Cannot write more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
*(int*)ptr = IPAddress.HostToNetworkOrder(value); |
|||
} |
|||
m_Offset = newOffset; |
|||
} |
|||
|
|||
public unsafe void Encode(long value) |
|||
{ |
|||
int newOffset = m_Offset + 8; |
|||
if (newOffset > m_Buffer.Length) |
|||
throw new InvalidOperationException("Buffer is full. Cannot write more data."); |
|||
|
|||
fixed(byte* ptr = &m_Buffer[m_Offset]) |
|||
{ |
|||
*(long*)ptr = IPAddress.HostToNetworkOrder(value); |
|||
} |
|||
m_Offset = newOffset; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 03435c1a2d82b46f98fe7980c199a46f |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue