[ 
https://issues.apache.org/jira/browse/DIRMINA-1040?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15407296#comment-15407296
 ] 

Silver Cheng commented on DIRMINA-1040:
---------------------------------------

Hi,
Yes, we have the encoder which is called by Mina ProtocolCodecFilter 
filterWrite(), you can see our encoder code(copy here as FYI) is very simple, 
just simply fill the pdu to IoBuffer, seem thread safe.

Below are one test call trace and the encoder codes
{noformat}
2016-08-04 14:00:23,406 DEBUG (1727921240@WebForward 0.0.0.0:5001-3) 
(1e813c48e90c2d162948) com.ipx.cpsmscapis.smpp.NorthSmppIoHandler - Sending 
DeliverSM: 00 00 00 9d 00 00 00 05 00 00 00 00 00 00 00 01 00 01 01 31 32 33 34 
35 36 37 00 01 01 38 30 30 38 30 30 00 04 00 00 00 00 00 00 00 00 6f 69 64 3a 
31 36 30 38 30 34 31 34 30 30 30 30 30 31 37 36 30 31 30 20 73 75 62 3a 30 30 
31 20 64 6c 76 72 64 3a 30 30 31 20 73 75 62 6d 69 74 20 64 61 74 65 3a 31 36 
30 38 30 34 31 34 30 30 20 64 6f 6e 65 20 64 61 74 65 3a 31 36 30 38 30 34 31 
34 30 30 20 73 74 61 74 3a 44 45 4c 49 56 52 44 20 65 72 72 3a 31 32 33 20 74 
65 78 74 3a
2016-08-04 14:00:23,407 DEBUG (1727921240@WebForward 0.0.0.0:5001-3) 
(1e813c48e90c2d162948) org.apache.mina.core.session.AbstractIoSession - ***1 
Enter Mina AbstractIoSession write method Object message: 00 00 00 9d 00 00 00 
05 00 00 00 00 00 00 00 01 00 01 01 31 32 33 34 35 36 37 00 01 01 38 30 30 38 
30 30 00 04 00 00 00 00 00 00 00 00 6f 69 64 3a 31 36 30 38 30 34 31 34 30 30 
30 30 30 31 37 36 30 31 30 20 73 75 62 3a 30 30 31 20 64 6c 76 72 64 3a 30 30 
31 20 73 75 62 6d 69 74 20 64 61 74 65 3a 31 36 30 38 30 34 31 34 30 30 20 64 
6f 6e 65 20 64 61 74 65 3a 31 36 30 38 30 34 31 34 30 30 20 73 74 61 74 3a 44 
45 4c 49 56 52 44 20 65 72 72 3a 31 32 33 20 74 65 78 74 3a;
2016-08-04 14:00:23,407 DEBUG (1727921240@WebForward 0.0.0.0:5001-3) 
(1e813c48e90c2d162948) com.ipx.cpsmscapis.smpp.codec.SmppEncoder - 
******SMPPEncoder Object message before out.write: 00 00 00 9d 00 00 00 05 00 
00 00 00 00 00 00 01 00 01 01 31 32 33 34 35 36 37 00 01 01 38 30 30 38 30 30 
00 04 00 00 00 00 00 00 00 00 6f 69 64 3a 31 36 30 38 30 34 31 34 30 30 30 30 
30 31 37 36 30 31 30 20 73 75 62 3a 30 30 31 20 64 6c 76 72 64 3a 30 30 31 20 
73 75 62 6d 69 74 20 64 61 74 65 3a 31 36 30 38 30 34 31 34 30 30 20 64 6f 6e 
65 20 64 61 74 65 3a 31 36 30 38 30 34 31 34 30 30 20 73 74 61 74 3a 44 45 4c 
49 56 52 44 20 65 72 72 3a 31 32 33 20 74 65 78 74 3a
2016-08-04 14:00:23,408 DEBUG (1727921240@WebForward 0.0.0.0:5001-3) 
(1e813c48e90c2d162948) com.ipx.cpsmscapis.smpp.codec.SmppEncoder - 
[java.lang.Thread.getStackTrace(Thread.java:1479), 
com.ipx.cpsmscapis.smpp.codec.SmppEncoder.encode(SmppEncoder.java:46), 
org.apache.mina.filter.codec.ProtocolCodecFilter.filterWrite(ProtocolCodecFilter.java:298),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.callPreviousFilterWrite(DefaultIoFilterChain.java:630),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1600(DefaultIoFilterChain.java:49),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.filterWrite(DefaultIoFilterChain.java:976),
 
org.apache.mina.filter.executor.ExecutorFilter.filterWrite(ExecutorFilter.java:611),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.callPreviousFilterWrite(DefaultIoFilterChain.java:630),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1600(DefaultIoFilterChain.java:49),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.filterWrite(DefaultIoFilterChain.java:976),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.filterWrite(DefaultIoFilterChain.java:900),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.callPreviousFilterWrite(DefaultIoFilterChain.java:630),
 
org.apache.mina.core.filterchain.DefaultIoFilterChain.fireFilterWrite(DefaultIoFilterChain.java:623),
 
org.apache.mina.core.session.AbstractIoSession.write(AbstractIoSession.java:563),
 
org.apache.mina.core.session.AbstractIoSession.write(AbstractIoSession.java:499),
 
com.ipx.cpsmscapis.smpp.NorthSmppIoHandler._sendDeliverSM(NorthSmppIoHandler.java:1143),
 
2016-08-04 14:00:23,408 DEBUG (1727921240@WebForward 0.0.0.0:5001-3) 
(1e813c48e90c2d162948) org.apache.mina.core.filterchain.DefaultIoFilterChain - 
**1.1** DefaultIoFilterChain write directly the message: HeapBuffer[pos=0 
lim=157 cap=157: 00 00 00 9D 00 00 00 05 00 00 00 00 00 00 00 01 00 01 01 31 32 
33 34 35 36 37 00 01 01 38 30 30...];

2016-08-04 14:00:23,410 DEBUG (NioProcessor-3) () 
org.apache.mina.core.polling.AbstractPollingIoProcessor - ***2 Mina 
AbstractPollingIoProcessor flushNow method msg: HeapBuffer[pos=0 lim=157 
cap=157: 00 00 00 9D 00 00 00 05 00 00 00 00 00 00 00 01 00 01 01 31 32 33 34 
35 36 37 00 01 01 38 30 30...];
2016-08-04 14:00:23,411 DEBUG (NioProcessor-3) () 
org.apache.mina.core.polling.AbstractPollingIoProcessor - ***3 Mina 
AbstractPollingIoProcessor writeBuffer  session's hasFragmentation: true ;  
session: HeapBuffer[pos=0 lim=157 cap=157: 00 00 00 9D 00 00 00 05 00 00 00 00 
00 00 00 01 00 01 01 31 32 33 34 35 36 37 00 01 01 38 30 30...];
2016-08-04 14:00:23,411 DEBUG (NioProcessor-3) () 
org.apache.mina.core.polling.AbstractPollingIoProcessor - ***3 Mina 
AbstractPollingIoProcessor writeBuffer  session's maxWrittenBytes: 98304 ;  
buf: 00 00 00 9D 00 00 00 05 00 00 00 00 00 00 00 01 00 01 01 31 32 33 34 35 36 
37 00 01 01 38 30 30 38 30 30 00 04 00 00 00 00 00 00 00 00 6F 69 64 3A 31 36 
30 38 30 34 31 34 30 30 30 30 30 31 37 36 30 31 30 20 73 75 62 3A 30 30 31 20 
64 6C 76 72 64 3A 30 30 31 20 73 75 62 6D 69 74 20 64 61 74 65 3A 31 36 30 38 
30 34 31 34 30 30 20 64 6F 6E 65 20 64 61 74 65 3A 31 36 30 38 30 34 31 34 30 
30 20 73 74 61 74 3A 44 45 4C 49 56 52 44 20 65 72 72 3A 31 32 33 20 74 65 78 
74 3A;
2016-08-04 14:00:23,411 DEBUG (NioProcessor-3) () 
org.apache.mina.core.polling.AbstractPollingIoProcessor - ***3.1  Before 
NioProcessor write method, in Mina AbstractPollingIoProcessor writeBuffer  
session's buf.remaining: 157;buf.position(): 0.
2016-08-04 14:00:23,411 DEBUG (NioProcessor-3) () 
org.apache.mina.transport.socket.nio.NioProcessor - ***4.0 Enter Mina 
NioProcessor write method buf.remaining: 157; session.getCurrentWriteMessage: 
HeapBuffer[pos=0 lim=157 cap=157: 00 00 00 9D 00 00 00 05 00 00 00 00 00 00 00 
01 00 01 01 31 32 33 34 35 36 37 00 01 01 38 30 30...];
2016-08-04 14:00:23,412 DEBUG (NioProcessor-3) () 
org.apache.mina.transport.socket.nio.NioProcessor - ***4.0 Enter Mina 
NioProcessor write method buf.position: 0; buf.buf(): 
java.nio.HeapByteBuffer[pos=0 lim=157 cap=157]
2016-08-04 14:00:23,412 DEBUG (NioProcessor-3) () 
org.apache.mina.transport.socket.nio.NioProcessor - ***4.0 Enter Mina 
NioProcessor write method length: 157;buf.getHexDump: 00 00 00 9D 00 00 00 05 
00 00 00 00 00 00 00 01 00 01 01 31 32 33 34 35 36 37 00 01 01 38 30 30 38 30 
30 00 04 00 00 00 00 00 00 00 00 6F 69 64 3A 31 36 30 38 30 34 31 34 30 30 30 
30 30 31 37 36 30 31 30 20 73 75 62 3A 30 30 31 20 64 6C 76 72 64 3A 30 30 31 
20 73 75 62 6D 69 74 20 64 61 74 65 3A 31 36 30 38 30 34 31 34 30 30 20 64 6F 
6E 65 20 64 61 74 65 3A 31 36 30 38 30 34 31 34 30 30 20 73 74 61 74 3A 44 45 
4C 49 56 52 44 20 65 72 72 3A 31 32 33 20 74 65 78 74 3A
2016-08-04 14:00:23,412 DEBUG (NioProcessor-3) () 
org.apache.mina.core.polling.AbstractPollingIoProcessor - ***5.0  After 
NioProcessor write method, in Mina AbstractPollingIoProcessor writeBuffer  
session's localWrittenBytes: 157
{noformat}
{noformat}
package com.ipx.cpsmscapis.smpp.codec;

import java.util.Arrays;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.smpp.pdu.PDU;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Smpp Protocol Encoder
 */
public class SmppEncoder extends ProtocolEncoderAdapter {

        //added by Silver
    /** A logger for this class */
    static Logger LOGGER = LoggerFactory.getLogger(SmppEncoder.class);
    
    /** A speedup for logs */
    static boolean DEBUG = LOGGER.isDebugEnabled();

        /* (non-Javadoc)
     * @see 
org.apache.mina.filter.codec.ProtocolEncoder#encode(org.apache.mina.core.session.IoSession,
 java.lang.Object, org.apache.mina.filter.codec.ProtocolEncoderOutput)
     */
    public void encode(IoSession session, Object message, ProtocolEncoderOutput 
out) throws Exception {
        PDU pdu = (PDU) message;

        if (DEBUG) {
            LOGGER.debug("******SMPPEncoder Object message before out.write: 
{}", pdu.toString());
        } 
        
        out.write(IoBuffer.wrap(pdu.getData().getBuffer()));
        
        //if (DEBUG) {
        //LOGGER.debug(Arrays.toString(new 
Thread().currentThread().getStackTrace()));
        //} 
        
    }
{noformat}

> Mina 2.0.4 truncated the PDU
> ----------------------------
>
>                 Key: DIRMINA-1040
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-1040
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>         Environment: Mina 2.0.4 version
> pl-1@ECE8000-Z1 ~ # /usr/java/latest/bin/java -version
> java version "1.6.0_45"
> Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
> Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
>            Reporter: Silver Cheng
>              Labels: PDU, truncated
>         Attachments: Mina-2.0.13-code delta.docx, apache-mina-2.0.13.zip, 
> mina log and tcpdump.zip, mina-debug-logging.zip, ms-mc1.zip, 
> tcpdump-10001.zip
>
>
> Background:
> our app messaging-server is to handle the SMPP protocol messages, and we use 
> Mina 2.0.4 for the lower TCP transport function.
> Problem:
> sometime, not often, I found that the PDU could be truncated by Mina from 
> tcpdump. I added some debug loggers to mina codes, and found that indeed the 
> PDU truncated is occuring at Mina. 
> from below logs, you can see in #3, the buf which got from WriteRequest got 
> truncated the first 16 bytes in Mina.  could you know if it's a known issue 
> in 2.0.4?
> Debug loggers & Logs: 
> 1. IoHandler:
>         WriteFuture future = iosession.write(deliverSm);
> 2016-07-14 01:52:19,865 DEBUG (62451235@WebForward 0.0.0.0:5001-21) 
> (bbfa6690c88ab98aec4e) com.ipx.cpsmscapis.smpp.NorthSmppIoHandler - Sending 
> DeliverSM: 00 00 00 be 00 00 00 05 00 00 00 00 00 00 03 02 00 01 01 31 32 30 
> 32 34 36 30 38 39 33 33 00 04 09 37 36 32 32 36 35 00 04 00 00 00 00 00 00 00 
> 00 6f 69 64 3a 32 31 36 30 37 31 34 30 31 30 30 31 32 34 32 32 38 35 38 20 73 
> 75 62 3a 30 30 31 20 64 6c 76 72 64 3a 30 30 31 20 73 75 62 6d 69 74 20 64 61 
> 74 65 3a 31 36 30 37 31 34 30 31 35 32 20 64 6f 6e 65 20 64 61 74 65 3a 31 36 
> 30 37 31 34 30 31 35 32 20 73 74 61 74 3a 44 45 4c 49 56 52 44 20 65 72 72 3a 
> 30 30 30 20 74 65 78 74 3a 04 27 00 01 02 00 1e 00 14 32 31 36 30 37 31 34 30 
> 31 30 30 31 32 34 32 32 38 35 38 00
> 2. AbstractIoSession:
>     public WriteFuture write(Object message, SocketAddress remoteAddress) {
>         if (message == null) {
>             throw new IllegalArgumentException("message");
>         }
>         
>       //added by Silver
>         if (DEBUG) {
>               if 
> (message.getClass().getName().equals("org.smpp.pdu.DeliverSM")) {
>                       LOGGER.debug( "***1 Enter Mina AbstractIoSession write 
> method Object message: {};",message);
>               }               
>         }
> 2016-07-14 01:52:19,866 DEBUG (62451235@WebForward 0.0.0.0:5001-21) 
> (bbfa6690c88ab98aec4e) org.apache.mina.core.session.AbstractIoSession - ***1 
> Enter Mina AbstractIoSession write method Object message: 00 00 00 be 00 00 
> 00 05 00 00 00 00 00 00 03 02 00 01 01 31 32 30 32 34 36 30 38 39 33 33 00 04 
> 09 37 36 32 32 36 35 00 04 00 00 00 00 00 00 00 00 6f 69 64 3a 32 31 36 30 37 
> 31 34 30 31 30 30 31 32 34 32 32 38 35 38 20 73 75 62 3a 30 30 31 20 64 6c 76 
> 72 64 3a 30 30 31 20 73 75 62 6d 69 74 20 64 61 74 65 3a 31 36 30 37 31 34 30 
> 31 35 32 20 64 6f 6e 65 20 64 61 74 65 3a 31 36 30 37 31 34 30 31 35 32 20 73 
> 74 61 74 3a 44 45 4c 49 56 52 44 20 65 72 72 3a 30 30 30 20 74 65 78 74 3a 04 
> 27 00 01 02 00 1e 00 14 32 31 36 30 37 31 34 30 31 30 30 31 32 34 32 32 38 35 
> 38 00;
> 3. AbstractPollingIoProcessor:
>     private int writeBuffer(S session, WriteRequest req,
>             boolean hasFragmentation, int maxLength, long currentTime)
>             throws Exception {
>         IoBuffer buf = (IoBuffer) req.getMessage();
>         
>         //add by silver
>         if (DEBUG) {
>             LOGGER.debug( "***3 Mina AbstractPollingIoProcessor writeBuffer  
> session's hasFragmentation: {} ;  session: {}; ", 
> hasFragmentation,session.getCurrentWriteMessage());
>             LOGGER.debug( "***3 Mina AbstractPollingIoProcessor writeBuffer  
> session's maxWrittenBytes: {} ;  buf: {};", maxLength,buf.getHexDump());
>             }
> 2016-07-14 01:52:19,866 DEBUG (NioProcessor-3) () 
> org.apache.mina.core.polling.AbstractPollingIoProcessor - ***3 Mina 
> AbstractPollingIoProcessor writeBuffer  session's maxWrittenBytes: 98304 ;  
> buf: 00 01 01 31 32 30 32 34 36 30 38 39 33 33 00 04 09 37 36 32 32 36 35 00 
> 04 00 00 00 00 00 00 00 00 6F 69 64 3A 32 31 36 30 37 31 34 30 31 30 30 31 32 
> 34 32 32 38 35 38 20 73 75 62 3A 30 30 31 20 64 6C 76 72 64 3A 30 30 31 20 73 
> 75 62 6D 69 74 20 64 61 74 65 3A 31 36 30 37 31 34 30 31 35 32 20 64 6F 6E 65 
> 20 64 61 74 65 3A 31 36 30 37 31 34 30 31 35 32 20 73 74 61 74 3A 44 45 4C 49 
> 56 52 44 20 65 72 72 3A 30 30 30 20 74 65 78 74 3A 04 27 00 01 02 00 1E 00 14 
> 32 31 36 30 37 31 34 30 31 30 30 31 32 34 32 32 38 35 38 00;



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to