MazWoz / jsmpp

Automatically exported from code.google.com/p/jsmpp
Apache License 2.0
0 stars 0 forks source link

read pdu #65

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
DefaultPDUReader.readPDU()

better read using while read() returns >= 0 and until totalReaded < len.
sometimes smpp pdu in tcp stream splits in different ip packets and read()
returns first part of pdu (it happens on linux, jdk 1.6, don't know about
other OS; it happens when smsc send a lot of pdu per second)

...
        if (commandLength > 16) {
            int len = commandLength - 16;
            int totalReaded = -1;
            synchronized (in) {
                totalReaded = in.read(b, 16, commandLength - 16);
            }
            if (totalReaded != len) {
                throw new IOException(
                        "Unexpected length of byte readed. Expecting " + len
                                + " but only read " + totalReaded);
            }
        }
...

Original issue reported on code.google.com by Nikolai.Ivanoff@gmail.com on 4 Mar 2010 at 5:25

GoogleCodeExporter commented 8 years ago
p.s. version 2.1.0

Original comment by Nikolai.Ivanoff@gmail.com on 5 Mar 2010 at 10:24

GoogleCodeExporter commented 8 years ago
if (commandLength > 16) {

            int len = commandLength - 16;

            int totalReaded = 16;

            synchronized (in) {

                while(totalReaded<len){
                     totalReaded += in.read(b, totalReaded,len-totalReaded);

                }
            }

}

May be this code withh help us in performing this task. This loop will make 
sure that
the required number of bytes are read if the stream is available. Just make 
sure that
if the client socket is closed, or if end-of-stream is reached, the read() 
method
throws IOException.

Original comment by hbkricha...@gmail.com on 19 Apr 2010 at 3:37

GoogleCodeExporter commented 8 years ago
better:

if (commandLength > 16) {
    int len = commandLength - 16;
    int totalReaded = 16;

    synchronized (in) {
        while(totalReaded<len){
            int readed = in.read(b, totalReaded, len-totalReaded);
            if (readed < 0) {
                throw new IOException("socket closed");
            }
            totalReaded += readed;
        }
    }
}

Original comment by Nikolai.Ivanoff@gmail.com on 23 Apr 2010 at 8:41

GoogleCodeExporter commented 8 years ago
The Suggested algorithms results the Time Out Exception.For example hence len = 
8,so 
  len-totalReaded will be negative 

So the algorithm should be as follows

if (commandLength > 16) {

 int len = commandLength - 16;
            int totalReaded = 16;

            synchronized (in) {
                while(len != 0){

                    int readed = in.read(b, totalReaded, len);
                    if (readed < 0) {
                        throw new IOException(
                                "Unexpected length of byte readed. Expecting " + len
                                        + " but only read " + totalReaded);
                    }
                    len -= readed;
                    totalReaded += readed;
                }
            }
}

Original comment by dominicu...@gmail.com on 24 Jun 2010 at 11:16

GoogleCodeExporter commented 8 years ago
Sorry for my mistake.

while(totalReaded<commandLength){
     totalReaded += in.read(b, totalReaded,commandLength-totalReaded);
.....
}

Original comment by hbkricha...@gmail.com on 2 Jul 2010 at 8:51

GoogleCodeExporter commented 8 years ago
I've tried the fix from dominicu...  It seems to work quite robustly. I've 
confirmed with tcpdumps that several reassembled pdus were sent. These with the 
orginal code would cause IOExceptions (a little harsh). Now with this fix they 
are processed quite happily.

Is there any reason why this isn't apart of the DefaultPDUReader? 

Regardless, the code allows for using a custom PDUReader quite easily. Maybe 
the addition of a SafePDUReader is in order if there is no desire to see this 
functionality in the DefaultPDUReader?

thanks, matt pinner

Original comment by mpin...@gmail.com on 2 Mar 2011 at 3:11

Attachments:

GoogleCodeExporter commented 8 years ago
How to read SMPP PDU and show?

Please hepl me.

Original comment by bulk24sm...@gmail.com on 27 May 2014 at 12:28