private CommunicationResult TryRead(string ipPort, int count, bool doEncryption) { ReadResult result = server.ReadWithTimeout(maxPingMs, ipPort, count); TTInstruction ins = TTInstruction.Empty; byte[] buffer = null; if (result.Status != ReadResultStatus.Success) { throw new FailedReceivingException($"Did not receive response ({result.Status})."); } if (doEncryption && clientEncryptor != null) { buffer = clientEncryptor.AESDecryptBytes(result.Data); } else { buffer = result.Data; } byte[] dat = null; if (!TTNet.UnpackTCPBuffer(buffer, ref ins, ref dat)) { throw new FailedReceivingException($"Received invalid response."); } return(new CommunicationResult(ins, dat)); }
public static bool UnpackTCPBuffer(byte[] buffer, ref TTInstruction instruction, ref List <byte> data) { if (buffer == null || buffer.Length < 1 || !Enum.IsDefined(typeof(TTInstruction), instruction)) { return(false); } instruction = (TTInstruction)buffer[0]; data = buffer.Skip(1).ToList(); return(true); }
// TCP public static bool UnpackTCPBuffer(byte[] buffer, ref TTInstruction instruction, ref byte[] data) { if (buffer == null || buffer.Length < 1 || !Enum.IsDefined(typeof(TTInstruction), instruction)) { return(false); } instruction = (TTInstruction)buffer[0]; data = new byte[buffer.Length - 1]; Array.Copy(buffer, 1, data, 0, buffer.Length - 1); // TODO Use buffer.Skip(1).ToArray() return(true); }
private CommunicationResult TryRead(int count, int?overrideTimeoutMs = null) { int timeout = overrideTimeoutMs ?? maxPingMs; ReadResult result = client.ReadWithTimeout(timeout, count); if (result.Status != ReadResultStatus.Success) { throw new FailedReceivingException($"Did not receive response ({result.Status})."); } TTInstruction ins = TTInstruction.Empty; byte[] dat = null; if (!TTNet.UnpackTCPBuffer(result.Data, ref ins, ref dat)) { throw new FailedReceivingException($"Received invalid response."); } return(new CommunicationResult(ins, dat)); }
public CommunicationResult(TTInstruction instruction, byte[] data) { this.instruction = instruction; this.data = data; success = true; }
public CommunicationResult() { instruction = TTInstruction.Empty; data = null; success = false; }
// General /// <summary> /// Returns true if data in buffer is in correct format, unpacks mac, instruction and data into variables. /// </summary> /// <param name="buffer">Data received by client</param> /// <param name="macAddress">MAC Address of remote host</param> /// <param name="instruction">Received TTransfer network instruction</param> /// <param name="data">Data remaining in buffer</param> /// <returns></returns> public static bool UnpackBuffer(byte[] buffer, ref PhysicalAddressSerializable macAddress, ref TTInstruction instruction, ref List <byte> data) { List <byte> b = new List <byte>(buffer); if (b.Count < 7 || !Enum.IsDefined(typeof(TTInstruction), instruction)) { return(false); } macAddress = new PhysicalAddressSerializable(b.GetRange(0, 6).ToArray()); instruction = (TTInstruction)b[6]; data = b.GetRange(7, b.Count - 7); return(true); }
/// <summary> /// Creates a presence buffer with device's Mac address, username and device type. /// </summary> public static void GeneratePresenceBuffer(PhysicalAddressSerializable mac, out byte[] buffer, TTInstruction instruction, string name = "", DeviceType type = DeviceType.Unknown) { if ((int)instruction < 10 || (int)instruction > 12) { new Exception("Presence buffer instruction out of range: " + instruction.ToString()); } var macBytes = mac.GetAddressBytes(); if (instruction == TTInstruction.Discovery_Bye) { buffer = new byte[7]; Array.Copy(macBytes, buffer, 6); buffer[6] = (byte)instruction; } else { var deviceTypeByte = (byte)type; var nameBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.Name); buffer = new byte[7 + 1 + nameBytes.Length]; Array.Copy(macBytes, buffer, 6); buffer[6] = (byte)instruction; buffer[7] = deviceTypeByte; Array.Copy(nameBytes, 0, buffer, 8, nameBytes.Length); } }