/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ using System; using System.Net; using System.Collections.Generic; using System.IO; namespace OpenTraffic.Network { abstract class Packet { public IPEndPoint ip; protected UdpClient n; private int Type; public byte[] data; public int Sequence; long timer; public int bytesLeft; private const int HEADERSIZE = 16; public void resetTimer(long tick) { timer = tick; } public bool CheckResend() { return (timer < DateTime.Now.Ticks); } // FORMAT type seq totsize offset public bool serialize(int packetMaxSize, int packet, byte[] bytes, out int packetSize) { int dataSize = data == null ? 0 : data.Length; int maxPacketSize = Math.Min(dataSize + HEADERSIZE, packetMaxSize); int offset = packet * (packetMaxSize - HEADERSIZE); int dataPacketLoad = dataSize - offset; dataPacketLoad = Math.Min(maxPacketSize - HEADERSIZE, dataPacketLoad); Array.Copy(BitConverter.GetBytes((int)Type), bytes, 4); Array.Copy(BitConverter.GetBytes((int)Sequence), 0, bytes, 4, 4); Array.Copy(BitConverter.GetBytes((int)dataSize), 0, bytes, 8, 4); Array.Copy(BitConverter.GetBytes((int)offset), 0, bytes, 12, 4); //Array.Copy(BitConverter.GetBytes((int)dataPacketLoad), 0, bytes, 16, 4); if (data != null) Array.Copy(data, offset, bytes,HEADERSIZE, dataPacketLoad); packetSize = dataPacketLoad + HEADERSIZE; return (dataPacketLoad + offset < dataSize); } public Packet(UdpClient n, int type, IPEndPoint ip) { this.n = n; this.ip = ip; this.Type = type; data = null; Sequence = 0; } public virtual void DoAction() { Console.WriteLine("ACTION:PACKET"); } public virtual void DoAck() { } public static int GetSequence(byte[] bytes) { return BitConverter.ToInt32(bytes, 4); } public static int GetType(byte[] bytes) { return BitConverter.ToInt32(bytes, 0); } public void AddData(byte[] bytes, int size) { int offset = BitConverter.ToInt32(bytes, 12); Array.Copy(bytes, HEADERSIZE, data, offset, size - HEADERSIZE); bytesLeft -= size - HEADERSIZE; // System.Console.WriteLine(Sequence + ": Added data from " + offset + // " to " + (offset + (size - HEADERSIZE)) + " Bytes left" + bytesLeft); } public bool HasRecievedAllPacket() { return (bytesLeft == 0); } public void Deserialize() { this.Deserialize(data); } // FORMAT type seq totsize offset protected virtual void Deserialize(byte[] bytes) { } public override string ToString() { return "[port" + this.ip.Port + "| seq: " + Sequence + "| type: " + Type + "]"; } } }