private string ExtractOnePendingPacket() { int pos = _pendingBuffer.IndexOf(PacketDelimiter); if (pos >= 0) { string packet = _pendingBuffer.Substring(0, pos); _pendingBuffer = _pendingBuffer.Substring(pos + PacketDelimiter.Length); byte[] data = _encoding.GetBytes(packet); Rc4Encryption.Encrypt(data); Array.Reverse(data); data = ApplyMiddleSwap(data); string content = _encoding.GetString(data); content = content.Trim(); string trimedDelimiter = PacketDelimiter.Trim(); while (content.EndsWith(trimedDelimiter)) { content = content.Substring(0, content.Length - trimedDelimiter.Length).Trim(); } return(content); } return(null); }
public async Task SendAsync(string content) { byte[] data = _encoding.GetBytes(content); data = AppendSecurityByte(data); data = ApplyMiddleSwap(data); Array.Reverse(data); Rc4Encryption.Encrypt(data); data = AppendPacketDelimiter(data); try { await _stream.WriteAsync(data, 0, data.Length); } catch (Exception) { IsConnected = false; } }