예제 #1
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);
        }
예제 #2
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);
 }
예제 #3
0
        private void _heartbeatTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DataContent dataContent = new DataContent();

            dataContent.Data = new byte[0];  // send empty message
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #4
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            SmallFileDataObject dataObject = (SmallFileDataObject)dataContent.Data;

            // Convert an object to a byte array
            dataContent.Data = Util.ObjectByteConverter.ObjectToByteArray(dataObject);
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #5
0
 public void FromHighLayerToHere(DataContent dataContent)
 {
     if (_state.Enabled && dataContent.Data != null)
     {
         byte[] encrypted = Encrypt((byte[])dataContent.Data);
         dataContent.Data = encrypted;
     }
     NextLowLayerEvent?.Invoke(dataContent);
 }
예제 #6
0
        private void _timeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DataContent dataContent = new DataContent();

            dataContent.IsHeartbeatTimeout = true;
            _timeoutTimer.Stop();
            _heartbeatTimer.Stop();
            NextHighLayerEvent?.Invoke(dataContent);
        }
예제 #7
0
 // forwarding
 public void FromHighLayerToHere(DataContent dataContent)
 {
     // server relay the message
     if (dataContent.Data.GetType() == typeof(byte[]))
     {
         NextLowLayerEvent?.Invoke(dataContent);
     }
     // discard string message
 }
예제 #8
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            List <byte> header_data = new List <byte>();

            byte[] now = BitConverter.GetBytes(DateTime.Now.ToBinary());
            header_data.AddRange(now);
            header_data.AddRange((byte[])dataContent.Data);
            dataContent.Data = header_data.ToArray();
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #9
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     _timeoutTimer.Stop();
     _timeoutTimer.Start();
     if (((byte[])dataContent.Data)?.Length == 0)  // discard empty message
     {
         return;
     }
     NextHighLayerEvent?.Invoke(dataContent);
 }
예제 #10
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            byte[] typeHeader;
            typeHeader = BitConverter.GetBytes((int)dataContent.Type);
            List <byte> header_body = new List <byte>();

            header_body.AddRange(typeHeader);
            header_body.AddRange((byte[])dataContent.Data);
            dataContent.Data = header_body.ToArray();
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #11
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            byte[] seqHeader;
            seqHeader = BitConverter.GetBytes(_thisSeq.Value);
            List <byte> header_body = new List <byte>();

            header_body.AddRange(seqHeader);
            header_body.AddRange((byte[])dataContent.Data);
            dataContent.Data = header_body.ToArray();
            ++_thisSeq;
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #12
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            byte[] data   = (byte[])dataContent.Data;
            int    length = data.Length;

            byte[]      lengthByte  = BitConverter.GetBytes(length); // 4 Bytes
            List <byte> prefix_data = new List <byte>();

            prefix_data.AddRange(lengthByte);
            prefix_data.AddRange(data);
            dataContent.Data = prefix_data.ToArray();
            NextLowLayerEvent?.Invoke(dataContent);
        }
예제 #13
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);
        }
예제 #14
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);
 }
예제 #15
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            if (!dataContent.IsValid)
            {
                // WORKAROUND - For DEBUG
                Console.WriteLine("[Error][Disconnect]");
                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("> ");

                dataContent.SockMgr?.Shutdown();
                return;
            }
            NextHighLayerEvent?.Invoke(dataContent);
        }
예제 #16
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);
        }
예제 #17
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);
        }
예제 #18
0
        // hint: add necessary field here

        public object Clone()
        {
            DataContent dataContent = new DataContent();

            dataContent.SockController        = this.SockController;
            dataContent.SockMgr               = this.SockMgr;
            dataContent.Data                  = (ICloneable)this.Data?.Clone();
            dataContent.IsShutdown            = this.IsShutdown;
            dataContent.Type                  = this.Type;
            dataContent.AesKey                = (byte[])AesKey?.Clone();
            dataContent.IsAesError            = this.IsAesError;
            dataContent.IsAckWrong            = this.IsAckWrong;
            dataContent.TransportState        = (TransportState)this.TransportState.Clone();
            dataContent.IsHeartbeatTimeout    = this.IsHeartbeatTimeout;
            dataContent.IsTimestampWrong      = this.IsTimestampWrong;
            dataContent.IsTypeWrong           = this.IsTypeWrong;
            dataContent.ExternalCallback      = this.ExternalCallback;
            dataContent.ExternalCallbackState = this.ExternalCallbackState;
            return(dataContent);
        }
예제 #19
0
        public void FromLowLayerToHere(DataContent dataContent)
        {
            if (!dataContent.IsValid)
            {
                if (dataContent.SockMgr.GetSockBase().Role == SocketRole.Listener)
                {
                    // 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);
        }
예제 #20
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);
        }
예제 #21
0
 public void FromLowLayerToHere(DataContent dataContent)
 {
     // block invalid dataContent
     if (dataContent.Data == null)
     {
         return;
     }
     // server start broadcasting
     foreach (SockMgr client in _state.SockController.GetSockList().Clients)
     {
         // don't re-sent to source
         if (client == _state.SockMgr)
         {
             continue;
         }
         // find the same class from other peer sockets and activate them
         ProtocolStackState peerStackState = client.GetProtocolStack().GetState();
         if (typeof(BroadcastProtocol) == peerStackState.MiddleProtocols[0]?.GetType())
         {
             peerStackState.MiddleProtocols[0].FromHighLayerToHere((SocketApp.Protocol.DataContent)dataContent.Clone());
         }
     }
 }
예제 #22
0
 private static int GetTypeIndex(DataContent dataContent)
 {
     return((int)dataContent.Type - 1);
 }
예제 #23
0
 public void FromHighLayerToHere(DataContent dataContent)
 {
     _heartbeatTimer.Stop();
     NextLowLayerEvent?.Invoke(dataContent);
     _heartbeatTimer.Start();
 }
예제 #24
0
 public void FromLowLayerToHere(DataContent data)
 {
     _state.MiddleProtocols.Last().FromLowLayerToHere(data);
 }
예제 #25
0
 public void FromHighLayerToHere(DataContent data)
 {
     _state.MiddleProtocols.First().FromHighLayerToHere(data);
 }
예제 #26
0
 private void OnNextHighLayerEvent(DataContent data)
 {
     NextHighLayerEvent?.Invoke(data);
 }
예제 #27
0
 protected abstract int FromLowLayerToHere_IndexSelection(DataContent dataContent);
예제 #28
0
 private void Branch_NextHighLayerEvent(DataContent dataContent)
 {
     NextHighLayerEvent?.Invoke(dataContent);
 }
예제 #29
0
 protected override int FromHighLayerToHere_IndexSelection(DataContent dataContent)
 {
     return(GetTypeIndex(dataContent));
 }
예제 #30
0
 public void FromHighLayerToHere(DataContent dataContent)
 {
     byte[] data = Encoding.UTF8.GetBytes((string)dataContent.Data);
     dataContent.Data = data;
     NextLowLayerEvent?.Invoke(dataContent);
 }