Skip to content

Commit

Permalink
Revert "[Remove] obsolete WsServer (#3582)" (#3603)
Browse files Browse the repository at this point in the history
This reverts commit 849b2c8.

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
cschuchardt88 and shargon authored Nov 30, 2024
1 parent 116c219 commit 6cafc3b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Neo/Network/P2P/Capabilities/NodeCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public static NodeCapability DeserializeFrom(ref MemoryReader reader)
NodeCapabilityType type = (NodeCapabilityType)reader.ReadByte();
NodeCapability capability = type switch
{
NodeCapabilityType.TcpServer => new ServerCapability(type),
#pragma warning disable CS0612 // Type or member is obsolete
NodeCapabilityType.TcpServer or NodeCapabilityType.WsServer => new ServerCapability(type),
#pragma warning restore CS0612 // Type or member is obsolete
NodeCapabilityType.FullNode => new FullNodeCapability(),
_ => throw new FormatException(),
};
Expand Down
8 changes: 8 additions & 0 deletions src/Neo/Network/P2P/Capabilities/NodeCapabilityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;

namespace Neo.Network.P2P.Capabilities
{
/// <summary>
Expand All @@ -23,6 +25,12 @@ public enum NodeCapabilityType : byte
/// </summary>
TcpServer = 0x01,

/// <summary>
/// Indicates that the node is listening on a WebSocket port.
/// </summary>
[Obsolete]
WsServer = 0x02,

#endregion

#region Others
Expand Down
6 changes: 4 additions & 2 deletions src/Neo/Network/P2P/Capabilities/ServerCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public class ServerCapability : NodeCapability
/// <summary>
/// Initializes a new instance of the <see cref="ServerCapability"/> class.
/// </summary>
/// <param name="type">The type of the <see cref="ServerCapability"/>. It must be <see cref="NodeCapabilityType.TcpServer"/></param>
/// <param name="type">The type of the <see cref="ServerCapability"/>. It must be <see cref="NodeCapabilityType.TcpServer"/> or <see cref="NodeCapabilityType.WsServer"/></param>
/// <param name="port">The port that the node is listening on.</param>
public ServerCapability(NodeCapabilityType type, ushort port = 0) : base(type)
{
if (type != NodeCapabilityType.TcpServer)
#pragma warning disable CS0612 // Type or member is obsolete
if (type != NodeCapabilityType.TcpServer && type != NodeCapabilityType.WsServer)
#pragma warning restore CS0612 // Type or member is obsolete
{
throw new ArgumentException(nameof(type));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ public void Size_Get()
{
var test = new ServerCapability(NodeCapabilityType.TcpServer) { Port = 1 };
test.Size.Should().Be(3);

#pragma warning disable CS0612 // Type or member is obsolete
test = new ServerCapability(NodeCapabilityType.WsServer) { Port = 2 };
#pragma warning restore CS0612 // Type or member is obsolete
test.Size.Should().Be(3);
}

[TestMethod]
public void DeserializeAndSerialize()
{
var test = new ServerCapability(NodeCapabilityType.TcpServer) { Port = 2 };
#pragma warning disable CS0612 // Type or member is obsolete
var test = new ServerCapability(NodeCapabilityType.WsServer) { Port = 2 };
var buffer = test.ToArray();

var br = new MemoryReader(buffer);
Expand All @@ -40,13 +46,21 @@ public void DeserializeAndSerialize()
Assert.AreEqual(test.Port, clone.Port);
Assert.AreEqual(test.Type, clone.Type);

clone = new ServerCapability(NodeCapabilityType.TcpServer, 123);
clone = new ServerCapability(NodeCapabilityType.WsServer, 123);
#pragma warning restore CS0612 // Type or member is obsolete
br = new MemoryReader(buffer);
((ISerializable)clone).Deserialize(ref br);

Assert.AreEqual(test.Port, clone.Port);
Assert.AreEqual(test.Type, clone.Type);

clone = new ServerCapability(NodeCapabilityType.TcpServer, 123);

Assert.ThrowsException<FormatException>(() =>
{
var br2 = new MemoryReader(buffer);
((ISerializable)clone).Deserialize(ref br2);
});
Assert.ThrowsException<ArgumentException>(() =>
{
_ = new ServerCapability(NodeCapabilityType.FullNode);
Expand Down

0 comments on commit 6cafc3b

Please sign in to comment.