/// <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); } }
public void AcceptCallback(IAsyncResult ar) { StateObject state = null; try { Socket server = (Socket)ar.AsyncState; Socket newUser = server.EndAccept(ar); //...建立一個對應的連接 //...設定接收及傳送的緩衝區 //newUser.ReceiveBufferSize = 32768; //newUser.SendBufferSize = 32768; //...建立一個保存連線狀態的物件 state = new StateObject(); state.headerBufferSize = 9; //...設定封包讀取大小 state.workSocket = newUser; //...連接進來的線路 state.receiveBuffer = new byte[state.headerBufferSize]; //string wanip = state.wanIP; //string wanport = state.wanPort.ToString(); //KConsole.Write(ErrorLevel.Debug, "Kernel>>AsynTCPSocket>>AcceptCallback>>連接進來的連線>>", wanip + ":" + wanport); // 每一條連接進來的請求都建立ConnectionSession string ConnectionSession = RandomSession.createSession(32); state.ConnectionSession = ConnectionSession; KConsole.Write(ErrorLevel.Response, "", "Kernel>>AsynTCPSocket>>AcceptCallback>>ConnectionSession:" + ConnectionSession); //KConsole.Write(ErrorLevel.Debug, "Kernel>>AsynTCPSocket>>AcceptCallback>>建立ConnectionSession>>", ConnectionSession); _allDone.Set(); //...開始接收請求 ,先接收封包的標頭 newUser.BeginReceive(state.receiveBuffer, 0, state.headerBufferSize, 0, new AsyncCallback(ReadCallback), state); } catch (Exception ex) { _allDone.Set(); _allDone.Reset(); KConsole.Write(ErrorLevel.Response, "", "Kernel>>AsynTCPSocket>>AcceptCallback>>Exception:" + ex.Message); //KConsole.Write(ErrorLevel.Serious, "Kernel>>AsynTCPSocket>>AcceptCallback>>", ex.Message); if (state != null) { ExceptionHandlingEvent(state); state = null; GC.Collect(); } } }
/// <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); } }
public void ReceiveCallback(IAsyncResult ar) { TCPClientStateObject state = null; bool closeSocket = false; try { state = (TCPClientStateObject)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/"; 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); 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; ReceiveEvent(state); //...接收完畢號 處理事件 //receiveDone.Set(); } else { //...當有夾帶內容時 state.receiveBuffer = null; state.receiveBuffer = new byte[state.receivePackageTotalSize]; //... 080609 by randy state.isReadHeader = true; //,,,將標記設定為讀過標頭,可以繼續接收資料 } } //...接收buffer的資料 userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallback), 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 ReceiveEvent(state); //...接收完畢號 處理事件 //receiveDone.Set(); } else if (state.receivePackageTotalSize > 0) { //...繼續接收 state.receiveBuffer = new byte[state.receivePackageTotalSize];//--randy,2008.06.09 } #endregion #region//...接收buffer的資料 //...接收buffer的資料 userClient.BeginReceive(state.receiveBuffer, 0, state.receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallback), state); #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) { //ExceptionHandlingEvent(state); ////receiveDone.Set(); //state = null; //GC.Collect(); if (ExceptionHandlingEvent != null) { ExceptionHandlingEvent(state); } state = null; GC.Collect(); } } }