123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.IO;
- using Vinno.IUS.Common.Network.Tcp;
- namespace Vinno.IUS.Common.Network.Channels
- {
- /// <summary>
- ///For Client, the InputChannel must be availabe, otherwise it means the connection is abort, need reconnect.
- ///For server, it's not need to care about it. because the client's InputChannel is Server's output.
- /// </summary>
- public sealed class InputChannel : ClientChannel, IInputChannel
- {
- /// <summary>
- /// Raised when buffer received.
- /// </summary>
- public event EventHandler<IBuffer> BufferReceived;
- public InputChannel(int channelId, ITcp tcp)
- : base(channelId, tcp)
- {
- }
- /// <summary>
- /// Start receive data from network.
- /// </summary>
- internal void StartReceive()
- {
- Receive();
- }
- /// <summary>
- /// Start receive data from network in async way.
- /// </summary>
- /// <returns></returns>
- internal async Task StartReceiveAsync()
- {
- while (!IsClosed)
- {
- await ReceiveAsync().ConfigureAwait(false);
- }
- }
- /// <summary>
- /// Method to receive data from network,
- /// Use async can save the thread resource of system.
- /// </summary>
- private void Receive()
- {
- Task.Run(() =>
- {
- while (!IsClosed)
- {
- try
- {
- var buffer = DoReceive();
- OnBufferReceived(buffer);
- }
- catch (Exception ex)
- {
- var convertedException = NetworkException.Convert(ex);
- if (!(convertedException is ConnectionTimeoutException))
- {
- Close();
- }
- }
- }
- });
- }
- /// <summary>
- /// Method to receive data from network,
- /// Use async can save the thread resource of system.
- /// </summary>
- private async Task ReceiveAsync()
- {
- try
- {
- var buffer = await DoReceiveAsync().ConfigureAwait(false);
- OnBufferReceived(buffer);
- }
- catch (Exception ex)
- {
- var convertedException = NetworkException.Convert(ex);
- if (!(convertedException is ConnectionTimeoutException))
- {
- Close();
- }
- }
- }
- /// <summary>
- /// Receive buffer.
- /// The steps is :
- /// 1. Validate the Header.
- /// 2. Receive the buffer.
- /// </summary>
- /// <returns></returns>
- private IBuffer DoReceive()
- {
- ReadHeader();
- return ReadBuffer();
- }
- /// <summary>
- /// Receive buffer.
- /// The steps is :
- /// 1. Validate the Header.
- /// 2. Receive the buffer.
- /// </summary>
- /// <returns></returns>
- private async Task<IBuffer> DoReceiveAsync()
- {
- await ReadHeaderAsync().ConfigureAwait(false);
- return await ReadBufferAsync().ConfigureAwait(false);
- }
-
- /// <summary>
- /// Handler of pushed buffer.
- /// </summary>
- /// <param name="e"></param>
- private void OnBufferReceived(IBuffer e)
- {
- BufferReceived?.Invoke(this, e);
- }
- public override string ToString()
- {
- return $"InputChannel-{base.ToString()}";
- }
- }
- }
|