SuperSocket使用 IRequestInfo 和 IReceiveFilter 等对象实现自定义协议
为什么你要使用自定义协议?
通信协议用于将接收到的二进制数据转化成您的应用程序可以理解的请求。 SuperSocket提供了一个内置的通信协议“命令行协议”定义每个请求都必须以回车换行"\r\n"结尾。
但是一些应用程序无法使用命令行协议由于不同的原因。 这种情况下,你需要使用下面的工具来实现你的自定义协议:
* data class
* RequestInfo
* ReceiveFilter
* ReceiveFilterFactory
* AppServer and AppSession
第一步、定义一个和协议合适的数据结构
数据结构是通信数据存储的结构形式,也是自定义协议的基础,每个客户端发送数据最后都解析到此数据结构(使用"简体中文(GB2312)",Encoding.GetEncoding("gb2312"编码)
)
public class MyData//20字节
{
/// <summary>
/// 开始符号6字节, "!Start"
/// </summary>
public string Start { get; set; }
/// <summary>
/// 消息类型,1字节
/// </summary>
public byte key { get; set; }
/// <summary>
/// 主体消息数据包长度,4字节
/// </summary>
public uint Lenght { get; set; }
/// <summary>
/// 4字节唯一设备识别符(Unique Device Identifier)
/// </summary>
public uint DeviceUDID { get; set; }
/// <summary>
/// 目标命令类型1字节
/// </summary>
public byte Type { get; set; }
/// <summary>
/// 主体消息
/// </summary>
public byte[] Body { get; set; }
/// <summary>
/// 结束符号4字节, "$End" ,
/// </summary>
public string End { get; set; } public override string ToString()
{
return string.Format("开始符号:{0},消息类型:{1},数据包长度:{2},唯一设备识别符:{3},目标命令类型:{4},主体消息:{5},结束符号:{6}",
Start, key, Lenght, Lenght, DeviceUDID, Type, Body, End);
}
}
第二步、请求(RequestInfo)
RequestInfo 是表示来自客户端请求的实体类。 每个来自客户端的请求都能应该被实例化为 RequestInfo 类型。 RequestInfo 类必须实现接口 IRequestInfo,该接口只有一个名为"Key"的字符串类型的属性:
根据你的应用程序的需要来定义你自己的请求类型。 例如:
public class MyRequestInfo : RequestInfo<MyData>
{
public MyRequestInfo(string key, MyData myData)
{
//如果需要使用命令行协议的话,那么key与命令类名称myData相同
Initialize(key, myData);
}
}
第三步、接收过滤器(ReceiveFilter)
接收过滤器(ReceiveFilter)用于将接收到的二进制数据转化成请求实例(RequestInfo)。
实现一个接收过滤器(ReceiveFilter), 你需要实现接口 IReceiveFilter:
public class MyReceiveFilter : IReceiveFilter<MyRequestInfo>
{
public Encoding encoding = Encoding.GetEncoding("gb2312");
/// <summary>
/// Gets the size of the left buffer.
/// </summary>
/// <value>
/// The size of the left buffer.
/// </value>
public int LeftBufferSize { get; }
/// <summary>
/// Gets the next receive filter.
/// </summary>
public IReceiveFilter<MyRequestInfo> NextReceiveFilter { get; } public FilterState State { get; private set; }
/// <summary>
/// 该方法将会在 SuperSocket 收到一块二进制数据时被执行,接收到的数据在 readBuffer 中从 offset 开始, 长度为 length 的部分。
/// </summary>
/// <param name="readBuffer">接收缓冲区, 接收到的数据存放在此数组里</param>
/// <param name="offset">接收到的数据在接收缓冲区的起始位置</param>
/// <param name="length">本轮接收到的数据的长度</param>
/// <param name="toBeCopied">表示当你想缓存接收到的数据时,是否需要为接收到的数据重新创建一个备份而不是直接使用接收缓冲区</param>
/// <param name="rest">这是一个输出参数, 它应该被设置为当解析到一个为政的请求后,接收缓冲区还剩余多少数据未被解析</param>
/// <returns></returns>
/// 当你在接收缓冲区中找到一条完整的请求时,你必须返回一个你的请求类型的实例.
/// 当你在接收缓冲区中没有找到一个完整的请求时, 你需要返回 NULL.
/// 当你在接收缓冲区中找到一条完整的请求, 但接收到的数据并不仅仅包含一个请求时,设置剩余数据的长度到输出变量 "rest". SuperSocket 将会检查这个输出参数 "rest", 如果它大于 0, 此 Filter 方法 将会被再次执行, 参数 "offset" 和 "length" 会被调整为合适的值.
public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
rest = 0;
if (length < 21)//没有数据
return null;
byte[] data = new byte[length];
Buffer.BlockCopy(readBuffer, offset, data, 0, length);
var str = encoding.GetString(data);
MyData myData=new MyData();
myData.Start= encoding.GetString(data,0,6);//6字节
myData.key= data[6];//1字节
myData.Lenght = BitConverter.ToUInt32(data, 7);//4字节 6 + 1
if (length < myData.Lenght + 20)
return null;
myData.DeviceUDID= BitConverter.ToUInt32(data, 11);//4字节 6 + 1+4
myData.Type = data[15];//1字节 6+1+4+4 myData.Body = new byte[myData.Lenght];//myData.Lenght字节
Buffer.BlockCopy(data, 16, myData.Body, 0, (int)myData.Lenght); myData.End= encoding.GetString(data, (int)(16+ myData.Lenght), 4);//4字节
if (myData.Start != "!Start" || myData.End != "$End")
return null;
rest =(int)(length-(20+ myData.Lenght));//未处理数据
return new MyRequestInfo(myData.key.ToString(),myData);
}
public void Reset()
{
}
- TRequestInfo: 类型参数 "TRequestInfo" 是你要在程序中使用的请求类型(RequestInfo);
- LeftBufferSize: 该接收过滤器已缓存数据的长度;
- NextReceiveFilter: 当下一块数据收到时,用于处理数据的接收过滤器实例;
- Reset(): 重设接收过滤器实例到初始状态;
Filter(....): 该方法将会在 SuperSocket 收到一块二进制数据时被执行,接收到的数据在 readBuffer 中从 offset 开始, 长度为 length 的部分。
TRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest);
- readBuffer: 接收缓冲区, 接收到的数据存放在此数组里
- offset: 接收到的数据在接收缓冲区的起始位置
- length: 本轮接收到的数据的长度
- toBeCopied: 表示当你想缓存接收到的数据时,是否需要为接收到的数据重新创建一个备份而不是直接使用接收缓冲区
- rest: 这是一个输出参数, 它应该被设置为当解析到一个为政的请求后,接收缓冲区还剩余多少数据未被解析
这儿有很多种情况需要你处理:
- 当你在接收缓冲区中找到一条完整的请求时,你必须返回一个你的请求类型的实例.
- 当你在接收缓冲区中没有找到一个完整的请求时, 你需要返回 NULL.
- 当你在接收缓冲区中找到一条完整的请求, 但接收到的数据并不仅仅包含一个请求时,设置剩余数据的长度到输出变量 "rest". SuperSocket 将会检查这个输出参数 "rest", 如果它大于 0, 此 Filter 方法 将会被再次执行, 参数 "offset" 和 "length" 会被调整为合适的值.
第四步、接收过滤器工厂(ReceiveFilterFactory)
接收过滤器工厂(ReceiveFilterFactory)用于为每个会话创建接收过滤器. 定义一个过滤器工厂(ReceiveFilterFactory)类型, 你必须实现接口 IReceiveFilterFactory. 类型参数 "TRequestInfo" 是你要在整个程序中使用的请求类型
/// <summary>
/// Receive filter factory interface
/// </summary>
/// <typeparam name="TRequestInfo">The type of the request info.</typeparam>
public class MyReceiveFilterFactory : IReceiveFilterFactory<MyRequestInfo>
{
/// <summary>
/// Creates the receive filter.
/// </summary>
/// <param name="appServer">The app server.</param>
/// <param name="appSession">The app session.</param>
/// <param name="remoteEndPoint">The remote end point.</param>
/// <returns>
/// the new created request filer assosiated with this socketSession
/// </returns>
//MyReceiveFilter<MyRequestInfo> CreateFilter(IAppServer appServer, IAppSession appSession, IPEndPoint remoteEndPoint);
public IReceiveFilter<MyRequestInfo> CreateFilter(IAppServer appServer, IAppSession appSession, IPEndPoint remoteEndPoint)
{
return new MyReceiveFilter();
}
}
第五步、和 AppSession,AppServer 配合工作
现在, 你已经有了 RequestInfo, ReceiveFilter 和 ReceiveFilterFactory, 但是你还没有正式使用它们. 如果你想让他们在你的程序里面可用, 你需要定义你们的 AppSession 和 AppServer 来使用他们.
public class MySession : AppSession<MySession, MyRequestInfo>
{
public uint DeviceUDID;
protected override void HandleException(Exception e)
{ }
} public class MyServer : AppServer<MySession, MyRequestInfo>
{
/// <summary>
/// 使用自定义协议工厂
/// </summary>
public MyServer()
: base(new MyReceiveFilterFactory())
{
}
}
第六步、服务端使用实例
MyServer myServer = new MyServer();
Encoding encoding = Encoding.GetEncoding("gb2312");
//Setup the appServer
if (!myServer.Setup(1990)) //Setup with listening port
{
MessageBox.Show("Failed to setup!");
return;
}
//Try to start the appServer
if (!myServer.Start())
{
MessageBox.Show("Failed to start!");
return;
}
myServer.NewSessionConnected += MyServer_NewSessionConnected;
myServer.NewRequestReceived += MyServer_NewRequestReceived;
private void MyServer_NewRequestReceived(MySession session, MyRequestInfo requestInfo)
{
var msg=encoding.GetString(requestInfo.Body.Body);
} private void MyServer_NewSessionConnected(MySession session)
{
session.Send("Welcome to SuperSocket Telnet Server");
}
SuperSocket使用 IRequestInfo 和 IReceiveFilter 等对象实现自定义协议的更多相关文章
- 使用SuperSocket实现自定义协议C/S设计
一.简介: 21世纪是出于互联网+的时代,许多传统行业和硬件挂钩的产业也逐步转向了系统集成智能化,简单来说就是需要软硬件的结合.这时,软硬件通讯便是这里面最主要的技术点,我们需要做到的是让硬件能够听懂 ...
- JSON对象(自定义对象)
JSON对象(自定义对象) 1.什么是JSON对象 JSON对象是属性的无序集合,在内存中也表现为一段连续的内存地址(堆内存) 1)JSON对象是属性的集合 2)这个集合是没有任何顺序的 2.JSON ...
- Unit07: document 对象 、 自定义对象 、 事件
Unit07: document 对象 . 自定义对象 . 事件 知识点: <!DOCTYPE html> <html> <head> <meta chars ...
- php 对象的自定义遍历
php对象的自定义遍历 对手册中的案例进行分析 更好的理解foreach() 的遍历步骤 class myIterator implements Iterator { private $positio ...
- 对象数组自定义排序--System.Collections.ArrayList.Sort()
使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...
- 【vue】@click绑定的函数,如何同时传入事件对象和自定义参数
知识很久不用的话,果然是容易忘的... 记记笔记,希望能加深点印象吧. [仅仅传入事件对象] html: <div id="app"> <button @clic ...
- SuperSocket 1.6.4 通过FixedHeaderReceiveFilter解析自定义协议
SuperSocket 提供了一些通用的协议解析工具, 你可以用他们简单而且快速的实现你自己的通信协议: TerminatorReceiveFilter (SuperSocket.SocketBase ...
- Effective Objective-C 2.0 — 第10条:在既有类中使用关联对象存放自定义数据
可以通过“关联对象”机制来把两个对象连起来 定义关联对象时可指定内存管理语义,用以模仿定义属性时所采用的“拥有关系”与“非拥有关系” 只有在其他做法不可行时才应选用关联对象,因为这种做法通常会引入难于 ...
- 给内置对象或自定义对象添加存取器属性(getter setter)的方法总结
funct = { get: function() { return this._x }, set: function(value) { this._x = value } } function Ob ...
随机推荐
- EntityFramework 学习 一 Add Entity Graph using DbContext:
//Create student in disconnected mode Student newStudent = new Student() { StudentName = "New S ...
- TCP/IP 协议中的编址
TCP/IP协议的互联网需要用到四个级别的地址:物理地址.逻辑地址.端口地址和特定应用地址 一.物理地址 物理地址称为链路地址,是由接点所在的局域网或广域网为该结点指定的地址. 这种地址的长度和格式随 ...
- Javascript两个数的比较
Strict equality using === 比较之前不转换类型, 如果不同类型,不相等, 如果相同类型:如果两个都不是numbers,只有自己和自己比较才相等,其他都不相等: 如果两个都是nu ...
- HDU 4714 Tree2cycle:贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...
- 异步刷新页面的前进与后退的实现--pushState replaceState
实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与页面展现内容一致(符合人们对传统网页的认识) 在不支持的浏览器下降级成传统网页的方式 使用到的API history.state 当 ...
- SmartGit(试用期30后),个人继续使用的方法。
在我们做项目的过程中,我们会用到SmartGit这个软件来将本地的MAVEN项目push到国内的码云(https://git.oschina.net)或者是国外的github网站进行项目的管理,这个时 ...
- Spring源码分析_01_ idea搭建spring源码阅读环境
二.参考资料 1.Intellij Idea如何导入spring源码
- 使用jquery执行ajax
$.ajax():返回其创建的XMLHttpRequest对象 回调函数:如果要处理$.ajax()得到的数据,则应该使用回调函数!beforeSend:在发送请求之后调用,需要一个XMLHttpRe ...
- Swift错误处理
相对于可选中运用值的存在与缺失来表达函数的成功与失败,错误处理可以推断失败的原因,并传播至程序的其他部分. throws关键词 一个函数可以通过在声明中添加throws关键词来抛出错误消息. func ...
- 不同类型input尺寸设置区别
最近发现为不用类型的input设置相同的尺寸,却得到了不一样的尺寸结果.发现不同类型的input的height和width竟然含义不同.在此小整理一下. (1)button类型 规律 button类型 ...