Skip to content

Commit

Permalink
Made project nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuchardt88 committed Jan 3, 2024
1 parent ecbb806 commit 307d263
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 104 deletions.
8 changes: 4 additions & 4 deletions src/WebSocketServer/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Neo.Plugins
{
internal class WebSocketClient : IDisposable, IEquatable<WebSocketClient>
{
public WebSocket Socket { get; init; }
public WebSocket? Socket { get; init; }

public bool IsConnected =>
Socket != null &&
Expand All @@ -25,7 +25,7 @@ public async Task SendJsonAsync(JToken message)
{
if (IsConnected)
{
await Socket.SendAsync(
await Socket!.SendAsync(
new(Encoding.UTF8.GetBytes(message.ToString())),
WebSocketMessageType.Text,
true,
Expand All @@ -50,13 +50,13 @@ public async Task CloseAsync(WebSocketCloseStatus status)

#region IEquatable

public bool Equals(WebSocketClient other) =>
public bool Equals(WebSocketClient? other) =>
ReferenceEquals(Socket, other?.Socket);

public override int GetHashCode() =>
HashCode.Combine(this, Socket);

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(obj, this))
return true;
Expand Down
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ internal class WebSocketConnection<TClient> : IDictionary<Guid, TClient>, IDispo
{
private readonly ConcurrentDictionary<Guid, TClient> _clients;

public static event WebSocketMessageSent OnMessageSent;
public static event WebSocketMessageReceived OnMessageReceived;
public static event WebSocketConnect OnConnect;
public static event WebSocketDisconnect OnDisconnect;
public static event WebSocketMessageSent? OnMessageSent;
public static event WebSocketMessageReceived? OnMessageReceived;
public static event WebSocketConnect? OnConnect;
public static event WebSocketDisconnect? OnDisconnect;

public WebSocketConnection()
{
Expand Down Expand Up @@ -125,10 +125,10 @@ public async Task ProcessClientAsync(WebSocket client, TaskCompletionSource tcs)
{
requestId = message.RequestId;

if (WebSocketServerPlugin.Methods.TryGetValue(message.Method, out var callMethod) == false)
if (WebSocketServerPlugin.Methods.TryGetValue(message.Method!, out var callMethod) == false)
throw new WebSocketException(-32601, "Method not found");

var obj = callMethod(message.Params);
var obj = callMethod(message.Params!);

if (obj is Task<JToken> responseTask)
obj = await responseTask.ConfigureAwait(false);
Expand Down Expand Up @@ -173,8 +173,8 @@ private async Task SendJsonAndCleanUpAsync(KeyValuePair<Guid, TClient> kvp, JTok

private static async Task<WebSocketRequestMessage> ReceiveMessageAsync(Guid clientId, WebSocket client)
{
var buffer = new byte[WebSocketServerSettings.Current.MessageSize]; // 4096 bytes
WebSocketReceiveResult receiveResult = null;
var buffer = new byte[WebSocketServerSettings.Current?.MessageSize ?? WebSocketServerSettings.Default.MessageSize]; // 4096 bytes
WebSocketReceiveResult? receiveResult = null;

using var ms = new MemoryStream();
while (receiveResult == null || receiveResult.EndOfMessage == false)
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketServer/WebSocketErrorResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Neo.Plugins
internal class WebSocketErrorResult
{
public int Code { get; init; }
public string Message { get; init; }
public string Message { get; init; } = string.Empty;
#if DEBUG
public string StackTrace { get; init; }
public string? StackTrace { get; init; }
#endif

public static WebSocketErrorResult Create(Exception exception) =>
Expand Down
4 changes: 3 additions & 1 deletion src/WebSocketServer/WebSocketExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public static JToken ToJson(this NotifyEventArgs args, bool showTxHash = true)
{
["scripthash"] = $"{args?.ScriptHash}",
["eventname"] = $"{args?.EventName}",
["state"] = new JArray(args?.State?.Select(s => s.ToJson())),
["state"] = args?.State?.Count > 0 ?
new JArray(args!.State.Select(s => s.ToJson())) :
Array.Empty<JToken?>(),
};

if (showTxHash)
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketServer/WebSocketMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Neo.Plugins
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class WebSocketMethodAttribute : Attribute
{
public string Name { get; set; }
public string Name { get; set; } = string.Empty;
}
}
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Neo.Plugins
{
internal class WebSocketRequestMessage
{
public Version Version { get; set; }
public Version? Version { get; set; }
public int RequestId { get; set; }
public string Method { get; set; }
public JArray Params { get; set; }
public string? Method { get; set; }
public JArray? Params { get; set; }

public static WebSocketRequestMessage FromJson(JToken message) =>
public static WebSocketRequestMessage FromJson(JToken? message) =>
new()
{
Version = new(message["version"].AsString()),
RequestId = checked((int)message["requestid"].AsNumber()),
Method = message["method"].AsString(),
Params = (JArray)message["params"],
Version = message?["version"] == null ? null : new(message["version"]!.AsString()),
RequestId = message?["requestid"] == null ? 0 : checked((int)message["requestid"]!.AsNumber()),
Method = message?["method"]?.AsString(),
Params = message?["params"] == null ? null : (JArray)message["params"]!,
};

}
Expand Down
12 changes: 6 additions & 6 deletions src/WebSocketServer/WebSocketResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class WebSocketResponseMessage : IEquatable<WebSocketResponseMessage>
public Version Version { get; private init; } = new("1.0");
public int RequestId { get; private init; }
public byte EventId { get; private init; }
public JToken Result { get; private init; }
public JToken? Result { get; private init; }

internal static WebSocketResponseMessage Create(int requestId, JToken result, WebSocketResponseMessageEvent eventId) =>
new()
Expand Down Expand Up @@ -39,9 +39,9 @@ public JToken ToJson() =>
public static WebSocketResponseMessage FromJson(JToken message) =>
new()
{
Version = new(message["version"].AsString()),
RequestId = checked((int)message["requestid"].AsNumber()),
EventId = (byte)message["eventid"].AsNumber(),
Version = message["version"] == null ? new("1.0") : new(message["version"]!.AsString()),
RequestId = message["requestid"] == null ? 0 : checked((int)message["requestid"]!.AsNumber()),
EventId = message["eventid"] == null ? (byte)0 : unchecked((byte)message["eventid"]!.AsNumber()),
Result = message["result"],
};

Expand All @@ -51,11 +51,11 @@ public override string ToString() =>
public byte[] ToArray() =>
Encoding.UTF8.GetBytes(ToString());

public bool Equals(WebSocketResponseMessage other) =>
public bool Equals(WebSocketResponseMessage? other) =>
other != null && other.EventId == EventId && other.RequestId == RequestId &&
other.Version == Version && other.Result == Result;

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as WebSocketResponseMessage);
Expand Down
3 changes: 2 additions & 1 deletion src/WebSocketServer/WebSocketServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<PackageId>Neo.Plugins.WebSocketServer</PackageId>
<RootNamespace>Neo.Plugins</RootNamespace>
<RootNamespace>Neo.Plugins</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
38 changes: 19 additions & 19 deletions src/WebSocketServer/WebSocketServerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Neo.Plugins
public delegate void WebSocketDisconnect(Guid clientId);
public delegate void WebSocketRequest(HttpContext httpContext);
public delegate void WebSocketServerStarted();
public delegate void WebSocketMessageReceived(Guid clientId, JToken json);
public delegate void WebSocketMessageSent(Guid clientId, JToken json);
public delegate void WebSocketMessageReceived(Guid clientId, JToken? json);
public delegate void WebSocketMessageSent(Guid clientId, JToken? json);

public class WebSocketServerPlugin : Plugin
{
Expand All @@ -35,19 +35,19 @@ public class WebSocketServerPlugin : Plugin

internal static readonly Dictionary<string, Func<JArray, object>> Methods;
private static readonly WebSocketConnection<WebSocketClient> _connections;
public static event WebSocketRequest OnRequest;
public static event WebSocketServerStarted OnServerStarted;
public static event WebSocketRequest? OnRequest;
public static event WebSocketServerStarted? OnServerStarted;

#endregion

private readonly WebSocketOptions _webSocketOptions;
private readonly List<NotifyEventArgs> _notifyEvents;

private BlockchainMethods blockchainMethods;
private NeoSystem _neoSystem;
private BlockchainMethods? blockchainMethods;
private NeoSystem? _neoSystem;


private IWebHost _host;
private IWebHost? _host;

#endregion

Expand Down Expand Up @@ -86,7 +86,7 @@ public override void Dispose()
_host?.Dispose();
_connections?.Dispose();
if (_neoSystem != null)
_neoSystem.MemPool.TransactionAdded -= OnMemPoolTransactionAdded;
_neoSystem.MemPool.TransactionAdded -= OnMemPoolTransactionAdded!;
Blockchain.Committing -= OnBlockchainCommitting;
Blockchain.Committed -= OnBlockchainCommitted;
GC.SuppressFinalize(this);
Expand All @@ -106,11 +106,11 @@ protected override void OnSystemLoaded(NeoSystem system)

if (WebSocketServerSettings.Current.DebugMode)
{
ApplicationEngine.Log += OnApplicationEngineLog;
ApplicationEngine.Log += OnApplicationEngineLog!;
Utility.Logging += OnUtilityLogging;
}

_neoSystem.MemPool.TransactionAdded += OnMemPoolTransactionAdded;
_neoSystem.MemPool.TransactionAdded += OnMemPoolTransactionAdded!;
//_neoSystem.MemPool.TransactionRemoved += OnMemPoolTransactionRemoved;

blockchainMethods = new BlockchainMethods(system);
Expand All @@ -130,7 +130,7 @@ private void OnMemPoolTransactionAdded(object sender, Network.P2P.Payloads.Trans
await _connections.SendAllJsonAsync(
WebSocketResponseMessage.Create(
RandomNumberGenerator.GetInt32(2, int.MaxValue),
e.ToJson(_neoSystem.Settings),
e.ToJson(_neoSystem?.Settings),
WebSocketResponseMessageEvent.MemoryPool)
.ToJson())
.ConfigureAwait(false));
Expand Down Expand Up @@ -303,9 +303,9 @@ private void StartWebSocketServer()
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(WebSocketServerSettings.Current.BindAddress, WebSocketServerSettings.Current.Port, config =>
options.Listen(WebSocketServerSettings.Current?.BindAddress ?? WebSocketServerSettings.Default.BindAddress, WebSocketServerSettings.Current?.Port ?? WebSocketServerSettings.Default.Port, config =>
{
if (string.IsNullOrEmpty(WebSocketServerSettings.Current.SslCertFile))
if (string.IsNullOrEmpty(WebSocketServerSettings.Current?.SslCertFile))
return;
config.UseHttps(WebSocketServerSettings.Current.SslCertFile, WebSocketServerSettings.Current.SslCertPassword, httpsConnectionAdapterOptions =>
{
Expand All @@ -316,8 +316,8 @@ private void StartWebSocketServer()
{
if (err != SslPolicyErrors.None)
return false;
X509Certificate2 authority = chain.ChainElements[^1].Certificate;
return WebSocketServerSettings.Current.TrustedAuthorities.Contains(authority.Thumbprint);
X509Certificate2 authority = chain!.ChainElements[^1].Certificate;
return WebSocketServerSettings.Current.TrustedAuthorities.Contains(authority!.Thumbprint);
};
});
});
Expand All @@ -328,9 +328,9 @@ private void StartWebSocketServer()
//})
.Configure(app =>
{
_webSocketOptions.KeepAliveInterval = TimeSpan.FromSeconds(WebSocketServerSettings.Current.ConcurrentProxyTimeout);
_webSocketOptions.KeepAliveInterval = TimeSpan.FromSeconds(WebSocketServerSettings.Current?.ConcurrentProxyTimeout ?? WebSocketServerSettings.Default.ConcurrentProxyTimeout);
foreach (var origin in WebSocketServerSettings.Current.AllowOrigins)
foreach (var origin in WebSocketServerSettings.Current?.AllowOrigins ?? WebSocketServerSettings.Default.AllowOrigins)
_webSocketOptions.AllowedOrigins.Add(origin);
app.UseWebSockets(_webSocketOptions);
Expand All @@ -345,7 +345,7 @@ private void StartWebSocketServer()

private async Task ProcessRequestsAsync(HttpContext context)
{
if (WebSocketServerSettings.Current.EnableBasicAuthentication)
if (WebSocketServerSettings.Current?.EnableBasicAuthentication ?? WebSocketServerSettings.Default.EnableBasicAuthentication)
{
if (IsAuthorized(context) == false)
{
Expand Down Expand Up @@ -383,7 +383,7 @@ private static bool IsAuthorized(HttpContext context)
{
var decodedParams = Encoding.UTF8.GetString(Convert.FromBase64String(authValue.Parameter));
var creds = decodedParams.Split(':', 2);
if (creds[0] == WebSocketServerSettings.Current.User && creds[1] == WebSocketServerSettings.Current.Pass)
if (creds[0] == WebSocketServerSettings.Current?.User && creds[1] == WebSocketServerSettings.Current?.Pass)
return true;

}
Expand Down
16 changes: 8 additions & 8 deletions src/WebSocketServer/WebSocketServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace Neo.Plugins
public class WebSocketServerSettings
{
public uint Network { get; private init; }
public IPAddress BindAddress { get; private set; }
public IPAddress BindAddress { get; private set; } = IPAddress.Loopback;
public int Port { get; private init; }
public string SslCertFile { get; private init; }
public string SslCertPassword { get; private init; }
public string[] TrustedAuthorities { get; private init; }
public string? SslCertFile { get; private init; }
public string? SslCertPassword { get; private init; }
public string[] TrustedAuthorities { get; private init; } = Array.Empty<string>();
public bool EnableBasicAuthentication { get; private init; }
public string User { get; private init; }
public string Pass { get; private init; }
public string[] AllowOrigins { get; private init; }
public string? User { get; private init; }
public string? Pass { get; private init; }
public string[] AllowOrigins { get; private init; } = Array.Empty<string>();
public uint ConcurrentProxyTimeout { get; private init; }
public uint MessageSize { get; private init; }
public long MaxGasInvoke { get; private init; }
Expand All @@ -41,7 +41,7 @@ public class WebSocketServerSettings
DebugMode = false,
};

public static WebSocketServerSettings Current { get; private set; }
public static WebSocketServerSettings? Current { get; private set; }

internal static void Load(IConfigurationSection section)
{
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketServer/WebSocketUtilityLogResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Neo.Plugins
{
internal class WebSocketUtilityLogResult
{
public string SourceName { get; init; }
public string? SourceName { get; init; }
public LogLevel LogLevel { get; init; }
public object Message { get; init; }
public object? Message { get; init; }

public static WebSocketUtilityLogResult Create(string sourceName, LogLevel level, object message) =>
new()
Expand Down
Loading

0 comments on commit 307d263

Please sign in to comment.