Browse Source

去除TChannel的start函数,channel一创建就在构造函数中startrecv

tanghai 11 years ago
parent
commit
7cb15395ac
2 changed files with 37 additions and 37 deletions
  1. 37 35
      CSharp/Platform/TNet/TChannel.cs
  2. 0 2
      CSharp/Platform/TNet/TService.cs

+ 37 - 35
CSharp/Platform/TNet/TChannel.cs

@@ -28,6 +28,8 @@ namespace TNet
 			this.service = service;
 			this.parser = new PacketParser(recvBuffer);
 			this.remoteAddress = this.socket.RemoteAddress;
+
+			StartRecv();
 		}
 
 		protected virtual void Dispose(bool disposing)
@@ -66,7 +68,7 @@ namespace TNet
 			this.sendBuffer.SendTo(buffer);
 			if (this.sendTimer == ObjectId.Empty)
 			{
-				this.sendTimer = this.service.Timer.Add(TimeHelper.Now() + SendInterval, this.SendTimerCallback);
+				this.sendTimer = this.service.Timer.Add(TimeHelper.Now() + SendInterval, this.StartSend);
 			}
 		}
 
@@ -78,39 +80,6 @@ namespace TNet
 			}
 		}
 
-		private async void SendTimerCallback()
-		{
-			try
-			{
-				while (true)
-				{
-					if (this.sendBuffer.Count == 0)
-					{
-						break;
-					}
-					int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
-					if (sendSize > this.sendBuffer.Count)
-					{
-						sendSize = this.sendBuffer.Count;
-					}
-					int n = await this.socket.SendAsync(
-						this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
-					this.sendBuffer.FirstIndex += n;
-					if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
-					{
-						this.sendBuffer.FirstIndex = 0;
-						this.sendBuffer.RemoveFirst();
-					}
-				}
-			}
-			catch (Exception e)
-			{
-				Log.Trace(e.ToString());
-			}
-
-			this.sendTimer = ObjectId.Empty;
-		}
-
 		public Task<byte[]> RecvAsync()
 		{
 			var tcs = new TaskCompletionSource<byte[]>();
@@ -146,7 +115,40 @@ namespace TNet
 			tcs.SetResult(packet);
 		}
 
-		public async void Start()
+		private async void StartSend()
+		{
+			try
+			{
+				while (true)
+				{
+					if (this.sendBuffer.Count == 0)
+					{
+						break;
+					}
+					int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
+					if (sendSize > this.sendBuffer.Count)
+					{
+						sendSize = this.sendBuffer.Count;
+					}
+					int n = await this.socket.SendAsync(
+						this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
+					this.sendBuffer.FirstIndex += n;
+					if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
+					{
+						this.sendBuffer.FirstIndex = 0;
+						this.sendBuffer.RemoveFirst();
+					}
+				}
+			}
+			catch (Exception e)
+			{
+				Log.Trace(e.ToString());
+			}
+
+			this.sendTimer = ObjectId.Empty;
+		}
+
+		private async void StartRecv()
 		{
 			try
 			{

+ 0 - 2
CSharp/Platform/TNet/TService.cs

@@ -71,7 +71,6 @@ namespace TNet
 			await newSocket.ConnectAsync(host, port);
 			TChannel channel = new TChannel(newSocket, this);
 			channels[newSocket.RemoteAddress] = channel;
-			channel.Start();
 			return channel;
 		}
 
@@ -85,7 +84,6 @@ namespace TNet
 			await acceptor.AcceptAsync(socket);
 			TChannel channel = new TChannel(socket, this);
 			channels[channel.RemoteAddress] = channel;
-			channel.Start();
 			return channel;
 		}