/****************************** * 说明:吞吐量Socket协议 * 创建人:龚宇超 * 创建日期:2018-01-23 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace Lskj.SocketService { /// /// 吞吐量Socket协议 /// public class ThroughputSocketProtocol : BaseSocketProtocol { public ThroughputSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken) : base(asyncSocketServer, asyncSocketUserToken) { _socketFlag = "Throughput"; } public override void Close() { base.Close(); } public override bool ProcessCommand(byte[] buffer, int offset, int count) //处理分完包的数据,子类从这个方法继承 { ThroughputSocketCommand command = StrToCommand(_incomingDataParser.Command); _outgoingDataAssembler.Clear(); _outgoingDataAssembler.AddResponse(); _outgoingDataAssembler.AddCommand(_incomingDataParser.Command); if (command == ThroughputSocketCommand.CyclePacket) return DoCyclePacket(buffer, offset, count); else { Program.Logger.Error("Unknow command: " + _incomingDataParser.Command); return false; } } public ThroughputSocketCommand StrToCommand(string command) { if (command.Equals(ProtocolKey.CyclePacket, StringComparison.CurrentCultureIgnoreCase)) return ThroughputSocketCommand.CyclePacket; else return ThroughputSocketCommand.None; } public bool DoCyclePacket(byte[] buffer, int offset, int count) { int cycleCount = 0; if (_incomingDataParser.GetValue(ProtocolKey.Count, ref cycleCount)) { _outgoingDataAssembler.AddSuccess(); cycleCount = cycleCount + 1; _outgoingDataAssembler.AddValue(ProtocolKey.Count, cycleCount); } else _outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, ""); return DoSendResult(buffer, offset, count); } } }