/// <summary> /// Reads a TPKT from the socket Async /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>Task TPKT Instace</returns> public static async Task <TPKT> ReadAsync(Stream stream) { var buf = new byte[4]; int len = await stream.ReadAsync(buf, 0, 4); if (len < 4) { throw new TPKTInvalidException("TPKT is incomplete / invalid"); } var pkt = new TPKT { Version = buf[0], Reserved1 = buf[1], Length = buf[2] * 256 + buf[3] //BigEndian }; if (pkt.Length > 0) { pkt.Data = new byte[pkt.Length - 4]; len = await stream.ReadAsync(pkt.Data, 0, pkt.Length - 4); if (len < pkt.Length - 4) { throw new TPKTInvalidException("TPKT is incomplete / invalid"); } } return(pkt); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="socket">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static TPDU Read(Socket socket) { var tpkt = TPKT.Read(socket); if (tpkt.Length > 0) { return(new TPDU(tpkt)); } return(null); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static TPDU Read(Stream stream) { var tpkt = TPKT.Read(stream); if (tpkt.Length > 0) { return(new TPDU(tpkt)); } return(null); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static async Task <TPDU> ReadAsync(Stream stream) { var tpkt = await TPKT.ReadAsync(stream); if (tpkt.Length > 0) { return(new TPDU(tpkt)); } return(null); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static async Task <TPDU> ReadAsync(Stream stream, CancellationToken cancellationToken) { var tpkt = await TPKT.ReadAsync(stream, cancellationToken).ConfigureAwait(false); if (tpkt.Length == 0) { throw new TPDUInvalidException("No protocol data received"); } return(new TPDU(tpkt)); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static TPDU Read(Stream stream) { var tpkt = TPKT.Read(stream); if (tpkt.Length == 0) { throw new TPDUInvalidException("No protocol data received"); } return(new TPDU(tpkt)); }
/// <summary> /// Reads COTP TPDU (Transport protocol data unit) from the network stream /// See: https://tools.ietf.org/html/rfc905 /// </summary> /// <param name="stream">The socket to read from</param> /// <returns>COTP DPDU instance</returns> public static async Task <TPDU> ReadAsync(Stream stream) { var tpkt = await TPKT.ReadAsync(stream); if (tpkt.Length == 0) { throw new TPDUInvalidException("No protocol data received"); } return(new TPDU(tpkt)); }
/// <summary> /// Reds a TPKT from the socket /// </summary> /// <param name="socket">The socket to read from</param> /// <returns>TPKT Instace</returns> public static TPKT Read(Socket socket) { var buf = new byte[4]; socket.Receive(buf, 4, SocketFlags.None); var pkt = new TPKT { Version = buf[0], Reserved1 = buf[1], Length = buf[2] * 256 + buf[3] //BigEndian }; if (pkt.Length > 0) { pkt.Data = new byte[pkt.Length - 4]; socket.Receive(pkt.Data, pkt.Length - 4, SocketFlags.None); } return(pkt); }
public TPDU(TPKT tPKT) { var br = new BinaryReader(new MemoryStream(tPKT.Data)); HeaderLength = br.ReadByte(); if (HeaderLength >= 2) { PDUType = br.ReadByte(); if (PDUType == 0xf0) //DT Data { var flags = br.ReadByte(); TPDUNumber = flags & 0x7F; LastDataUnit = (flags & 0x80) > 0; Data = br.ReadBytes(tPKT.Length - HeaderLength - 4); //4 = TPKT Size return; } //TODO: Handle other PDUTypes } Data = new byte[0]; }
public TPDU(TPKT tPKT) { TPkt = tPKT; HeaderLength = tPKT.Data[0]; // Header length excluding this length byte if (HeaderLength >= 2) { PDUType = (PduType)tPKT.Data[1]; if (PDUType == PduType.Data) //DT Data { var flags = tPKT.Data[2]; TPDUNumber = flags & 0x7F; LastDataUnit = (flags & 0x80) > 0; Data = new byte[tPKT.Data.Length - HeaderLength - 1]; // substract header length byte + header length. Array.Copy(tPKT.Data, HeaderLength + 1, Data, 0, Data.Length); return; } //TODO: Handle other PDUTypes } Data = new byte[0]; }