public Constructer() { _chunks = new List <object>(); SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.EnableNotifyMessage, true); var typeCol = new ColumnHeader { Name = "TypeCol", Text = "Type", Width = 60 }; var valueCol = new ColumnHeader { Name = "ValueCol", Text = "Value", Width = 194 }; var encodedCol = new ColumnHeader { Name = "EncodedCol", Text = "Encoded", Width = 104 }; Columns.AddRange(new[] { typeCol, valueCol, encodedCol }); FullRowSelect = true; GridLines = true; HeaderStyle = ColumnHeaderStyle.Nonclickable; MultiSelect = false; ShowItemToolTips = true; Size = new Size(386, 166); UseCompatibleStateImageBehavior = false; View = View.Details; LockColumns = true; _destination = HDestination.Server; _protocol = HProtocol.Modern; }
private void DataFromClient(IAsyncResult iAr) { try { if (_clientS == null) { return; } int length = _clientS.EndReceive(iAr); if (length < 1) { Disconnect(); return; } byte[] data = ByteUtils.CopyBlock(_clientB, 0, length); #region Official Socket Check if (!_hasOfficialSocket) { bool isModern = Modern.DecypherShort(data, 4) == 4000; if (_hasOfficialSocket = (isModern || Ancient.DecypherShort(data, 3) == 206)) { ResetHost(); _htcpExt.Stop(); _htcpExt = null; Protocol = isModern ? HProtocol.Modern : HProtocol.Ancient; OnConnected(EventArgs.Empty); } else { SendToServer(data); return; } } #endregion #region Decrypt/Split if (ClientDecrypt != null) { ClientDecrypt.Parse(data); } if (_toServerS == 3 && Protocol == HProtocol.Modern) { int dLength = data.Length >= 6 ? Modern.DecypherInt(data) : 0; RequestEncrypted = (dLength != data.Length - 4); } byte[][] chunks = RequestEncrypted ? new[] { data } : ByteUtils.Split(ref _clientC, data, HDestination.Server, Protocol); #endregion foreach (byte[] chunk in chunks) { ProcessOutgoing(chunk); } ReadClientData(); } catch { Disconnect(); } }
protected ushort ReadShort(ref int index, HProtocol protocol) { if (index >= Body.Length || index + 2 > Body.Length) { return(0); } var chunk = new byte[] { Body[index++], Body[index++] }; return(Protocol == HProtocol.Ancient ? Ancient.DecypherShort(chunk) : Modern.DecypherShort(chunk)); }
public void Disconnect() { if (!_disconnectAllowed) { return; } _disconnectAllowed = false; lock (_disconnectLock) { if (_clientS != null) { _clientS.Shutdown(SocketShutdown.Both); _clientS.Close(); _clientS = null; } if (_serverS != null) { _serverS.Shutdown(SocketShutdown.Both); _serverS.Close(); _serverS = null; } ResetHost(); if (_htcpExt != null) { _htcpExt.Stop(); _htcpExt = null; } Protocol = HProtocol.Modern; _toClientS = _toServerS = _socketCount = 0; _clientB = _serverB = _clientC = _serverC = null; _hasOfficialSocket = RequestEncrypted = ResponseEncrypted = false; ClientEncrypt = ClientDecrypt = ServerEncrypt = ServerDecrypt = null; if (Disconnected != null) { var disconnectedEventArgs = new DisconnectedEventArgs(); Disconnected(this, disconnectedEventArgs); if (disconnectedEventArgs.UnsubscribeFromEvents) { SKore.Unsubscribe(ref Connected); SKore.Unsubscribe(ref DataToClient); SKore.Unsubscribe(ref DataToServer); SKore.Unsubscribe(ref Disconnected); base.Dispose(false); } } } }
protected bool ReadBool(ref int index, HProtocol protocol) { if (index >= Body.Length || index + 1 > Body.Length) { return(false); } switch (Protocol) { case HProtocol.Modern: return(Body[index++] == 1); case HProtocol.Ancient: return(Body[index++] == 'I'); default: return(false); } }
protected string ReadString(ref int index, HProtocol protocol, HDestination destination) { if (index >= Body.Length) { return(string.Empty); } if (destination == HDestination.Server || protocol == HProtocol.Modern) { ushort length = ReadShort(ref index, protocol); byte[] data = ByteUtils.CopyBlock(Body, (index += length) - length, length); return(Encoding.Default.GetString(data)); } else { string chunk = _rawBody.Substring(index).Split((char)2)[0]; index += chunk.Length + 1; return(chunk); } }
protected int ReadInt(ref int index, HProtocol protocol) { if (index >= Body.Length) { return(0); } switch (Protocol) { case HProtocol.Modern: { if (index + 4 > Body.Length) { return(0); } return(Modern.DecypherInt(Body[index++], Body[index++], Body[index++], Body[index++])); } case HProtocol.Ancient: { int length = (Body[index] >> 3) & 7; if (length < 1) { length++; } if (index + length > Body.Length) { return(0); } int value = Ancient.DecypherInt(Body, index); index += length; return(value); } default: return(0); } }
public static byte[] Construct(ushort header, HDestination destination, HProtocol protocol, params object[] chunks) { var buffer = new List <byte>(); bool isAncient = (protocol == HProtocol.Ancient); if (isAncient && destination == HDestination.Server) { buffer.Add(64); } buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(header) : Modern.CypherShort(header)); buffer.AddRange(ConstructBody(destination, protocol, chunks)); if (!isAncient || destination == HDestination.Server) { buffer.InsertRange(isAncient ? 1 : 0, isAncient ? Ancient.CypherShort((ushort)(buffer.Count - 1)) : Modern.CypherInt(buffer.Count)); } else if (buffer[buffer.Count - 1] != 1) { buffer.Add(1); } return(buffer.ToArray()); }
public static byte[] ConstructBody(HDestination destination, HProtocol protocol, params object[] chunks) { var buffer = new List <byte>(); bool isAncient = (protocol == HProtocol.Ancient); for (int i = 0; i < chunks.Length; i++) { object chunk = chunks[i]; if (chunk == null) { throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i)); } var data = chunk as byte[]; if (data != null) { buffer.AddRange(data); } else { switch (Type.GetTypeCode(chunk.GetType())) { case TypeCode.Int32: { var value = (int)chunk; buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherInt(value) : Modern.CypherInt(value)); break; } case TypeCode.Boolean: { var value = (bool)chunk; buffer.Add(isAncient ? (byte)(value ? 73 : 72) : Convert.ToByte(value)); break; } case TypeCode.Byte: { var value = (byte)chunk; buffer.Add(value); break; } default: { string value = chunk.ToString(); if (!isAncient || destination == HDestination.Server) { ushort valueLength = (ushort)value.Length; buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(valueLength) : Modern.CypherShort(valueLength)); buffer.AddRange(Encoding.Default.GetBytes(value)); } else { buffer.AddRange(Encoding.Default.GetBytes(value)); buffer.Add(2); } break; } } } } return(buffer.ToArray()); }
public HMessage(ushort header, HDestination destination, HProtocol protocol, params object[] chunks) : this(Construct(header, destination, protocol, chunks), destination) { _logWriting = true; _appended.AddRange(chunks); }
public static byte[][] Split(ref byte[] cache, byte[] data, HDestination destination, HProtocol protocol) { lock (SplitLock) { if (cache != null) { data = Merge(cache, data); cache = null; } var chunks = new List <byte[]>(); if (protocol == HProtocol.Ancient && destination == HDestination.Client) { if (!data.Contains((byte)1)) { cache = data; } else { var buffer = new List <byte>(); foreach (byte value in data) { buffer.Add(value); if (value == 1) { chunks.Add(buffer.ToArray()); buffer.Clear(); } } if (buffer.Count > 0) { cache = buffer.ToArray(); } } } else { bool isAncient = (protocol == HProtocol.Ancient); int offset = isAncient ? 3 : 4; int length = isAncient ? Ancient.DecypherShort(data, 1) : Modern.DecypherInt(data); if (length == data.Length - offset) { chunks.Add(data); } else { do { if (length > data.Length - offset) { cache = data; break; } chunks.Add(CutBlock(ref data, 0, length + offset)); if (data.Length >= offset) { length = isAncient ? Ancient.DecypherShort(data, 1) : Modern.DecypherInt(data); } }while (data.Length != 0); } } return(chunks.ToArray()); } }