SuperSocket
SuperSocket copied to clipboard
ReceiveFilter和ExecuteCommand的执行机制的疑问
HI @kerryjiang
非常感谢你能提供这么好的socket框架。我在使用的过程中,遇到一些问题,想咨询您一下,谢谢
MyReceiveFilter
public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
try
{
rest = 0;
//按照包格式进行解析 头为 0x55
if (readBuffer[offset] == 0x55)
{
int status = 2; //1完整的一条 2不完整 3超过一条
MyRequestInfo res = new MyRequestInfo();
//构造 res
res.DeviceType = System.Convert.ToInt32(readBuffer[offset + 1].ToString(), 10);
res.MsgType = readBuffer[offset + 4];
res.MsgLength = System.Convert.ToInt32((readBuffer[offset + 5] << 8).ToString(), 10) +
System.Convert.ToInt32((readBuffer[offset + 6]).ToString(), 10);
res.MsgContent = new byte[res.MsgLength];
//判断是否超出长度
if (res.MsgLength+ offset + 7> readBuffer.Length-1)
{
return null;
}
for (int i = 0; i < res.MsgLength; i++)
{
res.MsgContent[i] = readBuffer[offset + 7 + i];
}
//拆包 9是除了内容之外的长度
int all_len = 9 + res.MsgLength;
if (length - all_len>9 && readBuffer[offset + all_len] ==0x55)
{
status = 3;
//超过一条
rest = length - all_len;
}
else
{
status = 1;
}
if (status == 1)
{
//完整的一条消息
res.Key = res.MsgType.ToString();
return res;
}
else if (status == 2)
{
return null;
}
else if (status == 3)
{
//当你在接收缓冲区中找到一条完整的请求, 但接收到的数据并不仅仅包含一个请求时,设置剩余数据的长度到输出变量 "rest"
res.Key = res.MsgType.ToString();
return res;
}
else
{
return null;
}
}
else
{
return null;
}
//throw new NotImplementedException();
}
catch (Exception e)
{
rest = 0;
MyLogger.Error(e.ToString());
return null;
}
}
Command
public override void ExecuteCommand(MySession session, MyRequestInfo requestInfo)
{
try
{
MyLogger.Debug("rec Heart start");
//更新心跳接收时间
SessionManager.UpdateHearTime(session);
//更新设备类型
SessionManager.UpdateDeviceType(session, requestInfo.DeviceType);
//0x02设备类型
//0x01代表消息类型
//1 代表 消息内容长度
//new byte[1] { 0x01 } 消息内容
ArraySegment<byte> rsp = new ArraySegment<byte>(CommandUtils.ProtocolDecode(0x02, 0x01, 1, new byte[1] { 0x01 }));
session.Send(rsp);
MyLogger.Debug("send Heart end");
}
catch (Exception e)
{
MyLogger.Error(e.ToString());
}
}
当我定义一个command的时候,如果我直接在ExecuteCommand里面写一些业务逻辑去执行,那么ReceiveFilter会在ExecuteCommand执行完毕之后,才会继续执行。
请问这里不是异步的吗?还是需要自己处理?谢谢
1.Filter request first https://github.com/kerryjiang/SuperSocket/blob/v1.6/SocketBase/AppSession.cs#L574 https://github.com/kerryjiang/SuperSocket/blob/v1.6/SocketBase/AppSession.cs#L515
2.Then execute command https://github.com/kerryjiang/SuperSocket/blob/v1.6/SocketBase/AppSession.cs#L580
Hi @chucklu ,I got it. Therefore, I should implement asynchronous in ‘command’ myself to prevent blocking from happening. Thanks
Please upgrade to SuperSocket 2.0.
It supports async commands very well.