private void StartDownload() { try { ClientLinkInfo clInfo = new ClientLinkInfo() { IpString = LinkInfos.FirstOrDefault(x => x.LName == "IpString").LValue, Port = Convert.ToInt32(LinkInfos.FirstOrDefault(x => x.LName == "Port").LValue) }; var localSavePath = LinkInfos.FirstOrDefault(x => x.LName == "LocalSavePath").LValue; var tempDirPath = LinkInfos.FirstOrDefault(x => x.LName == "TempDirPath").LValue; var clientSocket = new ClientSocket(); var result = clientSocket.StartClient(ClientInfo, DownloadInfo, clInfo, localSavePath, tempDirPath); if (result) { MessageBox.Show("DownLoad File Success!"); } else { MessageBox.Show("DownLoad File Failed!"); } } catch { MessageBox.Show("DownLoad File Failed!"); } }
private void StartDownload() { try { ClientLinkInfo clInfo = new ClientLinkInfo() { IpString = LinkInfos.FirstOrDefault(x => string.Equals(x.LName, "IpString", StringComparison.OrdinalIgnoreCase))?.LValue, Port = Convert.ToInt32(LinkInfos.FirstOrDefault(x => string.Equals(x.LName, "Port", StringComparison.OrdinalIgnoreCase))?.LValue) }; var localSavePath = LinkInfos.FirstOrDefault(x => string.Equals(x.LName, "LocalSavePath", StringComparison.OrdinalIgnoreCase))?.LValue; var tempDirPath = LinkInfos.FirstOrDefault(x => string.Equals(x.LName, "TempDirPath", StringComparison.OrdinalIgnoreCase))?.LValue; var clientSocket = new ClientSocket(); var result = clientSocket.StartClient(ClientInfo, DownloadInfo, clInfo, localSavePath, tempDirPath); if (result) { MessageBox.Show("DownLoad File Success!"); } else { MessageBox.Show("DownLoad File Failed!"); } } catch { MessageBox.Show("DownLoad File Failed!"); } }
//TestFile directory is used to Test socket //See Socket_APM-SAEA\TestFile static void Main(string[] args) { var mainFolder = AppDomain.CurrentDomain.BaseDirectory.Replace("bin", "TestFile"); ClientBasicInfo basicInfo = new ClientBasicInfo() { ProductName = "Airforce094", RevitVersion = "Revit2016", CurrentProductVersion = "18.1.6" }; var serverFilePath = Path.Combine(mainFolder, "Server\\Airforce094_Revit2016_18.1.6_18.1.7.zip"); DownloadFileInfo dlInfo = new DownloadFileInfo() { LatestProductVersion = "18.1.7", DownloadFileMd5 = Md5Utils.GetFileMd5(serverFilePath), DownloadFileTotalSize = new FileInfo(serverFilePath).Length }; ClientLinkInfo clInfo = new ClientLinkInfo() { IpString = "127.0.0.1", Port = 8885 }; var localSavePath = Path.Combine(mainFolder, "Local"); var tempFilesDir = Path.Combine(mainFolder, "TempFile"); var cs = new ClientSocket(); var result = cs.StartClient(basicInfo, dlInfo, clInfo, localSavePath, tempFilesDir); Console.WriteLine(result); Console.ReadKey(); }
public bool StartClient(ClientBasicInfo basicInfo, DownloadFileInfo dlInfo, ClientLinkInfo clInfo, string localSaveFolderPath, string tempFilesDir) { if (basicInfo == null || dlInfo == null || clInfo == null || string.IsNullOrEmpty(localSaveFolderPath) || string.IsNullOrEmpty(tempFilesDir)) { return(false); } if (!Directory.Exists(localSaveFolderPath)) { Directory.CreateDirectory(localSaveFolderPath); } if (!Directory.Exists(tempFilesDir)) { Directory.CreateDirectory(tempFilesDir); } else { DirectoryInfo di = new DirectoryInfo(tempFilesDir); di.Delete(true); Directory.CreateDirectory(tempFilesDir); } var updateFileName = $"{basicInfo.ProductName}_{basicInfo.RevitVersion}_{basicInfo.CurrentProductVersion}_{dlInfo.LatestProductVersion}"; _updateFileName = updateFileName; var localSavePath = Path.Combine(localSaveFolderPath, $"{updateFileName}.zip"); var downloadChannelsCount = DownloadSetting.DownloadChannelsCount; _downloadChannelsCount = downloadChannelsCount; try { IPAddress ipAddress = IPAddress.Parse(clInfo.IpString); IPEndPoint remoteEp = new IPEndPoint(ipAddress, clInfo.Port); int packetCount = downloadChannelsCount; long packetSize = dlInfo.DownloadFileTotalSize / packetCount; _packSize = packetSize; var tasks = new Task[packetCount]; for (int index = 0; index < packetCount; index++) { int packetNumber = index; var task = new Task(() => { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ComObject state = new ComObject { WorkSocket = client, PacketNumber = packetNumber }; client.BeginConnect(remoteEp, ConnectCallback, state); }); tasks[packetNumber] = task; task.Start(); } Task.WaitAll(tasks); ReceiveDone.WaitOne(); _isPacketsComplete = CheckPackets(TempReceivePacketDict, _downloadChannelsCount); CloseSockets(TempReceivePacketDict); if (_isPacketsComplete) { GenerateTempFiles(ResultPacketDict, downloadChannelsCount, tempFilesDir); FileUtils.CombineTempFiles(localSavePath, tempFilesDir); return(Md5Utils.IsMd5Equal(dlInfo.DownloadFileMd5, Md5Utils.GetFileMd5(localSavePath))); } return(false); } catch (Exception ex) { return(false); } }