Skip to content

Commit

Permalink
Update DotNettyMessageSender.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 authored Jun 18, 2024
1 parent c09fa16 commit 635c8ff
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions src/Surging.Core/Surging.Core.DotNetty/DotNettyMessageSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Surging.Core.DotNetty
{
/// <summary>
/// <summary>
/// 基于DotNetty的消息发送者基类。
/// </summary>
public abstract class DotNettyMessageSender
Expand Down Expand Up @@ -59,22 +59,42 @@ public void Dispose()
/// 发送消息。
/// </summary>
/// <param name="message">消息内容。</param>
/// <returns>一个任务。</returns>
/// <returns>一个任务。</returns>
public async Task SendAsync(TransportMessage message)
{
var buffer = GetByteBuffer(message);
await _channel.WriteAndFlushAsync(buffer);
try
{
await _channel.WriteAndFlushAsync(buffer);
}
finally
{
if (buffer != null)
{
ReferenceCountUtil.Release(buffer);
}
}
}

/// <summary>
/// 发送消息并清空缓冲区。
/// </summary>
/// <param name="message">消息内容。</param>
/// <returns>一个任务。</returns>
/// <returns>一个任务。</returns>
public async Task SendAndFlushAsync(TransportMessage message)
{
var buffer = GetByteBuffer(message);
await _channel.WriteAndFlushAsync(buffer);
try
{
await _channel.WriteAndFlushAsync(buffer);
}
finally
{
if (buffer != null)
{
ReferenceCountUtil.Release(buffer);
}
}
}

#endregion Implementation of IMessageSender
Expand All @@ -98,24 +118,44 @@ public DotNettyServerMessageSender(ITransportMessageEncoder transportMessageEnco
/// 发送消息。
/// </summary>
/// <param name="message">消息内容。</param>
/// <returns>一个任务。</returns>
/// <returns>一个任务。</returns>
public async Task SendAsync(TransportMessage message)
{
var buffer = GetByteBuffer(message);
await _context.WriteAsync(buffer);
try
{
await _context.WriteAsync(buffer);
}
finally
{
if (buffer != null)
{
ReferenceCountUtil.Release(buffer);
}
}
}

/// <summary>
/// 发送消息并清空缓冲区。
/// </summary>
/// <param name="message">消息内容。</param>
/// <returns>一个任务。</returns>
/// <returns>一个任务。</returns>
public async Task SendAndFlushAsync(TransportMessage message)
{
var buffer = GetByteBuffer(message);
await _context.WriteAndFlushAsync(buffer);
try
{
await _context.WriteAndFlushAsync(buffer);
}
finally
{
if (buffer != null)
{
ReferenceCountUtil.Release(buffer);
}
}
}

#endregion Implementation of IMessageSender
}
}
}

0 comments on commit 635c8ff

Please sign in to comment.