• 写自己的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
    本人对代码不做任何知识产权限制,也不保证所有的代码皆为原创。
  • 相关阅读:
    ubuntu-14.04.2-desktop-amd64.iso:ubuntu-14.04.2-desktop-amd64:安装Oracle11gR2
    ubuntu-15.04-desktop-amd64.iso:ubuntu-15.04-desktop-amd64:安装Oracle11gR2
    Ubuntu 使用文件 casper-rw 镜像文件 保存变更内容
    continue — Skip to the next iteration of a loop in a shell script
    Is there a TRY CATCH command in Bash
    tar 打包压缩
    oracle 重复只保留一条
    sed 删除 51, 54 行 输出到原文件
    ORA-01000: maximum open cursors exceeded
    Oracle 在字符串中输入单引号或特殊字符
  • 原文地址:https://www.cnblogs.com/selfteam/p/3924605.html
Copyright © 2020-2023  润新知