forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disconnect reason (Unity-Technologies#2280)
* disconnect reason
- Loading branch information
1 parent
f6db03f
commit 23be561
Showing
9 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Unity.Netcode | ||
{ | ||
internal struct DisconnectReasonMessage : INetworkMessage | ||
{ | ||
public string Reason; | ||
|
||
public void Serialize(FastBufferWriter writer) | ||
{ | ||
string reasonSent = Reason; | ||
if (reasonSent == null) | ||
{ | ||
reasonSent = string.Empty; | ||
} | ||
|
||
if (writer.TryBeginWrite(FastBufferWriter.GetWriteSize(reasonSent))) | ||
{ | ||
writer.WriteValueSafe(reasonSent); | ||
} | ||
else | ||
{ | ||
writer.WriteValueSafe(string.Empty); | ||
NetworkLog.LogWarning( | ||
"Disconnect reason didn't fit. Disconnected without sending a reason. Consider shortening the reason string."); | ||
} | ||
} | ||
|
||
public bool Deserialize(FastBufferReader reader, ref NetworkContext context) | ||
{ | ||
reader.ReadValueSafe(out Reason); | ||
return true; | ||
} | ||
|
||
public void Handle(ref NetworkContext context) | ||
{ | ||
((NetworkManager)context.SystemOwner).DisconnectReason = Reason; | ||
} | ||
}; | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Runtime/Messaging/DisconnectReasonMessage.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
com.unity.netcode.gameobjects/Tests/Editor/DisconnectMessageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using NUnit.Framework; | ||
using Unity.Collections; | ||
|
||
namespace Unity.Netcode.EditorTests | ||
{ | ||
public class DisconnectMessageTests | ||
{ | ||
[Test] | ||
public void EmptyDisconnectReason() | ||
{ | ||
var networkContext = new NetworkContext(); | ||
var writer = new FastBufferWriter(20, Allocator.Temp, 20); | ||
var msg = new DisconnectReasonMessage(); | ||
msg.Reason = string.Empty; | ||
msg.Serialize(writer); | ||
|
||
var fbr = new FastBufferReader(writer, Allocator.Temp); | ||
var recvMsg = new DisconnectReasonMessage(); | ||
recvMsg.Deserialize(fbr, ref networkContext); | ||
|
||
Assert.IsEmpty(recvMsg.Reason); | ||
} | ||
|
||
[Test] | ||
public void DisconnectReason() | ||
{ | ||
var networkContext = new NetworkContext(); | ||
var writer = new FastBufferWriter(20, Allocator.Temp, 20); | ||
var msg = new DisconnectReasonMessage(); | ||
msg.Reason = "Foo"; | ||
msg.Serialize(writer); | ||
|
||
var fbr = new FastBufferReader(writer, Allocator.Temp); | ||
var recvMsg = new DisconnectReasonMessage(); | ||
recvMsg.Deserialize(fbr, ref networkContext); | ||
|
||
Assert.AreEqual("Foo", recvMsg.Reason); | ||
} | ||
|
||
[Test] | ||
public void DisconnectReasonTooLong() | ||
{ | ||
var networkContext = new NetworkContext(); | ||
var writer = new FastBufferWriter(20, Allocator.Temp, 20); | ||
var msg = new DisconnectReasonMessage(); | ||
msg.Reason = "ThisStringIsWayLongerThanTwentyBytes"; | ||
msg.Serialize(writer); | ||
|
||
var fbr = new FastBufferReader(writer, Allocator.Temp); | ||
var recvMsg = new DisconnectReasonMessage(); | ||
recvMsg.Deserialize(fbr, ref networkContext); | ||
|
||
Assert.IsEmpty(recvMsg.Reason); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Tests/Editor/DisconnectMessageTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters