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(); } } }
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(); } } }