Пример #1
0
        public void SendSynAck()
        {
            var rph   = RPH.CreateSynAckPacket();
            var dgram = rph.Serialize();

            SetChecksum(ref dgram);
            _udpClient.Send(dgram, dgram.Length, _sender);
            _currentState = State.ReceivePacket;
        }
Пример #2
0
        public void SetChecksum(ref byte[] packet)
        {
            RPH rph = RPH.Deserialize(packet);
            var md5 = MD5.Create();

            rph._checksum = BitConverter.ToInt32(md5.ComputeHash(packet), 0);
            byte[] newHead = rph.Serialize();
            Array.Copy(newHead, packet, newHead.Length);
        }
Пример #3
0
        public bool IsCorrupt(RPH rph)
        {
            int checksum = rph._checksum;

            rph._checksum = 0;
            byte[] packet       = rph.Serialize();
            var    md5          = MD5.Create();
            int    calcChecksum = BitConverter.ToInt32(md5.ComputeHash(packet), 0);

            return(checksum != calcChecksum);
        }
Пример #4
0
        public static RPH CreateAckPacket(int ackNr, int seqNr)
        {
            RPH rph = new RPH
            {
                _seqNr      = 0,
                _ackNr      = ackNr,
                _fileSize   = 0,
                _syn        = false,
                _ack        = true,
                _fileEnding = ""
            };

            return(rph);
        }
Пример #5
0
        public static RPH CreateNAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr      = 0,
                _ackNr      = 0,
                _fileSize   = 0,
                _syn        = false,
                _ack        = false,
                _fileEnding = ""
            };

            return(rph);
        }
Пример #6
0
        public static RPH CreateSynPacket(int fileSize)
        {
            RPH rph = new RPH
            {
                _seqNr      = 0,
                _ackNr      = 0,
                _fileSize   = fileSize,
                _syn        = true,
                _ack        = false,
                _fileEnding = ""
            };

            return(rph);
        }
Пример #7
0
        public static RPH CreateNAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = 0,
                _fileSize = 0,
                _syn = false,
                _ack = false,
                _fileEnding = ""
            };

            return rph;
        }
Пример #8
0
        public static RPH CreateAckPacket(int ackNr, int seqNr)
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = ackNr,
                _fileSize = 0,
                _syn = false,
                _ack = true,
                _fileEnding = ""
            };

            return rph;
        }
Пример #9
0
        public static RPH CreateSynAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr      = 0,
                _ackNr      = 1,
                _fileSize   = 0,
                _syn        = true,
                _ack        = true,
                _fileEnding = ""
            };

            return(rph);
        }
Пример #10
0
        public bool IsCorrupt(byte[] packet)
        {
            var copyPacket = new byte[packet.Length];

            Array.Copy(packet, copyPacket, packet.Length);
            var recvRph      = RPH.Deserialize(packet);
            int recvChecksum = recvRph._checksum;

            recvRph._checksum = 0;
            byte[] newHead = recvRph.Serialize();
            Array.Copy(newHead, copyPacket, newHead.Length);
            var md5          = MD5.Create();
            int calcChecksum = BitConverter.ToInt32(md5.ComputeHash(copyPacket), 0);

            return(calcChecksum != recvChecksum);
        }
Пример #11
0
        public bool Init()
        {
            _acknowledgedBytes = 0;
            _fileEnding        = "";
            _fileRecv          = false;
            NrPackets          = 0;
            DisplayServerInformation();
            Console.WriteLine("Warte auf Verbindung von Clienten...");

            _sender = new IPEndPoint(IPAddress.Any, 0);
            IPEndPoint endAddress = new IPEndPoint(IPAddress.Any, ListenPort);

            try
            {
                if (_udpClient == null)
                {
                    _udpClient = new UdpClient(endAddress);
                }


                byte[] dgram = _udpClient.Receive(ref _sender);
                RPH    rph   = RPH.Deserialize(dgram);

                if (IsCorrupt(rph))
                {
                    // send nak
                    return(false);
                }
                else
                {
                    _currentState = State.SendSynAck;
                }

                _fileEnding    = rph._fileEnding;
                _payloadBuffer = new byte[rph._fileSize];

                return(true);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Пример #12
0
        static public RPH Deserialize(byte[] header)
        {
            var myPacket = new RPH();

            using (var m = new MemoryStream(header))
            {
                using (var reader = new BinaryReader(m))
                {
                    myPacket._checksum   = reader.ReadInt32();
                    myPacket._seqNr      = reader.ReadInt32();
                    myPacket._ackNr      = reader.ReadInt32();
                    myPacket._fileSize   = reader.ReadInt32();
                    myPacket._fileEnding = reader.ReadString();
                    myPacket._syn        = reader.ReadBoolean();
                    myPacket._ack        = reader.ReadBoolean();
                }
                return(myPacket);
            }
        }
Пример #13
0
        public void AckPacket(byte[] buf)
        {
            // Deserialize our protocol
            var rph = RPH.Deserialize(buf);

            // Calculate the size of our protocol
            int headSize = rph.GetByteLength();

            // if received packet is a retransmission or corrupt or packet reordering took place
            if (IsCorrupt(buf) || _acknowledgedBytes != rph._seqNr)
            {
                // Send NAck
                Console.WriteLine("Packet was corrupt or it was a retransmission");
                var ack   = RPH.CreateNAckPacket();
                var dgram = ack.Serialize();
                SetChecksum(ref dgram);
                _udpClient.Send(dgram, dgram.Length, _sender);
            }
            else if (!IsCorrupt(buf) && rph._seqNr == _acknowledgedBytes)
            {
                _acknowledgedBytes = rph._seqNr + buf.Length - headSize;
                // Copy received bytes into receive buffer
                Array.Copy(buf, headSize, _payloadBuffer, rph._seqNr, buf.Length - headSize);
                var ack   = RPH.CreateAckPacket(_acknowledgedBytes, 0);
                var dgram = ack.Serialize();
                SetChecksum(ref dgram);
                _udpClient.Send(dgram, dgram.Length, _sender);

                if (_payloadBuffer.Length == _acknowledgedBytes)
                {
                    _currentState = State.SaveToFile;
                }
            }
            else
            {
                Console.WriteLine("Not defined");
            }
            NrPackets++;
//             Console.WriteLine(NrPackets + ".Paket");
//             Console.WriteLine("Sequenznummer: " + rph._seqNr);
//             Console.WriteLine("AckNr: " + _acknowledgedBytes);
        }
Пример #14
0
        public static RPH CreateSynAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = 1,
                _fileSize = 0,
                _syn = true,
                _ack = true,
                _fileEnding = ""
            };

            return rph;
        }
Пример #15
0
 public bool IsSyn(RPH rph)
 {
     return(!IsCorrupt(rph) && rph._syn);
 }
Пример #16
0
        public static int GetPacketByteLength()
        {
            RPH rph = new RPH();

            return(rph.GetByteLength());
        }
Пример #17
0
 public static int GetPacketByteLength()
 {
     RPH rph = new RPH();
     return rph.GetByteLength();
 }
Пример #18
0
 public static RPH Deserialize(byte[] header)
 {
     var myPacket = new RPH();
     using (var m = new MemoryStream(header))
     {
         using (var reader = new BinaryReader(m))
         {
             myPacket._checksum = reader.ReadInt32();
             myPacket._seqNr = reader.ReadInt32();
             myPacket._ackNr = reader.ReadInt32();
             myPacket._fileSize = reader.ReadInt32();
             myPacket._fileEnding = reader.ReadString();
             myPacket._syn = reader.ReadBoolean();
             myPacket._ack = reader.ReadBoolean();
         }
         return myPacket;
     }
 }
Пример #19
0
        public static RPH CreateSynPacket(int fileSize)
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = 0,
                _fileSize = fileSize,
                _syn = true,
                _ack = false,
                _fileEnding = ""
            };

            return rph;
        }
Пример #20
0
 public bool IsSyn(RPH rph)
 {
     return !IsCorrupt(rph) && rph._syn;
 }
Пример #21
0
 public bool IsCorrupt(RPH rph)
 {
     int checksum = rph._checksum;
     rph._checksum = 0;
     byte[] packet = rph.Serialize();
     var md5 = MD5.Create();
     int calcChecksum = BitConverter.ToInt32(md5.ComputeHash(packet), 0);
     return checksum != calcChecksum;
 }