OutputChannelProxy.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Threading.Tasks;
  3. using Vinno.IUS.Common.IO;
  4. namespace Vinno.IUS.Common.Network.Channels
  5. {
  6. internal class OutputChannelProxy : ChannelProxy<OutputChannel>, IOutputChannelProxy
  7. {
  8. private bool _timeoutHappened;
  9. public OutputChannelProxy(OutputChannel channel, ChannelFactory<OutputChannel> factory) : base(channel, factory)
  10. {
  11. }
  12. /// <summary>
  13. /// Send buffer to remote.
  14. /// </summary>
  15. /// <param name="buffer">The buffer to send</param>
  16. /// <param name="timeout">Wait forever if the value is 0</param>
  17. /// <returns>The return buffer from remote.</returns>
  18. public IBuffer Send(IBuffer buffer, int timeout = 0)
  19. {
  20. return Channel.Send(buffer, timeout);
  21. }
  22. /// <summary>
  23. /// Send buffer to remote.
  24. /// </summary>
  25. /// <param name="buffer">The buffer to send</param>
  26. /// <param name="resultCallback">When receive a part of buffers, the callback will be called.</param>
  27. /// <param name="timeout">Wait forever if the value is 0</param>
  28. public void Send(IBuffer buffer, Func<IBuffer, bool> resultCallback, int timeout = 0)
  29. {
  30. Channel.Send(buffer, resultCallback, timeout);
  31. }
  32. /// <summary>
  33. /// Send buffer to remote by async way.
  34. /// </summary>
  35. /// <param name="buffer">The buffer to send</param>
  36. /// <param name="timeout">Wait forever if the value is 0</param>
  37. /// <returns>The return buffer from remote.</returns>
  38. public async Task<IBuffer> SendAsync(IBuffer buffer, int timeout = 0)
  39. {
  40. return await Channel.SendAsync(buffer, timeout).ConfigureAwait(false);
  41. }
  42. /// <summary>
  43. /// Send buffer to remote by asnyc way.
  44. /// </summary>
  45. /// <param name="buffer">The buffer to send</param>
  46. /// <param name="resultCallback">When receive a part of buffers, the callback will be called.</param>
  47. /// <param name="timeout">Wait forever if the value is 0</param>
  48. public async Task SendAsync(IBuffer buffer, Func<IBuffer, bool> resultCallback, int timeout = 0)
  49. {
  50. await Channel.SendAsync(buffer, resultCallback, timeout).ConfigureAwait(false);
  51. }
  52. protected override void DoDispose()
  53. {
  54. if (_timeoutHappened)
  55. {
  56. Channel.Close();
  57. }
  58. else
  59. {
  60. base.DoDispose();
  61. }
  62. }
  63. /// <summary>
  64. /// Indicate the channel was timeout
  65. /// </summary>
  66. /// <param name="channelId">The channelId</param>
  67. public void SetTimeoutExceptionFlag(out int channelId)
  68. {
  69. channelId = Channel.Id;
  70. _timeoutHappened = true;
  71. }
  72. }
  73. }