private void send_command(Command_Server command_code, object data) { if (Unit_No == -1) { return; } AsyncObject ao = new AsyncObject(1); command_data_server CD = new command_data_server(Unit_No, command_code, data); // 문자열을 바이트 배열으로 변환 //ao.Buffer = GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CD))); ao.Buffer = Data_structure.Combine(Encoding.Unicode.GetBytes("^^^"), GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CD)))); ao.WorkingSocket = m_ClientSocket; // 전송 시작! try { m_ClientSocket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, m_fnSendHandler, ao); } catch (Exception ex) { Console.WriteLine("SENDING ERROR: {0}", ex.Message); // 서버와 연결이 끊기면 여기서 문제가 생긴다. Make_Client_Event(Unit_Event_Type.Server_Connection_Broken, 0); Connected = false; } }
/// <summary> /// 获取录制字节数据 /// </summary> /// <returns></returns> public byte[] GetClipData() { if (m_MicroPhoneSource.clip == null) { Debug.Log("GetClipData audio.clip is null"); return(null); } int sample = m_MicroPhoneSource.clip.frequency * Mathf.CeilToInt(m_CurrentRecordDuration); float[] samples = new float[sample]; m_MicroPhoneSource.clip.GetData(samples, 0); byte[] ret = null; using (MemoryStreamExt ms = new MemoryStreamExt()) { for (int i = 0; i < samples.Length; ++i) { ms.WriteFloat(samples[i]); } ret = ms.ToArray(); } ret = GZipCompress.Compress(ret); return(ret); }
/// <summary> /// 封装数据包 /// </summary> /// <param name="data">要发送的数据</param> /// <returns>封装好的数据</returns> private byte[] MakeDatas(byte[] data, int protoCode) { int leng = 0; bool isCompress; if (data == null) { leng = 5; isCompress = false; } else { data = EncryptUtil.NetEncrypt(data, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected); isCompress = data.Length >= COMPRESS_LENGTH; if (isCompress) { data = GZipCompress.Compress(data); } leng = data.Length + 5; } byte[] head = new byte[4]; head[0] = (byte)((leng & 0xFF000000) >> 24); head[1] = (byte)((leng & 0x00FF0000) >> 16); head[2] = (byte)((leng & 0x0000FF00) >> 8); head[3] = (byte)((leng & 0x000000FF)); byte[] protoCodeBuffer = BitConverter.GetBytes(protoCode); Array.Reverse(protoCodeBuffer); using (MemoryStreamExt ms = new MemoryStreamExt()) { ms.Write(head, 0, 4); ms.WriteBool(isCompress); ms.Write(protoCodeBuffer, 0, protoCodeBuffer.Length); if (data != null) { ms.Write(data, 0, data.Length); } return(ms.ToArray()); } }
private void send_command(Command_Server command_code, object data) //즉시 보낸다. { if (Unit_No == -1) return; AsyncObject ao = new AsyncObject(1); command_data_server CD = new command_data_server(Unit_No, command_code, data); // 문자열을 바이트 배열으로 변환 ao.Buffer = Data_structure.Combine(Encoding.Unicode.GetBytes("^^^"), GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CD)))); //ao.Buffer = GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CD))); ao.WorkingSocket = m_ClientSocket; // 전송 시작! try { m_ClientSocket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, m_fnSendHandler, ao); } catch (Exception ex) { Console.WriteLine("전송 중 오류 발생!\n메세지: {0}", ex.Message); Flag_Restart = true; } }
/// <summary> /// 是否选择文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Sfd_FileOk(object sender, CancelEventArgs e) { var sfd = sender as System.Windows.Forms.SaveFileDialog; var inputPath = txtInput.Text.Trim(); var zipFileName = sfd.FileName.Trim(); VersionFileInfo.ApplicationName = txtAppID.Text.Trim(); var versionFileInfo = new VersionFileInfo(); versionFileInfo.AppID = txtAppID.Text.Trim(); versionFileInfo.Version = txtVersion.Text.Trim(); versionFileInfo.FileName = System.IO.Path.GetFileName(zipFileName); versionFileInfo.IsZip = true; versionFileInfo.SaveAs(); var path = VersionFileInfo.LocalFileInfoPath + @"\" + VersionFileInfo.FileInfoName; System.IO.File.Copy(path, inputPath + @"\" + VersionFileInfo.FileInfoName, true); System.IO.File.Copy(path, zipFileName.Substring(0, zipFileName.Length - versionFileInfo.FileName.Length) + @"\" + VersionFileInfo.FileInfoName, true); System.IO.File.Delete(path); var controller = await this.ShowProgressAsync("提示信息", "启动生成", false, MetroDialogSettings); controller.SetIndeterminate(); //测试压缩和解压缩 GZipCompress gz = new GZipCompress(); gz.Message = message => { controller.SetMessage(message + System.Environment.NewLine); //Message = message + System.Environment.NewLine; }; await System.Threading.Tasks.Task.Factory.StartNew(() => { //压缩 gz.DirPath = inputPath; //@"D:\Users\Tidus\Desktop\EAP-System-备份20170315\EAP-System"; gz.ZipFileName = zipFileName; //@"D:\Users\Tidus\Desktop\EAP-System-备份20170315\zz.gz"; gz.Compress(); //Message = "-------------------------------------------------------------------------" + System.Environment.NewLine; //Message = "-------------------------------------------------------------------------" + System.Environment.NewLine; //Message = "-------------------------------------------------------------------------" + System.Environment.NewLine; //解压缩 //gz.DirPath = inputPath + @"\..\" + "test"; //@"D:\Users\Tidus\Desktop\EAP-System-备份20170315\test"; //gz.ZipFileName = zipFileName;// @"D:\Users\Tidus\Desktop\EAP-System-备份20170315\zz.gz"; //gz.DeCompress(); }); await this.ShowMessageAsync("提示信息", "自动更新包已创建完成!", MessageDialogStyle.Affirmative, MetroDialogSettings); await controller.CloseAsync(); txtAppID.Text = ""; txtInput.Text = ""; txtVersion.Text = ""; //MessageBox.Show("已完成!", "提示信息", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); }
private static void Thread_Responser_() { command_data_server CMD; while (Connected) { while (Responser_Q.Count > 0) { lock (Responser_Q) { CMD = (command_data_server)Responser_Q.Dequeue(); } try { AsyncObject ao = new AsyncObject(1); // 문자열을 바이트 배열으로 변환 //ao.Buffer = GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CMD))); ao.Buffer = Data_structure.Combine(Encoding.Unicode.GetBytes("^^^"), GZipCompress.Compress(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(CMD)))); ao.WorkingSocket = m_ClientSocket; ao.WorkingSocket.BeginSend(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, m_fnSendHandler, ao); } catch (Exception ex) { Console.Write("Error Send Message [ Command : {0}, data : {1}, receiver]", CMD.Command_code.ToString(), CMD.data != null ? CMD.data.ToString() : "", CMD.Sender.ToString()); Console.Write("Error String: {0}", ex); } } Thread.Sleep(100); } }
// byte[] = byte[] /// <summary> /// GZip压缩后3DES加密,密钥长度必需是24字节 /// </summary> /// <param name="hexStringKey">密钥</param> /// <param name="source"></param> /// <returns></returns> public static byte[] GZipAnd3Des(string hexStringKey, byte[] source) { byte[] gZipResult = GZipCompress.Compress(source); return(Cryptography.TripleDesEncrypt(hexStringKey, gZipResult)); }