mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-01 03:32:38 +00:00
59 lines
1.3 KiB
C#
59 lines
1.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace QuantumUNET.Transport
|
|
{
|
|
internal struct QChannelPacket
|
|
{
|
|
private int m_Position;
|
|
private readonly byte[] m_Buffer;
|
|
private readonly bool m_IsReliable;
|
|
|
|
public QChannelPacket(int packetSize, bool isReliable)
|
|
{
|
|
m_Position = 0;
|
|
m_Buffer = new byte[packetSize];
|
|
m_IsReliable = isReliable;
|
|
}
|
|
|
|
public void Reset() => m_Position = 0;
|
|
|
|
public bool IsEmpty() => m_Position == 0;
|
|
|
|
public void Write(byte[] bytes, int numBytes)
|
|
{
|
|
Array.Copy(bytes, 0, m_Buffer, m_Position, numBytes);
|
|
m_Position += numBytes;
|
|
}
|
|
|
|
public bool HasSpace(int numBytes) => m_Position + numBytes <= m_Buffer.Length;
|
|
|
|
public bool SendToTransport(QNetworkConnection conn, int channelId)
|
|
{
|
|
var result = true;
|
|
if (!conn.TransportSend(m_Buffer, (ushort)m_Position, channelId, out var b))
|
|
{
|
|
if (!m_IsReliable || b != 4)
|
|
{
|
|
Debug.LogError($"Failed to send internal buffer channel:{channelId} bytesToSend:{m_Position}");
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
if (b != 0)
|
|
{
|
|
if (m_IsReliable && b == 4)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Debug.LogError($"Send Error: {(NetworkError)b} channel:{channelId} bytesToSend:{m_Position}");
|
|
result = false;
|
|
}
|
|
|
|
m_Position = 0;
|
|
return result;
|
|
}
|
|
}
|
|
} |