Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketConnection: make getActiveWebSockets() static #1469

Closed
wants to merge 1 commit into from

Conversation

tius2000
Copy link
Contributor

@tius2000 tius2000 commented Oct 8, 2018

IMHO getActiveWebSockets() should be declared static. This it allows it to be used with and without an object reference.

BTW: It is marked deprecated, what method should be used instead?

@slaff
Copy link
Contributor

slaff commented Oct 9, 2018

IMHO getActiveWebSockets() should be declared static.

What is the use-case for directly using getActiveWebSockets? If it is to broadcast a message we already have that implemented. getActiveWebSockets gives a powerful access to the websocket instances that can be abused and can easily lead to memory leaks.

@mikee47
Copy link
Contributor

mikee47 commented Oct 9, 2018

Consider the following scenario:

  1. I/O (or other) request received via websocket
  2. Request executed asynchronously
  3. time passes...
  4. Client closes websocket
  5. Another client is assigned websocket, WebSocketConnection object coincidentally has same address as ours
  6. More time passes...
  7. Callback invoked on completion, result sent back to originating websocket

At (7) we need to validate the websocket to (a) ensure it's still valid, and (b) hasn't been reassigned to another client. My solution to this is was to assign a unique CID to the WebSocketConnection (actually an inherited class) at (1), then searching the active websockets list for a matching CID at (7). It uses getActiveWebsockets() do to this. Note that CID cannot be the WebSocketConnection object address.

I'll have a PR for this at some point...

@tius2000
Copy link
Contributor Author

tius2000 commented Oct 9, 2018

What is the use-case for directly using getActiveWebSockets?

If it is to broadcast a message we already have that implemented. getActiveWebSockets gives a
powerful access to the websocket instances that can be abused and can easily lead to memory leaks.

In this specific case, it is about the distribution of messages to all clients that have subscribed to a specific message types (publish-subscribe pattern). Broadcast can't be used because only subscribing clients should receive the specific messages. There is no specific object reference in the context of the event that triggers the message generation. The subscription information is stored very efficiently with setUserData(). Managing a separate lists of subscribing clients would be possible, but less efficient.

@mikee47
Copy link
Contributor

mikee47 commented Oct 9, 2018

@tuis2000 One of my changes is to provide a createConnection() callback in WebsocketResource to allow WebSocketConnection to be overridden. That should make your scenario even easier to implement.

@slaff
Copy link
Contributor

slaff commented Nov 7, 2018

@tius2000 If you want this PR merged in this release, please, rebase it on the latest develop branch. Notice that now WebSocketConnection.h is called WebsocketConnection.h.

@slaff
Copy link
Contributor

slaff commented Nov 24, 2018

I'll have a PR for this at some point...

@mikee47 Do you want to submit your PR related to websockets? There are no major changes in the pipeline from me related to websockets.

mikee47 added a commit to mikee47/Sming that referenced this pull request Feb 24, 2019
…tatic`, returning `const WebsocketList` to prevent dangerous modifications. See SmingHub#1469
@mikee47
Copy link
Contributor

mikee47 commented Feb 24, 2019

One of my changes is to provide a createConnection() callback in WebsocketResource to allow WebSocketConnection to be overridden.

I've mulled this over and I think my suggestion of overriding WebsocketConnection will just confuse things and won't add anything to the existing method using callbacks. However, there are clear use cases for access to the active websockets list, so I propose un-deprecating it, making it static, but also make it less 'dangerous' by making it return a const WebsocketList.

@slaff
Copy link
Contributor

slaff commented Feb 25, 2019

so I propose un-deprecating it, making it static, but also make it less 'dangerous' by making it return a const WebsocketList.

Sounds good. Send a PR with the proposed changes. Meanwhile I am closing this PR.

@slaff slaff closed this Feb 25, 2019
mikee47 added a commit to mikee47/Sming that referenced this pull request Feb 25, 2019
…tatic`, returning `const WebsocketList` to prevent dangerous modifications. See SmingHub#1469
mikee47 added a commit to mikee47/Sming that referenced this pull request Feb 25, 2019
…tatic`, returning `const WebsocketList` to prevent dangerous modifications. See SmingHub#1469
slaff pushed a commit that referenced this pull request Feb 26, 2019
* Fix memory leaks in `HttpConnection`

Change `waitingQueue` from un-owned `RequestQueue*` to `RequestQueue`
Free request if queue is full in `send(HttpRequest*)`

* Tidy up

* Code out of header

* Merge `HttpClientConnection` into `HttpConnection`

* HttpConnection -> HttpClientConnection

* HttpConnectionBase -> HttpConnection

* Move `isActive()` from `HttpClientConnection` into `HttpConnection` - applicable to both server and client

* Resolve `HttpClientConnection` methods temporarily un-deprecated in #1629

* Revise `HttpConnection` so it can be used with either client or server

Move `response` member variable into `HttpConnection` as both client and server have this.
Move `getResponse()` method from `HttpClientConnection` into `HttpConnection`
Add virtual `getRequest()` method to HttpConnection
Move applicable methods from `HttpClientConnection` into `HttpConnection`

Note: `send(HttpRquest*)` made virtual in previous commit

* Fix comments

* Fix deprecated method calls

* Don't initialise `userData` in `WebsocketConnection` constructor

* Un-deprecate `WebsocketConnection::getActiveWebsockets()` and make `static`, returning `const WebsocketList` to prevent dangerous modifications. See #1469
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants