Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
性能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jan 2, 2016
1 parent cc58cb6 commit 040e007
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Net40/NetworkSocket/ByteRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ByteRange(byte[] buffer, int offset, int count)
/// </summary>
/// <param name="size">新的ByteRange大小</param>
/// <returns></returns>
public IEnumerable<IByteRange> SplitBySize(int size)
public IEnumerable<ByteRange> SplitBySize(int size)
{
if (size >= this.Count)
{
Expand Down
3 changes: 1 addition & 2 deletions Net40/NetworkSocket/Http/Result/FileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ public void ExecuteResult(HttpResponse response)
using (var stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read))
{
const int size = 8 * 1024;
var bytes = new byte[size];
var state = response.WriteHeader((int)stream.Length);

while (state == true)
{
var bytes = new byte[size];
var length = stream.Read(bytes, 0, size);
if (length == 0)
{
break;
}

var content = new ByteRange(bytes, 0, length);
state = response.WriteContent(content);
}
Expand Down
7 changes: 0 additions & 7 deletions Net40/NetworkSocket/IByteRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,5 @@ public interface IByteRange
/// 获取字节数组
/// </summary>
byte[] Buffer { get; }

/// <summary>
/// 分割为大小相等的ByteRange集合
/// </summary>
/// <param name="size">新的ByteRange大小</param>
/// <returns></returns>
IEnumerable<IByteRange> SplitBySize(int size);
}
}
41 changes: 12 additions & 29 deletions Net40/NetworkSocket/Internal/IocpTcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public IocpTcpSession()
{
this.sendArg.Completed += this.SendCompleted;
this.recvArg.Completed += this.RecvCompleted;
BufferManager.SetBuffer(this.sendArg);
BufferManager.SetBuffer(this.recvArg);
}

Expand Down Expand Up @@ -131,34 +130,20 @@ public override void Send(IByteRange byteRange)
throw new SocketException((int)SocketError.NotConnected);
}

var byteRanges = byteRange.SplitBySize(this.sendArg.Count);
foreach (var range in byteRanges)
{
this.SendByteRange(range);
}
}

/// <summary>
/// 发送一个小于缓冲区的数据范围
/// </summary>
/// <param name="byteRange">数据范围</param>
private bool SendByteRange(IByteRange byteRange)
{
// 如果发送过程已停止,则本次直接发送
if (Interlocked.CompareExchange(ref this.PendingSendCount, 1, 0) == 0)
{
return this.TrySendByteRangeAsync(byteRange);
this.TrySendByteRangeAsync(byteRange);
}

// 添加数据到缓存区
this.PendingSendByteRanges.Enqueue(byteRange);

// 如果发送过程已停止,则启动发送缓存中的数据
if (Interlocked.Increment(ref this.PendingSendCount) == 1)
else
{
return this.TrySendByteRangeAsync(null);
this.PendingSendByteRanges.Enqueue(byteRange);
// 如果发送过程已停止,则启动发送缓存中的数据
if (Interlocked.Increment(ref this.PendingSendCount) == 1)
{
this.TrySendByteRangeAsync(null);
}
}
return true;
}


Expand All @@ -167,19 +152,17 @@ private bool SendByteRange(IByteRange byteRange)
/// 发送完成将触发SendCompleted方法
/// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
/// </summary>
private bool TrySendByteRangeAsync(IByteRange byteRange)
private void TrySendByteRangeAsync(IByteRange byteRange)
{
if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
{
Interlocked.Exchange(ref this.PendingSendCount, 0);
return false;
return;
}

Buffer.BlockCopy(byteRange.Buffer, byteRange.Offset, this.sendArg.Buffer, this.sendArg.Offset, byteRange.Count);
this.sendArg.SetBuffer(this.sendArg.Offset, byteRange.Count);

return base.TryInvokeAction(() =>
base.TryInvokeAction(() =>
{
this.sendArg.SetBuffer(byteRange.Buffer, byteRange.Offset, byteRange.Count);
if (this.Socket.SendAsync(this.sendArg) == false)
{
this.SendCompleted(this.Socket, this.sendArg);
Expand Down
32 changes: 11 additions & 21 deletions Net40/NetworkSocket/Internal/SslTcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,46 +198,36 @@ public override void Send(IByteRange byteRange)
throw new SocketException((int)SocketError.NotConnected);
}

this.SendByteRange(byteRange);
}

/// <summary>
/// 发送一个小于缓冲区的数据范围
/// </summary>
/// <param name="byteRange">数据范围</param>
private bool SendByteRange(IByteRange byteRange)
{
// 如果发送过程已停止,则本次直接发送
if (Interlocked.CompareExchange(ref this.PendingSendCount, 1, 0) == 0)
{
return this.TrySendByteRangeAsync(byteRange);
this.TrySendByteRangeAsync(byteRange);
}

// 添加数据到缓存区
this.PendingSendByteRanges.Enqueue(byteRange);

// 如果发送过程已停止,则启动发送缓存中的数据
if (Interlocked.Increment(ref this.PendingSendCount) == 1)
else
{
return this.TrySendByteRangeAsync(null);
this.PendingSendByteRanges.Enqueue(byteRange);
// 如果发送过程已停止,则启动发送缓存中的数据
if (Interlocked.Increment(ref this.PendingSendCount) == 1)
{
this.TrySendByteRangeAsync(null);
}
}
return true;
}

/// <summary>
/// 尝试异步发送一个ByteRange
/// 发送完成将触发SendCompleted方法
/// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
/// </summary>
private bool TrySendByteRangeAsync(IByteRange byteRange)
private void TrySendByteRangeAsync(IByteRange byteRange)
{
if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
{
Interlocked.Exchange(ref this.PendingSendCount, 0);
return false;
return;
}

return base.TryInvokeAction(() =>
base.TryInvokeAction(() =>
this.sslStream.BeginWrite(
byteRange.Buffer,
byteRange.Offset,
Expand Down

0 comments on commit 040e007

Please sign in to comment.