From 7cb15395acef6e3e7e6429ecfec1ba7322521d19 Mon Sep 17 00:00:00 2001 From: tanghai Date: Wed, 7 Jan 2015 17:06:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4TChannel=E7=9A=84start?= =?UTF-8?q?=E5=87=BD=E6=95=B0,channel=E4=B8=80=E5=88=9B=E5=BB=BA=E5=B0=B1?= =?UTF-8?q?=E5=9C=A8=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E4=B8=ADstartrecv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSharp/Platform/TNet/TChannel.cs | 72 ++++++++++++++++---------------- CSharp/Platform/TNet/TService.cs | 2 - 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/CSharp/Platform/TNet/TChannel.cs b/CSharp/Platform/TNet/TChannel.cs index 8fd775d9..70adb6c2 100644 --- a/CSharp/Platform/TNet/TChannel.cs +++ b/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 RecvAsync() { var tcs = new TaskCompletionSource(); @@ -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 { diff --git a/CSharp/Platform/TNet/TService.cs b/CSharp/Platform/TNet/TService.cs index a50084f7..8704ad42 100644 --- a/CSharp/Platform/TNet/TService.cs +++ b/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; } -- GitLab