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

RPC-JSON over WebSockets #847

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0f70f8d
init project websocketserver
cschuchardt88 Nov 22, 2023
e2de661
Updated json
cschuchardt88 Nov 22, 2023
35d7c70
dotnet format
cschuchardt88 Nov 23, 2023
983d543
Removed unused code
cschuchardt88 Nov 23, 2023
528e1ca
Fixed codeql
cschuchardt88 Nov 23, 2023
cf968be
Added EnableBasicAuthentication, User, Pass, AllowOrigins and Concurr…
cschuchardt88 Nov 23, 2023
5a76fab
Updated SslCert to SslCertFile
cschuchardt88 Nov 23, 2023
9410d92
fix codeql again
cschuchardt88 Nov 23, 2023
665e4f5
revert *.csproj files configuation
cschuchardt88 Nov 23, 2023
e5a0e29
code clean up
cschuchardt88 Nov 25, 2023
8c8f611
fixed format
cschuchardt88 Nov 25, 2023
8e1c9f7
format again
cschuchardt88 Nov 25, 2023
e20e84c
one more time
cschuchardt88 Nov 25, 2023
ec72689
one more (project files) dotnet format dont work on them
cschuchardt88 Nov 25, 2023
8d10b0c
Add new features
cschuchardt88 Nov 26, 2023
5df1de0
changed function to static
cschuchardt88 Nov 26, 2023
03015d7
dotnet format
cschuchardt88 Nov 26, 2023
5acf717
Merge branch 'master' into WebSocketServer
cschuchardt88 Dec 3, 2023
2f8d05f
Fixed disconnect issue
cschuchardt88 Dec 3, 2023
67e0f91
Build out basic blockchain methods for getting data
cschuchardt88 Dec 3, 2023
fc67dee
INIT PROJECT
cschuchardt88 Dec 3, 2023
f21c286
Fixed MERGE
cschuchardt88 Dec 3, 2023
4bc67df
Fixed sln file
cschuchardt88 Dec 3, 2023
2597889
Added basic protocol methods for the node information.
cschuchardt88 Dec 3, 2023
8b240b5
Add mempool and code clean up
cschuchardt88 Dec 5, 2023
207fe70
Merge branch 'master' into WebSocketServer
cschuchardt88 Dec 6, 2023
f54ed8f
Code clean up
cschuchardt88 Dec 12, 2023
86d915d
Touched up code and reuseable with ws protocol changes
cschuchardt88 Dec 22, 2023
3d999cb
Updated SendAllJson, added pluginId
cschuchardt88 Dec 22, 2023
baaccad
Added Invoke Contract
cschuchardt88 Dec 25, 2023
b7af661
Fixed exception null in InvokeContract
cschuchardt88 Dec 25, 2023
19b8da6
Fixed gasconsumed in InvokeContract method, was output as string, now…
cschuchardt88 Dec 25, 2023
987dc46
Remove virtual methods from WebSocketConnection
cschuchardt88 Dec 25, 2023
8e53db8
Fixed IEquatable for WebSocketResponseMessage
cschuchardt88 Dec 25, 2023
f6ff6a8
Add some wallet methods to the api, not completed yet.
cschuchardt88 Dec 26, 2023
53cefdf
Added more wallet methods
cschuchardt88 Dec 29, 2023
3bb4306
Add more wallet methods
cschuchardt88 Dec 29, 2023
1832fd8
finsished the wallet api
cschuchardt88 Dec 30, 2023
8dcb91e
Update WalletSessionManager.cs
cschuchardt88 Dec 30, 2023
074649f
Remove TClient and null checks
shargon Jan 3, 2024
8927853
Prevent problems with date seasons changes
shargon Jan 3, 2024
32a867e
Revert "Remove TClient and null checks"
cschuchardt88 Jan 3, 2024
1ec7644
Fixed WebSocketClient
cschuchardt88 Jan 3, 2024
ecbb806
Fixed error
cschuchardt88 Jan 3, 2024
307d263
Made project nullable
cschuchardt88 Jan 3, 2024
23d9a34
Update BlockchainMethods.cs
cschuchardt88 Jan 3, 2024
ac6526f
Merge branch 'master' into WebSocketServer
cschuchardt88 Jan 12, 2024
580b864
format
cschuchardt88 Jan 12, 2024
7d8f492
RPC-JSON added still needs programming
cschuchardt88 Jan 12, 2024
35675e2
Added event system
cschuchardt88 Jan 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 158 additions & 151 deletions neo-modules.sln

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/WebSocketServer/WalletSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Neo.Wallets;
using System;

namespace Neo.Plugins
{
public class WalletSession
{
public Wallet Wallet { get; private init; }
public DateTime Expires { get; private set; }

public WalletSession(
Wallet wallet)
{
Wallet = wallet;
ResetExpiration();
}

public void ResetExpiration() =>
Expires = DateTime.Now.AddSeconds(WebSocketServerSettings.Current?.WalletSessionTimeout ?? WebSocketServerSettings.Default.WalletSessionTimeout);
}
}
32 changes: 32 additions & 0 deletions src/WebSocketServer/WalletSessionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Plugins
{
public class WalletSessionManager : ConcurrentDictionary<Guid, WalletSession>
{
private readonly PeriodicTimer _timer;

public WalletSessionManager()
{
_timer = new(TimeSpan.FromSeconds(1));
_ = Task.Run(SessionTimeoutAsync);
}

private async Task SessionTimeoutAsync()
{
while (await _timer.WaitForNextTickAsync())
{
var killAll = this.Where(w => w.Value.Expires <= DateTime.Now)
.Select(s => Task.Run(() =>
{
TryRemove(s);
}));
await Task.WhenAll(killAll);
}
}
}
}
86 changes: 86 additions & 0 deletions src/WebSocketServer/WebSocketClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Neo.Json;
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Neo.Plugins
{
internal class WebSocketClient : IDisposable, IEquatable<WebSocketClient>
{
public WebSocket Socket { get; init; }
Copy link
Member

Choose a reason for hiding this comment

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

required and remove null checks?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not null there. Hints the reason why no ? and init on property. We don't want null.

Copy link
Member

Choose a reason for hiding this comment

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

It is not null there. Hints the reason why no ? and init on property. We don't want null.

Yep, but it's used with ? inside the class

Copy link
Member Author

@cschuchardt88 cschuchardt88 Jan 3, 2024

Choose a reason for hiding this comment

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

Right and that reason, is for if client disconnect and the class becomes disposed. But you should never pass in a null WebSocket.

Copy link
Member

Choose a reason for hiding this comment

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

I marked the project as nullable and I detected it, I was going to fix it, I think that new projects should be nullable


public bool IsConnected =>
Socket != null &&
Socket.State == WebSocketState.Open;

public void Dispose()
{
Socket?.Dispose();
GC.SuppressFinalize(this);
}

public async Task SendJsonAsync(JToken message)
{
if (IsConnected)
{
await Socket!.SendAsync(
new(Encoding.UTF8.GetBytes(message.ToString())),
WebSocketMessageType.Text,
true,
CancellationToken.None).ConfigureAwait(false);
}
}

public async Task CloseAsync(WebSocketCloseStatus status)
{
if (Socket == null)
return;
switch (Socket.State)
{
case WebSocketState.Connecting:
case WebSocketState.Open:
await Socket.CloseOutputAsync(status, string.Empty, CancellationToken.None).ConfigureAwait(false);
break;
default:
break;
}
}

#region IEquatable

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

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

public override bool Equals(object obj)
{
if (ReferenceEquals(obj, this))
return true;
if (obj == null)
return false;
if (obj is not WebSocketClient wsObj)
return false;
return Equals(wsObj);
}

public static bool operator ==(WebSocketClient left, WebSocketClient right)
{
if (left as object is null || right as object is null)
return Equals(left, right);
return left.Equals(right);
}

public static bool operator !=(WebSocketClient left, WebSocketClient right)
{
if (left as object is null || right as object is null)
return Equals(left, right) == false;
return left.Equals(right) == false;
}

#endregion
}
}
Loading