写自己的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 == 0)
                                {
                                    _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));
                }
            }
        }
    }
View Code
复制代码

我在这里采用的是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 = 4;
            int package_length = length + package_head;
            byte[] buffer = null;
            if (length > 0)
            {
                if (offset + package_length <= data.Length)
                {
                    buffer = new byte[length];
                    Array.Copy(data, offset + package_head, buffer, 0, length);
                    offset += package_length;
                }
            }
            else
            {
                offset = -1;
            }
            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, 0, buffer, 0, head.Length);
            Buffer.BlockCopy(data, 0, buffer, head.Length, data.Length);

            return buffer;
        }
    }
View Code
复制代码

 

posted on   双调  阅读(1030)  评论(3编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示