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

fix: throw InvalidOperationException if network client attempts to send messages while disconnected #1118

Merged
merged 6 commits into from
Dec 21, 2022

Conversation

SoftwareGuy
Copy link
Contributor

Fixes a NullReferenceException being fired since the NetworkClient's Player reference is null when trying to use the Send(...) function while disconnected. Discovered by myself while implementing some backend stuff in Mirage Standalone.

Instead, throw a InvalidOperationException and explain to the user that sending things when disconnected is bad.

Check if the connection reference is null before we send off the data. If it's null, throw a InvalidOperationException instead of letting it throw a NullReferenceException.
Comment on lines 51 to 58
try
{
clientNetClient.Send(new byte[] { 0x13, 0x37, 0x69 });
}
catch (Exception ex)
{
Assert.That(ex is InvalidOperationException);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use Assert.Throws instead

Comment on lines 30 to 43
[Test]
// Ensure that the created client game object and it's component(s) isn't null.
public void EnsureNetworkClientIsNotNull()
{
Assert.IsNotNull(clientGO);
Assert.IsNotNull(clientNetClient);
}

[Test]
// Ensure that the client object's Player reference is null.
public void EnsureNetworkClientPlayerIsNull()
{
Assert.IsNull(clientNetClient.Player);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably dont need these tests as they are just validating the setup,

I think debug.assert is better for stuff like this because these checks are more for debugging a test if it goes wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, makes sense… I’ll use Debug.Assert in the setup routine.

Comment on lines 314 to 316
if (Player == null)
throw new InvalidOperationException("Cannot send the data. Send was called while the NetworkClient connection was null. " +
"The connection may have been disconnected or terminated.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would bee good to have a "throw not connected" function for these 3 since the message iis the same, less duplicate code/strings if we ever want to change it

something like

void Send() {
  if (Player == null)
    ThrowSendNotConnected();
   
  ...
}
void ThrowSendNotConnected() {
  throw new InvalidOperationException(...);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good point. I have no problems with that, I’ll amend it.

@James-Frowen James-Frowen merged commit b7ae4de into master Dec 21, 2022
@James-Frowen James-Frowen deleted the fix-nre-disconnected-send branch December 21, 2022 13:02
github-actions bot pushed a commit that referenced this pull request Dec 21, 2022
## [129.3.1](v129.3.0...v129.3.1) (2022-12-21)

### Bug Fixes

* throw InvalidOperationException if network client attempts to send messages while disconnected ([#1118](#1118)) ([b7ae4de](b7ae4de))
@github-actions
Copy link
Contributor

🎉 This PR is included in version 129.3.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants