博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastSocket学习笔记~制定自已的传输协议~续~制定基于FastSocket的协议
阅读量:7074 次
发布时间:2019-06-28

本文共 4247 字,大约阅读时间需要 14 分钟。

,它使用简单,功能强大,扩展灵活,目前在新浪的生产环境中已经被广泛使用,所以它的性能,安全等各方面我们绝对可以信赖,今天我们来说一个话题,,这次我们制作一个基于FastSocket的传输协议,它的意义重大,当fastSocket提供的协议不能满足项目要求时,我们就必须硬着头皮去自己写了,还好,fastsocket为我们铺好了路,我们只要按着这条路走下去,就可以了。

自定义的协议格式如下

说干就干

首先,如果要想扩展一个自己的协议,要对 client和server端分别进行开发,下面我们来看一下client的开发

我们要添加的类有三个文件组成,分别是DSSBinaryProtocol,DSSBinaryResponse和一个使用这个协议的客户端入口DSSBinarySocketClient

DSSBinaryProtocol

 
View Code

DSSBinaryResponse

 
View Code

DSSBinarySocketClient

 
View Code

然后,我们再来说一下server端的开发,它有两个文件组成,分别是DSSBinaryCommandInfo,DSSBinaryProtocol

DSSBinaryCommandInfo

 
View Code

DSSBinaryProtocol

 
View Code

除了上面两个文件外,我们还要修改服务端的管理类

///     /// Socket server manager.    ///     public class SocketServerManager    {        #region Private Members        static private readonly List
_listHosts = new List
(); #endregion #region Static Methods ///
/// 初始化Socket Server /// static public void Init() { Init("socketServer"); } ///
/// 初始化Socket Server /// ///
static public void Init(string sectionName) { if (string.IsNullOrEmpty(sectionName)) throw new ArgumentNullException("sectionName"); Init(ConfigurationManager.GetSection(sectionName) as Config.SocketServerConfig); } ///
/// 初始化Socket Server /// ///
static public void Init(Config.SocketServerConfig config) { if (config == null) throw new ArgumentNullException("config"); if (config.Servers == null) return; foreach (Config.Server serverConfig in config.Servers) { //inti protocol var objProtocol = GetProtocol(serverConfig.Protocol); if (objProtocol == null) throw new InvalidOperationException("protocol"); //init custom service var tService = Type.GetType(serverConfig.ServiceType, false); if (tService == null) throw new InvalidOperationException("serviceType"); var serviceFace = tService.GetInterface(typeof(ISocketService<>).Name); if (serviceFace == null) throw new InvalidOperationException("serviceType"); var objService = Activator.CreateInstance(tService); if (objService == null) throw new InvalidOperationException("serviceType"); //init host. var host = Activator.CreateInstance(typeof(SocketServer<>).MakeGenericType( serviceFace.GetGenericArguments()), objService, objProtocol, serverConfig.SocketBufferSize, serverConfig.MessageBufferSize, serverConfig.MaxMessageSize, serverConfig.MaxConnections) as BaseSocketServer; host.AddListener(serverConfig.Name, new IPEndPoint(IPAddress.Any, serverConfig.Port)); _listHosts.Add(host); } } ///
/// get protocol. /// ///
///
static public object GetProtocol(string protocol) { switch (protocol) { case Protocol.ProtocolNames.AsyncBinary: return new Protocol.AsyncBinaryProtocol(); case Protocol.ProtocolNames.Thrift: return new Protocol.ThriftProtocol(); case Protocol.ProtocolNames.CommandLine: return new Protocol.CommandLineProtocol(); case Protocol.ProtocolNames.DSSBinary: return new Protocol.DSSBinaryProtocol(); } return Activator.CreateInstance(Type.GetType(protocol, false)); } ///
/// 启动服务 /// static public void Start() { foreach (var server in _listHosts) server.Start(); } ///
/// 停止服务 /// static public void Stop() { foreach (var server in _listHosts) server.Stop(); } #endregion }

从上面的代码中,我们看到了自己新加的协议DSSBinary,我们可以在配置文件中对它进行配置,方法和之前说的一样,在这里就不再重复了。

感谢各位的阅读!

 本文转自博客园张占岭(仓储大叔)的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
Wordpress如何更换网站主机?
查看>>
Java连接Oracle数据库简单实例
查看>>
Exchange2010 dag 的部署
查看>>
Linux/UNIX的scp命令用法详解
查看>>
Eclipse(MyEclipse)插件Jigloo的下载与安装
查看>>
软件设计的思想与哲学
查看>>
非常实用的linux系统命令
查看>>
NFS在Centos 6.3下的安装
查看>>
git pull 和本地文件冲突解决
查看>>
iOS音频AAC视频H264编码 推流最佳方案
查看>>
python基础教程(第2版)第五章读后总结;
查看>>
关于在eclipse中使用tomcat的笔记
查看>>
Android自定义控件实现简单的轮播图控件
查看>>
centos 6.4下的samba服务器的构建
查看>>
持续交付:价值主张
查看>>
二进制、八进制、十进制、十六进制之间转换
查看>>
sqlmap 本地安装
查看>>
[计算机术语]缺省
查看>>
JS --事件
查看>>
printStream 和printWriter区别
查看>>