Beispiel #1
0
        private async Task DiscoverAllIPv6Async(BinaryWriter reqPacket, List <DiscoveredServerInfo> liResults)
        {
            // Give IPv4 Broadcast a bit of a headstart, because IPv4 results tend to be a bit more robust and easier to read for the user
            await Task.Delay(700);

            try
            {
                using (UdpClient udpV6Client = new UdpClient(new IPEndPoint(IPAddress.IPv6Any, 0)))
                {
                    udpV6Client.Client.SendTimeout = 1000;

                    await Ipv6MulticastDiscoveryPacketsAsync(udpV6Client, reqPacket);

                    LowResStopWatch sw = new LowResStopWatch(true);
                    while (sw.ElapsedMilliseconds < TIMEOUT - 700)
                    {
                        Task timeout = Task.Delay(TIMEOUT - 700);
                        Task <UdpReceiveResult> recv = udpV6Client.ReceiveAsync();
                        if (await Task.WhenAny(timeout, recv) == recv)
                        {
                            HandleAnswer(await recv, liResults, "IPv6 Multicast");
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.d(TAG, "IPv6 UDP Socket Error: " + e.Message);
            }
        }
Beispiel #2
0
        private async Task DiscoverAllIPv4Async(BinaryWriter reqPacket, List <DiscoveredServerInfo> liResults)
        {
            try
            {
                using (UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 0)))
                {
                    udpClient.Client.SendTimeout = 1000;
                    udpClient.EnableBroadcast    = true;

                    await Ipv4BroadcastDiscoveryPacketsAsync(udpClient, reqPacket);

                    LowResStopWatch sw = new LowResStopWatch(true);
                    while (sw.ElapsedMilliseconds < TIMEOUT)
                    {
                        Task timeout = Task.Delay(TIMEOUT);
                        Task <UdpReceiveResult> recv = udpClient.ReceiveAsync();
                        if (await Task.WhenAny(timeout, recv) == recv)
                        {
                            HandleAnswer(await recv, liResults, "IPv4 Broadcast");
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.d(TAG, "IPv4 UDP Socket Error: " + e.Message);
            }
        }
Beispiel #3
0
        public async Task UploadFile(BbRemoteServer server)
        {
            m_CancelToken       = new CancellationTokenSource();
            Status              = EStatus.TRANSFERRING;
            m_swUploadStartTime = new LowResStopWatch(true);
            Log.i(TAG, "Sending " + FileName + " (" + FileSize.ToXByteSize() + ")", true);
            try
            {
                using (m_stream = File.OpenRead(FullPath))
                {
                    long lPosition = 0;
                    while (lPosition < FileSize && server.Attached)
                    {
                        m_CancelToken.Token.ThrowIfCancellationRequested();
                        BbTcpPacket_SendFileData packet = new BbTcpPacket_SendFileData(server.UsedProtocolVersion, FileID, (ulong)lPosition);
                        int nToRead = (int)Math.Min(packet.MaxDataSize, FileSize - lPosition);
                        int nRead   = await packet.WriteFileData(m_stream, nToRead, m_CancelToken.Token);

                        if (nRead != nToRead)
                        {
                            throw new IOException("Unexpected read error (read less than expected)");
                        }
                        lPosition += nRead;
                        await server.SendLowPriorityDataPacketAsync(packet, m_CancelToken.Token);
                    }
                }
            }
            finally
            {
                m_CancelToken.Dispose();
                m_CancelToken = null;
            }
            Status = EStatus.FINISHED;
            m_swUploadStartTime.Stop();
            Log.i(TAG, "Finished sending file " + FileName, true);
        }
Beispiel #4
0
        public async Task Write(MemoryStream stream, ulong lDataStartPos)
        {
            // Calling WriteAsync on the same stream multiple times parallelly might (unknown) lead to unexpected write race conditions
            // To be safe we use ActionBlocks to write Non-Blocking but sequential
            if (m_writeAction == null)
            {
                m_swDownloadStartTime = new LowResStopWatch(true);
                m_CancelToken         = new CancellationTokenSource();
                m_writeAction         = new ActionBlock <MemoryStream>(ms =>
                {
                    try
                    {
                        m_fileStream.Write(ms.GetBuffer(), (int)ms.Position, (int)ms.Remaining());
                    }
                    catch (Exception e)
                    {
                        Log.e(TAG, "Error Writing file " + FileName + " - " + e.Message);
                        m_bError = true;
                    }
                },
                                                                       new ExecutionDataflowBlockOptions
                {
                    CancellationToken = m_CancelToken.Token
                });
            }
            if (m_bError)
            {
                throw new IOException("Write Error in Actionblock");
            }

            if (lDataStartPos != DataReceived)
            {
                throw new IOException("Wrong start position for data chunk");
            }

            DataReceived += (ulong)stream.Remaining();

            if (DataReceived > FileSize)
            {
                throw new IOException("More data received than expected file size");
            }

            m_writeAction.Post(stream);

            if (DataReceived == FileSize)
            {
                m_writeAction.Complete();
                try
                {
                    await m_writeAction.Completion;
                }
                catch (Exception e)
                {
                    Log.e(TAG, "Error while waiting for Completition of file " + FileName + " - " + e.Message);
                    throw e;
                }
                m_writeAction = null;
                m_CancelToken.Dispose();

                if (m_fileStream.Position != (long)FileSize)
                {
                    throw new Exception("Unexpected position in file stream while completing");
                }
                m_fileStream.Close();
                m_fileStream = null;

                FinalFilePath = BasePath + Path.DirectorySeparatorChar + FileName;
                for (int i = 1; File.Exists(FinalFilePath); i++)
                {
                    String fileBase = Path.GetFileNameWithoutExtension(FileName);
                    String ext      = Path.GetExtension(FileName);
                    FinalFilePath = BasePath + Path.DirectorySeparatorChar + fileBase + "_" + i.ToString() + ext;
                }
                File.Move(TempFilePath, FinalFilePath);
                TempFilePath = "";
                if (m_swDownloadStartTime != null)
                {
                    m_swDownloadStartTime.Stop();
                }

                await Task.Run(() => FetchThumbnail());

                Complete = true;
            }
        }