簡體   English   中英

緩沖凈值中的損壞

[英]buffer corruption in netty

使用netty(with camel)時出現一個奇怪的錯誤,我們使用LengthFieldBasedFrameDecoder進行通信,客戶端是來自第三方的套接字程序,我們在服務器端使用了netty(camel-netty組件)。

有時將兩條消息“合並”為一條,因此即將出現的數據全部錯誤。

例如:

客戶端發送兩條消息:

[10] AAAAAAAAAAAAAAAAAAA和[10] BBBBBBBBBBBBBBBBBB

其中[10]是長度字節,AAAAAAAAAA是數據。

但在服務器上我們得到了[10] AAAAAA [10] BBBBBBBBBBBBBBBBBAAAAAAAAAAAA

似乎第一個消息被第二個消息拆分,因此解碼器將數據解釋為:

[10] AAAAAA [10] BBBBBBBB

BBBBBBBBAAAAAAAAAA ................................................. ..

這樣第一個消息的長度是正確的,但是數據是錯誤的,第二個消息的長度是“ BB”,並且得到了更長的數據包。

希望我能清楚地描述,以前有人遇到過嗎?

聽起來您正在兩個線程中寫入同一流。

您的LengthFieldBasedFrameDecoder是否擴展了FrameDecoder 而且是單身還是沒有?

實際上,我也遇到過同樣的問題,我同意彼得的觀點。

我查看了FrameDecoder ,發現有一個名為“ cumulation”ChannelBuffer屬性,該屬性將共享給解碼器的所有Channel。

讓我們看一下FrameDecoder.messageReceived方法:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

    Object m = e.getMessage();
    if (!(m instanceof ChannelBuffer)) {
        ctx.sendUpstream(e);
        return;
    }

    ChannelBuffer input = (ChannelBuffer) m; // here is the buffer from the channel
    if (!input.readable()) {
        return;
    }

    ChannelBuffer cumulation = cumulation(ctx); // here is the buffer wrapped by the FrameDecoder
    if (cumulation.readable()) {
        cumulation.discardReadBytes();

        // where "[10]AAA[10]BBBBB" happens
        cumulation.writeBytes(input); 

        // if code run here,we will get the wrong buffer
        callDecode(ctx, e.getChannel(), cumulation, e.getRemoteAddress());
    } else {
        callDecode(ctx, e.getChannel(), input, e.getRemoteAddress());
        if (input.readable()) {
            cumulation.writeBytes(input);
        }
    }
}

我認為使用FrameDecoder的正確方法是使其變成多寫。

好吧,這被證明是駱駝網組件的“錯誤”,我將在稍后發布駱駝項目的修復程序。 在此之前,請小心使用駱駝網絡組件,尤其是不要使用未使用@sharable批注標記的編碼器/解碼器,因為狀態可能在不同連接之間共享,所以將導致此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM