Exemplo n.º 1
0
        private bool GetDonwloadBandwidth(long countBytes, ref byte[] downloadData, byte[] endDown)
        {
            if (FunctionUtil.ContainsArrays(downloadData, endDown))
            {
                // bytes * 8bits / 7s * 1E+6 = X Mbits
                double bandwidth = ((double)(countBytes * 8L) / (double)(7.0 * 1E+6));
                network.BandwidthDownload = Convert.ToString(bandwidth);
                downloadData = null;

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        private async Task <bool> BandwidthCalculation()
        {
            System.Diagnostics.Debug.WriteLine("[ProfileNetworkFull]: Download");
            PublishProgress(55);

            streamSocket = new StreamSocket();
            await streamSocket.ConnectAsync(hostName, server.BandwidthPort.ToString());

            DataWriter writer = new DataWriter(streamSocket.OutputStream);
            DataReader reader = new DataReader(streamSocket.InputStream);

            byte[] endDown    = Encoding.UTF8.GetBytes("end_down");
            byte[] endSession = Encoding.UTF8.GetBytes("end_session");

            writer.WriteString("down");
            await writer.StoreAsync();

            await writer.FlushAsync();

            uint bufferSize = 8192;

            reader.InputStreamOptions = InputStreamOptions.Partial;

            long countBytes = 0L;

            byte[] downloadData = new byte[bufferSize];
            while (true)
            {
                uint readTransmittedData = await reader.LoadAsync(bufferSize);

                countBytes += readTransmittedData;

                if (readTransmittedData == bufferSize)
                {
                    reader.ReadBytes(downloadData);
                    if (GetDonwloadBandwidth(countBytes, ref downloadData, endDown))
                    {
                        break;
                    }
                }
                else
                {
                    byte[] tempData = new byte[readTransmittedData];
                    reader.ReadBytes(tempData);
                    //System.Diagnostics.Debug.WriteLine("[ProfileNetworkFull]: " + readTransmittedData + "/" + countBytes);
                    if (GetDonwloadBandwidth(countBytes, ref tempData, endDown))
                    {
                        break;
                    }
                }
            }

            if (halted)
            {
                return(false);
            }

            System.Diagnostics.Debug.WriteLine("[ProfileNetworkFull]: Upload");
            PublishProgress(75);
            writer.WriteString("up");
            await writer.StoreAsync();

            await writer.FlushAsync();

            System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew();
            while (stopWatch.ElapsedMilliseconds < 11000)
            {
                writer.WriteBytes(data);
                await writer.StoreAsync();

                await writer.FlushAsync();
            }
            stopWatch.Stop();

            writer.WriteString("end_up");
            await writer.StoreAsync();

            await writer.FlushAsync();

            var readFinish = await reader.LoadAsync(64);

            var endSessionBuffer = new byte[readFinish];

            reader.ReadBytes(endSessionBuffer);

            if (FunctionUtil.ContainsArrays(endSessionBuffer, endSession))
            {
                string   dataBlock = Encoding.UTF8.GetString(endSessionBuffer, 0, endSessionBuffer.Length);
                string[] res       = dataBlock.Split(new char[] { ':' });
                network.BandwidthUpload = res[1];
            }

            Close(ref writer);
            Close(ref reader);
            Close(ref streamSocket);

            endSessionBuffer = null;

            return(true);
        }