private void StockClient_onClose(object sender, CloseEvent e) { if (logger.IsWarnEnabled) logger.WarnFormat("[QuoteService.onClose] QuoteService disconnected... remoteIP={0}, remotePort={1}", e.RemoteIP, e.Port); e.Message = "與伺服器中斷連線,請檢查網路設定。"; OnDisconnect(new QuoteDisconnectEvent(this.DataSource, e.RemoteIP, e.Port)); ReLogin(); //重新登入伺服器 }
private void ReceiveCallback(object sender, SocketAsyncEventArgs e) { bool bError = false; Socket cHandler = (Socket)sender; if (e.SocketError == SocketError.Success) { int iBytesRead = e.BytesTransferred; if (iBytesRead > 0) { SocketToken cToken = (SocketToken)e.UserToken; if (ReceiveProc != null) { if (logger.IsDebugEnabled) logger.DebugFormat("[ZSocket.ReceiveCallback] Socket({0}) packetSize={1}", cHandler.Handle.ToInt32(), iBytesRead); try { cToken.ReceiveBuffer.Length = iBytesRead; if (logger.IsDebugEnabled) logger.DebugFormat("[ZSocket.ReceiveCallback] Socket({0}) Packet Dump:{1}", cHandler.Handle.ToInt32(), cToken.ReceiveBuffer.ToString()); ReceiveProc(this, new ReceiveEvent(cHandler, cToken)); } catch (Exception __errExcep) { bError = true; if (logger.IsErrorEnabled) logger.ErrorFormat("{0}\r\n{1}", __errExcep.Message, __errExcep.StackTrace); } } if (!bError && cHandler.Connected) { try { e.SetBuffer(cToken.ReceiveBuffer.Data, 0, SocketToken.MAX_BUFFER_SIZE); if (!cHandler.ReceiveAsync(e)) ReceiveCallback(sender, e); } catch { bError = true; } } else { bError = true; } } else { bError = true; } } else { bError = true; } if (bError) { e.Completed -= ReceiveCallback; //取消ReceiveCallback事件 lock (__cSocketPool) { __cSocketPool.Push(e); //回收Socket到pool } CloseEvent cCloseEvent = null; if (CloseProc != null) { cCloseEvent = new CloseEvent(cHandler); } try { if (cHandler.Connected) { cHandler.Shutdown(SocketShutdown.Both); cHandler.Disconnect(true); //斷開連結 } } catch { } if (CloseProc != null) { CloseProc(cHandler, cCloseEvent); //發送關閉事件 } cHandler.Close(); //關閉Socket連結 if (logger.IsDebugEnabled) logger.DebugFormat("[ZSocket.ReceiveCallback] Socket({0}) Socket closed...", cHandler.Handle.ToInt32()); } }