public UpdateClient_2G(UpdateServer_2G server, Socket sok, MainWindow mainw) { _server_2G = server; client = sok; if (client != null) { client.ReceiveBufferSize = _recBufferSize; } main = mainw; }
private void btnStartUpdate_Click(object sender, RoutedEventArgs e) { if (!StartUpdate) { if (!startDebug) { MessageBox.Show(this, "请先启动远程诊断服务!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (string.IsNullOrWhiteSpace(this.txtUpdateFilePath.Text) || this.txtUpdateFilePath.Text.Trim() == "点击右侧浏览按钮选择文件") { MessageBox.Show(this, "还没有选择升级固件!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (string.IsNullOrWhiteSpace(this.txtUpdatePort.Text)) { MessageBox.Show(this, "还没有输入更新服务器端口号!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } else { if (Regex.IsMatch(this.txtUpdatePort.Text.Trim(), @"^\d+$")) //确认是数字 { bool suc = ushort.TryParse(this.txtUpdatePort.Text.Trim(), out updatePort1); if (!suc) { MessageBox.Show(this, "输入的更新服务器端口号范围不正确!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } } else { MessageBox.Show(this, "输入的更新服务器端口号不正确!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information); this.txtUpdatePort.Text = "4518"; return; } } if (string.IsNullOrEmpty(updateVer)) { MessageBox.Show(this, "更新版本号解析错误!请检查升级文件!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop); return; } try { strUpdatefmt = "$UPDUPD,U@d8k%4#jD,"; //头 strUpdatefmt += "{0},0,"; //版本号 FileInfo fileInfo = new FileInfo(filePath); FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read); long fileSize = fs.Length; fs.Position = 0; fileStream = new byte[fileSize]; fs.Read(fileStream, 0, fileStream.Length); fs.Close(); strUpdatefmt += fileSize + ","; //升级文件大小 strUpdatefmt += fileInfo.LastWriteTime.ToString("HH:mm:ss MM-dd-yyyy,"); //文件修改时间 UInt32 Crc32 = 0; int readBufSize = 512; int position = 0; do { int readCount = (int)fileSize - position; readCount = readCount > readBufSize ? readBufSize : readCount; byte[] fileBuf = fileStream.Where((b, index) => index >= position && index < (position + readCount)) .ToArray(); Crc32 = CRC.crc_32_calc(fileBuf, (UInt16)(readCount * 8), Crc32); position += readCount; } while (position < fileSize); strUpdatefmt += Crc32.ToString("X") + ",1,"; //CRC校验 strUpdatefmt += serverIP + ":" + updatePort; //ip和端口 } catch (Exception ex) { MessageBox.Show(this, "升级文件打开错误,请检查升级文件!\r\n错误信息:" + ex.Message, "启动失败", MessageBoxButton.OK, MessageBoxImage.Error); } if (string.IsNullOrWhiteSpace(strUpdatefmt)) { MessageBox.Show(this, "获取\"更新SMS串\"出错,请检查升级文件是否正确!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error); return; } this.txtSMS.Text = strUpdate(); btnEnterMac_Click(null, null); if (updateServer_2G == null) { updateServer_2G = new UpdateServer_2G(); updateServer_2G.main = this; } updateServer_2G.Start(new Action(() => { this.btnStartUpdate.Dispatcher.BeginInvoke(new Action(() => { this.StartUpdate = true; this.btnStartUpdate.Content = "停止服务\n(已启动)"; this.MenuItemStartUpdate.IsEnabled = this.MenuItemStopUpdate.IsEnabled = this.MenuItemForceUpdate.IsEnabled = this.MenuItemPauseUpdate.IsEnabled = true; this.btnSelectUpdateCarFile.IsEnabled = this.btnSelectUpdateFile.IsEnabled = false; })); })); //启动更新服务器 } else { if (updateServer_2G.Stop()) { this.StartUpdate = false; this.btnStartUpdate.Content = "启动升级"; this.MenuItemStartUpdate.IsEnabled = this.MenuItemStopUpdate.IsEnabled = this.MenuItemForceUpdate.IsEnabled = this.MenuItemPauseUpdate.IsEnabled = false; this.btnSelectUpdateCarFile.IsEnabled = this.btnSelectUpdateFile.IsEnabled = true; } } }