plasticrake / tplink-smarthome-api

TP-Link Smarthome WiFi API
MIT License
1.02k stars 142 forks source link

Send payload+fin in one go #109

Closed aanon4 closed 4 years ago

aanon4 commented 4 years ago

Send the payload using socket.end so we immediately close our end of the connection (we're not sending anything else afterwards). This doesn't stop us from receiving the reply (the connection is half-closed). The device calls are much faster this way.

Also, don't clear the timeout when we get the reply, but wait until we get the close event. This stops the connection hanging if the remote end fails to shutdown the TCP connection correctly (which seems to happen occasionally with some devices).

I've only used this project via your homebridge project, but these changes improve reliability dramatically.

aanon4 commented 4 years ago

Somewhat problematically, this now breaks you simulator :-( It appears the simulator uses 'socket.address()' to log some debugging, but this will fail once the client end has closed it's end of the connection, which this pull request does. Also, I note the simulator will also close its end of the connection as soon as it sees the client close, even if there is pending data waiting (using setTimeout). With the current API code this situation can't happen of course, but this pull requests changes this behavior.

Not sure how to address this without submitted a pull request against the simulator also.

plasticrake commented 4 years ago

Thanks! I think this LGTM. I'll take a look this weekend and also update the simulator to match.

aanon4 commented 4 years ago

Well, I upgraded to iOS14 today and suddenly this all fails on my HS300 ... which makes no sense of course, but I'd hold off on this until I get to the bottom of it.

aanon4 commented 4 years ago

I'm going to pull this (and the patch on the simulator). I dont understand why it did work (and was totally solid) but now doesn't work on my HS300 but is okay on my KP400.

plasticrake commented 4 years ago

Just a note now that I've tested this with my devices. It only works for my HS100 and HS110v1. It fails on HS105, HS110v2, HS300, and LB130. Definitely another one of those differences between devices, hardware revisions, and firmware versions.