Exemplo n.º 1
0
        public void AssertThatRangeCanBeRemoved()
        {
            var oneArray = new int[5];

            for (var i = 0; i < oneArray.Length; i++)
            {
                oneArray[i] = 1;
            }

            var twoArray = new int[5];

            for (var i = 0; i < twoArray.Length; i++)
            {
                twoArray[i] = 2;
            }

            _fastList.AddRange(twoArray);
            _fastList.AddRange(oneArray);
            _fastList.AddRange(twoArray);

            _fastList.RemoveRange(5, 5);

            Assert.AreEqual(10, _fastList.Length);

            for (var i = 0; i < 10; i++)
            {
                Assert.AreEqual(2, _fastList.buffer[i]);
            }
        }
Exemplo n.º 2
0
        public void Sort(FastList <BlittableJsonDocumentBuilder.PropertyTag> properties)
        {
            var index = GetPropertiesHashedIndex(properties);

            // Sort object properties metadata by property names
            if (_propertiesNeedSorting)
            {
                UpdatePropertiesSortOrder();
            }

            var cachedSort = _cachedSorts[index];

            if (cachedSort?.Sorting.Count != properties.Count)
            {
                UnlikelySortProperties(properties);
                return;
            }

            // we are frequently going to see documents with ids in the same order
            // so we can take advantage of that by remember the previous sort, we
            // check if the values are the same, and if so, save the sort

            var sortingList = cachedSort.Sorting;

            for (int i = 0; i < properties.Count; i++)
            {
                var sortingProp = sortingList[i];
                var sortedProp  = properties[i];

                if (sortingProp.Property.Equals(sortedProp.Property))
                {
                    sortingProp.Tmp = sortedProp;
                }
                else
                {
                    UnlikelySortProperties(properties);
                    return;
                }
            }

            // ReSharper disable once ForCanBeConvertedToForeach
            int sortingListCount = sortingList.Count;

            for (int i = 0; i < sortingListCount; i++)
            {
                var sortingProp = sortingList[i];
                properties[sortingProp.SortedPosition] = sortingProp.Tmp;
            }

            int finalCount = cachedSort.FinalCount;

            if (properties.Count != finalCount)
            {
                properties.RemoveRange(finalCount, properties.Count - finalCount);
            }
        }
Exemplo n.º 3
0
        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort port = (SerialPort)(sender);
            {
                int count = port.Read(recvBuffer, 0, 128);

                for (int i = 0; i < count; i++)
                {
                    dataBuffer.Add(recvBuffer[i]);
                }
            }


            int numPassed = 0;

            for (int i = 0; i < dataBuffer.Count; i++)
            {
                if (dataBuffer[i] == PackHeader &&
                    dataBuffer[i + 1] == (byte)PackType.Recv)
                {
                    if (i < dataBuffer.Count - sizeof(DataRecvPack))
                    {
                        DataRecvPack pack;
                        pack.Header   = dataBuffer[i];
                        pack.Type     = (PackType)dataBuffer[i + 1];
                        pack.Velocity = dataBuffer[i + 2];
                        pack.Angle    = dataBuffer[i + 3];
                        pack.Btn1     = dataBuffer[i + 4];
                        pack.Btn2     = dataBuffer[i + 5];

                        for (int j = 0; j < PreservedSize; j++)
                        {
                            pack.Preserved[j] = dataBuffer[i + 6 + j];
                        }

                        if (dataBuffer[i + 6 + PreservedSize] == EndOfPack)
                        {
                            lock (syncHelper)
                            {
                                dataPackBuffer.Add(ref pack);
                            }
                            numPassed = i + 6 + PreservedSize + 1;
                        }
                    }
                }
            }

            if (numPassed != 0)
            {
                dataBuffer.RemoveRange(0, numPassed);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Retrieve a chunk of the given size and fill destination array with samples
 /// </summary>
 /// <returns>True if a chunk is available, false otherwise</returns>
 public bool RetrieveChunk(float[] destination)
 {
     if (samples.Count < destination.Length)
     {
         return(false);
     }
     for (int i = 0; i < destination.Length; i++)
     {
         destination[i] = samples[i];
     }
     samples.RemoveRange(0, destination.Length);
     return(true);
 }
Exemplo n.º 5
0
        public byte[] DownloadData()
        {
            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                if (_currentStream == null)
                {
                    Trace.WriteLine("Connecting to " + _ip + " for " + _uri);

                    _errored                         = false;
                    _currentTcpClient                = new TcpClient();
                    _currentTcpClient.NoDelay        = true;
                    _currentTcpClient.ReceiveTimeout = 500;
                    _currentTcpClient.SendTimeout    = 500;
                    _currentTcpClient.Connect(_ip, _uri.Port);
                    _currentStream              = _currentTcpClient.GetStream();
                    _currentStream.ReadTimeout  = 500;
                    _currentStream.WriteTimeout = 500;

                    byte[] initialBuffer = Encoding.ASCII.GetBytes(string.Format(_initialRequestFormat, _uri.ToString(), _uri.Host));
                    _currentStream.Write(initialBuffer, 0, initialBuffer.Length);

                    //_readyNextRequest = Encoding.ASCII.GetBytes(string.Format(_nextRequestFormat, uri.ToString(), uri.Host));
                    _readyNextRequest = initialBuffer;
                }
                else
                {
                    _currentStream.Write(_readyNextRequest, 0, _readyNextRequest.Length);
                }

                _responseBuffer.Clear();

                int bytesRead;

                int  bytesAnalyzed;
                bool endOfHeadersReached = false;

                int    contentLength = -1;
                string httpVersion   = null;

                do
                {
                    bytesRead = _currentStream.Read(_buffer, 0, _buffer.Length);

                    _responseBuffer.AddRange(_buffer, 0, bytesRead);
                    IList <string> newHeaders = AnalyzeHeaders(_responseBuffer.GetInternalBuffer(), _responseBuffer.Count, out bytesAnalyzed, out endOfHeadersReached);

                    _responseBuffer.RemoveRange(0, bytesAnalyzed);

                    foreach (string header in newHeaders)
                    {
                        Trace.WriteLine(header);

                        if (header.StartsWith("HTTP/1"))
                        {
                            string[] splitted         = header.Split(_spaceCharArray);
                            string   httpResponseCode = splitted[1];

                            if (httpResponseCode != "200")
                            {
                                throw new InvalidDataException("httpResponseCode is " + httpResponseCode);
                            }

                            httpVersion = splitted[0];
                        }
                        else if (header.StartsWith("Content-Length:"))
                        {
                            contentLength = int.Parse(header.Split(_spaceCharArray)[1]);
                        }
                        else if (header == "Connection: keep-alive")
                        {
                            _keepAlive = true;
                        }
                        else if (header == "Connection: close")
                        {
                            _keepAlive = false;
                        }
                    }
                }while (!endOfHeadersReached);

                if (contentLength < 0 && httpVersion != "HTTP/1.0")
                {
                    throw new InvalidDataException("Content-Length is " + contentLength);
                }

                Trace.WriteLine("HTTP Version is " + httpVersion);

                if (httpVersion != "HTTP/1.0")
                {
                    while (_responseBuffer.Count < contentLength)
                    {
                        bytesRead = _currentStream.Read(_buffer, 0, _buffer.Length);
                        _responseBuffer.AddRange(_buffer, 0, bytesRead);
                    }
                }
                else
                {
                    _keepAlive = false;

                    try
                    {
                        while ((bytesRead = _currentStream.Read(_buffer, 0, _buffer.Length)) > 0)
                        {
                            _responseBuffer.AddRange(_buffer, 0, bytesRead);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                byte[] result = _responseBuffer.ToArray();
                return(result);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Connecting to " + _ip + " for " + _uri);
                Trace.WriteLine("Exception from " + _ip + " for " + _uri + ": " + ex.ToString());

                _errored   = true;
                _keepAlive = false;

                if (_currentStream != null)
                {
                    _currentStream.Close();
                    _currentStream = null;
                }

                if (_currentTcpClient != null)
                {
                    _currentTcpClient.Close();
                    _currentTcpClient = null;
                }

                throw;
            }
            finally
            {
                if (!_keepAlive)
                {
                    if (_currentStream != null)
                    {
                        _currentStream.Close();
                        _currentStream = null;
                    }

                    if (_currentTcpClient != null)
                    {
                        _currentTcpClient.Close();
                        _currentTcpClient = null;
                    }
                }

                Trace.WriteLine("DownloadData " + sw.Elapsed);
            }

            //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            ////request.KeepAlive = true;
            ////request.IfModifiedSince = _lastModified;

            ////request.Host = "www.oref.org.il";

            //using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            //{
            //    //_lastModified = response.LastModified;

            //    using (Stream responseStream = response.GetResponseStream())
            //    using (StreamReader reader = new StreamReader(responseStream, Encoding.Unicode))
            //    {
            //        return reader.ReadToEnd();
            //    }
            //}
        }