using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace SocketTransfer.SDK { public class Socket : IDisposable { /// /// 分割Socket数据包 /// /// Socket数据包 /// Socket片段定义 /// Socket数据块 public static byte[][] Disperse(byte[] buffer, params int[] phases) { byte[][] target = new byte[phases.Length][]; int cursor = 0; for (int i = 0; i < phases.Length; i++) { target[i] = new byte[phases[i]]; for (int j = 0; j < phases[i]; j++) { target[i][j] = buffer[cursor + j]; } cursor += phases[i]; } return target; } /// /// 聚合Socket数据块 /// /// Socket数据块 /// Socket片段定义 /// Socket数据包 public static byte[] Aggregate(byte[][] buffer, params int[] phases) { byte[] target = new byte[phases.Sum()]; int cursor = 0; for (int i = 0; i < phases.Length; i++) { for (int j = 0; j < phases[i]; j++) { target[cursor + j] = buffer[i][j]; } cursor += phases[i]; } return target; } #region 属性 => Phases <= int[] _Phases; /// /// 数据段 /// [Browsable(false)] public int[] Phases { get { return _Phases; } } #endregion #region 属性 => Encoding <= Encoding _Encoding; /// /// 编码规则 /// [Browsable(false)] public Encoding Encoding { get { return _Encoding; } } #endregion #region 属性 => Source <= byte[] _Source; /// /// 数据包 /// [Browsable(false)] public byte[] Source { get { return _Source; } } #endregion #region 构造 => Socket <= protected Socket(byte[] source, Encoding encoding, params int[] phases) { _Encoding = encoding; _Phases = phases; _Source = source; } #endregion public virtual bool Valid() { if (_Phases.TakeWhile(p => p <= 0).Count() > 0) { return false; } return true; } #region SetValue protected void SetValue(int index, string value) { SetValue(_Encoding, _Source, _Phases, index, value); } public static void SetValue(Encoding encoding, byte[] source, int[] phases, int index, string value) { try { int length = phases[index]; int start = phases.Take(index).Sum(); byte[] temp = value == null ? new byte[0] : encoding.GetBytes(value); if (_Align) { for (int i = start; i < start + length; i++) { if (i - start < temp.Length) { source[i] = temp[i - start]; } else { source[i] = temp.Length == 0 ? (byte)0 : _Space; } } } //else //{ // for (int i = start + length - 1; i >= start; i--) // { // if (temp.Length + i - start - length >= 0) // { // source[i] = temp[temp.Length + i - start - length]; // } // else // { // source[i] = temp.Length == 0 ? (byte)0 : _Space; // } // } //} } catch (Exception e) { throw new Exception("SetValue Error!" + e.ToString()); } } #endregion const byte _Space = 32; const bool _Align = true; #region GetValue protected string GetValue(int index) { return GetValue(_Encoding, _Source, _Phases, index); } public static string GetValue(Encoding encoding, byte[] source, int[] phases, int index) { try { int length = phases[index]; int start = phases.Take(index).Sum(); byte[] temp = new byte[length]; for (int i = start; i < start + length; i++) { temp[i - start] = source[i]; } return encoding.GetString(temp).Replace("\0", "").Trim(); } catch (Exception e) { throw new Exception("GetValue Error!" + e.ToString()); } } #endregion protected void RebuildSource() { bool flag = true; for (int i = 0; i < _Source.Length; i++) { if (flag && _Source[_Source.Length - i - 1] == 0) { continue; } else { flag = false; if (_Source[_Source.Length - i - 1] == 0) _Source[_Source.Length - i - 1] = _Space; } } } protected void ExtendSource() { if (_Phases.Sum() > _Source.Length) { byte[] temp = new byte[_Phases.Sum()]; for (int i = 0; i < temp.Length; i++) { if (i < _Source.Length) { temp[i] = _Source[i]; } else { temp[i] = 0; } } _Source = temp; } } public static int Length(byte[] source) { return source.Where(b => b != 0).Count(); } #region Dispose public void Dispose() { _Source = null; _Phases = null; _Encoding = null; } #endregion } }