SCTP通信中的stream和ssn

1. 指定在某個stream上發(fā)送消息

在調(diào)用sctp_sendmsg函數(shù)發(fā)送消息的時候,我們可以設(shè)置stream ID,

sctp_sendmsg(sock_op->socket_fd(), message, MAX_BUFFER, 
                    (sockaddr *)&servaddr, sizeof(servaddr), 0, 0, stream, 0, 0);

倒數(shù)第三個參數(shù)就是stream,我們可以指定在某個stream發(fā)送消息,

  • 一個SCTP association包含多個stream,默認情況下最大有10個stream;
  • 每個stream上的傳輸相互獨立,比如說stream 0的傳輸出現(xiàn)阻塞了,不會影響stream 1上的消息傳輸
  • 每個stream上傳輸?shù)臄?shù)據(jù)包都有序列號

2. 設(shè)置最大最小stream數(shù)

可以通過設(shè)置socket的屬性來設(shè)置最大的stream數(shù),

sctp_initmsg initmsg;
sctp_event_subscribe evnts;  
    
// Specify that a maximum of 5 streams will be available per socket 
memset( &initmsg, 0, sizeof(initmsg) );
initmsg.sinit_num_ostreams = 5;
initmsg.sinit_max_instreams = 5;
initmsg.sinit_max_attempts = 4;
if(0 != setsockopt( sock_fd, IPPROTO_SCTP, SCTP_INITMSG,
                     &initmsg, sizeof(initmsg) ))

3. 同一個stream上數(shù)據(jù)包的序列

發(fā)送端每一個消息發(fā)送五次,都在同一個stream 1上發(fā)送,

printf("[Client]: Input message content!\n");
std::cin >> message ;
for(int i = 0; i < 5; i++)
{
    std::string newMsg = std::move(message + (std::to_string(i)));
    
    printf("[Client]: Send message: %s\n", newMsg.c_str()); 

    sctp_sendmsg(sock_op->socket_fd(), newMsg.c_str(), MAX_BUFFER, 
            (sockaddr *)&servaddr, sizeof(servaddr), 0, 0, stream, 0, 0);
}

第一次發(fā)送5個‘hello’,Client端打印如下,

[Client]: Send message: hello0
[Client]: Send message: hello1
[Client]: Send message: hello2
[Client]: Send message: hello3
[Client]: Send message: hello4

我們再來看一下Sever端的接收情況,同時把每個消息的ssn打印出來,

====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello0') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(0)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello1') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(1)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello2') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(2)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello3') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(3)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('hello4') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(4)

可以看到,每個消息都有一個自己的序列號,保證消息是有序的,
我們繼續(xù)再往server端發(fā)送5個'world',server端的接收消息依舊是有序的,

 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world0') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(5)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world1') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(6)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world2') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(7)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world3') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(8)
 ====================================================================
[Server]: Wait for new message or command([0: Exit]; [1: NewMessage])...
[Server]: SCTP message('world4') received from IP/Port(127.0.0.1:47378) on assoc(0x1e) / stream(1), ssn(9)

4. 如果出現(xiàn)丟包的情況

這里我們沒法兒模擬丟包的場景,unix網(wǎng)絡(luò)編程這本書中描述的,如果一個數(shù)據(jù)包丟失,也就是如果出現(xiàn)NACK

  • 這個stream上的后續(xù)的數(shù)據(jù)包會保存到隊列中;
  • 其他的stream不受影響;
  • 丟失的數(shù)據(jù)包會進行重傳,重傳成功后,后續(xù)隊列中的數(shù)據(jù)包繼續(xù)按順序發(fā)送;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 實時消息協(xié)議---流的分塊 版權(quán)聲明: 版權(quán)(c)2009 Adobe系統(tǒng)有限公司。全權(quán)所有。 摘要: 本備忘錄描...
    一個人zy閱讀 2,072評論 0 9
  • 1.OkHttp源碼解析(一):OKHttp初階2 OkHttp源碼解析(二):OkHttp連接的"前戲"——HT...
    隔壁老李頭閱讀 21,588評論 24 176
  • 個人翻譯,轉(zhuǎn)載請注明出處,謝謝! Adobe's Real Time Messaging Protocol 摘要 ...
    SniperPan閱讀 2,896評論 1 17
  • kafka的定義:是一個分布式消息系統(tǒng),由LinkedIn使用Scala編寫,用作LinkedIn的活動流(Act...
    時待吾閱讀 5,539評論 1 15

友情鏈接更多精彩內(nèi)容