SupperSocket深入淺出(二)
阿新 • • 發佈:2017-11-15
break jcs led filter serve str cell fig font
如果還沒有看SuperStock深入淺出(一) ,請先看
這一章,主要說下命令是如果運行的。剛開始的時候會發現拷別人的代碼命令是可以運行的,在修改的過程中突然發現命令無效了?
這裏什麽原因?,我先把代碼貼出來。
SuperSocket.SocketBase.AppServerBase類,可以執行命令代碼,但你會發現
if (m_RequestHandler == null)語名,只有m_RequestHandler為空時才執行。
protected virtual void ExecuteCommand(TAppSession session, TRequestInfo requestInfo) {if (m_RequestHandler == null) { var commandProxy = GetCommandByName(requestInfo.Key); if (commandProxy != null) { var command = commandProxy.Command; var commandFilters = commandProxy.Filters; session.CurrentCommand= requestInfo.Key; var cancelled = false; if (commandFilters == null) { command.ExecuteCommand(session, requestInfo); } else { varcommandContext = new CommandExecutingContext(); commandContext.Initialize(session, requestInfo, command); for (var i = 0; i < commandFilters.Length; i++) { var filter = commandFilters[i]; filter.OnCommandExecuting(commandContext); if (commandContext.Cancel) { cancelled = true; if(Logger.IsInfoEnabled) Logger.Info(session, string.Format("The executing of the command {0} was cancelled by the command filter {1}.", command.Name, filter.GetType().ToString())); break; } } if (!cancelled) { try { command.ExecuteCommand(session, requestInfo); } catch (Exception exc) { commandContext.Exception = exc; } for (var i = 0; i < commandFilters.Length; i++) { var filter = commandFilters[i]; filter.OnCommandExecuted(commandContext); } if (commandContext.Exception != null && !commandContext.ExceptionHandled) { try { session.InternalHandleExcetion(commandContext.Exception); } catch { } } } } if(!cancelled) { session.PrevCommand = requestInfo.Key; if (Config.LogCommand && Logger.IsInfoEnabled) Logger.Info(session, string.Format("Command - {0}", requestInfo.Key)); } } else { session.InternalHandleUnknownRequest(requestInfo); } session.LastActiveTime = DateTime.Now; } else { session.CurrentCommand = requestInfo.Key; try { m_RequestHandler(session, requestInfo); } catch (Exception e) { session.InternalHandleExcetion(e); } session.PrevCommand = requestInfo.Key; session.LastActiveTime = DateTime.Now; if (Config.LogCommand && Logger.IsInfoEnabled) Logger.Info(session, string.Format("Command - {0}", requestInfo.Key)); } Interlocked.Increment(ref m_TotalHandledRequests); }
那麽 m_RequestHandler 對象又是什麽呢,其它很簡單,
private RequestHandler<TAppSession, TRequestInfo> m_RequestHandler;
/// <summary>
/// Occurs when a full request item received.
/// </summary>
public virtual event RequestHandler<TAppSession, TRequestInfo> NewRequestReceived
{
add { m_RequestHandler += value; }
remove { m_RequestHandler -= value; }
}
//app.NewRequestReceived += new SuperSocket.SocketBase.RequestHandler<JCSoftSession, JCSocket.RequestInfo.JCSoftRequestInfo>(app_NewRequestReceived);
你只要不對Appserver.NewRequestReceived 監聽事件就可以了使用命令了
那麽在AppServer中 ExecuteCommand 又是怎樣執行的呢。
大家可以這樣想,首先一定要接收到客戶端消息,才會調用命令,這樣不難我們就相會想到Session對像,有了正確的判斷,你不對發現在下面的代碼
SuperSocket.SocketBase.AppSession 類
int IAppSession.ProcessRequest(byte[] readBuffer, int offset, int length, bool toBeCopied) { int rest, offsetDelta; while (true) { var requestInfo = FilterRequest(readBuffer, offset, length, toBeCopied, out rest, out offsetDelta); if (requestInfo != null) { try { AppServer.ExecuteCommand(this, requestInfo); } catch (Exception e) { HandleException(e); } } if (rest <= 0) { return offsetDelta; } //Still have data has not been processed offset = offset + length - rest; length = rest; } }
分析到這裏,就可以結束了。
SupperSocket深入淺出(二)