Ver Fonte

1.Kcp包装一下,不要暴露intptr出去
2.服务端也开启消息池,压测发现,开启消息池,压测性能更平稳

tanghai há 2 anos atrás
pai
commit
0bcf8785db

+ 33 - 39
Unity/Assets/Scripts/Core/Module/Network/KChannel.cs

@@ -17,11 +17,9 @@ namespace ET
 	
 	public class KChannel : AChannel
 	{
-		public readonly KService Service;
-		
-		private Socket socket;
+		private readonly KService Service;
 
-		public IntPtr kcp { get; private set; }
+		private Kcp kcp { get; set; }
 
 		private readonly Queue<KcpWaitSendMessage> waitSendMessages = new Queue<KcpWaitSendMessage>();
 		
@@ -69,38 +67,36 @@ namespace ET
 
 		private void InitKcp()
 		{
-			this.Service.KcpPtrChannels.Add(this.kcp, this);
+			this.Service.KcpPtrChannels.Add(this.kcp.Id, this);
 			
 			switch (this.Service.ServiceType)
 			{
 				case ServiceType.Inner:
-					Kcp.KcpNodelay(kcp, 1, 10, 2, 1);
-					Kcp.KcpWndsize(kcp, 1024, 1024);
-					Kcp.KcpSetmtu(kcp, 1400); // 默认1400
-					Kcp.KcpSetminrto(kcp, 30);
+					this.kcp.Nodelay(1, 10, 2, 1);
+					this.kcp.SetWndSize(1024, 1024);
+					this.kcp.SetMtu(1400); // 默认1400
+					this.kcp.SetMinrto(30);
 					break;
 				case ServiceType.Outer:
-					Kcp.KcpNodelay(kcp, 1, 10, 2, 1);
-					Kcp.KcpWndsize(kcp, 256, 256);
-					Kcp.KcpSetmtu(kcp, 470);
-					Kcp.KcpSetminrto(kcp, 30);
+					this.kcp.Nodelay(1, 10, 2, 1);
+					this.kcp.SetWndSize(256, 256);
+					this.kcp.SetMtu(470);
+					this.kcp.SetMinrto(30);
 					break;
 			}
 
 		}
 		
 		// connect
-		public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
+		public KChannel(uint localConn, IPEndPoint remoteEndPoint, KService kService)
 		{
 			this.LocalConn = localConn;
 			this.ChannelType = ChannelType.Connect;
 			
 			Log.Info($"channel create: {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
 			
-			this.kcp = IntPtr.Zero;
 			this.Service = kService;
 			this.RemoteAddress = remoteEndPoint;
-			this.socket = socket;
 			this.CreateTime = kService.TimeNow;
 
 			this.Connect(this.CreateTime);
@@ -108,7 +104,7 @@ namespace ET
 		}
 
 		// accept
-		public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
+		public KChannel(uint localConn, uint remoteConn, IPEndPoint remoteEndPoint, KService kService)
 		{
 			this.ChannelType = ChannelType.Accept;
 			
@@ -118,8 +114,7 @@ namespace ET
 			this.LocalConn = localConn;
 			this.RemoteConn = remoteConn;
 			this.RemoteAddress = remoteEndPoint;
-			this.socket = socket;
-			this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.Service.Id));
+			this.kcp = new Kcp(this.RemoteConn, new IntPtr(this.Service.Id));
 			this.InitKcp();
 			
 			this.CreateTime = kService.TimeNow;
@@ -153,13 +148,12 @@ namespace ET
 				Log.Error(e);
 			}
 
-			if (this.kcp != IntPtr.Zero)
+			if (this.kcp != null)
 			{
-				Kcp.KcpRelease(this.kcp);
-				this.kcp = IntPtr.Zero;
+				this.Service.KcpPtrChannels.Remove(this.kcp.Id);
+				this.kcp.Release();
+				this.kcp = null;
 			}
-
-			this.socket = null;
 		}
 
 		public void HandleConnnect()
@@ -170,7 +164,7 @@ namespace ET
 				return;
 			}
 
-			this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.Service.Id));
+			this.kcp = new Kcp(this.RemoteConn, new IntPtr(this.Service.Id));
 			this.InitKcp();
 
 			Log.Info($"channel connected: {this.LocalConn} {this.RemoteConn} {this.RemoteAddress}");
@@ -221,7 +215,7 @@ namespace ET
 				buffer.WriteTo(0, KcpProtocalType.SYN);
 				buffer.WriteTo(1, this.LocalConn);
 				buffer.WriteTo(5, this.RemoteConn);
-				this.socket.SendTo(buffer, 0, 9, SocketFlags.None, this.RemoteAddress);
+				this.Service.Socket.SendTo(buffer, 0, 9, SocketFlags.None, this.RemoteAddress);
 				// 这里很奇怪 调用socket.LocalEndPoint会动到this.RemoteAddressNonAlloc里面的temp,这里就不仔细研究了
 				Log.Info($"kchannel connect {this.LocalConn} {this.RemoteConn} {this.RealAddress}");
 
@@ -250,14 +244,14 @@ namespace ET
 				return;
 			}
 
-			if (this.kcp == IntPtr.Zero)
+			if (this.kcp == null)
 			{
 				return;
 			}
 			
 			try
 			{
-				Kcp.KcpUpdate(this.kcp, timeNow);
+				this.kcp.Update(timeNow);
 			}
 			catch (Exception e)
 			{
@@ -266,7 +260,7 @@ namespace ET
 				return;
 			}
 
-			uint nextUpdateTime = Kcp.KcpCheck(this.kcp, timeNow);
+			uint nextUpdateTime = this.kcp.Check(timeNow);
 			this.Service.AddToUpdate(nextUpdateTime, this.Id);
 		}
 
@@ -277,7 +271,7 @@ namespace ET
 				return;
 			}
 
-			Kcp.KcpInput(this.kcp, date, offset, length);
+			this.kcp.Input(date, offset, length);
 			this.Service.AddToUpdate(0, this.Id);
 
 			while (true)
@@ -286,7 +280,7 @@ namespace ET
 				{
 					break;
 				}
-				int n = Kcp.KcpPeeksize(this.kcp);
+				int n = this.kcp.Peeksize();
 				if (n < 0)
 				{
 					break;
@@ -300,7 +294,7 @@ namespace ET
 				if (this.needReadSplitCount > 0) // 说明消息分片了
 				{
 					byte[] buffer = readMemory.GetBuffer();
-					int count = Kcp.KcpRecv(this.kcp, buffer, (int)this.readMemory.Length - this.needReadSplitCount, n);
+					int count = this.kcp.Recv(buffer, (int)this.readMemory.Length - this.needReadSplitCount, n);
 					this.needReadSplitCount -= count;
 					if (n != count)
 					{
@@ -329,7 +323,7 @@ namespace ET
 					this.readMemory.Seek(0, SeekOrigin.Begin);
 					
 					byte[] buffer = readMemory.GetBuffer();
-					int count = Kcp.KcpRecv(this.kcp, buffer, 0, n);
+					int count = this.kcp.Recv(buffer, 0, n);
 					if (n != count)
 					{
 						break;
@@ -397,7 +391,7 @@ namespace ET
 				// 每个消息头部写下该channel的id;
 				buffer.WriteTo(1, this.LocalConn);
 				Marshal.Copy(bytes, buffer, 5, count);
-				this.socket.SendTo(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddress);
+				this.Service.Socket.SendTo(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddress);
 			}
 			catch (Exception e)
 			{
@@ -432,14 +426,14 @@ namespace ET
 			// 超出maxPacketSize需要分片
 			if (count <= maxPacketSize)
 			{
-				Kcp.KcpSend(this.kcp, memoryStream.GetBuffer(), (int)memoryStream.Position, count);
+				this.kcp.Send(memoryStream.GetBuffer(), (int)memoryStream.Position, count);
 			}
 			else
 			{
 				// 先发分片信息
 				this.sendCache.WriteTo(0, 0);
 				this.sendCache.WriteTo(4, count);
-				Kcp.KcpSend(this.kcp, this.sendCache, 0, 8);
+				this.kcp.Send(this.sendCache, 0, 8);
 
 				// 分片发送
 				int alreadySendCount = 0;
@@ -449,7 +443,7 @@ namespace ET
 					
 					int sendCount = leftCount < maxPacketSize? leftCount: maxPacketSize;
 					
-					Kcp.KcpSend(this.kcp, memoryStream.GetBuffer(), (int)memoryStream.Position + alreadySendCount, sendCount);
+					this.kcp.Send(memoryStream.GetBuffer(), (int)memoryStream.Position + alreadySendCount, sendCount);
 					
 					alreadySendCount += sendCount;
 				}
@@ -469,13 +463,13 @@ namespace ET
 			
 			MemoryBuffer memoryStream = this.Service.Fetch(message);
 
-			if (this.kcp == IntPtr.Zero)
+			if (this.kcp == null)
 			{
 				throw new Exception("kchannel connected but kcp is zero!");
 			}
 			
 			// 检查等待发送的消息,如果超出最大等待大小,应该断开连接
-			int n = Kcp.KcpWaitsnd(this.kcp);
+			int n = this.kcp.Waitsnd();
 			int maxWaitSize = 0;
 			switch (this.Service.ServiceType)
 			{

+ 24 - 27
Unity/Assets/Scripts/Core/Module/Network/KService.cs

@@ -30,7 +30,7 @@ namespace ET
     {
         public const int ConnectTimeoutTime = 20 * 1000;
 
-        public readonly Dictionary<IntPtr, KChannel> KcpPtrChannels = new Dictionary<IntPtr, KChannel>();
+        public readonly Dictionary<long, KChannel> KcpPtrChannels = new();
         
         // KService创建的时间
         private readonly long startTime;
@@ -44,7 +44,7 @@ namespace ET
             }
         }
 
-        private Socket socket;
+        public Socket Socket;
 
 
 #region 回调方法
@@ -52,7 +52,7 @@ namespace ET
         static KService()
         {
             //Kcp.KcpSetLog(KcpLog);
-            Kcp.KcpSetoutput(KcpOutput);
+            Kcp.SetOutput(KcpOutput);
         }
         
 #if ENABLE_IL2CPP
@@ -87,9 +87,9 @@ namespace ET
                     return 0;
                 }
                 
-                KService kService = NetServices.Instance.Get(user.ToInt32()) as KService;
+                KService kService = NetServices.Instance.Get((int)user.ToInt64()) as KService;
                 
-                if (!kService.KcpPtrChannels.TryGetValue(kcp, out KChannel kChannel))
+                if (!kService.KcpPtrChannels.TryGetValue(kcp.ToInt64(), out KChannel kChannel))
                 {
                     return 0;
                 }
@@ -111,32 +111,32 @@ namespace ET
         {
             this.ServiceType = serviceType;
             this.startTime = TimeHelper.ClientNow();
-            this.socket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
+            this.Socket = new Socket(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
             if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
-                this.socket.SendBufferSize = Kcp.OneM * 64;
-                this.socket.ReceiveBufferSize = Kcp.OneM * 64;
+                this.Socket.SendBufferSize = Kcp.OneM * 64;
+                this.Socket.ReceiveBufferSize = Kcp.OneM * 64;
             }
 
             try
             {
-                this.socket.Bind(ipEndPoint);
+                this.Socket.Bind(ipEndPoint);
             }
             catch (Exception e)
             {
                 throw new Exception($"bind error: {ipEndPoint}", e);
             }
 
-            NetworkHelper.SetSioUdpConnReset(this.socket);
+            NetworkHelper.SetSioUdpConnReset(this.Socket);
         }
 
         public KService(AddressFamily addressFamily, ServiceType serviceType)
         {
             this.ServiceType = serviceType;
             this.startTime = TimeHelper.ClientNow();
-            this.socket = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
+            this.Socket = new Socket(addressFamily, SocketType.Dgram, ProtocolType.Udp);
 
-            NetworkHelper.SetSioUdpConnReset(this.socket);
+            NetworkHelper.SetSioUdpConnReset(this.Socket);
         }
 
         // 保存所有的channel
@@ -164,7 +164,7 @@ namespace ET
         
         public override bool IsDispose()
         {
-            return this.socket == null;
+            return this.Socket == null;
         }
 
         public override void Dispose()
@@ -176,8 +176,8 @@ namespace ET
                 this.Remove(channelId);
             }
 
-            this.socket.Close();
-            this.socket = null;
+            this.Socket.Close();
+            this.Socket = null;
         }
 
         public override (uint, uint) GetChannelConn(long channelId)
@@ -202,14 +202,14 @@ namespace ET
 
         private void Recv()
         {
-            if (this.socket == null)
+            if (this.Socket == null)
             {
                 return;
             }
 
-            while (socket != null && this.socket.Available > 0)
+            while (this.Socket != null && this.Socket.Available > 0)
             {
-                int messageLength = this.socket.ReceiveFrom_NonAlloc(this.cache, ref this.ipEndPoint);
+                int messageLength = this.Socket.ReceiveFrom_NonAlloc(this.cache, ref this.ipEndPoint);
 
                 // 长度小于1,不是正常的消息
                 if (messageLength < 1)
@@ -275,7 +275,7 @@ namespace ET
                                 buffer.WriteTo(1, kChannel.LocalConn);
                                 buffer.WriteTo(5, kChannel.RemoteConn);
                                 buffer.WriteTo(9, connectId);
-                                this.socket.SendTo(buffer, 0, 13, SocketFlags.None, this.ipEndPoint);
+                                this.Socket.SendTo(buffer, 0, 13, SocketFlags.None, this.ipEndPoint);
                             }
                             catch (Exception e)
                             {
@@ -315,7 +315,7 @@ namespace ET
                                     break;
                                 }
 
-                                kChannel = new KChannel(localConn, remoteConn, this.socket, this.ipEndPoint.Clone(), this);
+                                kChannel = new KChannel(localConn, remoteConn, this.ipEndPoint.Clone(), this);
                                 this.waitAcceptChannels.Add(kChannel.RemoteConn, kChannel); // 连接上了或者超时后会删除
                                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
                                 
@@ -344,7 +344,7 @@ namespace ET
                                 buffer.WriteTo(5, kChannel.RemoteConn);
                                 Log.Info($"kservice syn: {kChannel.Id} {remoteConn} {localConn}");
                                 
-                                this.socket.SendTo(buffer, 0, 9, SocketFlags.None, kChannel.RemoteAddress);
+                                this.Socket.SendTo(buffer, 0, 9, SocketFlags.None, kChannel.RemoteAddress);
                             }
                             catch (Exception e)
                             {
@@ -460,7 +460,7 @@ namespace ET
             {
                 // 低32bit是localConn
                 uint localConn = (uint)id;
-                kChannel = new KChannel(localConn, this.socket, address, this);
+                kChannel = new KChannel(localConn, address, this);
                 this.localConnChannels.Add(kChannel.LocalConn, kChannel);
             }
             catch (Exception e)
@@ -479,7 +479,6 @@ namespace ET
             kChannel.Error = error;
             
             Log.Info($"kservice remove channel: {id} {kChannel.LocalConn} {kChannel.RemoteConn} {error}");
-            this.localConnChannels.Remove(id);
             this.localConnChannels.Remove(kChannel.LocalConn);
             if (this.waitAcceptChannels.TryGetValue(kChannel.RemoteConn, out KChannel waitChannel))
             {
@@ -488,8 +487,6 @@ namespace ET
                     this.waitAcceptChannels.Remove(kChannel.RemoteConn);
                 }
             }
-
-            this.KcpPtrChannels.Remove(kChannel.kcp);
             
             kChannel.Dispose();
         }
@@ -498,7 +495,7 @@ namespace ET
         {
             try
             {
-                if (this.socket == null)
+                if (this.Socket == null)
                 {
                     return;
                 }
@@ -510,7 +507,7 @@ namespace ET
                 buffer.WriteTo(9, (uint) error);
                 for (int i = 0; i < times; ++i)
                 {
-                    this.socket.SendTo(buffer, 0, 13, SocketFlags.None, address);
+                    this.Socket.SendTo(buffer, 0, 13, SocketFlags.None, address);
                 }
             }
             catch (Exception e)

+ 0 - 6
Unity/Assets/Scripts/Core/Module/Network/MessagePool.cs

@@ -15,7 +15,6 @@ namespace ET
         // 只有客户端才用消息池,服务端不使用
         public MessageObject Fetch(Type type)
         {
-#if UNITY
             lock (this.pool)
             {
                 MessageObject messageObject;
@@ -36,11 +35,6 @@ namespace ET
                 messageObject.IsFromPool = true;
                 return messageObject;
             }
-#else
-            return Activator.CreateInstance(type) as MessageObject;
-#endif
-            
-
         }
 
         public void Recycle(MessageObject obj)

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Benchmark/BenchmarkClientComponentSystem.cs

@@ -29,7 +29,7 @@ namespace ET.Server
 
             async ETTask Call(Session s)
             {
-                await s.Call(new C2G_Benchmark());
+                using G2C_Benchmark benchmark = await s.Call(C2G_Benchmark.Create(true)) as G2C_Benchmark;
             }
             
             for (int j = 0; j < 100000000; ++j)

+ 2 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Benchmark/C2G_BenchmarkHandler.cs

@@ -6,7 +6,8 @@ namespace ET.Server
     public class C2G_BenchmarkHandler: MessageHandler<C2G_Benchmark, G2C_Benchmark>
     {
         protected override async ETTask Run(Session session, C2G_Benchmark request, G2C_Benchmark response)
-        {            
+        {
+            using C2G_Benchmark _ = request;
             BenchmarkServerComponent benchmarkServerComponent = session.DomainScene().GetComponent<BenchmarkServerComponent>();
             if (benchmarkServerComponent.Count++ % 1000000 == 0)
             {

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageHandler.cs

@@ -43,7 +43,7 @@ namespace ET.Server
     
     
     [EnableClass]
-    public abstract class ActorMessageHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : class, IActorRequest where Response : class, IActorResponse
+    public abstract class ActorMessageHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : MessageObject, IActorRequest where Response : MessageObject, IActorResponse
     {
         protected abstract ETTask Run(E unit, Request request, Response response);
 
@@ -64,7 +64,7 @@ namespace ET.Server
                 }
 
                 int rpcId = request.RpcId;
-                Response response = Activator.CreateInstance<Response>();
+                Response response = NetServices.Instance.FetchMessage<Response>();
                 
                 try
                 {

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/ActorMessageLocationHandler.cs

@@ -41,7 +41,7 @@ namespace ET.Server
     
     
     [EnableClass]
-    public abstract class ActorMessageLocationHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : class, IActorLocationRequest where Response : class, IActorLocationResponse
+    public abstract class ActorMessageLocationHandler<E, Request, Response>: IMActorHandler where E : Entity where Request : MessageObject, IActorLocationRequest where Response : MessageObject, IActorLocationResponse
     {
         protected abstract ETTask Run(E unit, Request request, Response response);
 
@@ -62,7 +62,7 @@ namespace ET.Server
                 }
 
                 int rpcId = request.RpcId;
-                Response response = Activator.CreateInstance<Response>();
+                Response response = NetServices.Instance.FetchMessage<Response>();
                 
                 try
                 {

+ 2 - 2
Unity/Assets/Scripts/Model/Server/Module/Message/MessageHandler.cs

@@ -2,7 +2,7 @@ using System;
 
 namespace ET.Server
 {
-    public abstract class MessageHandler<Request, Response>: IMHandler where Request : class, IRequest where Response : class, IResponse
+    public abstract class MessageHandler<Request, Response>: IMHandler where Request : MessageObject, IRequest where Response : MessageObject, IResponse
     {
         protected abstract ETTask Run(Session session, Request request, Response response);
 
@@ -24,7 +24,7 @@ namespace ET.Server
                 int rpcId = request.RpcId;
                 long instanceId = session.InstanceId;
 
-                Response response = Activator.CreateInstance<Response>();
+                Response response = NetServices.Instance.FetchMessage<Response>();
 
                 try
                 {

+ 61 - 50
Unity/Assets/Scripts/ThirdParty/Kcp/Kcp.cs

@@ -9,8 +9,23 @@ namespace ET
     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     public delegate void KcpLog(IntPtr buf, int len, IntPtr kcp, IntPtr user);
 
-    public static class Kcp
+    public class Kcp
     {
+        private IntPtr intPtr;
+
+        public Kcp(uint conv, IntPtr user)
+        {
+            this.intPtr = ikcp_create(conv, user);
+        }
+
+        public long Id
+        {
+            get
+            {
+                return this.intPtr.ToInt64();
+            }
+        }
+
         public const int OneM = 1024 * 1024;
         public const int InnerMaxWaitSize = 1024 * 1024;
         public const int OuterMaxWaitSize = 1024 * 1024;
@@ -62,42 +77,37 @@ namespace ET
         [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
         private static extern int ikcp_wndsize(IntPtr kcp, int sndwnd, int rcvwnd);
         
-        public static uint KcpCheck(IntPtr kcp, uint current)
+        public uint Check(uint current)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            uint ret = ikcp_check(kcp, current);
+            uint ret = ikcp_check(this.intPtr, current);
             return ret;
         }
         
-        public static IntPtr KcpCreate(uint conv, IntPtr user)
-        {
-            return ikcp_create(conv, user);
-        }
-
-        public static void KcpFlush(IntPtr kcp)
+        public void Flush()
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            ikcp_flush(kcp);
+            ikcp_flush(this.intPtr);
         }
 
-        public static uint KcpGetconv(IntPtr ptr)
+        public uint GetConv()
         {
-            if (ptr == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            return ikcp_getconv(ptr);
+            return ikcp_getconv(this.intPtr);
         }
 
-        public static int KcpInput(IntPtr kcp, byte[] buffer, int offset, int len)
+        public int Input(byte[] buffer, int offset, int len)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
@@ -105,32 +115,32 @@ namespace ET
             {
                 throw new Exception($"kcp error, KcpInput {buffer.Length} {offset} {len}");
             }
-            int ret = ikcp_input(kcp, buffer, offset, len);
+            int ret = ikcp_input(this.intPtr, buffer, offset, len);
             return ret;
         }
 
-        public static int KcpNodelay(IntPtr kcp, int nodelay, int interval, int resend, int nc)
+        public int Nodelay(int nodelay, int interval, int resend, int nc)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            return ikcp_nodelay(kcp, nodelay, interval, resend, nc);
+            return ikcp_nodelay(this.intPtr, nodelay, interval, resend, nc);
         }
 
-        public static int KcpPeeksize(IntPtr kcp)
+        public int Peeksize()
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            int ret = ikcp_peeksize(kcp);
+            int ret = ikcp_peeksize(this.intPtr);
             return ret;
         }
 
-        public static int KcpRecv(IntPtr kcp, byte[] buffer, int index, int len)
+        public int Recv(byte[] buffer, int index, int len)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
@@ -140,22 +150,23 @@ namespace ET
                 throw new Exception($"kcp error, KcpRecv error: {index} {len}");
             }
             
-            int ret = ikcp_recv(kcp, buffer, index, len);
+            int ret = ikcp_recv(this.intPtr, buffer, index, len);
             return ret;
         }
 
-        public static void KcpRelease(IntPtr kcp)
+        public void Release()
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            ikcp_release(kcp);
+            ikcp_release(this.intPtr);
+            this.intPtr = IntPtr.Zero;
         }
 
-        public static int KcpSend(IntPtr kcp, byte[] buffer, int offset, int len)
+        public int Send(byte[] buffer, int offset, int len)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
@@ -165,66 +176,66 @@ namespace ET
                 throw new Exception($"kcp error, KcpSend {buffer.Length} {offset} {len}");
             }
             
-            int ret = ikcp_send(kcp, buffer, offset, len);
+            int ret = ikcp_send(this.intPtr, buffer, offset, len);
             return ret;
         }
 
-        public static void KcpSetminrto(IntPtr kcp, int minrto)
+        public void SetMinrto(int minrto)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            ikcp_setminrto(kcp, minrto);
+            ikcp_setminrto(this.intPtr, minrto);
         }
 
-        public static int KcpSetmtu(IntPtr kcp, int mtu)
+        public int SetMtu(int mtu)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            return ikcp_setmtu(kcp, mtu);
+            return ikcp_setmtu(this.intPtr, mtu);
         }
 
-        public static void KcpSetoutput(KcpOutput output)
+        public static void SetOutput(KcpOutput output)
         {
             KcpOutput = output;
             ikcp_setoutput(KcpOutput);
         }
         
-        public static void KcpSetLog(KcpLog kcpLog)
+        public static void SetLog(KcpLog kcpLog)
         {
             KcpLog = kcpLog;
             ikcp_setlog(KcpLog);
         }
 
-        public static void KcpUpdate(IntPtr kcp, uint current)
+        public void Update(uint current)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            ikcp_update(kcp, current);
+            ikcp_update(this.intPtr, current);
         }
 
-        public static int KcpWaitsnd(IntPtr kcp)
+        public int Waitsnd()
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            int ret = ikcp_waitsnd(kcp);
+            int ret = ikcp_waitsnd(this.intPtr);
             return ret;
         }
 
-        public static int KcpWndsize(IntPtr kcp, int sndwnd, int rcvwnd)
+        public int SetWndSize(int sndwnd, int rcvwnd)
         {
-            if (kcp == IntPtr.Zero)
+            if (this.intPtr == IntPtr.Zero)
             {
                 throw new Exception($"kcp error, kcp point is zero");
             }
-            return ikcp_wndsize(kcp, sndwnd, rcvwnd);
+            return ikcp_wndsize(this.intPtr, sndwnd, rcvwnd);
         }
     }
 }