Esempio n. 1
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);
        }
Esempio n. 2
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);
        }