mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
Update
Update
This commit is contained in:
@@ -389,7 +389,7 @@ namespace TEngine.Runtime
|
||||
}
|
||||
|
||||
m_SendState.Reset();
|
||||
m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);
|
||||
m_ReceiveState.SetLength(ReceiveState.DefaultBufferLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -622,7 +622,7 @@ namespace TEngine.Runtime
|
||||
HandleResponse(packet);
|
||||
}
|
||||
|
||||
m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);
|
||||
m_ReceiveState.SetLength(ReceiveState.DefaultBufferLength);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
@@ -7,7 +7,7 @@ namespace TEngine.Runtime
|
||||
{
|
||||
private sealed class ReceiveState : IDisposable
|
||||
{
|
||||
private const int DefaultBufferLength = 1024 * 64;
|
||||
public const int DefaultBufferLength = 1024 * 64;
|
||||
private MemoryStream m_Stream;
|
||||
private bool m_Disposed;
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace TEngine.Runtime
|
||||
}
|
||||
}
|
||||
|
||||
public void PrepareForPacketHeader(int packetHeaderLength)
|
||||
public void SetLength(int packetHeaderLength)
|
||||
{
|
||||
Reset(packetHeaderLength);
|
||||
}
|
||||
|
@@ -25,6 +25,17 @@ namespace TEngine.Runtime
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLength(int targetLength)
|
||||
{
|
||||
if (targetLength < 0)
|
||||
{
|
||||
throw new Exception("Target length is invalid.");
|
||||
}
|
||||
|
||||
m_Stream.Position = 0L;
|
||||
m_Stream.SetLength(targetLength);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
m_Stream.Position = 0L;
|
||||
|
@@ -60,7 +60,6 @@ namespace TEngine.Runtime
|
||||
/// <summary>
|
||||
/// 序列化消息包。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">消息包类型。</typeparam>
|
||||
/// <param name="packet">要序列化的消息包。</param>
|
||||
/// <param name="destination">要序列化的目标流。</param>
|
||||
/// <returns>是否序列化成功。</returns>
|
||||
@@ -77,7 +76,6 @@ namespace TEngine.Runtime
|
||||
/// <summary>
|
||||
/// 反序列化消息包。
|
||||
/// </summary>
|
||||
/// <param name="packetHeader">消息包头。</param>
|
||||
/// <param name="source">要反序列化的来源流。</param>
|
||||
/// <param name="customErrorData">用户自定义错误数据。</param>
|
||||
/// <returns>反序列化后的消息包。</returns>
|
||||
@@ -86,17 +84,13 @@ namespace TEngine.Runtime
|
||||
// 注意:此函数并不在主线程调用!
|
||||
customErrorData = null;
|
||||
|
||||
MainPack packet = null;
|
||||
Type packetType = typeof(MainPack);
|
||||
if (packetType != null)
|
||||
var buffer = (source as MemoryStream).GetBuffer();
|
||||
int count = BitConverter.ToInt32(buffer, 0);
|
||||
using (var stream = new System.IO.MemoryStream(buffer,4,count))
|
||||
{
|
||||
packet = (MainPack)RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, MemoryPool.Acquire(packetType), packetType, PrefixStyle.Fixed32, 0);
|
||||
var result = RuntimeTypeModel.Default.Deserialize(stream,MemoryPool.Acquire(typeof(MainPack)), typeof(MainPack));
|
||||
return result as MainPack;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Warning("Can not deserialize packet for packet id '{0}'.", packet.actioncode.ToString());
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -145,7 +145,11 @@ namespace TEngine.Runtime
|
||||
{
|
||||
try
|
||||
{
|
||||
m_Socket.BeginSend(m_SendState.Stream.GetBuffer(), (int)m_SendState.Stream.Position, (int)(m_SendState.Stream.Length - m_SendState.Stream.Position), SocketFlags.None, m_SendCallback, m_Socket);
|
||||
var buffer = m_SendState.Stream.GetBuffer();
|
||||
var buffBodyCount = buffer[0];
|
||||
var buffTotalCount = m_NetworkChannelHelper.PacketHeaderLength + buffBodyCount;
|
||||
m_SendState.Stream.SetLength(buffTotalCount);
|
||||
m_Socket.BeginSend(buffer,0,buffTotalCount, SocketFlags.None, m_SendCallback, m_Socket);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -187,12 +191,12 @@ namespace TEngine.Runtime
|
||||
throw;
|
||||
}
|
||||
|
||||
m_SendState.Stream.Position += bytesSent;
|
||||
if (m_SendState.Stream.Position < m_SendState.Stream.Length)
|
||||
{
|
||||
SendAsync();
|
||||
return;
|
||||
}
|
||||
// m_SendState.Stream.Position += bytesSent;
|
||||
// if (m_SendState.Stream.Position < m_SendState.Stream.Length)
|
||||
// {
|
||||
// SendAsync();
|
||||
// return;
|
||||
// }
|
||||
|
||||
m_SentPacketCount++;
|
||||
m_SendState.Reset();
|
||||
@@ -200,6 +204,7 @@ namespace TEngine.Runtime
|
||||
|
||||
private void ReceiveAsync()
|
||||
{
|
||||
m_ReceiveState.Stream.SetLength(ReceiveState.DefaultBufferLength);
|
||||
try
|
||||
{
|
||||
m_Socket.BeginReceive(m_ReceiveState.Stream.GetBuffer(), (int)m_ReceiveState.Stream.Position, (int)(m_ReceiveState.Stream.Length - m_ReceiveState.Stream.Position), SocketFlags.None, m_ReceiveCallback, m_Socket);
|
||||
@@ -250,14 +255,11 @@ namespace TEngine.Runtime
|
||||
return;
|
||||
}
|
||||
|
||||
m_ReceiveState.Stream.Position += bytesReceived;
|
||||
if (m_ReceiveState.Stream.Position < m_ReceiveState.Stream.Length)
|
||||
{
|
||||
ReceiveAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
m_ReceiveState.Stream.Position = 0L;
|
||||
|
||||
m_ReceiveState.Stream.SetLength(bytesReceived);
|
||||
|
||||
ProtoUtils.PrintBuffer(m_ReceiveState.Stream.GetBuffer());
|
||||
|
||||
bool processSuccess = false;
|
||||
processSuccess = ProcessPacket();
|
||||
|
@@ -225,6 +225,7 @@ namespace TEngine.Runtime
|
||||
}
|
||||
|
||||
m_ReceiveState.Stream.Position = 0L;
|
||||
m_ReceiveState.Stream.SetLength(bytesReceived);
|
||||
|
||||
bool processSuccess = false;
|
||||
processSuccess = ProcessPacket();
|
||||
|
Reference in New Issue
Block a user