/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Data in TSDU</returns> public static async Task <byte[]> ReadAsync(Stream stream) { var segment = await TPDU.ReadAsync(stream); if (segment == null) { return(null); } var buffer = new byte[segment.Data.Length]; var output = new MemoryStream(buffer); output.Write(segment.Data, 0, segment.Data.Length); while (!segment.LastDataUnit) { segment = await TPDU.ReadAsync(stream); Array.Resize(ref buffer, buffer.Length + segment.Data.Length); var lastPosition = output.Position; output = new MemoryStream(buffer); output.Write(segment.Data, (int)lastPosition, segment.Data.Length); } return(buffer.Take((int)output.Position).ToArray()); }
/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Data in TSDU</returns> public static async Task <byte[]> ReadAsync(Stream stream, CancellationToken cancellationToken) { var segment = await TPDU.ReadAsync(stream, cancellationToken).ConfigureAwait(false); if (segment.LastDataUnit) { return(segment.Data); } // More segments are expected, prepare a buffer to store all data var buffer = new byte[segment.Data.Length]; Array.Copy(segment.Data, buffer, segment.Data.Length); while (!segment.LastDataUnit) { segment = await TPDU.ReadAsync(stream, cancellationToken).ConfigureAwait(false); var previousLength = buffer.Length; Array.Resize(ref buffer, buffer.Length + segment.Data.Length); Array.Copy(segment.Data, 0, buffer, previousLength, segment.Data.Length); } return(buffer); }
/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Data in TSDU</returns> public static byte[] Read(Stream stream) { var segment = TPDU.Read(stream); if (segment == null) { return(null); } var buffer = new byte[segment.Data.Length]; var output = new MemoryStream(buffer); output.Write(segment.Data, 0, segment.Data.Length); while (!segment.LastDataUnit) { segment = TPDU.Read(stream); Array.Resize(ref buffer, buffer.Length + segment.Data.Length); var lastPosition = output.Position; output.Dispose(); output = null; output = new MemoryStream(buffer); output.Write(segment.Data, (int)lastPosition, segment.Data.Length); } var result = buffer.Take((int)output.Position).ToArray(); output.Dispose(); output = null; buffer = null; return(result); }
/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <returns>Data in TSDU</returns> public static byte[] Read(Socket socket) { var segment = TPDU.Read(socket); if (segment == null) { return(null); } var output = new MemoryStream(segment.Data.Length); output.Write(segment.Data, 0, segment.Data.Length); while (!segment.LastDataUnit) { segment = TPDU.Read(socket); output.Write(segment.Data, (int)output.Position, segment.Data.Length); } return(output.GetBuffer()); }
/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Data in TSDU</returns> public static async Task <byte[]> ReadAsync(Stream stream) { var segment = await TPDU.ReadAsync(stream); if (segment == null) { return(null); } var output = new MemoryStream(segment.Data.Length); output.Write(segment.Data, 0, segment.Data.Length); while (!segment.LastDataUnit) { segment = await TPDU.ReadAsync(stream); output.Write(segment.Data, (int)output.Position, segment.Data.Length); } return(output.GetBuffer().Take((int)output.Position).ToArray()); }
/// <summary> /// Reads the full COTP TSDU (Transport service data unit) /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Data in TSDU</returns> public static byte[] Read(Stream stream) { var segment = TPDU.Read(stream); if (segment.LastDataUnit) { return(segment.Data); } // More segments are expected, prepare a buffer to store all data var buffer = new byte[segment.Data.Length]; Array.Copy(segment.Data, buffer, segment.Data.Length); while (!segment.LastDataUnit) { segment = TPDU.Read(stream); var previousLength = buffer.Length; Array.Resize(ref buffer, buffer.Length + segment.Data.Length); Array.Copy(segment.Data, 0, buffer, previousLength, segment.Data.Length); } return(buffer); }
static void Main(string[] args) { APDU myApdu = new APDU(); TPDU myTpdu = new TPDU(); List <byte> dnpPkt = new List <byte>(); //params should be in the following order //confirm, unsolicited, function, group, variation, prefixQualifier, [range] OR [start index, stop index] myApdu.buildAPDU(ref dnpPkt, 0x00, 0x00, 0x02, 0x0A, 0x02, 0x00, 0x00, 0x02, 0x01, 0x01, 0x01); myTpdu.buildTPDU(ref dnpPkt); DPDU myDpdu = new DPDU(); //List<byte> dpdu = new List<byte>(); /*dpdu.Add(0x05); * dpdu.Add(0x64); * dpdu.Add(0x05); * dpdu.Add(0xF2); * dpdu.Add(0x01); * dpdu.Add(0x00); * dpdu.Add(0xEF); * dpdu.Add(0xFF);*/ myDpdu.buildDPDU(ref dnpPkt, 0xC4, 1, 65519); //dst=1, src=65519 foreach (byte element in dnpPkt) { Console.Write(element.ToString("X2") + " "); } Console.ReadLine(); }