Files
lserp_cs_6.0/插件库/Lskj.SocketService/AsyncSocketProtocol/ControlSocketProtocol.cs
T
cyf ab56a9bcf7 基线 SVN r240
SVN-Revision: r240
2025-02-06 06:46:06 +00:00

94 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace Lskj.SocketService
{
public class ControlSocketProtocol : BaseSocketProtocol
{
public ControlSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
: base(asyncSocketServer, asyncSocketUserToken)
{
_socketFlag = "Control";
}
public override void Close()
{
base.Close();
}
public override bool ProcessCommand(byte[] buffer, int offset, int count) //处理分完包的数据,子类从这个方法继承
{
ControlSocketCommand command = StrToCommand(_incomingDataParser.Command);
_outgoingDataAssembler.Clear();
_outgoingDataAssembler.AddResponse();
_outgoingDataAssembler.AddCommand(_incomingDataParser.Command);
if (!CheckLogined(command)) //检测登录
{
_outgoingDataAssembler.AddFailure(ProtocolCode.UserHasLogined, "");
return DoSendResult();
}
if (command == ControlSocketCommand.Login)
return DoLogin();
else if (command == ControlSocketCommand.Active)
return DoActive();
else if (command == ControlSocketCommand.GetClients)
return DoGetClients();
else
{
Program.Logger.Error("Unknow command: " + _incomingDataParser.Command);
return false;
}
}
public ControlSocketCommand StrToCommand(string command)
{
if (command.Equals(ProtocolKey.Active, StringComparison.CurrentCultureIgnoreCase))
return ControlSocketCommand.Active;
else if (command.Equals(ProtocolKey.Login, StringComparison.CurrentCultureIgnoreCase))
return ControlSocketCommand.Login;
else if (command.Equals(ProtocolKey.GetClients, StringComparison.CurrentCultureIgnoreCase))
return ControlSocketCommand.GetClients;
else
return ControlSocketCommand.None;
}
public bool CheckLogined(ControlSocketCommand command)
{
if ((command == ControlSocketCommand.Login) | (command == ControlSocketCommand.Active))
return true;
else
return _logined;
}
public bool DoGetClients()
{
AsyncSocketUserToken[] userTokenArray = null;
_asyncSocketServer.AsyncSocketUserTokenList.CopyList(ref userTokenArray);
_outgoingDataAssembler.AddSuccess();
string socketText = "";
for (int i = 0; i < userTokenArray.Length; i++)
{
try
{
socketText = userTokenArray[i].ConnectSocket.LocalEndPoint.ToString() + "\t"
+ userTokenArray[i].ConnectSocket.RemoteEndPoint.ToString() + "\t"
+ (userTokenArray[i].AsyncSocketInvokeElement as BaseSocketProtocol).SocketFlag + "\t"
+ (userTokenArray[i].AsyncSocketInvokeElement as BaseSocketProtocol).UserName + "\t"
+ userTokenArray[i].AsyncSocketInvokeElement.ConnectDT.ToString() + "\t"
+ userTokenArray[i].AsyncSocketInvokeElement.ActiveDT.ToString();
_outgoingDataAssembler.AddValue(ProtocolKey.Item, socketText);
}
catch (Exception E)
{
Program.Logger.ErrorFormat("Get client error, message: {0}", E.Message);
Program.Logger.Error(E.StackTrace);
}
}
return DoSendResult();
}
}
}