Exemplo n.º 1
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     if (dataContent.Data == null)
     {
         NextHighLayerEvent?.Invoke(dataContent);
         return;
     }
     try
     {
         byte[] timestampBytes = ((byte[])dataContent.Data).Take(8).ToArray();
         byte[] data           = ((byte[])dataContent.Data).Skip(8).ToArray();
         long   timestampLong  = BitConverter.ToInt64(timestampBytes);
         dataContent.Data = data;
         DateTime timestamp = DateTime.FromBinary(timestampLong);
         // flexible expire time for different lengths
         double expireInterval = ((byte[])dataContent.Data).Length / _expireSpeedBytePerSec + _extraTolerationInSec;
         if (expireInterval < (DateTime.Now - timestamp).TotalSeconds)
         {
             dataContent.IsTimestampWrong = true;
         }
     }
     catch (Exception)
     {
         dataContent.IsTimestampWrong = true;
     }
     NextHighLayerEvent?.Invoke(dataContent);
 }
Exemplo n.º 2
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            byte[] typeHeader;
            byte[] body;
            // TCP segment has not been sufficiently collected
            if ((byte[])dataContent.Data == null)
            {
                dataContent.Type = DataProtocolType.Management;
                NextHighLayerEvent?.Invoke(dataContent);
                return;
            }
            // invalid header
            if (((byte[])dataContent.Data).Length < 4)
            {
                dataContent.IsTypeWrong = true;
                NextHighLayerEvent?.Invoke(dataContent);
                return;
            }
            // first 4 bytes is the type header
            typeHeader = ((byte[])dataContent.Data).Take(4).ToArray();
            body       = ((byte[])dataContent.Data).Skip(4).ToArray();
            int typeIndex = BitConverter.ToInt32(typeHeader);

            dataContent.Data = body;
            if (typeIndex >= (int)DataProtocolType.MaxInvalid || typeIndex <= (int)DataProtocolType.Undefined)
            {
                dataContent.IsTypeWrong = true;
                NextHighLayerEvent?.Invoke(dataContent);
                return;  // report if out of range  // it might due to falsely decryption on AES layer
            }
            dataContent.Type = (DataProtocolType)typeIndex;
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 3
0
        private void _timeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DataContent dataContent = new DataContent();

            dataContent.IsHeartbeatTimeout = true;
            _timeoutTimer.Stop();
            _heartbeatTimer.Stop();
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 4
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     _timeoutTimer.Stop();
     _timeoutTimer.Start();
     if (((byte[])dataContent.Data)?.Length == 0)  // discard empty message
     {
         return;
     }
     NextHighLayerEvent?.Invoke(dataContent);
 }
Exemplo n.º 5
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            if (dataContent.Data == null)
            {
                dataContent.Data = "<Unintelligible>";
                NextHighLayerEvent?.Invoke(dataContent);
                return;
            }
            string data = Encoding.UTF8.GetString((byte[])dataContent.Data);

            dataContent.Data = data;
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 6
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            if (!dataContent.IsValid)
            {
                // WORKAROUND - For DEBUG
                Console.WriteLine("[Error][Block]");
                Console.WriteLine($"Ack Wrong {dataContent.IsAckWrong}");
                Console.WriteLine($"AES Error {dataContent.IsAesError}");
                Console.WriteLine($"Heartbeat Timeout {dataContent.IsHeartbeatTimeout}");
                Console.WriteLine($"Timestamp Wrong {dataContent.IsTimestampWrong}");
                Console.WriteLine($"IsTypeWrong {dataContent.IsTypeWrong}");
                Console.Write("> ");

                return;
            }
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 7
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     if (_state.Enabled && dataContent.Data != null)
     {
         try
         {
             byte[] decrypted;
             decrypted        = Decrypt((byte[])dataContent.Data);
             dataContent.Data = decrypted;
         }
         catch (CryptographicException)
         {
             dataContent.IsAesError = true;
         }
     }
     NextHighLayerEvent?.Invoke(dataContent);
 }
Exemplo n.º 8
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            TransportState state = dataContent.TransportState;

            // not receiving enough (this is receiver)
            if (dataContent.Data == null)
            {
                //  reset
                if (state.PendingLength == 0)
                {
                    _prevReceivedLength = state.ReceivedLength;
                    _prevTime           = DateTime.Now;
                    _prevSpeed          = 0;
                    return;
                }
                //  count speed
                if (DateTime.Now == _prevTime)
                {
                    state.Speed = double.MaxValue;
                }
                else
                {
                    state.Speed = (1 - alpha) * _prevSpeed + alpha * (state.ReceivedLength - _prevReceivedLength) / 1024 / (DateTime.Now - _prevTime).TotalSeconds;
                }
                //  update
                _prevSpeed          = state.Speed;
                _prevReceivedLength = state.ReceivedLength;
                _prevTime           = DateTime.Now;
                // write state in dataContent
                Byte[] data = Util.ObjectByteConverter.ObjectToByteArray(state);
                dataContent.Data = data;
                // tell peer about the transport process
                NextLowLayerEvent?.Invoke((DataContent)dataContent.Clone());
            }
            // still sending (this is sender)
            else
            {
                // get tranport process info from receiver
                dataContent.TransportState = (TransportState)Util.ObjectByteConverter.ByteArrayToObject((byte[])dataContent.Data);
            }
            // tell this app
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 9
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            _bufferMgr.AddBytes((byte[])dataContent.Data, ((byte[])dataContent.Data).Length);
            dataContent.Data = null;

            byte[] data = _bufferMgr.GetAdequateBytes();
            while (data.Length > 0)
            {
                DataContent newDataContent = (DataContent)dataContent.Clone();
                newDataContent.Data = data;
                NextHighLayerEvent?.Invoke(newDataContent);

                data = _bufferMgr.GetAdequateBytes();
            }

            dataContent.TransportState.PendingLength  = _bufferMgr.GetPendingLength();
            dataContent.TransportState.ReceivedLength = _bufferMgr.GetReceivedLength();
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 10
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            byte[] seqHeader;
            byte[] body;
            int    seq;

            // it is a state only packet
            if (dataContent.Data == null)
            {
                NextHighLayerEvent?.Invoke(dataContent);
                return;
            }
            try
            {
                // parse header
                seqHeader = ((byte[])dataContent.Data).Take(4).ToArray();
                body      = ((byte[])dataContent.Data).Skip(4).ToArray();
                seq       = BitConverter.ToInt32(seqHeader);
                // remove header from dataContent
                dataContent.Data = body;
                // init
                if (_receiveWindow == null)
                {
                    _receiveWindow = new SlidingWindow(seq, _windowSize);
                }
                // check validation
                if (!_receiveWindow.IsValid(seq))
                {
                    dataContent.IsAckWrong = true;
                }
                // update
                _receiveWindow.Update(seq);
            }
            catch (Exception)
            {
                dataContent.IsAckWrong = true;
            }
            NextHighLayerEvent?.Invoke(dataContent);
        }
Exemplo n.º 11
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     // Convert a byte array to an Object
     dataContent.Data = (ICloneable)Util.ObjectByteConverter.ByteArrayToObject((byte[])dataContent.Data);
     NextHighLayerEvent?.Invoke(dataContent);
 }
Exemplo n.º 12
0
 private void OnNextHighLayerEvent(DataContent data)
 {
     NextHighLayerEvent?.Invoke(data);
 }
Exemplo n.º 13
0
 private void Branch_NextHighLayerEvent(DataContent dataContent)
 {
     NextHighLayerEvent?.Invoke(dataContent);
 }
Exemplo n.º 14
0
 private void OnNextHighLayerEvent_stackForClient(DataContent dataContent)
 {
     NextHighLayerEvent?.Invoke(dataContent);
 }