using System; using System.Threading.Tasks; using Vinno.IUS.Common.IO; namespace Vinno.IUS.Common.Network.Channels { internal class OutputChannelProxy : ChannelProxy, IOutputChannelProxy { private bool _timeoutHappened; public OutputChannelProxy(OutputChannel channel, ChannelFactory factory) : base(channel, factory) { } /// /// Send buffer to remote. /// /// The buffer to send /// Wait forever if the value is 0 /// The return buffer from remote. public IBuffer Send(IBuffer buffer, int timeout = 0) { return Channel.Send(buffer, timeout); } /// /// Send buffer to remote. /// /// The buffer to send /// When receive a part of buffers, the callback will be called. /// Wait forever if the value is 0 public void Send(IBuffer buffer, Func resultCallback, int timeout = 0) { Channel.Send(buffer, resultCallback, timeout); } /// /// Send buffer to remote by async way. /// /// The buffer to send /// Wait forever if the value is 0 /// The return buffer from remote. public async Task SendAsync(IBuffer buffer, int timeout = 0) { return await Channel.SendAsync(buffer, timeout).ConfigureAwait(false); } /// /// Send buffer to remote by asnyc way. /// /// The buffer to send /// When receive a part of buffers, the callback will be called. /// Wait forever if the value is 0 public async Task SendAsync(IBuffer buffer, Func resultCallback, int timeout = 0) { await Channel.SendAsync(buffer, resultCallback, timeout).ConfigureAwait(false); } protected override void DoDispose() { if (_timeoutHappened) { Channel.Close(); } else { base.DoDispose(); } } /// /// Indicate the channel was timeout /// /// The channelId public void SetTimeoutExceptionFlag(out int channelId) { channelId = Channel.Id; _timeoutHappened = true; } } }