diff --git a/CSharp/Platform/TNet/TChannel.cs b/CSharp/Platform/TNet/TChannel.cs index 8fd775d923282dfb150da19152a38d48d0b03918..70adb6c2d83d91e473b5de101b67eadc2e888152 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 a50084f75bb0ac24070939b1bbb35f9ad330ccac..8704ad42aa717c9e4395eac3aa7a5f2b42b045cb 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; }