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

Resuming from Sleep and other connection fixes #163

Merged
merged 7 commits into from
Jul 18, 2024
Merged

Conversation

gbakeman
Copy link
Contributor

@gbakeman gbakeman commented Jul 1, 2024

Socket

  • Simplified ConnectionStatus boolean property while maintaining null check
  • Simplified NUT and Net version properties
  • Removed redundant Socket object in favor of TcpClient for underlying socket management
  • Removed unused SocketDisconnected event
  • Simplify Connection logic to only do the basic work of establishing streams for reading and writing
  • Gather version information immediately after successful connection, and within error tolerant try-catch blocks so as to bypass non-critical errors
  • Login subroutine refers to instance fields instead of parameters (login info should never change during lifetime of the instance)
  • Corrected LOGIN command syntax
  • Disconnect subroutine is more likely to dispose of all related objects when called
  • Support NUT network version 1.3

UPS Device

  • Small tweaks to properties
  • Removed ReConnected event - was duplicating calls with the Connected event
  • Polling interval is now read and set correctly. No longer writable after instantiation.
  • Wrap Login method for socket

WinNUT

  • Remove unused UpdateMethod property and field
  • Add support for Suspend system power mode change to try to prepare WinNUT for suspension (isn't always called before system suspends.)
  • Reinforced Resume system power mode change to cleanup WinNUT's state if it was left connected while suspending previously
  • WinNUT initiates the call to Login to the UPS now (support for accessing UPS without a login as intended by NUT)

Related

We previously didn't support the Suspend power mode change, which meant WinNUT would be paused in the middle of execution (and with an open socket) if the system goes to sleep. Now WinNUT has the chance to close down operations and connections, which seems to at least help with errors while changing power states. It looks like WinNUT still tries to reconnect too quickly after the system resumes from sleep (before networking is fully restored?) In the interest of reducing notification spam, it may be best to have WinNUT wait a few seconds before reconnecting.
Variable was essentially unused except for within the Update GUI trigger function, when it's predetermined anyways.
- Socket Disconnect method now calls the Close_Socket method and raises the SocketDisconnected event in more cases
- System Resume event now tries to disconnect the UPS if it's still considered connected.
Nut_Socket.vb
- Removed Socket object and any relating code since TcpClient is already providing the functionality.
- Remove Disconnected event since this will be provided through exceptions or intentional commands.
- Fixed use of LOGIN protocol command
- Simplify Disconnect method

UPS_Device.vb
- Created dedicated Login method that interfaces with the socket layer.

WinNUT.vb
- Calls Login method only when a username is provided. Fixes forced/unintended logins.
@gbakeman gbakeman changed the title Handle Suspend Power Event Resuming from Sleep and other connection fixes Jul 2, 2024
Nut_Socket.vb
- ConnectionStatus property checks for null client object before checking .Connected property
- TcpClient object is left null at construction, and only instantiated during a Connection subroutine.
- Disconnect subroutine doesn't explicitly check for connection status on the TcpClient object before running anymore. This allows it to dispose/close all objects regardless.

UPS_Device.vb
- Fixed polling interval not being applied to timer. Associated property is now read only, and the interval is set directly on the timer during construction.
- Fixed missing call to socket's Disconnect subroutine, and added a general exception catch-all.
- Removed Login code from Reconnect subroutine, leaving that to external code.
- Removed ReConnected event since it seems to duplicate the Connected event's calls.

WinNUT.vb
- Moved Login call to connection finalization subroutine.
- Added LogException call during protocol error handling.
@kirsch33
Copy link

kirsch33 commented Jul 5, 2024

i am happy to see this PR--i previously have been using a fork of this repo I made for myself which the only change being the MaxRetry variable within UPS_Device.vb vastly increased. that is what has resolved the connection issues for me.

related to #65, i believe it makes sense for this PR to also address a configurable option for the MaxRetry interval.

considering this software is otherwise perfect for me besides the connection issue, it would be greatly appreciated and thank you for all your work on this!

@gbakeman
Copy link
Contributor Author

gbakeman commented Jul 6, 2024

Hi @kirsch33, thanks for your interest and sorry you had to fork just for that small change! 😄 To be honest, I wasn't planning on tackling #65 here since I'm helping another user who's experiencing a multitude of issues at the moment. Generally though, I think I'm leaning more towards indefinite/persistent connections instead of a set amount before giving up - let me know in #65 if you think it should be different.

@kirsch33
Copy link

kirsch33 commented Jul 9, 2024

@gbakeman that sounds good to me! and its no issue. this app is perfect otherwise, or else I wouldnt have went thru all that trouble lol so kudos to you and the other maintainers.

- Removed redundant IsConnected property in favor of ConnectionStatus
- Renamed Nut and Net version properties and converted them to auto-implemented properties.
- Wrapped version query statements in try-catch blocks in case servers that throw an error for these queries (Synology in particular) may not have further problems with other queries.
- Added support for Net version 1.3
@gbakeman gbakeman merged commit 8c5c8c0 into dev-2.3 Jul 18, 2024
1 check passed
@gbakeman gbakeman deleted the suspend-handling branch July 18, 2024 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WinNUT cannot reconnect automatically after Windows wakes up from sleep.
2 participants