OkSocket icon indicating copy to clipboard operation
OkSocket copied to clipboard

response body data keep shifted

Open juicycool92 opened this issue 6 years ago • 2 comments

hello, thanks to nice module! it save my days.

but i struggled at receiving server's data.

lets have a look at wiresharks log that server response

Frame 69088: 82 bytes on wire (656 bits), 82 bytes captured (656 bits) on interface \Device\NPF_{86000046-317D-4591-92BB-CECBE0014CE2}, id 0
Ethernet II, Src: Espressi_1a:e2:09 (c4:4f:33:1a:e2:09), Dst: ASUSTekC_dd:9d:ec (40:b0:76:dd:9d:ec)
Internet Protocol Version 4, Src: 192.168.0.64, Dst: 192.168.0.30
Transmission Control Protocol, Src Port: 4444, Dst Port: 57308, Seq: 2185, Ack: 325, Len: 28
    Source Port: 4444
    Destination Port: 57308
    [Stream index: 541]
    [TCP Segment Len: 28]
    Sequence number: 2185    (relative sequence number)
    Sequence number (raw): 8695
    [Next sequence number: 2213    (relative sequence number)]
    Acknowledgment number: 325    (relative ack number)
    Acknowledgment number (raw): 883360533
    0101 .... = Header Length: 20 bytes (5)
    Flags: 0x018 (PSH, ACK)
    Window size value: 5420
    [Calculated window size: 5420]
    [Window size scaling factor: -2 (no window scaling used)]
    Checksum: 0xf640 [unverified]
    [Checksum Status: Unverified]
    Urgent pointer: 0
    [SEQ/ACK analysis]
    [Timestamps]
    TCP payload (28 bytes)
Data (28 bytes)
    Data: 02000000000000000000000000000000…
    [Length: 28]

and this is actual body data that i want to send

02 00 00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  01 00

its 28 byte of hex values.

and below code is my okSocket options.

mOkOptions = new OkSocketOptions.Builder()
          .setPulseFrequency(100)
          .setPulseFeedLoseTimes(5000)
          .setReconnectionManager(new NoneReconnect())
          .setConnectTimeoutSecond(10)
          .setReadPackageBytes(28)
          .setReaderProtocol((new IReaderProtocol() {
            @Override
            public int getHeaderLength() {
              return 20;
            }

            @Override
            public int getBodyLength(byte[] header, ByteOrder byteOrder) {
              return 28;
            }
          }))
          .setCallbackThreadModeToken(new OkSocketOptions.ThreadModeToken() {
            @Override
            public void handleCallbackEvent(ActionDispatcher.ActionRunnable runnable) {
              handler.post(runnable);
            }
          })
          .build();

and finally, below each rows are my heartbeating response

BODY : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
BODY : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
BODY : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0
BODY : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0
BODY : 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
BODY : 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
BODY : 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
BODY : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

in my head, it should be 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0

but instead its shifted 2 bytes each response.

what have i done? am i totally misunderstood of response datas? is there known issue?

can you help?

Thanks.

juicycool92 avatar Feb 02 '20 02:02 juicycool92

Tks your questions. The question you raised is a common one, many people don't know "what should I define ReadProtocol here ?" As your code

mOkOptions = new OkSocketOptions.Builder()
          .setPulseFrequency(100)
          .setPulseFeedLoseTimes(5000)
          .setReconnectionManager(new NoneReconnect())
          .setConnectTimeoutSecond(10)
          .setReadPackageBytes(28)
          .setReaderProtocol((new IReaderProtocol() {
            @Override
            public int getHeaderLength() {
              return 20;
            }

            @Override
            public int getBodyLength(byte[] header, ByteOrder byteOrder) {
              return 28;
            }
          }))
          .setCallbackThreadModeToken(new OkSocketOptions.ThreadModeToken() {
            @Override
            public void handleCallbackEvent(ActionDispatcher.ActionRunnable runnable) {
              handler.post(runnable);
            }
          })
          .build();
  • First getBodyLength(byte[] header, ByteOrder byteOrder) Method should not HardCode here because the body length is dynamic, I think you'd better read the body length from your Header bytes array.
  • Second setReadPackageBytes(28) 28 is a magic number here, you should ignore this option in common situation, setReadPackageBytes() option is design for huge package if developer need.

Oksocket will cut the bytes array(Package) and send it in to the next one when one of Package is more than 50 bytes. But sometimes developers want a huge package(which is more than 50 bytes), and we provide a method to change the default cut behavior.

Accodnig to you description above, I suggest you :

mOkOptions = new OkSocketOptions.Builder()
          ...
          .setReadPackageBytes(28) // Delete this
          .setReaderProtocol((new IReaderProtocol() {
            @Override
            public int getHeaderLength() {
              return 20; // I guess 20 is tcp header?
            }

            @Override
            public int getBodyLength(byte[] header, ByteOrder byteOrder) {
              return 28; // if 20 is TCP header I think the payload length should from header rather than hard code. "getBodyLength" equles "getPlayloadLength" 
            }
          }))
          ...
          .build();

Hope to resolve your questions, the mind is open glad to hear

xuuhaoo avatar Feb 20 '20 17:02 xuuhaoo

Hello, Can I bypass this header mechanism? Can I get all data that send from the client. I have import all module so i can able to edit code. please guild us.

tej-mpc avatar Aug 19 '21 05:08 tej-mpc