blocshop/sockets-for-cordova

Write Method Stop Working

Closed this issue · 3 comments

Hi,

I'm facing an issue related to socket's Write function when we call it in an app that was built using PhoneGapBuild.

Here is the code:

BaseController.prototype.OnConnect = function (k) {
 that.timerHandle = window.setInterval(function () {
            that.SendReadCommand.call(that);
},100);
}
BaseController.prototype.SendReadCommand = function () {
    try {        
        this.socket.write(this.dpGenerator.SetReadCommandDataPack(), this.WriteOnSuccess, this.WriteOnError);
        this._sendCommandCounter++;
    }
    catch (ex) {
        RaiseAPIExceptionEvent(ex);
    }
}

The Write function successfully executes and WriteOnSuccess callback function is called. However, it stops working after ~1000+ calls. Once it stop working, neither success nor failed callback functions is called, in other words Write function seems halted.
Interestingly, this behavior is only observed when application is built on PhoneGapBuild environment, however, if application is built using Cordova CLI and plugins are locally added to project then Write function never stop working even after 1000+ calls.
In another observation, once the function stops working if we double tap home button and go to multi-tasking view, Write function switches from halted state to normal state and success callback methods start calling, against pending Write calls. Right after executing pending Write calls it halts again.

Just to share few things related to platform and devices, the issue is only observed and checked on iOS platform so far, and the TCP device is connected through WiFi which means there is not Internet access while communicating with TCP device using socket API/plugin.

Note: cz.blocshop.socketsforcordova v1.1.0 is being used.

Looking forward to answers and support. Thanks.

Hi,

I think I know why is this happening, but not completely sure.

The socket write method doesn't call success callback, when the underlying native socket is blocked by the OS. So it means on of these 2 things:

  • the device is probably not able to send data in such a speed you are writing to socket
  • the remote server is not able to consume data in such a speed and slowsdown sending of ACKs packets

In both situations the internal buffer of OS is filled to its maximum and any other subsequent calls to write methods are blocked. But they should be unblocked after some time (when buffer become more emptier).

I don't know how many data you are sending, but you should wait for success callback before you call write method again.

It's just my guess. I don't know why multitasking triggers success callbacks and I don't know why the behavior on PhoneGap build is different.

Let me know if it helps.

Thanks for the response.

We have tried few things based on your suggestions. First we tried waiting long enough to expect success callback to be unblocked after some time but it never unblocked unless we double tap home button.

Next we tried second approach that involves waiting for success callback before calling write method again. We did this by recursively calling write function on success callback. Doing so, luckily, did not block the success callback, and we get the indication on TCP device's screen that the data is being written successfully even at faster rate then before and with no blocks even after 50k+ calls.
However, calling write function recursively gets the main thread of application busy, and makes it hard for user to perform other actions.

Though we are internally investigating the issue that how calling same function differently have different behavior - one blocks callback and other don't.
Also, we would like to have your thoughts on this. Thanks.

Note: We are sending 40bytes of data on each write call.

Using PhoneGap version "cli-5.1.1" instead of "3.7.0" resolves this issue apparently.
Will let you know if any new issue arises.