/******************************
* 说明:收到数据的解析器,用于解析返回的内容
* 创建人:龚宇超
* 创建日期:2018-01-23
* 修改人:
* 修改日期:
* 修改备注:
* 版本:1.0.0.0
******************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lskj.SocketService
{
///
/// 收到数据的解析器,用于解析返回的内容
///
public class IncomingDataParser
{
private string _header;
private string _command;
private List _names;
private List _values;
///
/// 解析头数据
///
/// The header.
public string Header { get { return _header; } }
///
/// 解析command数据
///
/// The command.
public string Command { get { return _command; } }
public List Names { get { return _names; } }
public List Values { get { return _values; } }
public IncomingDataParser()
{
_names = new List();
_values = new List();
}
///
/// 说明:解析协议文本
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol text.
/// true if XXXX, false otherwise.
public bool DecodeProtocolText(string protocolText)
{
_header = "";
_names.Clear();
_values.Clear();
int speIndex = protocolText.IndexOf(ProtocolKey.ReturnWrap);
if (speIndex < 0)
{
return false;
}
else
{
string[] tmpNameValues = protocolText.Split(new string[] { ProtocolKey.ReturnWrap }, StringSplitOptions.RemoveEmptyEntries);
if (tmpNameValues.Length < 2) //每次命令至少包括两行
return false;
for (int i = 0; i < tmpNameValues.Length; i++)
{
string[] tmpStr = tmpNameValues[i].Split(new string[] { ProtocolKey.EqualSign }, StringSplitOptions.None);
if (tmpStr.Length > 1) //存在等号
{
if (tmpStr.Length > 2) //超过两个等号,返回失败
return false;
if (tmpStr[0].Equals(ProtocolKey.Command, StringComparison.CurrentCultureIgnoreCase))
{
_command = tmpStr[1];
}
else
{
_names.Add(tmpStr[0].ToLower());
_values.Add(tmpStr[1]);
}
}
}
return true;
}
}
///
/// 说明:
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// List<System.String>.
public List GetValue(string protocolKey)
{
List result = new List();
for (int i = 0; i < _names.Count; i++)
{
if (protocolKey.Equals(_names[i], StringComparison.CurrentCultureIgnoreCase))
result.Add(_values[i]);
}
return result;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref string value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
{
value = _values[index];
return true;
}
else
return false;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref short value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
{
return short.TryParse(_values[index], out value);
}
else
return false;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref int value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
return int.TryParse(_values[index], out value);
else
return false;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref long value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
return long.TryParse(_values[index], out value);
else
return false;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref Single value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
return Single.TryParse(_values[index], out value);
else
return false;
}
///
/// 说明:通过Key获取Value
/// 创建人:龚宇超
/// 创建日期:2018-01-24
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The protocol key.
/// The value.
/// true if XXXX, false otherwise.
public bool GetValue(string protocolKey, ref Double value)
{
int index = _names.IndexOf(protocolKey.ToLower());
if (index > -1)
return Double.TryParse(_values[index], out value);
else
return false;
}
}
}