浏览代码

TChannel Connect 改为下一帧读写.否则有概率出现asynchronous socket operation is already in progress using 问题 (#406)

【找了很久也没找到可能出现同时操作的地方.但改回原来的下一帧读写后就没再出现了】
BeanTang 3 年之前
父节点
当前提交
16be2df3c2

+ 7 - 12
Unity/Assets/Scripts/Core/Module/Network/TChannel.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Concurrent;
 using System.IO;
 using System.Net;
 using System.Net.Sockets;
@@ -46,8 +45,8 @@ namespace ET
 			this.RemoteAddress = ipEndPoint;
 			this.isConnected = false;
 			this.isSending = false;
-
-			this.ConnectAsync();
+			
+			this.Service.Queue.Enqueue(new TArgs(){Op = TcpOp.ConnectRemote,ChannelId = this.Id});
 		}
 		
 		public TChannel(long id, Socket socket, TService service)
@@ -136,7 +135,7 @@ namespace ET
 			}
 		}
 
-		private void ConnectAsync()
+		public void ConnectAsync()
 		{
 			this.outArgs.RemoteEndPoint = this.RemoteAddress;
 			if (this.socket.ConnectAsync(this.outArgs))
@@ -200,7 +199,7 @@ namespace ET
 			}
 		}
 
-		public void OnRecvComplete(object o)
+		public void OnRecvComplete(SocketAsyncEventArgs o)
 		{
 			this.HandleRecv(o);
 			
@@ -212,14 +211,12 @@ namespace ET
 			this.Service.Queue.Enqueue(new TArgs() { Op = TcpOp.StartRecv, ChannelId = this.Id});
 		}
 
-		private void HandleRecv(object o)
+		private void HandleRecv(SocketAsyncEventArgs e)
 		{
 			if (this.socket == null)
 			{
 				return;
 			}
-			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
-
 			if (e.SocketError != SocketError.Success)
 			{
 				this.OnError((int)e.SocketError);
@@ -319,7 +316,7 @@ namespace ET
 			}
 		}
 
-		public void OnSendComplete(object o)
+		public void OnSendComplete(SocketAsyncEventArgs o)
 		{
 			HandleSend(o);
 			
@@ -328,14 +325,12 @@ namespace ET
 			this.Service.Queue.Enqueue(new TArgs() { Op = TcpOp.StartSend, ChannelId = this.Id});
 		}
 
-		private void HandleSend(object o)
+		private void HandleSend(SocketAsyncEventArgs e)
 		{
 			if (this.socket == null)
 			{
 				return;
 			}
-			
-			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
 
 			if (e.SocketError != SocketError.Success)
 			{

+ 7 - 1
Unity/Assets/Scripts/Core/Module/Network/TService.cs

@@ -12,7 +12,7 @@ namespace ET
 	{
 		StartSend,
 		StartRecv,
-		SocketAsyncEventArgs,
+		ConnectRemote,
 	}
 	
 	public struct TArgs
@@ -207,6 +207,12 @@ namespace ET
 							tChannel.StartRecv();
 							break;
 						}
+						case TcpOp.ConnectRemote:
+						{
+							TChannel tChannel = this.Get(result.ChannelId);
+							tChannel.ConnectAsync();
+							break;
+						}
 					}
 					continue;
 				}