Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
    /// <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);
    }
Exemplo n.º 3
0
        private void handleDataReceive(IAsyncResult ar)
        {
            // 넘겨진 추가 정보를 가져옵니다.
            // AsyncState 속성의 자료형은 Object 형식이기 때문에 형 변환이 필요합니다~!
            AsyncObject ao = (AsyncObject)ar.AsyncState;

            // 받은 바이트 수 저장할 변수 선언
            Int32 recvBytes;

            try
            {
                // 자료를 수신하고, 수신받은 바이트를 가져옵니다.
                recvBytes = ao.WorkingSocket.EndReceive(ar);
            }
            catch
            {
                // 예외가 발생하면 함수 종료!
                return;
            }

            // 수신받은 자료의 크기가 1 이상일 때에만 자료 처리
            if (recvBytes > 0)
            {
                // 공백 문자들이 많이 발생할 수 있으므로, 받은 바이트 수 만큼 배열을 선언하고 복사한다.
                Byte[] msgByte = new Byte[recvBytes];
                Array.Copy(ao.Buffer, msgByte, recvBytes);
                //string msg = Encoding.Unicode.GetString(msgByte);
                //string msg = Encoding.Unicode.GetString(GZipCompress.Decompress(msgByte));
                // 받은 메세지를 출력

                /////
                byte[][] ss = Data_structure.Separate(msgByte, Encoding.Unicode.GetBytes("^^^"));

                for (int i = 0; i < ss.Length; i++)
                {
                    if (ss[i].Length < 5)
                        continue;
                    Received_Command(Encoding.Unicode.GetString(GZipCompress.Decompress(ss[i])));
                }
                ////
                //Received_Command(msg);
            }

            try
            {
                // 자료 처리가 끝났으면~
                // 이제 다시 데이터를 수신받기 위해서 수신 대기를 해야 합니다.
                // Begin~~ 메서드를 이용해 비동기적으로 작업을 대기했다면
                // 반드시 대리자 함수에서 End~~ 메서드를 이용해 비동기 작업이 끝났다고 알려줘야 합니다!
                ao.WorkingSocket.BeginReceive(ao.Buffer, 0, ao.Buffer.Length, SocketFlags.None, m_fnReceiveHandler, ao);
            }
            catch (Exception ex)
            {
                // 예외가 발생하면 예외 정보 출력 후 함수를 종료한다.
                Console.WriteLine("자료 수신 대기 도중 오류 발생! 메세지: {0}", ex.Message);
                return;
            }
        }
Exemplo n.º 4
0
 public void OnUpdate()
 {
     while (true)
     {
         if (m_ReceiveCount < 5)
         {
             ++m_ReceiveCount;
             lock (m_ReceiveQueue)
             {
                 if (m_ReceiveQueue.Count > 0)
                 {
                     byte[] buffer          = m_ReceiveQueue.Dequeue();
                     byte[] protocodeBuffer = new byte[4];
                     byte[] protoContent    = new byte[buffer.Length - 5];
                     bool   isCompress      = false;
                     using (MemoryStreamExt ms = new MemoryStreamExt(buffer))
                     {
                         isCompress = ms.ReadBool();
                         ms.Read(protocodeBuffer, 0, protocodeBuffer.Length);
                         Array.Reverse(protocodeBuffer);
                         int protoCode = BitConverter.ToInt32(protocodeBuffer, 0);
                         LogSystem.Log("=============================================================收到消息" + protoCode);
                         ms.Read(protoContent, 0, protoContent.Length);
                         if (isCompress)
                         {
                             protoContent = GZipCompress.DeCompress(protoContent);
                         }
                         if (protoCode == OP_PLAYER_CLOSE.CODE)
                         {
                             LogSystem.LogSpecial("服务器返回关闭消息,网络连接断开");
                             Close(true);
                         }
                         else if (protoCode == OP_SYS_HEART.CODE)
                         {
                             lastHeart = OP_SYS_HEART.decode(protoContent);
                         }
                         else
                         {
                             //return;
                             NetDispatcher.Instance.Dispatch(protoCode, protoContent);
                         }
                     }
                 }
                 else
                 {
                     break;
                 }
             }
         }
         else
         {
             m_ReceiveCount = 0;
             break;
         }
     }
     m_SocketStateDic[m_CurrentState].OnUpdate();
 }
Exemplo n.º 5
0
 private void btnUnZip_Click(object sender, EventArgs e)
 {
     if (rbGZip.Checked)
     {
         txt23.Text = GZipCompress.DeCompressStringFromBase64(txt22.Text);
     }
     else
     {
         txt23.Text = ZipCompress.DeCompressStringFromBase64(txt22.Text);
     }
 }
Exemplo n.º 6
0
 private void btnZipToHex_Click(object sender, EventArgs e)
 {
     if (rbGZip.Checked)
     {
         txt22.Text = GZipCompress.CompressStringToHex(txt21.Text);
     }
     else
     {
         txt22.Text = ZipCompress.CompressStringToHex(txt21.Text);
     }
 }
Exemplo n.º 7
0
    /// <summary>
    /// 播放外部音频
    /// </summary>
    /// <param name="bytes"></param>
    public void PlayExternalAudio(byte[] bytes)
    {
        bytes = GZipCompress.DeCompress(bytes);
        float[] fl = new float[bytes.Length / 4];

        using (MemoryStreamExt ms = new MemoryStreamExt(bytes))
        {
            for (int i = 0; i < fl.Length; ++i)
            {
                fl[i] = ms.ReadFloat();
            }
        }

        AudioClip clip = AudioClip.Create("MicroAudio", fl.Length, 1, FREQUENCY, false);

        clip.SetData(fl, 0);

        m_MicroQueue.Enqueue(clip);
    }
Exemplo n.º 8
0
    /// <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());
        }
    }
Exemplo n.º 9
0
        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;
            }

        }
Exemplo n.º 10
0
        /// <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);
        }
Exemplo n.º 11
0
        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);
            }
        }
Exemplo n.º 12
0
        // byte[] = byte[]

        /// <summary>
        /// 3DES解密后GZip解压缩,密钥长度必需是24字节
        /// </summary>
        /// <param name="hexStringKey">密钥串</param>
        /// <param name="encryptSource"></param>
        /// <returns></returns>
        public static byte[] Un3DesAndUnGZip(string hexStringKey, byte[] encryptSource)
        {
            byte[] desResult = Cryptography.TripleDesDecrypt(hexStringKey, encryptSource);
            return(GZipCompress.DeCompress(desResult));
        }
Exemplo n.º 13
0
        // 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));
        }