国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

寫自己的Socket框架(三)

2019-11-17 02:57:52
字體:
來源:轉載
供稿:網友

寫自己的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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霍邱县| 西昌市| 黄龙县| 惠安县| 霍山县| 故城县| 靖远县| 喀喇| 宝应县| 宜兰县| 定安县| 永定县| 泰和县| 西乌| 醴陵市| 尼木县| 通海县| 墨江| 崇左市| 宜宾县| 兰州市| 淮阳县| 应用必备| 陇川县| 唐河县| 寿光市| 若羌县| 邹平县| 时尚| 固镇县| 浦北县| 太保市| 高阳县| 鄂伦春自治旗| 岳阳市| 长沙县| 区。| 隆德县| 宁海县| 长白| 体育|