如果还没有看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 = ; 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 = ; 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 <= )
{
return offsetDelta;
} //Still have data has not been processed
offset = offset + length - rest;
length = rest;
}
}

最后命令的写法要用public 修饰一下

分析到这里,就可以结束了。

05-12 18:26