|
|
@@ -1,5 +1,6 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
@@ -33,11 +34,11 @@ namespace ETModel
|
|
|
|
|
|
private uint lastRecvTime;
|
|
|
|
|
|
- private readonly byte[] cacheBytes = new byte[ushort.MaxValue];
|
|
|
-
|
|
|
public uint Conn;
|
|
|
|
|
|
public uint RemoteConn;
|
|
|
+
|
|
|
+ public Packet packet = new Packet(ushort.MaxValue);
|
|
|
|
|
|
// accept
|
|
|
public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
|
|
|
@@ -50,6 +51,8 @@ namespace ETModel
|
|
|
kcp = new KCP(this.RemoteConn, this);
|
|
|
kcp.SetOutput(this.Output);
|
|
|
kcp.NoDelay(1, 10, 2, 1); //fast
|
|
|
+ kcp.SetMTU(470);
|
|
|
+ kcp.WndSize(256, 256);
|
|
|
this.isConnected = true;
|
|
|
|
|
|
this.lastRecvTime = kService.TimeNow;
|
|
|
@@ -100,7 +103,8 @@ namespace ETModel
|
|
|
this.kcp = new KCP(responseConn, this);
|
|
|
kcp.SetOutput(this.Output);
|
|
|
kcp.NoDelay(1, 10, 2, 1); //fast
|
|
|
-
|
|
|
+ kcp.SetMTU(470);
|
|
|
+ kcp.WndSize(256, 256);
|
|
|
this.lastRecvTime = this.GetService().TimeNow;
|
|
|
|
|
|
HandleSend();
|
|
|
@@ -108,10 +112,10 @@ namespace ETModel
|
|
|
|
|
|
public void HandleAccept(uint requestConn)
|
|
|
{
|
|
|
- cacheBytes.WriteTo(0, KcpProtocalType.ACK);
|
|
|
- cacheBytes.WriteTo(4, requestConn);
|
|
|
- cacheBytes.WriteTo(8, this.Conn);
|
|
|
- this.socket.SendTo(cacheBytes, 0, 12, SocketFlags.None, remoteEndPoint);
|
|
|
+ packet.Bytes.WriteTo(0, KcpProtocalType.ACK);
|
|
|
+ packet.Bytes.WriteTo(4, requestConn);
|
|
|
+ packet.Bytes.WriteTo(8, this.Conn);
|
|
|
+ this.socket.SendTo(packet.Bytes, 0, 12, SocketFlags.None, remoteEndPoint);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -119,9 +123,9 @@ namespace ETModel
|
|
|
/// </summary>
|
|
|
private void Connect(uint timeNow)
|
|
|
{
|
|
|
- cacheBytes.WriteTo(0, KcpProtocalType.SYN);
|
|
|
- cacheBytes.WriteTo(4, this.Conn);
|
|
|
- this.socket.SendTo(cacheBytes, 0, 8, SocketFlags.None, remoteEndPoint);
|
|
|
+ packet.Bytes.WriteTo(0, KcpProtocalType.SYN);
|
|
|
+ packet.Bytes.WriteTo(4, this.Conn);
|
|
|
+ this.socket.SendTo(packet.Bytes, 0, 8, SocketFlags.None, remoteEndPoint);
|
|
|
|
|
|
// 200毫秒后再次update发送connect请求
|
|
|
this.GetService().AddToNextTimeUpdate(timeNow + 200, this.Id);
|
|
|
@@ -129,11 +133,11 @@ namespace ETModel
|
|
|
|
|
|
private void DisConnect()
|
|
|
{
|
|
|
- cacheBytes.WriteTo(0, KcpProtocalType.FIN);
|
|
|
- cacheBytes.WriteTo(4, this.Conn);
|
|
|
- cacheBytes.WriteTo(8, this.RemoteConn);
|
|
|
+ packet.Bytes.WriteTo(0, KcpProtocalType.FIN);
|
|
|
+ packet.Bytes.WriteTo(4, this.Conn);
|
|
|
+ packet.Bytes.WriteTo(8, this.RemoteConn);
|
|
|
//Log.Debug($"client disconnect: {this.Conn}");
|
|
|
- this.socket.SendTo(cacheBytes, 0, 12, SocketFlags.None, remoteEndPoint);
|
|
|
+ this.socket.SendTo(packet.Bytes, 0, 12, SocketFlags.None, remoteEndPoint);
|
|
|
}
|
|
|
|
|
|
public void Update(uint timeNow)
|
|
|
@@ -144,7 +148,7 @@ namespace ETModel
|
|
|
// 5秒连接不上,报错
|
|
|
if (timeNow - this.lastRecvTime > 5 * 1000)
|
|
|
{
|
|
|
- this.OnError((int)SocketError.ConnectionRefused);
|
|
|
+ this.OnError(ErrorCode.ERR_KcpConnectFail);
|
|
|
return;
|
|
|
}
|
|
|
Connect(timeNow);
|
|
|
@@ -152,9 +156,9 @@ namespace ETModel
|
|
|
}
|
|
|
|
|
|
// 超时断开连接
|
|
|
- if (timeNow - this.lastRecvTime > 20 * 1000)
|
|
|
+ if (timeNow - this.lastRecvTime > 40 * 1000)
|
|
|
{
|
|
|
- this.OnError((int)SocketError.Disconnecting);
|
|
|
+ this.OnError(ErrorCode.ERR_KcpTimeout);
|
|
|
return;
|
|
|
}
|
|
|
this.kcp.Update(timeNow);
|
|
|
@@ -189,7 +193,8 @@ namespace ETModel
|
|
|
this.OnError((int)SocketError.NetworkReset);
|
|
|
return;
|
|
|
}
|
|
|
- int count = this.kcp.Recv(this.cacheBytes, 0, this.cacheBytes.Length);
|
|
|
+ this.packet.Stream.SetLength(ushort.MaxValue);
|
|
|
+ int count = this.kcp.Recv(this.packet.Bytes, 0, ushort.MaxValue);
|
|
|
if (count <= 0)
|
|
|
{
|
|
|
return;
|
|
|
@@ -197,17 +202,14 @@ namespace ETModel
|
|
|
|
|
|
lastRecvTime = timeNow;
|
|
|
|
|
|
- this.packet.Flag = this.cacheBytes[0];
|
|
|
- this.packet.Opcode = BitConverter.ToUInt16(this.cacheBytes, 1);
|
|
|
- this.packet.Bytes = this.cacheBytes;
|
|
|
- this.packet.Offset = Packet.Index;
|
|
|
- this.packet.Length = (ushort) (count - Packet.Index);
|
|
|
+ this.packet.Flag = this.packet.Bytes[0];
|
|
|
+ this.packet.Opcode = BitConverter.ToUInt16(this.packet.Bytes, 1);
|
|
|
+ this.packet.Stream.SetLength(count);
|
|
|
+ this.packet.Stream.Seek(Packet.Index, SeekOrigin.Begin);
|
|
|
this.OnRead(packet);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Packet packet = new Packet();
|
|
|
-
|
|
|
public void Output(byte[] bytes, int count, object user)
|
|
|
{
|
|
|
this.socket.SendTo(bytes, 0, count, SocketFlags.None, this.remoteEndPoint);
|
|
|
@@ -240,7 +242,7 @@ namespace ETModel
|
|
|
byte[] bytes;
|
|
|
if (this.isConnected)
|
|
|
{
|
|
|
- bytes = this.cacheBytes;
|
|
|
+ bytes = this.packet.Bytes;
|
|
|
}
|
|
|
else
|
|
|
{
|