コード例 #1
0
        /// <summary>
        /// Gzip壓縮
        /// </summary>
        public static int GzipCompress(ref byte[] srcBuffer, ref byte[] cmpBuffer)
        {
            string _CompressSaveFilePath = null;

            try
            {
                string directoryName = "";
                if (OSType != 0)
                {
                    directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Compress\\";
                }
                else
                {
                    directoryName = "./" + "Compress/";
                }

                DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                if (!Createdir.Exists)
                {
                    Createdir.Create();
                }

                _CompressSaveFilePath = directoryName + RandomSession.createSession(64);

                using (FileStream fs_saveCompress = new FileStream(_CompressSaveFilePath, FileMode.Create, FileAccess.Write))
                {
                    using (GZipStream ZipStream = new GZipStream(fs_saveCompress, CompressionMode.Compress, true))
                    {
                        ZipStream.Write(srcBuffer, 0, srcBuffer.Length);
                    }
                }

                using (FileStream fs_open = new FileStream(_CompressSaveFilePath, FileMode.Open, FileAccess.Read))
                {
                    cmpBuffer = new byte[Convert.ToInt32(fs_open.Length)];
                    fs_open.Read(cmpBuffer, 0, Convert.ToInt32(fs_open.Length));
                }

                return(1);
            }
            catch (Exception ex)
            {
                KConsole.Write(ErrorLevel.Serious, "Kernel>>Tools>>GzipCompress>>", ex.Message);
                return(0);
            }
            finally
            {
                DiskIO.Del(_CompressSaveFilePath);
            }
        }
コード例 #2
0
        public void ReceiveCallback(IAsyncResult ar)
        {
            //TCPClientStateObject state1 = null;
            TCPClientStateObject state = null;
            bool closeSocket           = false;

            try
            {
                state = (TCPClientStateObject)ar.AsyncState;
                //MsgBackQue.Enqueue(state1);
                //object obj = MsgBackQue.Dequeue();
                //state = (TCPClientStateObject)obj;
                Socket userClient = state.workSocket;
                int    bytesRead  = 0;

                //...判斷socket連線是否斷掉
                if (userClient == null || !userClient.Connected)
                {
                    closeSocket = true;
                    return;
                }

                bytesRead = userClient.EndReceive(ar);



                if (bytesRead > 0)
                {
                    #region//...是否讀過 Header
                    if (!state.isReadHeader)
                    {
                        //...建立接收封包的存放位置
                        string directoryName = "/" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "/Process/";

                        if (OSType != 0)
                        {
                            directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Process\\";
                        }
                        DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                        if (!Createdir.Exists)
                        {
                            Createdir.Create();
                        }
                        //state.receiveFileTemporarily = directoryName + RandomSession.createSession(64);
                        state.receiveFileTemporarily = directoryName + "cli_" + Guid.NewGuid().ToString();

                        byte[] HeadReceiveBuffer = new byte[bytesRead];
                        Array.Copy(state.receiveBuffer, HeadReceiveBuffer, bytesRead);
                        state.ListHeadBuffer.AddRange(HeadReceiveBuffer);
                        state.headerBufferSize = state.headerBufferSize - bytesRead;

                        if (state.headerBufferSize == 0)
                        {
                            state.headerBufferSize = 9;
                            byte[] TempHeadReceiveBuffer = state.ListHeadBuffer.ToArray();
                            state.ListHeadBuffer.Clear();
                            using (MemoryStream ms_readheader = new MemoryStream(TempHeadReceiveBuffer, 0, TempHeadReceiveBuffer.Length))
                            {
                                BinaryReader br = new BinaryReader(ms_readheader);
                                state.ver  = br.ReadByte();                     //...封包版本號
                                state.cmd1 = br.ReadInt16();                    //...命令1
                                state.cmd2 = br.ReadInt16();                    //...命令2
                                state.receivePackageTotalSize = br.ReadInt32(); //...封包內容總長度
                            }

                            if (state.receivePackageTotalSize <= 0)
                            {
                                //...沒有夾帶內容
                                state.isReadHeader  = false;                            //...讀取完畢將標記改為false
                                state.receiveBuffer = new byte[state.headerBufferSize]; //...Buffer 清空
                                ReceiveEvent(state);                                    //...接收完畢號 處理事件
                            }
                            else
                            {
                                state.receiveBuffer = new byte[state.receivePackageTotalSize]; //... 080609 by randy
                                state.isReadHeader  = true;                                    //,,,將標記設定為讀過標頭,可以繼續接收資料
                            }
                        }
                        else if (state.headerBufferSize > 0)
                        {
                            //...繼續接收
                            state.receiveBuffer = new byte[state.headerBufferSize];//--randy,2008.06.09
                        }
                        //...接收buffer的資料
                        if (userClient == null || !userClient.Connected)
                        {
                            closeSocket = true;
                            return;
                        }
                        if (userClient.Connected)
                        {
                            userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallback), state);
                            return;
                        }
                        else
                        {
                            closeSocket = true;
                            return;
                        }
                    }
                    #endregion

                    #region//...儲存數據

                    DiskIO.Save(state.receiveFileTemporarily, state.receiveBuffer, bytesRead);
                    state.receivePackageTotalSize = state.receivePackageTotalSize - bytesRead;//--randy,2008.06.09
                    if (state.receivePackageTotalSize == 0)
                    {
                        //...接收完畢
                        state.isReadHeader  = false;                            //...讀取Header reset
                        state.receiveBuffer = new byte[state.headerBufferSize]; //...recevice buffer reset
                        ReceiveEvent(state);                                    //...接收完畢號 處理事件
                        //receiveDone.Set();
                    }
                    else if (state.receivePackageTotalSize > 0)
                    {
                        //...繼續接收
                        state.receiveBuffer = new byte[state.receivePackageTotalSize];//--randy,2008.06.09
                    }
                    #endregion

                    #region//...接收buffer的資料
                    //...接收buffer的資料
                    if (userClient.Connected)
                    {
                        userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallback), state);
                    }
                    else
                    {
                        this._connected = false;
                        closeSocket     = true;
                    }
                    #endregion
                }
                else
                {
                    KConsole.Write(ErrorLevel.Warn, "Kernel>>AsynTCPClient>>ReadCallback>>", "接收到的數據長度<=0");
                    closeSocket = true;
                }
            }
            catch (Exception ex)
            {
                KConsole.Write(ErrorLevel.Serious, "Kernel>>AsynTCPClient>>ReceiveCallback>>", ex.Message);
                this._connected = false;
                closeSocket     = true;
            }
            finally
            {
                if (closeSocket)
                {
                    if (ExceptionHandlingEvent != null)
                    {
                        ExceptionHandlingEvent(state);
                    }
                    //receiveDone.Set();
                    state = null;
                    //GC.Collect();
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Gzip 解壓縮
        /// </summary>
        public static int Decompress(byte[] _srcBuffer, ref byte[] deCmpBuffer)
        {
            string _DecompressSaveFilePath = null;

            try
            {
                string directoryName = "";
                if (OSType != 0)
                {
                    directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Compress\\";
                }
                else
                {
                    directoryName = "./" + "Compress/";
                }


                DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                if (!Createdir.Exists)
                {
                    Createdir.Create();
                }

                _DecompressSaveFilePath = directoryName + RandomSession.createSession(64);


                using (MemoryStream ms_open = new MemoryStream(_srcBuffer))
                {
                    using (GZipStream zipStream = new GZipStream(ms_open, CompressionMode.Decompress))
                    {
                        using (FileStream fs_saveDecompress = new FileStream(_DecompressSaveFilePath, FileMode.Create, FileAccess.Write, FileShare.Write))
                        {
                            using (BinaryWriter bw = new BinaryWriter(fs_saveDecompress))
                            {
                                int    offset     = 0;
                                int    BufferSize = 512;
                                int    bytesRead  = 0;
                                byte[] buffer     = new byte[BufferSize];
                                while (true)
                                {
                                    bytesRead = zipStream.Read(buffer, offset, BufferSize);
                                    if (bytesRead == 0)
                                    {
                                        break;
                                    }
                                    bw.Write(buffer, 0, bytesRead);
                                }
                            }
                        }


                        using (FileStream fs_open = new FileStream(_DecompressSaveFilePath, FileMode.Open, FileAccess.Read))
                        {
                            deCmpBuffer = new byte[Convert.ToInt32(fs_open.Length)];
                            fs_open.Read(deCmpBuffer, 0, Convert.ToInt32(fs_open.Length));
                        }
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                ///KConsole.Write(ErrorLevel.Serious, "Kernel>>Tools>>Decompress>>", ex.Message);
                Console.Write("Kernel>>Tools>>Decompress>>" + ex.Message);
                return(0);
            }
            finally
            {
                DiskIO.Del(_DecompressSaveFilePath);
            }
        }
コード例 #4
0
        public void ReadCallback(IAsyncResult ar)
        {
            StateObject state       = null;
            bool        closeSocket = false;

            try
            {
                state = (StateObject)ar.AsyncState;
                Socket userClient = state.workSocket;
                int    bytesRead  = 0;

                //...判斷socket連線是否斷掉
                if (userClient == null || !userClient.Connected)
                {
                    closeSocket = true;
                    return;
                }

                bytesRead = userClient.EndReceive(ar);



                if (bytesRead > 0)
                {
                    #region//...是否讀過 Header
                    if (!state.isReadHeader)
                    {
                        //...建立接收封包的存放位置
                        //string directoryName = "/"+Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "/Process/";
                        string directoryName = "./Process/";

                        if (OSType != 0)
                        {
                            directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\Process\\";
                        }
                        DirectoryInfo Createdir = new DirectoryInfo(directoryName);
                        if (!Createdir.Exists)
                        {
                            Createdir.Create();
                        }
                        //state.receiveFileTemporarily = directoryName + RandomSession.createSession(64);
                        state.receiveFileTemporarily = directoryName + Guid.NewGuid();

                        using (MemoryStream ms_readheader = new MemoryStream(state.receiveBuffer, 0, bytesRead))
                        {
                            BinaryReader br = new BinaryReader(ms_readheader);
                            state.ver  = br.ReadByte();                     //...封包版本號
                            state.cmd1 = br.ReadInt16();                    //...命令1
                            state.cmd2 = br.ReadInt16();                    //...命令2
                            state.receivePackageTotalSize = br.ReadInt32(); //...封包內容總長度

                            if (state.receivePackageTotalSize <= 0)
                            {
                                //...沒有夾帶內容
                                state.isReadHeader           = false;                            //...讀取完畢將標記改為false
                                state.receiveBuffer          = null;
                                state.receiveBuffer          = new byte[state.headerBufferSize]; //...Buffer 清空
                                state.receiveFileTemporarily = null;
                                GoToProcess(state);                                              //...接收完畢號 處理事件
                            }
                            else
                            {
                                //...當有夾帶內容時
                                state.receiveBuffer = null;
                                state.receiveBuffer = new byte[state.receivePackageTotalSize]; //... 080609 by randy
                                state.isReadHeader  = true;                                    //,,,將標記設定為讀過標頭,可以繼續接收資料
                            }
                        }

                        //...接收buffer的資料
                        if (userClient == null || !userClient.Connected)
                        {
                            closeSocket = true;
                            return;
                        }

                        userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReadCallback), state);
                        return;
                    }
                    #endregion

                    #region //...儲存數據

                    DiskIO.Save(state.receiveFileTemporarily, state.receiveBuffer, bytesRead);


                    state.receivePackageTotalSize = state.receivePackageTotalSize - bytesRead;//--randy,2008.06.09
                    if (state.receivePackageTotalSize == 0)
                    {
                        //...接收完畢
                        state.isReadHeader  = false;                            //...讀取Header reset
                        state.receiveBuffer = new byte[state.headerBufferSize]; //...recevice buffer reset
                        GoToProcess(state);                                     //...接收完畢號 處理事件
                    }
                    else if (state.receivePackageTotalSize > 0)
                    {
                        //...繼續接收
                        state.receiveBuffer = new byte[state.receivePackageTotalSize];//--randy,2008.06.09
                    }
                    #endregion

                    #region//...接收buffer的資料
                    //...接收buffer的資料
                    if (userClient == null || !userClient.Connected)
                    {
                        closeSocket = true;
                        return;
                    }
                    userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReadCallback), state);

                    #endregion
                }
                else
                {
                    //KConsole.Write(ErrorLevel.Response, "", "Kernel>>AsynTCPSocket>>ReadCallback>>Exception: 接收到的數據長度<=0");
                    //KConsole.Write(ErrorLevel.Warn, "Kernel>>AsynTCPSocket>>ReadCallback>>", "接收到的數據長度<=0");
                    closeSocket = true;
                }
            }
            catch (Exception ex)
            {
                //KConsole.Write(ErrorLevel.Serious, "Kernel>>AsynTCPSocket>>ReadCallback>>", ex.Message);
                KConsole.Write(ErrorLevel.Response, "", "Kernel>>AsynTCPSocket>>ReadCallback>>Exception:" + ex.Message);
                closeSocket = true;
            }
            finally
            {
                if (closeSocket)
                {
                    if (ExceptionHandlingEvent != null)
                    {
                        ExceptionHandlingEvent(state);
                    }
                    state = null;
                    GC.Collect();
                }
            }
        }