Port Unification Overview(端口統一)
針對在一個端口上開放多種協議的服務,類似下圖:
通過Port unification Filter去判斷三種協議,然后針對不同協議傳遞到下一個Filter執行后續工作。
其中Finder的Grizzly官方給了一個簡單用例,針對協議包頭是"add"的Finder
-
/** -
* {@link ProtocolFinder}, responsible to determine if incoming byte buffer -
* represents ADD-service request. -
*/ -
public class AddProtocolFinder implements ProtocolFinder { -
-
private final static byte [] magic = { 'a' , 'd' , 'd' }; -
-
/** -
* {@inheritDoc} -
*/ -
@Override -
public Result find ( final PUContext puContext , final FilterChainContext ctx ) { -
// Get the input Buffer -
final Buffer inputBuffer = ctx . getMessage (); -
-
final int bytesToCompare = Math . min ( magic . length , inputBuffer . remaining ()); -
-
final int bufferStart = inputBuffer . position (); -
-
// Compare incoming bytes with ADD-service protocol magic -
for ( int i = 0 ; i < bytesToCompare ; i ++) { -
if ( magic [ i ] != inputBuffer . get ( bufferStart + i )) { -
// If at least one byte doesn't match - it's not ADD-service protocol -
return Result . NOT_FOUND ; -
} -
} -
-
// if we check entire magic - return FOUND, or NEED_MORE_DATA otherwise -
return bytesToCompare == magic . length ? -
Result . FOUND : Result . NEED_MORE_DATA ; -
} -
-
}
這個簡單例子應該明白怎么判斷了吧,然后就是轉到對應的協議
?
-------------------------------
理論學習暫且到此為止,水平實在有限,本人僅使用了基礎功能,所以大部分擴展都沒有去學習,希望有高人可以補充
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

