123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using System.Net;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.IO;
- namespace Vinno.IUS.Common.Network.Channels
- {
- public abstract class Channel: IChannel
- {
- private static readonly Random Random = new Random();
- private const byte MajorVersion = 2;
- private const byte MinorVersion = 1;
- private bool _closing;
- /// <summary>
- /// Gets the ChannelId for this Channel;
- /// </summary>
- public int Id { get; private set; }
- /// <summary>
- /// Gets if the Channel is Closed.
- /// </summary>
- public bool IsClosed { get; protected set; }
- /// <summary>
- /// Raised when channel was closed.
- /// </summary>
- public event EventHandler Closed;
- /// <summary>
- /// Gets the Remote host info, for Server, this property means client, for client, this property
- /// means server.
- /// </summary>
- public IPEndPoint Remote { get; set; }
- /// <summary>
- /// Gets the Local host info, for Server, this property means server, for client, this property
- /// means client.
- /// </summary>
- public IPEndPoint Local { get; set; }
- public Channel(int channelId)
- {
- Id = channelId;
- }
- /// <summary>
- /// Update the channel's Id.
- /// </summary>
- /// <param name="channelId"></param>
- public void UpdateChannelId(int channelId)
- {
- Id = channelId;
- }
- protected void ValidateHeader(byte[] headerData)
- {
- if (headerData.Length != 8)
- {
- throw new ErrorDataException("Head data length error.");
- }
- var versionData = new byte[2];
- var xyData = new byte[2];
- var zData = new short[1];
- Buffer.BlockCopy(headerData, 0, versionData, 0, versionData.Length);
- if (versionData[0] != MajorVersion)
- {
- throw new ErrorDataException("Major version not match.");
- }
- Buffer.BlockCopy(headerData, 4, xyData, 0, xyData.Length);
- Buffer.BlockCopy(headerData, 6, zData, 0, zData.Length * sizeof(short));
- var x = xyData[0];
- var y = xyData[1];
- var z = zData[0];
- //Calc to see if is valid data.
- if (z != x * 2 + y * 2 - (x - y))
- {
- throw new ErrorDataException("Xyz data error.");
- }
- }
- protected byte[] GetHeaderData()
- {
- var headerData = new byte[8];
- var versionData = new[] { MajorVersion, MinorVersion };
- var x = (byte)Random.Next(byte.MaxValue);
- var y = (byte)Random.Next(byte.MaxValue);
- var z = (short)(x * 2 + y * 2 - (x - y));
- var xyData = new[] { x, y };
- var zData = new[] { z };
- Buffer.BlockCopy(versionData, 0, headerData, 0, versionData.Length);
- Buffer.BlockCopy(xyData, 0, headerData, 4, xyData.Length);
- Buffer.BlockCopy(zData, 0, headerData, 6, zData.Length * sizeof(short));
- return headerData;
- }
- /// <summary>
- /// Read and validate the header from remote.
- /// </summary>
- /// <param name="timeout">Wait forever if the value is 0</param>
- protected abstract void ReadHeader(int timeout = 0);
- /// <summary>
- /// run task
- /// </summary>
- /// <returns></returns>
- protected virtual async Task ReadHeaderAsync(int timeout = 0)
- {
- await Task.Run(() => { });
- }
- /// <summary>
- /// Write the header before write buffer.
- /// </summary>
- /// <param name="timeout">Wait forever if the value is 0</param>
- protected abstract void WriteHeader(int timeout = 0);
-
- /// <summary>
- /// Write the header before write buffer by async way.
- /// </summary>
- /// <param name="timeout">Wait forever if the value is 0</param>
- protected virtual async Task WriteHeaderAsync(int timeout = 0)
- {
- await Task.Run(() => { });
- }
- /// <summary>
- /// Close the channel, and raise the event of close.
- /// </summary>
- public virtual void Close()
- {
- if (!_closing)
- {
- _closing = true;
- IsClosed = true;
- OnClosed();
- _closing = false;
- }
- }
- protected virtual void OnClosed()
- {
- Closed?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|