Exemplo n.º 1
0
        private async Task ProcessServerMessage(NetworkStream stream)
        {
            switch (await stream.ReadObjAsync(ReportProgress))
            {
            case TrackMetadata[] metas:
                var items = metas.Select(x => new ListViewItem(x.ListItem)).ToArray();
                Dispatcher.Post(() =>
                {
                    lvRemoteList.Items.Clear();
                    lvRemoteList.Items.AddRange(items);
                });
                break;

            case FileBlob file:
                var repo = await Dispatcher.Send(() => TbPath.Text);

                var path      = Path.Combine(repo, file.Name);
                var preExists = File.Exists(path);
                try
                {
                    using (FileStream fs = File.Create(path))
                    {
                        await fs.WriteAsync(file.Body, 0, file.Body.Length).ContinueWith(async t =>
                        {
                            await t;
                            fs.Close();
                            if (!preExists)
                            {
                                Dispatcher.Post(() =>
                                {
                                    var meta = CommonBehavior.GetMetadata(Wmp, path);
                                    Playlist.Add(new ListViewItem(meta.ListItem)
                                    {
                                        Tag = meta
                                    });
                                });
                            }
                        }).ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                ServerMessage = null;
                Dispatcher.Post(() => { Text = "Music Player Client"; });
                break;

            case Announcement msg:
                ServerMessage = msg.Message;
                ReportProgress(0);
                break;
            }
        }
Exemplo n.º 2
0
        public void WhenExecuted_QueryShouldReturnResult()
        {
            // arrange
            var query    = new MyFirstQuery(1);
            var expected = new SomeInfo(1, 2);

            // act
            var result = _dispatcher.Send(query);

            // assert
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 3
0
        public void WhenExecuted_CommandShouldReportSuccess()
        {
            // arrange
            var command = new MyFirstCommand(1, string.Empty);

            // act
            _dispatcher.Send(command);
            var inquiry = new Inquiry(_getCommandResult);
            var answer  = inquiry.For(command);

            // assert
            Assert.IsTrue(answer.Success);
        }
Exemplo n.º 4
0
        private async void ClientInstance(TcpClient client)
        {
            string ip = (client.Client.RemoteEndPoint as IPEndPoint)?.ToString();

            Log($"Connected from {ip}\r\n");
            try
            {
                var stream = client.GetStream();
                await PushTrackList(stream);

                Task <object> readTask  = stream.ReadObjAsync();
                Task <bool>   knockTask = Dispatcher.WaitAsync();

                var token = WorkerAbort.Token;
                token.Register(() => stream.Close());
                while (!token.IsCancellationRequested)
                {
                    await Task.WhenAny(readTask, knockTask);

                    if (readTask.IsCompleted)
                    {
                        if (readTask.IsFaulted)
                        {
                            return;
                        }
                        switch (await readTask)
                        {
                        case DownloadRequest req:
                            var item = await UIDispatcher.Send(() => LvTracks.Items[req.Index].Tag) as TrackMetadata;

                            var blob = new FileBlob()
                            {
                                Name = Path.GetFileName(item.Path),
                                Body = File.ReadAllBytes(item.Path)
                            };
                            Log($"{ip} requested {blob.Name}\r\n");
                            await stream.WriteObjAsync(new Announcement($"Downloading {blob.Name}"));

                            await new MemoryStream(SerialUtility.GetPrefixedSerial(blob))
                            .ThrottleCopyTo(stream, 2 * 1024 * 1024, token);
                            //await stream.WriteObjAsync(blob);
                            break;

                        case FileBlob file:
                            var repo = await UIDispatcher.Send(() =>
                            {
                                TbLog.AppendText($"{ip} is uploading {file.Name}\r\n");
                                return(TbPath.Text);
                            });

                            var path      = Path.Combine(repo, file.Name);
                            var preExists = File.Exists(path);
                            try
                            {
                                using (FileStream fs = File.Create(path))
                                {
                                    await fs.WriteAsync(file.Body, 0, file.Body.Length).ConfigureAwait(false);
                                }
                                UIDispatcher.Post(() =>
                                {
                                    TbLog.AppendText($"{ip} uploaded {file.Name}\r\n");
                                    BtnBrowse.PerformClick();
                                });
                            }
                            catch (Exception e)
                            {
                                Log(e.Message + "\r\n");
                            }
                            break;
                        }
                        readTask = stream.ReadObjAsync();
                    }
                    if (knockTask.IsCompleted)
                    {
                        await PushTrackList(stream);

                        knockTask = Dispatcher.WaitAsync();
                    }
                }
            }
            catch (Exception e)
            {
                Log(e.Message + "\r\n");
            }
            finally
            {
                client.Close();
                Log($"Disconnected from {ip}\r\n");
            }
        }