內置的常用協議實現模版

內置的常用協議實現模版

中文(中國)Toggle Dropdown
?
v1.6Toggle Dropdown

關鍵字: TerminatorReceiveFilter, CountSpliterReceiveFilter, FixedSizeReceiveFilter, BeginEndMarkReceiveFilter, FixedHeaderReceiveFilter

閱讀了前面一篇文檔之后, 你可能會覺得用 SuperSocket 來實現你的自定義協議并不簡單。 為了讓這件事變得更容易一些, SuperSocket 提供了一些通用的協議解析工具, 你可以用他們簡單而且快速的實現你自己的通信協議:

  • TerminatorReceiveFilter?(SuperSocket.SocketBase.Protocol.TerminatorReceiveFilter, SuperSocket.SocketBase)
  • CountSpliterReceiveFilter?(SuperSocket.Facility.Protocol.CountSpliterReceiveFilter, SuperSocket.Facility)
  • FixedSizeReceiveFilter?(SuperSocket.Facility.Protocol.FixedSizeReceiveFilter, SuperSocket.Facility)
  • BeginEndMarkReceiveFilter?(SuperSocket.Facility.Protocol.BeginEndMarkReceiveFilter, SuperSocket.Facility)
  • FixedHeaderReceiveFilter?(SuperSocket.Facility.Protocol.FixedHeaderReceiveFilter, SuperSocket.Facility)

TerminatorReceiveFilter - 結束符協議

與命令行協議類似,一些協議用結束符來確定一個請求.

例如, 一個協議使用兩個字符 "##" 作為結束符, 于是你可以使用類 "TerminatorReceiveFilterFactory":

/// <summary>
/// TerminatorProtocolServer
/// Each request end with the terminator "##"
/// ECHO Your message##
/// </summary>
public class TerminatorProtocolServer : AppServer
{public TerminatorProtocolServer(): base(new TerminatorReceiveFilterFactory("##")){}
}

默認的請求類型是 StringRequestInfo, 你也可以創建自己的請求類型, 不過這樣需要你做一點額外的工作:

基于TerminatorReceiveFilter實現你的接收過濾器(ReceiveFilter):

public class YourReceiveFilter : TerminatorReceiveFilter<YourRequestInfo>
{//More code
}

實現你的接收過濾器工廠(ReceiveFilterFactory)用于創建接受過濾器實例:

public class YourReceiveFilterFactory : IReceiveFilterFactory<YourRequestInfo>
{//More code
}

然后在你的 AppServer 中使用這個接收過濾器工廠(ReceiveFilterFactory).

CountSpliterReceiveFilter - 固定數量分隔符協議

有些協議定義了像這樣格式的請求 "#part1#part2#part3#part4#part5#part6#part7#". 每個請求有7個由 '#' 分隔的部分. 這種協議的實現非常簡單:

/// <summary>
/// Your protocol likes like the format below:
/// #part1#part2#part3#part4#part5#part6#part7#
/// </summary>
public class CountSpliterAppServer : AppServer
{public CountSpliterAppServer(): base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) // 7 parts but 8 separators{}
}

你也可以使用下面的類更深入的定制這種協議:

CountSpliterReceiveFilter<TRequestInfo>
CountSpliterReceiveFilterFactory<TReceiveFilter>
CountSpliterReceiveFilterFactory<TReceiveFilter, TRequestInfo>

FixedSizeReceiveFilter - 固定請求大小的協議

在這種協議之中, 所有請求的大小都是相同的。如果你的每個請求都是有9個字符組成的字符串,如"KILL BILL", 你應該做的事就是想如下代碼這樣實現一個接收過濾器(ReceiveFilter):

class MyReceiveFilter : FixedSizeReceiveFilter<StringRequestInfo>
{public MyReceiveFilter(): base(9) //傳入固定的請求大小{}protected override StringRequestInfo ProcessMatchedRequest(byte[] buffer, int offset, int length, bool toBeCopied){//TODO: 通過解析到的數據來構造請求實例,并返回}
}

然后在你的 AppServer 類中使用這個接受過濾器 (ReceiveFilter):

public class MyAppServer : AppServer
{public MyAppServer(): base(new DefaultReceiveFilterFactory<MyReceiveFilter, StringRequestInfo>()) //使用默認的接受過濾器工廠 (DefaultReceiveFilterFactory){}
}

BeginEndMarkReceiveFilter - 帶起止符的協議

在這類協議的每個請求之中 都有固定的開始和結束標記。例如, 我有個協議,它的所有消息都遵循這種格式 "!xxxxxxxxxxxxxx$"。因此,在這種情況下, "!" 是開始標記, "$" 是結束標記,于是你的接受過濾器可以定義成這樣:

class MyReceiveFilter : BeginEndMarkReceiveFilter<StringRequestInfo>
{//開始和結束標記也可以是兩個或兩個以上的字節private readonly static byte[] BeginMark = new byte[] { (byte)'!' };private readonly static byte[] EndMark = new byte[] { (byte)'$' };public MyReceiveFilter(): base(BeginMark, EndMark) //傳入開始標記和結束標記{}protected override StringRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length){//TODO: 通過解析到的數據來構造請求實例,并返回}
}

然后在你的 AppServer 類中使用這個接受過濾器 (ReceiveFilter):

public class MyAppServer : AppServer
{public MyAppServer(): base(new DefaultReceiveFilterFactory<MyReceiveFilter, StringRequestInfo>()) //使用默認的接受過濾器工廠 (DefaultReceiveFilterFactory){}
}

FixedHeaderReceiveFilter - 頭部格式固定并且包含內容長度的協議

這種協議將一個請求定義為兩大部分, 第一部分定義了包含第二部分長度等等基礎信息. 我們通常稱第一部分為頭部.

例如, 我們有一個這樣的協議: 頭部包含 6 個字節, 前 4 個字節用于存儲請求的名字, 后兩個字節用于代表請求體的長度:

/// +-------+---+-------------------------------+
/// |request| l |                               |
/// | name  | e |    request body               |
/// |  (4)  | n |                               |
/// |       |(2)|                               |
/// +-------+---+-------------------------------+

使用 SuperSocket, 你可以非常方便的實現這種協議:

class MyReceiveFilter : FixedHeaderReceiveFilter<BinaryRequestInfo>
{public MyReceiveFilter(): base(6){}protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length){return (int)header[offset + 4] * 256 + (int)header[offset + 5];}protected override BinaryRequestInfo ResolveRequestInfo(ArraySegment<byte> header, byte[] bodyBuffer, int offset, int length){return new BinaryRequestInfo(Encoding.UTF8.GetString(header.Array, header.Offset, 4), bodyBuffer.CloneRange(offset, length));}
}

你需要基于類FixedHeaderReceiveFilter實現你自己的接收過濾器.

  • 傳入父類構造函數的 6 表示頭部的長度;
  • 方法"GetBodyLengthFromHeader(...)" 應該根據接收到的頭部返回請求體的長度;
  • 方法 ResolveRequestInfo(....)" 應該根據你接收到的請求頭部和請求體返回你的請求類型的實例.

然后你就可以使用接收或者自己定義的接收過濾器工廠來在 SuperSocket 中啟用該協議.

  • Prev: 內置的命令行協議
  • Next: 使用 IRequestInfo 和 IReceiveFilter 等等其他對象來實現自定義協議

? 2018 - GetDocs.Net -?Hosted by BuyVM

轉載于:https://www.cnblogs.com/liuslayer/p/8624336.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390908.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390908.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390908.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

機器學習 來源框架_機器學習的秘密來源:策展

機器學習 來源框架成功的機器學習/人工智能方法 (Methods for successful Machine learning / Artificial Intelligence) It’s widely stated that data is the new oil, and like oil, data needs the right refinement to evolve to be utilised perfectly. The power of ma…

linux gcc 示例_最好的Linux示例

linux gcc 示例Linux is a powerful operating system that powers most servers and most mobile devices. In this guide, we will show you examples of how to use some of its most powerful features. This involves using the Bash command line.Linux是功能強大的操作系…

帆軟報表和jeecg的進一步整合--ajax給后臺傳遞map類型的參數

下面是頁面代碼&#xff1a; <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8"%> <%include file"/context/mytags.jsp"%> <% String deptIds (String)request.getAttribute("…

@Nullable 注解的用法

問題&#xff1a;Nullable 注解的用法 我看到java中的一些方法聲明為: void foo(Nullable Object obj){…}在這里Nullable是什么意思?這是不是意味著輸入可以為空? 沒有這個注解&#xff0c;輸入仍然可以是null&#xff0c;所以我猜這不是它的用法? 回答一 它清楚地說明…

WebLogic調用WebService提示Failed to localize、Failed to create WsdlDefinitionFeature

在本地Tomcat環境下調用WebService正常&#xff0c;但是部署到WebLogic環境中&#xff0c;則提示警告&#xff1a;[Failed to localize] MEX0008.PARSING_MDATA_FAILURE<SOAP_1_2 ......警告&#xff1a;[Failed to localize] MEX0008.PARSING_MDATA_FAILURE<SOAP_1_1 ..…

呼吁開放外網_服裝數據集:呼吁采取行動

呼吁開放外網Getting a dataset with images is not easy if you want to use it for a course or a book. Yes, there are many datasets with images, but few of them are suitable for commercial or educational use.如果您想將其用于課程或書籍&#xff0c;則獲取帶有圖像…

git push命令_Git Push命令解釋

git push命令The git push command allows you to send (or push) the commits from your local branch in your local Git repository to the remote repository.git push命令允許您將提交(或推送 )從本地Git存儲庫中的本地分支發送到遠程存儲庫。 To be able to push to you…

在Java里面使用Pairs或者二元組

問題&#xff1a;在Java里面使用Pairs或者二元組 在Java里面&#xff0c;我的Hashtable要用到一個元組結構。在Java里面&#xff0c;我可以使用的什么數據結構呢&#xff1f; Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table ...回答一 我不認…

github 搜索技巧

1、關鍵詞 指定開發語言 bitcoin language:javascript 2、關鍵詞 stars 數量 forks 數量 bitcoin stars:>100 forks:>50

React JS 組件間溝通的一些方法

剛入門React可能會因為React的單向數據流的特性而遇到組件間溝通的麻煩&#xff0c;這篇文章主要就說一說如何解決組件間溝通的問題。 1.組件間的關系 1.1 父子組件 ReactJS中數據的流動是單向的&#xff0c;父組件的數據可以通過設置子組件的props傳遞數據給子組件。如果想讓子…

數據可視化分析票房數據報告_票房收入分析和可視化

數據可視化分析票房數據報告Welcome back to my 100 Days of Data Science Challenge Journey. On day 4 and 5, I work on TMDB Box Office Prediction Dataset available on Kaggle.歡迎回到我的100天數據科學挑戰之旅。 在第4天和第5天&#xff0c;我將研究Kaggle上提供的TM…

sql limit子句_SQL子句解釋的位置:之間,之間,類似和其他示例

sql limit子句什么是SQL Where子句&#xff1f; (What is a SQL Where Clause?) WHERE子句(和/或IN &#xff0c; BETWEEN和LIKE ) (The WHERE Clause (and/or, IN , BETWEEN , and LIKE )) The WHERE clause is used to limit the number of rows returned.WHERE子句用…

在Java里面使用instanceof的性能影響

問題&#xff1a;在Java里面使用instanceof的性能影響 我正在寫一個應用程序&#xff0c;其中一種設計方案包含了instanceof操作的大量使用。雖然我知道面向對象設計通常試圖避免使用instanceof&#xff0c;但那是另一回事了&#xff0c;這個問題純粹只是討論與性能有關。我想…

Soot生成控制流圖

1.將soot.jar文件復制到工程bin目錄下&#xff1b;2.在cmd中執行如下命令java -cp soot-trunck.jar soot.tools.CFGViewer --soot-classpath .;"%JAVA_HOME%"\jre\lib\rt.jar com.wauoen.paper.classes.Activity其中&#xff0c;JAVA_HOME是jdk目錄&#xff1b;com.w…

Centos 6.5安裝MySQL-python

報錯信息&#xff1a;Using cached MySQL-python-1.2.5.zip Complete output from command python setup.py egg_info: sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 1, in <module&g…

react 最佳實踐_最佳React教程

react 最佳實踐React is a JavaScript library for building user interfaces. It was voted the most loved in the “Frameworks, Libraries, and Other Technologies” category of Stack Overflow’s 2017 Developer Survey.React是一個用于構建用戶界面JavaScript庫。 在S…

先知模型 facebook_Facebook先知

先知模型 facebook什么是先知&#xff1f; (What is Prophet?) “Prophet” is an open-sourced library available on R or Python which helps users analyze and forecast time-series values released in 2017. With developers’ great efforts to make the time-series …

Java里面的靜態代碼塊

問題&#xff1a;Java里面的靜態代碼塊 I was looking over some code the other day and I came across: 前幾天我在看一些代碼時發現&#xff1a; static {... }我是c轉來的&#xff0c;我不知道為啥要這樣干。這個代碼也編譯成功了&#xff0c;沒出錯誤。這里的"stat…

搭建Maven私服那點事

摘要&#xff1a;本文主要介紹在CentOS7.1下使用nexus3.6.0搭建maven私服&#xff0c;以及maven私服的使用&#xff08;將自己的Maven項目指定到私服地址、將第三方項目jar上傳到私服供其他項目組使用&#xff09; 一、簡介 Maven是一個采用純Java編寫的開源項目管理工具, Mave…

lee最短路算法_Lee算法的解釋:迷宮運行并找到最短路徑

lee最短路算法Lee算法是什么&#xff1f; (What is the Lee Algorithm?) The Lee algorithm is one possible solution for maze routing problems. It always gives an optimal solution, if one exists, but is slow and requires large memory for dense layout.Lee算法是迷…