写自己的Socket框架(三)

时间:2023-03-09 03:30:55
写自己的Socket框架(三)

在通信写完了以后,应用层接收到Socket抛上来的byte[],这个时候对于实际的写逻辑的开发者来说,这样的数据并不友好,我们就需要在应用层统一一个包的规则(应用层协议),处理完以后,然后再传给实际的逻辑层去处理。

以下是一个常用的Command模式。既接收到传递过来的包以后,根据Command(命令)来执行对应的Command(逻辑)。

我们假定我们的包(以下所有的包都指的是应用层的包,而非Socket层的包)分为 命令头/数据 两块。

public class InterUnit
{
public string Command;
public JToken Body;
}

因为采用Command模式,我们定义了一个接口ICommand

    public interface ICommand
{
InterUnit Execute(InterUnit unit);
}

命令的Command,如何跟实际逻辑对应起来,常用的有Ioc,但是你也可以硬编码,我采用的是Attribute的方式,来对应起来。

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class CommandAttribute : Attribute
{
public CommandAttribute(string command)
{
this.Command = command;
} public string Command;
}

对应起来以后,那就需要在接到包的地方,去根据Command找到对应的Class来执行逻辑。

public class CommandFactory
{
private Dictionary<string, ICommand> _commandMap; public CommandFactory()
{
if (_commandMap == null)
{
_commandMap = new Dictionary<string, ICommand>();
}
} /// <summary>
/// 通过反射将标注了CommandAttribute的实例,放入字典。
/// 不需要等到需要调用时,才去动态的注入。
/// </summary>
/// <param name="assembly"></param>
public void Init(params string[] assembly)
{
if (assembly != null)
{
foreach (string s in assembly)
{
var ass = Assembly.Load(s);
if (ass != null)
{
var types = ass.GetTypes();
foreach (var type in types)
{
CommandAttribute attr = type.GetCustomAttribute(typeof(CommandAttribute), false) as CommandAttribute;
if (attr != null)
{
if (attr.Command == null || attr.Command.Length == )
{
_commandMap[type.Name] = Activator.CreateInstance(type) as ICommand;
}
else
{
_commandMap[attr.Command] = Activator.CreateInstance(type) as ICommand;
}
}
}
}
}
}
} public void ExecuteCommand(SocketSession session, InterUnit unit)
{
if(_commandMap.ContainsKey(unit.Command))
{
ICommand command = _commandMap[unit.Command];
var rtv = command.Execute(unit);
if (rtv != null)
{
session.Send(BsonHelper.ToBson<InterUnit>(unit));
}
}
}
}

我在这里采用的是Bson的格式,作为数据来传递。

有一个地方需要注意的就是,在Send的时候,实际上我们并没有定义Socket的包的格式,因为在协议的地方已经处理了这个事情,会将你发送过去的数据,自动加上包头。

public interface IProtocol
{
byte[] OnDataReceivedCallBack(byte[] data, ref int offset); byte[] OnDataSendBefore(byte[] data);
} public class DefaultProtocol : IProtocol
{
public byte[] OnDataReceivedCallBack(byte[] data, ref int offset)
{
int length = BitConverter.ToInt32(data, offset);
int package_head = ;
int package_length = length + package_head;
byte[] buffer = null;
if (length > )
{
if (offset + package_length <= data.Length)
{
buffer = new byte[length];
Array.Copy(data, offset + package_head, buffer, , length);
offset += package_length;
}
}
else
{
offset = -;
}
return buffer;
} public byte[] OnDataSendBefore(byte[] data)
{
int length = data.Length;
var head = BitConverter.GetBytes(length);
byte[] buffer = new byte[data.Length + head.Length];
Buffer.BlockCopy(head, , buffer, , head.Length);
Buffer.BlockCopy(data, , buffer, head.Length, data.Length); return buffer;
}
}