TG.WXCRM.V4/WX.CRM.DataSynServer/Socket/ZxReceiveFilter.cs

76 lines
3.3 KiB
C#

using SuperSocket.Facility.Protocol;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Text;
using WX.CRM.Common;
namespace WX.CRM.DataSynServer.Socket
{
public class ZxReceiveFilter : BeginEndMarkReceiveFilter<StringRequestInfo>
{
public ZxReceiveFilter()
: base(Config.BeginMark, Config.EndMark)
{
}
protected override StringRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length)
{
try
{
var str = Encoding.UTF8.GetString(readBuffer, offset, length);
//LogHelper.Info("请求数据:" + str);
if (!str.StartsWith(Config.BeginMarkStr) || !str.EndsWith(Config.EndMarkStr))
return new StringRequestInfo(Constants.UnKnow, null, null);
str = str.Remove(0, Config.BeginMarkStr.Length);
str = str.Remove(str.Length - Config.EndMarkStr.Length, Config.EndMarkStr.Length);
//LogHelper.Info("去除协议字符数据:" + str);
var result = str.ToObject<StringRequestInfo>();
//var result = Utility.JSONToObject<StringRequestInfo>(str);
//LogHelper.Info("服务端接收到数据:" + result.Key + "," + result.Body);
if (result == null)
return new StringRequestInfo(Constants.UnKnow, null, null);
return result;
}
catch (Exception ex)
{
LogHelper.Error($"解析请求出错:{ex}");
Console.WriteLine(ex.Message);
return null;
}
}
}
//public class ZxReceiveFilter : TerminatorReceiveFilter<StringRequestInfo>
//{
// public ZxReceiveFilter() : base(Encoding.UTF8.GetBytes("||"))
// {
// }
// protected override StringRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length)
// {
// try
// {
// var str = Encoding.UTF8.GetString(readBuffer, offset, length);
// //if (!str.StartsWith(Config.BeginMarkStr) || !str.EndsWith(Config.EndMarkStr))
// // return new StringRequestInfo(Constants.UnKnow, null, null);
// LogHelper.Info("请求数据:" + str);
// if (!str.EndsWith("^^"))
// return new StringRequestInfo(Constants.UnKnow, null, null);
// //str = str.Remove(0, Config.BeginMarkStr.Length);
// str = str.Remove(str.Length - Config.EndMarkStr.Length, Config.EndMarkStr.Length);
// LogHelper.Info("去除协议字符数据:" + str);
// var result = str.ToObject<StringRequestInfo>();
// //var result = Utility.JSONToObject<StringRequestInfo>(str);
// LogHelper.Info("服务端接收到数据:" + result.Key + "," + result.Body);
// if (result == null)
// return new StringRequestInfo(Constants.UnKnow, null, null);
// return result;
// }
// catch (Exception ex)
// {
// LogHelper.Error($"解析请求出错:{ex}");
// Console.WriteLine(ex.Message);
// return null;
// }
// }
//}
}