提交 7cb15395 编写于 作者: T tanghai

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

上级 2a6edd13
......@@ -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
{
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册