/// 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 ping packet for the protocol defined
/// and implemented by the UdpMediaClientSocket. The packet
/// consist of a packet type identifier, a sender GUID and
/// a Int64 sequence number.
///
public class PingPacket : INetworkPacket
{
///
/// The specified length of the packet header in octects.
///
public const int HeaderLengthInOctets = 25;
///
/// The specified id of the packet type. It is used to identify a packet.
///
public const byte PacketId = 3;
private const int packetIdHeaderIndex = 0;
private const int sourceGuidHeaderIndex = 1;
private const int sequenceHeaderIndex = 17;
private byte[] sourceGuid;
///
/// String represenstation of the Guid identifying the sender.
///
public string SourceGuid
{
get { return (sourceGuid == null || sourceGuid.Length != 16) ? "" : new Guid(sourceGuid).ToString(); }
}
private Int64 sequence;
///
/// The function sets and gets the packet sequence number.
///
public Int64 Sequence
{
get { return sequence; }
set { sequence = value; }
}
///
/// The function to initializes a new ping packet with no information.
///
public PingPacket() { }
///
/// The function initializes a new ping packet that has the provided sequence number
/// and sender GUID.
///
/// Guid of the sender.
/// Packet sequence number.
public PingPacket(string guid, Int64 sequenceNumber) : this(guid)
{
sequence = sequenceNumber;
}
///
/// The function initializes a new ping packet that has the provided sender GUID.
///
/// The guid of the sender.
public PingPacket(string guid)
{
Guid userGuid;
if (!Guid.TryParse(guid, out userGuid))
throw new ArgumentException("Guid format error");
sourceGuid = userGuid.ToByteArray();
}
///
/// The function saves the given sequence number and reurns the data of the given sequence.
///
/// The sequence to retrive.
/// The data of given the sequence.
public byte[] GetBytes(long sequenceNumber)
{
sequence = sequenceNumber;
return GetBytes();
}
///
/// The function gets the guid and the sequence from the data 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);
}
///
/// The function returns the data of the given sequence.
///
/// The packet data.
public byte[] GetBytes()
{
byte[] networkPacketData = new byte[HeaderLengthInOctets];
networkPacketData[packetIdHeaderIndex] = PacketId;
Array.Copy(sourceGuid, 0, networkPacketData, sourceGuidHeaderIndex, sourceGuid.Length);
byte[] sequenceBytes = BitConverter.GetBytes(sequence);
Array.Copy(sequenceBytes, 0, networkPacketData, sequenceHeaderIndex, sequenceBytes.Length);
return networkPacketData;
}
}
}