如果還沒有看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
{
var commandContext = 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;
}
}
最後指令的寫法要用public 修飾一下
分析到這裡,就可以結束了。
以上隻是個人想法和實踐經驗所得,如果有文字錯誤和文法錯誤,請加以指點!
QQ:247039968
emil:[email protected]
無論是美女的歌聲,還是鬣狗的狂吠,無論是鳄魚的眼淚,還是惡狼的嚎叫,都不會使我動搖