public void WaitForSynAck()
        {
            EndPoint sender = (EndPoint)_endPoint;
            var      ackn   = new byte[RPH.GetPacketByteLength()];

            // set the timeout for receiving acks (retransmission timeout)
            _sendingSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout);
            try
            {
                _sendingSocket.ReceiveFrom(ackn, ref sender);

                var rph = RPH.Deserialize(ackn);
                if (IsCorrupt(rph) || !IsSynAck(rph))
                {
                    _currentState = State.EstablishConnection;
                    return;
                }
                else
                {
                    Console.WriteLine("Connection Established");
                    _currentState = State.SendPacket;
                }
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.TimedOut)
                {
                    Console.WriteLine(e.ToString());
                    Console.WriteLine("No Connection could be established");
                }
                _currentState = State.EstablishConnection;
            }
        }
        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);
        }
        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);
        }
        private void WaitForAck()
        {
            EndPoint sender = (EndPoint)_endPoint;
            var      ackn   = new byte[RPH.GetPacketByteLength()];

            _tries++;

            if (MaxRetransTries < _tries)
            {
                _currentState = State.FatalError;
                return;
            }

            try
            {
                _sendingSocket.ReceiveFrom(ackn, ref sender);

                var rph = RPH.Deserialize(ackn);

                // Ack is corrupt
                if (IsCorrupt(rph, ackn) || !IsAck(rph))
                {
                    // If Ack is corrupt resend Packet
                    Console.WriteLine("Corrupt or NAck: Resend Packet");
                }
                else if (IsAck(rph) && !IsCorrupt(rph) && rph._ackNr != _localSeqNr)
                {
                    _tries = 0;
                    Debug.Assert(rph != null, "rph != null");
                    _localSeqNr  += _filechunk.Length;
                    _currentState = State.SendPacket;
                }
                else
                {
                    Console.WriteLine("Not defined");
                }
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.TimedOut)
                {
                    // Timeout: Resend Packet
                    Console.WriteLine("Timeout: Resend Packet");
                    SendOnePacket();
                }
                else
                {
                    Console.WriteLine("Fatal Error");
                    _currentState = State.FatalError;
                    return;
                }
            }
        }
Esempio n. 5
0
        public static RPH CreateSynAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr    = 0,
                _ackNr    = 1,
                _fileSize = 0,
                _syn      = true,
                _ack      = true
            };

            return(rph);
        }
Esempio n. 6
0
        public static RPH CreateSynAckPacket()
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = 1,
                _fileSize = 0,
                _syn = true,
                _ack = true
            };

            return rph;
        }
Esempio n. 7
0
        public static RPH CreatePacketHeader(int seqNr, int ackNr)
        {
            RPH rph = new RPH
            {
                _seqNr = seqNr,
                _ackNr = ackNr,
                _syn = false,
                _fileSize = 0,
                _ack = true
            };

            return rph;
        }
Esempio n. 8
0
        public static RPH CreatePacketHeader(int seqNr, int ackNr)
        {
            RPH rph = new RPH
            {
                _seqNr    = seqNr,
                _ackNr    = ackNr,
                _syn      = false,
                _fileSize = 0,
                _ack      = true
            };

            return(rph);
        }
Esempio n. 9
0
        public static RPH CreateSynPacket(int fileSize, string fileEnding)
        {
            RPH rph = new RPH
            {
                _seqNr      = 0,
                _ackNr      = 0,
                _fileSize   = fileSize,
                _syn        = true,
                _ack        = false,
                _fileEnding = fileEnding
            };

            return(rph);
        }
        public bool IsCorrupt(RPH rph, 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);
        }
Esempio n. 11
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);
            }
        }
        public void SendSyn(int filelength, string fileEnding)
        {
            // Establish the connection
            RPH syn = RPH.CreateSynPacket(filelength, fileEnding);

            byte[] dgram = syn.Serialize();
            SetChecksum(ref dgram);
            try
            {
                _sendingSocket?.SendTo(dgram, _endPoint);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.ToString());
                _tries++;
                if (_tries > MaxConTries)
                {
                    return;
                }
                SendSyn(filelength, fileEnding);
                _tries = 0;
            }
        }
        private void SendOnePacket()
        {
            int headSize = RPH.GetPacketByteLength();

            byte[] packetBuf = null;

            if (!IsLastPacket(PacketSizeBytes - headSize))
            {
                _filechunk = FileToChunk(_fileData, _localSeqNr, PacketSizeBytes - headSize);
                packetBuf  = new byte[PacketSizeBytes];
            }
            else
            {
                _filechunk = FileToChunk(_fileData, _localSeqNr, _fileData.Length - _localSeqNr);
                packetBuf  = new byte[_filechunk.Length + headSize];
                _fileSend  = true;
            }

            var rph = RPH.CreatePacketHeader(_localSeqNr, 0);

            byte[] header = rph.Serialize();

            // Copy together one packet: header with payload
            Array.Copy(header, packetBuf, headSize);
            Array.Copy(_filechunk, 0, packetBuf, headSize, _filechunk.Length);

            // Last set the Checksum of the packet
            SetChecksum(ref packetBuf);

            // Now Send the packet
            _sendingSocket.SendTo(packetBuf, _endPoint);

            Console.WriteLine("Paket versendet");

            // Change State
            _currentState = State.WaitForAck;
        }
Esempio n. 14
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;
     }
 }
Esempio n. 15
0
        public static RPH CreateSynPacket(int fileSize, string fileEnding)
        {
            RPH rph = new RPH
            {
                _seqNr = 0,
                _ackNr = 0,
                _fileSize = fileSize,
                _syn = true,
                _ack = false,
                _fileEnding = fileEnding
            };

            return rph;
        }
Esempio n. 16
0
        public static int GetPacketByteLength()
        {
            RPH rph = new RPH();

            return(rph.GetByteLength());
        }
Esempio n. 17
0
 public bool IsSynAck(RPH rph)
 {
     return rph._ack && rph._syn && rph._ackNr == 1;
 }
Esempio n. 18
0
        public bool IsCorrupt(RPH rph, 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;
        }
Esempio n. 19
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;
 }
Esempio n. 20
0
 public bool IsAck(RPH rph)
 {
     return  rph._ack && !rph._syn;
 }
 public bool IsSynAck(RPH rph)
 {
     return(rph._ack && rph._syn && rph._ackNr == 1);
 }
Esempio n. 22
0
 public static int GetPacketByteLength()
 {
     RPH rph = new RPH();
     return rph.GetByteLength();
 }
 public bool IsAck(RPH rph)
 {
     return(rph._ack && !rph._syn);
 }