/// Version 1.0.0 /// Last modified 23.5.2014 /// /// Copyright (C) 2014 Veli-Mikko Puupponen /// /// Halyri-system is a prototype emergency call system. Its purpose is to /// demonstrate the use of the advanced capabilities available in the current /// generation smartphones in facilitating the emergency service dispatcher's /// capability to determine the nature of the emergency and to dispatch help. /// /// For more information, see the README file of this package. /// /// The MIT License (MIT) /// /// Permission is hereby granted, free of charge, to any person obtaining a copy /// of this software and associated documentation files (the "Software"), to /// deal in the Software without restriction, including without limitation the /// rights to use, copy, modify, merge, publish, distribute, sublicense, /// and/or sell copies of the Software, and to permit persons to whom the /// Software is furnished to do so, subject to the following conditions: /// /// The above copyright notice and this permission notice shall be included in /// all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING /// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS /// IN THE SOFTWARE. /// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SimpleUdpMediaClient.Packets { /// Veli-Mikko Puupponen /// /// The class represents a subsequent portion of MediaPacket. /// MediaContinuationPackets are used to transfer the data /// that remains after the data worth first MTU is taken from /// the MediaPacket and costructed into a MediaHeaderPacket. /// The combination of a MediaHeaderPacket and the subsequent /// MediaContinuationPackets is a transfer sequence. /// /// This packet consist of a packet type identifier, sender GUID, /// a Int64 sequence number, packet count and packet number in the /// transfer sequence. /// /// This packet must always have payload data. /// public class MediaContinuationPacket : INetworkPacket { /// /// The specified length of the packet header in octets. /// public const int HeaderLengthInOctets = 33; /// /// The specified id of the packet type. It is used to identify a packet. /// public const byte PacketId = 1; private const int packetIdHeaderIndex = 0; private const int sourceGuidHeaderIndex = 1; private const int sequenceHeaderIndex = 17; private const int totalPacketCountHeaderIndex = 25; private const int PacketNumberHeaderIndex = 29; private byte[] sourceGuid; /// /// The function to get the Guid identifying the sender. /// public string SourceGuid { get { return (sourceGuid == null || sourceGuid.Length != 16) ? "" : new Guid(sourceGuid).ToString(); } } private Int64 sequence; /// /// The function to get and set the packet sequence number. /// public Int64 Sequence { get { return sequence; } set { sequence = value; } } private Int32 totalPackets; /// /// The function to get the total number of packets in the related transfer sequence. /// public Int32 TotalPacketCount { get { return totalPackets; } } private Int32 packetNumber; /// /// The function to get the number of this packet in the related transfer sequence. /// public Int32 PacketNumber { get { return packetNumber; } } private byte[] payloadData; /// /// The function to get the payload data. /// public byte[] PayloadData { get { return payloadData; } } /// /// The function initializes a new MediaContinuationPacket with no /// parameters. /// public MediaContinuationPacket() { } /// /// The function initializes a new MediaContinuationPacket instance that has the /// specified source Guid, packet order number, packet count, payload data /// and sequence number. /// /// The first continuation packet in a network level transfer sequence always /// has packetOrderNumber = 1. /// /// The string representation of the sender's Guid. /// The number of the this packet in the transfer sequence. /// The total count of packets in the transfer sequence. /// The payload data for this packet. /// The sequence number for this packet. Must not /// vary within a transfer sequence. public MediaContinuationPacket(byte[] guid, Int32 packetOrderNumber, Int32 packetCount, byte[] payload, Int64 sequenceNumber) : this(guid, packetOrderNumber, packetCount, payload) { sequence = sequenceNumber; } /// /// The function initializes a new MediaContinuationPacket instance that has the /// specified source Guid, packet order number, packet count and payload data. /// The sequence number must be provided prior any calls to the parameterless GetBytes. /// /// The first continuation packet in a network level transfer sequence always /// has packetOrderNumber = 1. /// /// The string representation of the sender's Guid. /// The number of the this packet in the transfer sequence. /// The total count of packets in the transfer sequence. /// The payload data for this packet. public MediaContinuationPacket(byte[] guid, Int32 packetOrderNumber, Int32 packetCount, byte[] payload) { sourceGuid = guid; totalPackets = packetCount; packetNumber = packetOrderNumber; payloadData = payload; } /// /// The function saves the given sequence number and returns the data header of the currently saved sequence. /// /// The data header of current the sequence. public byte[] GetBytes(long sequenceNumber) { sequence = sequenceNumber; return GetBytes(); } /// /// The function gets the sequence, the total amount of packets, the packet number and /// the target guid from the header of the given data and saves them. /// /// The given data. public void FromBytes(byte[] packetBytes) { if (packetBytes.Length < HeaderLengthInOctets) throw new ArgumentException("Data length insufficient for headers."); if (packetBytes[packetIdHeaderIndex] != PacketId) throw new ArgumentException("Incompatible packet type."); byte[] guidBytes = new byte[sequenceHeaderIndex - sourceGuidHeaderIndex]; Array.Copy(packetBytes, sourceGuidHeaderIndex, guidBytes, 0, guidBytes.Length); sourceGuid = guidBytes; sequence = BitConverter.ToInt64(packetBytes, sequenceHeaderIndex); totalPackets = BitConverter.ToInt32(packetBytes, totalPacketCountHeaderIndex); packetNumber = BitConverter.ToInt32(packetBytes, PacketNumberHeaderIndex); if (packetBytes.Length > HeaderLengthInOctets) { payloadData = new byte[packetBytes.Length - HeaderLengthInOctets]; Array.Copy(packetBytes, HeaderLengthInOctets, payloadData, 0, payloadData.Length); } } /// /// The function returns the data header of the currently saved sequence. /// /// The data header of current the sequence. public byte[] GetBytes() { int payloadLength = (payloadData == null) ? 0 : payloadData.Length; byte[] networkPacketData = new byte[HeaderLengthInOctets + payloadLength]; networkPacketData[packetIdHeaderIndex] = PacketId; Array.Copy(sourceGuid, 0, networkPacketData, sourceGuidHeaderIndex, sourceGuid.Length); byte[] sequenceBytes = BitConverter.GetBytes(sequence); Array.Copy(sequenceBytes, 0, networkPacketData, sequenceHeaderIndex, sequenceBytes.Length); byte[] totalPacketsBytes = BitConverter.GetBytes(totalPackets); Array.Copy(totalPacketsBytes, 0, networkPacketData, totalPacketCountHeaderIndex, totalPacketsBytes.Length); byte[] packetNumberBytes = BitConverter.GetBytes(packetNumber); Array.Copy(packetNumberBytes, 0, networkPacketData, PacketNumberHeaderIndex, packetNumberBytes.Length); if (payloadLength > 0) Array.Copy(payloadData, 0, networkPacketData, HeaderLengthInOctets, payloadData.Length); return networkPacketData; } } }