提交 0e6d98ac 编写于 作者: 若汝棋茗

新增文件传输示例

上级 dbfb5a9b
......@@ -83,10 +83,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventBusClient", "examples\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FileTransfer简单示例", "FileTransfer简单示例", "{24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileClientGUI", "examples\FileTransfer简单示例\FileClientGUI\FileClientGUI.csproj", "{3CF9F527-FBD6-4332-84C5-C9D9A39571CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileClientApp", "examples\FileTransfer简单示例\FileClientApp\FileClientApp.csproj", "{B070790D-2D68-4B36-AC60-6A498BAAF56F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TouchRpc简单示例", "TouchRpc简单示例", "{54A8216A-AA96-4293-9285-D2173E1D2E69}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TouchRpcServerApp", "examples\TouchRpc简单示例\TouchRpcServerApp\TouchRpcServerApp.csproj", "{613B3E85-59DE-4314-9A3A-EEE7C03E207F}"
......@@ -107,10 +103,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "examples\Tcp
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebSocket简单示例", "WebSocket简单示例", "{84D1227F-3419-4BCD-9331-8B61B2C19784}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSocketConsoleApp", "examples\WebSocket简单示例\WebSocketConsoleApp\WebSocketConsoleApp.csproj", "{5F9D5028-12C8-4819-9B8E-ACE017B63132}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSocketConsoleApp", "examples\WebSocket简单示例\WebSocketConsoleApp\WebSocketConsoleApp.csproj", "{5F9D5028-12C8-4819-9B8E-ACE017B63132}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WSFormClientApp", "examples\WebSocket简单示例\WSFormClientApp\WSFormClientApp.csproj", "{1B247610-9FAD-4434-BA15-B1DB1F71B7C5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileServiceGUI", "examples\FileTransfer简单示例\FileServiceGUI\FileServiceGUI.csproj", "{2017B959-1B58-453A-B65D-2F737E6B96DC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileClientGUI", "examples\FileTransfer简单示例\FileClientGUI\FileClientGUI.csproj", "{8BFDA2E0-E7B6-4592-86B8-53426A8C0848}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -193,14 +193,6 @@ Global
{30A64984-243D-4745-807E-B084BA51B968}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30A64984-243D-4745-807E-B084BA51B968}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30A64984-243D-4745-807E-B084BA51B968}.Release|Any CPU.Build.0 = Release|Any CPU
{3CF9F527-FBD6-4332-84C5-C9D9A39571CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CF9F527-FBD6-4332-84C5-C9D9A39571CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CF9F527-FBD6-4332-84C5-C9D9A39571CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CF9F527-FBD6-4332-84C5-C9D9A39571CA}.Release|Any CPU.Build.0 = Release|Any CPU
{B070790D-2D68-4B36-AC60-6A498BAAF56F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B070790D-2D68-4B36-AC60-6A498BAAF56F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B070790D-2D68-4B36-AC60-6A498BAAF56F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B070790D-2D68-4B36-AC60-6A498BAAF56F}.Release|Any CPU.Build.0 = Release|Any CPU
{613B3E85-59DE-4314-9A3A-EEE7C03E207F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{613B3E85-59DE-4314-9A3A-EEE7C03E207F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{613B3E85-59DE-4314-9A3A-EEE7C03E207F}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -233,6 +225,14 @@ Global
{1B247610-9FAD-4434-BA15-B1DB1F71B7C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B247610-9FAD-4434-BA15-B1DB1F71B7C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B247610-9FAD-4434-BA15-B1DB1F71B7C5}.Release|Any CPU.Build.0 = Release|Any CPU
{2017B959-1B58-453A-B65D-2F737E6B96DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2017B959-1B58-453A-B65D-2F737E6B96DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2017B959-1B58-453A-B65D-2F737E6B96DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2017B959-1B58-453A-B65D-2F737E6B96DC}.Release|Any CPU.Build.0 = Release|Any CPU
{8BFDA2E0-E7B6-4592-86B8-53426A8C0848}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BFDA2E0-E7B6-4592-86B8-53426A8C0848}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BFDA2E0-E7B6-4592-86B8-53426A8C0848}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BFDA2E0-E7B6-4592-86B8-53426A8C0848}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -272,8 +272,6 @@ Global
{A6358094-56E0-4DF3-BA95-319D676C0A04} = {5E9585EE-934F-41B4-9ECA-FB0C55A155EF}
{30A64984-243D-4745-807E-B084BA51B968} = {5E9585EE-934F-41B4-9ECA-FB0C55A155EF}
{24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4} = {517CF381-4C15-4519-9BC4-92DC4C412C8D}
{3CF9F527-FBD6-4332-84C5-C9D9A39571CA} = {24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4}
{B070790D-2D68-4B36-AC60-6A498BAAF56F} = {24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4}
{54A8216A-AA96-4293-9285-D2173E1D2E69} = {517CF381-4C15-4519-9BC4-92DC4C412C8D}
{613B3E85-59DE-4314-9A3A-EEE7C03E207F} = {54A8216A-AA96-4293-9285-D2173E1D2E69}
{D367F50D-0B27-4B46-9872-695DC6B021BF} = {54A8216A-AA96-4293-9285-D2173E1D2E69}
......@@ -286,6 +284,8 @@ Global
{84D1227F-3419-4BCD-9331-8B61B2C19784} = {517CF381-4C15-4519-9BC4-92DC4C412C8D}
{5F9D5028-12C8-4819-9B8E-ACE017B63132} = {84D1227F-3419-4BCD-9331-8B61B2C19784}
{1B247610-9FAD-4434-BA15-B1DB1F71B7C5} = {84D1227F-3419-4BCD-9331-8B61B2C19784}
{2017B959-1B58-453A-B65D-2F737E6B96DC} = {24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4}
{8BFDA2E0-E7B6-4592-86B8-53426A8C0848} = {24212B0A-8DAA-4AC3-BB5D-F3AC485EABA4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DB787235-A13A-4A3D-B5A8-5DFEB6511EEE}
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TouchSocket" Version="0.1.4" />
</ItemGroup>
</Project>
using System;
using TouchSocket.Core;
using TouchSocket.Core.Config;
using TouchSocket.Core.Log;
using TouchSocket.Core.Run;
using TouchSocket.Rpc.TouchRpc;
using TouchSocket.Sockets;
namespace ClientApp
{
internal class Program
{
private static void Main(string[] args)
{
TcpTouchRpcClient client = new TouchSocketConfig()
.SetRemoteIPHost("127.0.0.1:7789")
.SetVerifyToken("File")
.BuildWithTcpTouchRpcClient();
client.FileTransfering += (client, e) =>
{
//有可能是上传,也有可能是下载
client.Logger.Message($"服务器请求传输文件,ID={client.ID},请求类型={e.TransferType},文件名={e.FileInfo.FileName}");
};
client.FileTransfered += (client, e) =>
{
//传输结束,但是不一定成功,需要从e.Result判断状态。
client.Logger.Message($"服务器传输文件结束,ID={client.ID},请求类型={e.TransferType},文件名={e.FileInfo.FileName},请求状态={e.Result}");
};
client.Logger.Message("连接成功");
//第一个参数是请求路径,第二个是保存路径。
FileRequest fileRequest = new FileRequest(@"D:\System\Windows.iso", $@"Windows.iso");
fileRequest.Flags = TransferFlags.BreakpointResume;//尝试断点续传,使用断点续传时,会验证MD5值
FileOperator fileOperator = new FileOperator();//实例化本次传输的控制器,用于获取传输进度、速度、状态等。
fileOperator.Timeout = 60 * 1000;//当传输大文件,且启用断点续传时,服务器可能会先计算MD5,而延时响应,所以需要设置超时时间。
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
LoopAction loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
{
if (fileOperator.Result.ResultCode != ResultCode.Default)
{
loop.Dispose();
}
client.Logger.Message($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
});
loopAction.RunAsync();
Metadata metadata = new Metadata();//传递到服务器的元数据
metadata.Add("1", "1");
metadata.Add("2", "2");
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
IResult result = client.PullFile(fileRequest, fileOperator, metadata);
client.Logger.Message(result.ToString());
Console.ReadKey();
}
}
}
\ No newline at end of file
<Application x:Class="FileClientGUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources />
</Application>
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......
......@@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="RRQMSkin" Version="4.0.0" />
<PackageReference Include="RRQMSkin" Version="3.7.1" />
<PackageReference Include="TouchSocket" Version="0.1.4" />
</ItemGroup>
......
......@@ -91,6 +91,7 @@
<controls:InputBox Width="100" VerticalAlignment="Center" InputFilter="Uint" TextChanged="InputBox_TextChanged" />
<Button Margin="10,0,0,0" VerticalAlignment="Center" Click="Button_Click" Content="取消传输" />
<Button Margin="10,0,0,0" VerticalAlignment="Center" Click="PingButton_Click" Content="Ping" />
</StackPanel>
</GroupBox>
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......@@ -75,10 +75,9 @@ namespace FileClientGUI
fileClient = new TcpTouchRpcClient();
fileClient.FileTransfering += this.FileClient_BeforeFileTransfer;
fileClient.Received += FileClient_Received;
fileClient.Disconnected += this.FileClient_Disconnected;
fileClient.Setup(new TouchSocketConfig()
.SetRemoteIPHost(new IPHost("127.0.0.1:7789"))
.SetBufferLength(1024 * 64)
.SetVerifyToken("FileService"));
try
......@@ -96,6 +95,10 @@ namespace FileClientGUI
}
}
private void FileClient_Disconnected(ITcpClientBase client, ClientDisconnectedEventArgs e)
{
}
private void FileClient_Received(TcpTouchRpcClient client, short protocol, ByteBlock byteBlock)
{
ShowMsg($"收到数据:协议={protocol},数据长度:{byteBlock.Len - 2}");
......@@ -172,8 +175,8 @@ namespace FileClientGUI
fileRequest.Flags = TransferFlags.BreakpointResume;
}
FileOperator fileOperator = new FileOperator();
//fileOperator.SetMaxSpeed(10240);
FileInfo fileInfo = new FileInfo(fileRequest.Path);
TransferModel model = new TransferModel()
{
FileLength = FileUtility.ToFileLengthString(fileInfo.Length),
......@@ -253,5 +256,10 @@ namespace FileClientGUI
{
this.fileClient.Send(10, new byte[] { 1, 2, 3 });
}
private void PingButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this.fileClient.Ping().ToString());
}
}
}
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/eo2w71/rrqm
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
......
<Application x:Class="FileServiceGUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml">
<Application.Resources />
</Application>
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.Windows;
namespace FileServiceGUI
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
}
}
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net45</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\Images\完成.png" />
<None Remove="Resources\Images\未完成.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="RRQMSkin" Version="3.7.1" />
<PackageReference Include="TouchSocket" Version="0.1.4" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\Images\完成.png" />
<Resource Include="Resources\Images\未完成.png" />
</ItemGroup>
</Project>
<Window x:Class="FileServiceGUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="RRQM_Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FileServiceGUI"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="服务器" Width="800"
Height="600" mc:Ignorable="d">
<Window.Resources>
<Style x:Key="ListBox_Transfer" TargetType="ListBox">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="border" Height="40" Margin="2" BorderBrush="Transparent" BorderThickness="1"
CornerRadius="3"
ToolTip="{Binding Mes}">
<Grid Margin="3" Background="#92BCE1">
<Grid.RowDefinitions>
<RowDefinition Height="7*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<ProgressBar Grid.RowSpan="2" Background="Transparent" BorderThickness="0" Foreground="#43E17C" Maximum="1"
Value="{Binding Progress}" />
<TextBlock Margin="5,0,0,0" VerticalAlignment="Center" FontSize="15" FontWeight="Bold"
Text="{Binding FileName}" />
<TextBlock Grid.Row="1" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="10" Foreground="#70779D"
Text="{Binding FilePath}" />
<StackPanel Grid.RowSpan="2" Margin="0,0,5,0" HorizontalAlignment="Right" VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock Width="70" VerticalAlignment="Center"
Text="{Binding TransferType}" />
<TextBlock Width="70" VerticalAlignment="Center"
Text="{Binding FileLength}" />
<TextBlock Width="70" VerticalAlignment="Center"
Text="{Binding Speed}" />
<Image Width="30" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"
Source="{Binding Status}" />
</StackPanel>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#0AE459" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#2E8CEF" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<GroupBox Margin="5" Header="功能区">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="cb_resume" VerticalContentAlignment="Center" Content="断点续传" />
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Width="70" Height="30" Margin="5,0" Click="Button_Click_1" Content="Push" />
<Button Width="70" Height="30" Margin="5,0,5,2" VerticalAlignment="Bottom" Click="Button_Click_2"
Content="Pull" />
</StackPanel>
</Grid>
</GroupBox>
<GroupBox Grid.Column="1" Margin="5" Header="传输控制">
<StackPanel Grid.ColumnSpan="3" Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="传输限速:" />
<controls:InputBox Width="100" VerticalAlignment="Center" InputFilter="Uint" TextChanged="InputBox_TextChanged" />
<Button Margin="10,0,0,0" VerticalAlignment="Center" Click="Button_Click" Content="取消传输" />
</StackPanel>
</GroupBox>
<GroupBox Grid.Row="1" Grid.RowSpan="2" Margin="5" Header="在线客户端">
<ListBox x:Name="ListBox_Clients" BorderThickness="0" DisplayMemberPath="ID" />
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="2" Margin="5" Header="当前队列">
<ListBox x:Name="ListBox_LocalTransfer" SelectionChanged="ListBox_SelectionChanged"
Style="{StaticResource ListBox_Transfer}" />
</GroupBox>
<GroupBox Grid.Row="2" Grid.Column="2" Margin="5" Header="远程队列">
<ListBox x:Name="ListBox_RemoteTransfer" SelectionChanged="ListBox_SelectionChanged"
Style="{StaticResource ListBox_Transfer}" />
</GroupBox>
<GroupBox Grid.Row="3" Grid.ColumnSpan="3" Margin="5" Background="AliceBlue" Header="日志">
<TextBox x:Name="msgBox" Background="Transparent" BorderThickness="0" />
</GroupBox>
</Grid>
</Window>
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using FileServiceGUI.Models;
using FileServiceGUI.Win;
using RRQMSkin.MVVM;
using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using TouchSocket.Core;
using TouchSocket.Core.ByteManager;
using TouchSocket.Core.Config;
using TouchSocket.Core.IO;
using TouchSocket.Rpc.TouchRpc;
using TouchSocket.Sockets;
namespace FileServiceGUI
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.Loaded += this.MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.clients = new RRQMList<TcpTouchRpcSocketClient>();
this.remoteModels = new RRQMList<TransferModel>();
this.localModels = new RRQMList<TransferModel>();
this.ListBox_Clients.ItemsSource = this.clients;
this.ListBox_RemoteTransfer.ItemsSource = this.remoteModels;
this.ListBox_LocalTransfer.ItemsSource = this.localModels;
this.Start();
}
private RRQMList<TcpTouchRpcSocketClient> clients;
private RRQMList<TransferModel> remoteModels;
private RRQMList<TransferModel> localModels;
private void ShowMsg(string msg)
{
this.UIInvoke(() =>
{
this.msgBox.AppendText($"{msg}\r\n");
});
}
private void UIInvoke(Action action)
{
this.Dispatcher.Invoke(() =>
{
action.Invoke();
});
}
private TcpTouchRpcService fileService;
private void Start()
{
//启动
if (this.fileService != null)
{
return;
}
this.fileService = new TcpTouchRpcService();
this.fileService.Received += this.FileService_Received;
this.fileService.Connected += this.FileService_Connected;
this.fileService.Disconnected += this.FileService_Disconnected;
this.fileService.FileTransfering += this.FileService_FileTransfering;
var config = new TouchSocketConfig();
config.SetListenIPHosts(new IPHost[] { new IPHost(7789) })
.SetVerifyToken("FileService");
try
{
this.fileService.Setup(config);
this.fileService.Start();
this.ShowMsg("启动成功");
}
catch (Exception ex)
{
this.ShowMsg(ex.Message);
}
}
private void FileService_Received(TcpTouchRpcSocketClient socketClient, short protocol, ByteBlock byteBlock)
{
this.ShowMsg($"收到数据:协议={protocol},数据长度:{byteBlock.Len - 2}");
if (protocol == -1)
{
socketClient.Send(byteBlock.ToArray(2));
}
else
{
socketClient.Send(protocol, byteBlock.ToArray(2));
}
}
private void FileService_FileTransfering(TcpTouchRpcSocketClient client, FileOperationEventArgs e)
{
TransferModel model = new TransferModel();
model.FileOperator = e.FileOperator;
model.TransferType = e.TransferType;
switch (e.TransferType)
{
case TransferType.Push:
model.FilePath = e.FileRequest.SavePath;
model.FileLength = FileUtility.ToFileLengthString(e.FileInfo.FileLength);
break;
case TransferType.Pull:
model.FilePath = e.FileRequest.Path;
model.FileLength = FileUtility.ToFileLengthString(new FileInfo(e.FileRequest.Path).Length);
break;
default:
break;
}
model.Start();
this.UIInvoke(() =>
{
this.remoteModels.Add(model);
});
}
private void FileService_Disconnected(TcpTouchRpcSocketClient client, TouchSocketEventArgs e)
{
this.UIInvoke(() =>
{
this.clients.Remove(client);
});
}
private void FileService_Connected(TcpTouchRpcSocketClient client, TouchSocketEventArgs e)
{
this.UIInvoke(() =>
{
this.clients.Add(client);
});
}
private TransferModel transferModel;
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedItem is TransferModel transferModel)
{
this.transferModel = transferModel;
}
}
private void InputBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (this.transferModel == null || this.transferModel.FileOperator.Result.ResultCode != ResultCode.Default)
{
MessageBox.Show("请选择一个条目,然后控制。");
return;
}
try
{
if (string.IsNullOrEmpty(((TextBox)sender).Text))
{
return;
}
//this.transferModel.FileOperator.SetMaxSpeed(int.Parse(((TextBox)sender).Text));
}
catch (Exception ex)
{
this.ShowMsg(ex.Message);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//取消
if (this.transferModel == null || this.transferModel.FileOperator.Result.ResultCode != ResultCode.Default)
{
MessageBox.Show("请选择一个条目,然后控制。");
return;
}
try
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
this.transferModel.FileOperator.Token = tokenSource.Token;
tokenSource.Cancel();
}
catch (Exception ex)
{
this.ShowMsg(ex.Message);
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//Push
if (this.ListBox_Clients.SelectedItem is TcpTouchRpcSocketClient client)
{
PushWindow pushWindow = new PushWindow();
if (pushWindow.SelectRequest(out FileRequest fileRequest))
{
if (this.cb_resume.IsChecked == true)
{
fileRequest.Flags = TransferFlags.BreakpointResume;
}
FileOperator fileOperator = new FileOperator();
FileInfo fileInfo = new FileInfo(fileRequest.Path);
TransferModel model = new TransferModel()
{
FileLength = FileUtility.ToFileLengthString(fileInfo.Length),
FilePath = fileRequest.Path,
FileOperator = fileOperator,
TransferType = TransferType.Push
};
model.Start();
this.localModels.Add(model);
client.PushFileAsync(fileRequest, fileOperator);
}
}
else
{
MessageBox.Show("请选择一个客户端");
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//Pull
if (this.ListBox_Clients.SelectedItem is TcpTouchRpcSocketClient client)
{
PullWindow pullWindow = new PullWindow();
if (pullWindow.SelectRequest(out FileRequest fileRequest))
{
if (this.cb_resume.IsChecked == true)
{
fileRequest.Flags = TransferFlags.BreakpointResume;
}
FileOperator fileOperator = new FileOperator();
TransferModel model = new TransferModel()
{
FileLength = "未知",
FilePath = fileRequest.Path,
FileOperator = fileOperator,
TransferType = TransferType.Pull
};
model.Start();
this.localModels.Add(model);
client.PullFileAsync(fileRequest, fileOperator);
}
}
else
{
MessageBox.Show("请选择一个客户端");
}
}
}
}
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TouchSocket.Core;
using TouchSocket.Core.IO;
using TouchSocket.Core.Run;
using TouchSocket.Rpc.TouchRpc;
namespace FileServiceGUI.Models
{
public class TransferModel : RRQMSkin.MVVM.ObservableObject
{
public string FileName => string.IsNullOrEmpty(this.FilePath) ? null : Path.GetFileName(this.FilePath);
public string FilePath { get; set; }
public string FileLength { get; set; }
private string speed;
public string Speed
{
get => this.speed;
set => this.SetProperty(ref this.speed, value);
}
private float progress;
public float Progress
{
get => this.progress;
set => this.SetProperty(ref this.progress, value);
}
private ImageSource status;
public ImageSource Status
{
get => this.status;
set => this.SetProperty(ref this.status, value);
}
public FileOperator FileOperator { get; set; }
public TransferType TransferType { get; set; }
private string mes;
public string Mes
{
get => this.mes;
set => this.SetProperty(ref this.mes, value);
}
public void Start()
{
LoopAction loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
{
if (this.FileOperator.Result.ResultCode == ResultCode.Default)
{
this.Progress = this.FileOperator.Progress;
this.Speed = FileUtility.ToFileLengthString(this.FileOperator.Speed());
}
else if (this.FileOperator.Result.ResultCode == ResultCode.Success)
{
App.Current.Dispatcher.Invoke(() =>
{
this.Status = new BitmapImage(new Uri("Resources/Images/完成.png", UriKind.RelativeOrAbsolute));
});
this.Progress = 1;
loop.Dispose();
}
else
{
App.Current.Dispatcher.Invoke(() =>
{
this.Status = new BitmapImage(new Uri("Resources/Images/未完成.png", UriKind.RelativeOrAbsolute));
});
this.Mes = this.FileOperator.Result.ToString();
loop.Dispose();
}
});
loopAction.RunAsync();
}
}
}
\ No newline at end of file
<Window x:Class="FileServiceGUI.Win.PullWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="PullWindow"
Width="500" Height="170" mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="请求路径:" />
<TextBox x:Name="tb1" Grid.Column="1" Margin="10,0" VerticalAlignment="Center" TextChanged="tb1_TextChanged" />
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="文件保存路径:" />
<TextBox x:Name="tb2" Grid.Row="1" Grid.Column="1" Margin="10,0" VerticalAlignment="Center" />
<Button Grid.Row="2" Grid.ColumnSpan="3" Width="100" Height="30" Click="Button_Click"
Content="确定" />
</Grid>
</Window>
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
using TouchSocket.Rpc.TouchRpc;
namespace FileServiceGUI.Win
{
/// <summary>
/// PullWindow.xaml 的交互逻辑
/// </summary>
public partial class PullWindow : Window
{
public PullWindow()
{
this.InitializeComponent();
}
public bool SelectRequest(out FileRequest fileRequest)
{
this.ShowDialog();
fileRequest = this.fileRequest;
return this.go;
}
private void tb1_TextChanged(object sender, TextChangedEventArgs e)
{
this.tb2.Text = System.IO.Path.GetFileName(this.tb1.Text);
}
private FileRequest fileRequest;
private bool go;
private void Button_Click(object sender, RoutedEventArgs e)
{
this.fileRequest = new FileRequest()
{
Path = this.tb1.Text,
SavePath = this.tb2.Text
};
this.go = true;
this.Close();
}
}
}
\ No newline at end of file
<Window x:Class="FileServiceGUI.Win.PushWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FileServiceGUI.Win"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="PushWindow"
Width="509" Height="172" mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="选择文件:" />
<TextBox x:Name="tb1" Grid.Column="1" Margin="10,0" VerticalAlignment="Center" />
<Button Grid.Column="2" Width="30" VerticalAlignment="Center" Click="Button_Click_1" Content="..." />
<TextBlock Grid.Row="1" VerticalAlignment="Center" Text="文件保存路径:" />
<TextBox x:Name="tb2" Grid.Row="1" Grid.Column="1" Margin="10,0" VerticalAlignment="Center" />
<Button Grid.Row="2" Grid.ColumnSpan="3" Width="100" Height="30" Click="Button_Click"
Content="确定" />
</Grid>
</Window>
\ No newline at end of file
//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using Microsoft.Win32;
using System.Windows;
using TouchSocket.Rpc.TouchRpc;
namespace FileServiceGUI.Win
{
/// <summary>
/// PushWindow.xaml 的交互逻辑
/// </summary>
public partial class PushWindow : Window
{
public PushWindow()
{
this.InitializeComponent();
}
public bool SelectRequest(out FileRequest fileRequest)
{
this.ShowDialog();
fileRequest = this.fileRequest;
return this.go;
}
private FileRequest fileRequest;
private bool go;
private void Button_Click(object sender, RoutedEventArgs e)
{
this.fileRequest = new FileRequest()
{
Path = this.tb1.Text,
SavePath = this.tb2.Text
};
this.go = true;
this.Close();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
if (!string.IsNullOrEmpty(dialog.FileName))
{
this.tb1.Text = dialog.FileName;
this.tb2.Text = System.IO.Path.GetFileName(dialog.FileName);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册