2020-12-16 09:08:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QuantumUNET.Transport
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
internal struct QChannelPacket
|
2020-12-16 09:08:38 +00:00
|
|
|
|
{
|
|
|
|
|
private int m_Position;
|
2020-12-18 20:32:16 +00:00
|
|
|
|
private readonly byte[] m_Buffer;
|
|
|
|
|
private readonly bool m_IsReliable;
|
2020-12-16 09:08:38 +00:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
public QChannelPacket(int packetSize, bool isReliable)
|
2020-12-16 09:08:38 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
public bool SendToTransport(QNetworkConnection conn, int channelId)
|
2020-12-16 09:08:38 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-18 21:39:21 +00:00
|
|
|
|
|
2020-12-16 09:08:38 +00:00
|
|
|
|
if (b != 0)
|
|
|
|
|
{
|
|
|
|
|
if (m_IsReliable && b == 4)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-18 21:39:21 +00:00
|
|
|
|
|
2020-12-16 09:08:38 +00:00
|
|
|
|
Debug.LogError($"Send Error: {(NetworkError)b} channel:{channelId} bytesToSend:{m_Position}");
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
2021-06-18 21:39:21 +00:00
|
|
|
|
|
2020-12-16 09:08:38 +00:00
|
|
|
|
m_Position = 0;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|