Skip to content

Commit

Permalink
Web sockets (#117)
Browse files Browse the repository at this point in the history
* Init

* Basic connection and open messages

* Able to connect and receive basic messages

* Ping pong

* Fixing signature issues with Secp256k1

* Adding security to message validation

* Adding websockets

* Renaming folder

* Polish for first draft

* BuildAndConnectAsync

* OOps

* Fixing reading tag if not there for messages

* Adding a bypass to bls crytography

* Adding comments

* Fixing line endings?

* Cleanup. Removing MD Api file generator too

* Adding tests and docs

* Update README.md

---------

Co-authored-by: Gekctek <[email protected]>
  • Loading branch information
Gekctek and Gekctek authored Jan 12, 2024
1 parent a5fd40b commit 2793bba
Show file tree
Hide file tree
Showing 236 changed files with 12,481 additions and 23,716 deletions.
9 changes: 7 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[*]
charset = utf-8
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true


[*.{cs,razor}]
dotnet_style_qualification_for_property = true:error
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: dotnet test --no-build --verbosity normal

- name: Publish Agent Dlls
run: dotnet publish ./src/Agent --no-restore -c Release -o ./publish-agent
run: dotnet publish ./src/WebSockets --no-restore -c Release -o ./publish-agent

- name: Upload Agent Binaries Artifact
uses: actions/upload-artifact@v2
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ jobs:

- name: Push Agent
run: dotnet nuget push EdjCase.ICP.Agent.${{ github.event.release.tag_name }}.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json


- name: Pack WebSockets
run: dotnet pack src/WebSockets/EdjCase.ICP.WebSockets.csproj --configuration Release /p:Version=${{ github.event.release.tag_name }} --output . --include-symbols --include-source

- name: Push WebSockets
run: dotnet nuget push EdjCase.ICP.WebSockets.${{ github.event.release.tag_name }}.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json

- name: Pack Client Generator
run: dotnet pack src/ClientGenerator/EdjCase.ICP.ClientGenerator.csproj --configuration Release /p:Version=${{ github.event.release.tag_name }} --output . --include-symbols --include-source

Expand Down
50 changes: 25 additions & 25 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/Sample/bin/Debug/net6.0/Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Sample",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/Sample/bin/Debug/net6.0/Sample.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Sample",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
34 changes: 17 additions & 17 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}",
"/property:GenerateFullPaths=true"
],
"problemMatcher": "$msCompile"
}
]
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}",
"/property:GenerateFullPaths=true"
],
"problemMatcher": "$msCompile"
}
]
}
246 changes: 133 additions & 113 deletions ICP.sln

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2022 Ethan Celletti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2022 Ethan Celletti
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
120 changes: 60 additions & 60 deletions UnityAssets/UnityHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,84 @@

public class UnityHttpClient : IHttpClient
{
public async Task<HttpResponse> GetAsync(string url)
{
using (UnityWebRequest request = UnityWebRequest.Get(GetUri(url)))
{
await request.SendWebRequest();
return ParseResponse(request);
}
}
public async Task<HttpResponse> GetAsync(string url, CancellationToken? cancellationToken = null)
{
using (UnityWebRequest request = UnityWebRequest.Get(GetUri(url)))
{
await request.SendWebRequest();
return ParseResponse(request);
}
}

public async Task<HttpResponse> PostAsync(string url, byte[] cborBody)
{
using (UnityWebRequest request = new())
{
request.method = "POST";
request.uri = GetUri(url);
request.downloadHandler = new DownloadHandlerBuffer();
public async Task<HttpResponse> PostAsync(string url, byte[] cborBody, CancellationToken? cancellationToken = null)
{
using (UnityWebRequest request = new())
{
request.method = "POST";
request.uri = GetUri(url);
request.downloadHandler = new DownloadHandlerBuffer();

request.uploadHandler = new UploadHandlerRaw(cborBody);
request.uploadHandler.contentType = "application/cbor";
await request.SendWebRequest();
return ParseResponse(request);
}
}
request.uploadHandler = new UploadHandlerRaw(cborBody);
request.uploadHandler.contentType = "application/cbor";
await request.SendWebRequest();
return ParseResponse(request);
}
}

private static Uri GetUri(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
return new Uri("https://ic0.app" + path);
}
private static Uri GetUri(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
return new Uri("https://ic0.app" + path);
}

private static HttpResponse ParseResponse(UnityWebRequest request)
{
if (request.result != UnityWebRequest.Result.Success)
{
throw new Exception("Failed UnityWebRequest: " + request.error);
}
HttpStatusCode statusCode = (HttpStatusCode)request.responseCode;
byte[] data = request.downloadHandler.data;
return new HttpResponse(statusCode, () => Task.FromResult(data));
}
private static HttpResponse ParseResponse(UnityWebRequest request)
{
if (request.result != UnityWebRequest.Result.Success)
{
throw new Exception("Failed UnityWebRequest: " + request.error);
}
HttpStatusCode statusCode = (HttpStatusCode)request.responseCode;
byte[] data = request.downloadHandler.data;
return new HttpResponse(statusCode, () => Task.FromResult(data));
}

}


internal class UnityWebRequestAwaiter : INotifyCompletion
{
private UnityWebRequestAsyncOperation asyncOp;
private Action continuation;
private UnityWebRequestAsyncOperation asyncOp;
private Action continuation;

public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
{
this.asyncOp = asyncOp;
asyncOp.completed += OnRequestCompleted;
}
public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
{
this.asyncOp = asyncOp;
asyncOp.completed += OnRequestCompleted;
}

public bool IsCompleted { get { return asyncOp.isDone; } }
public bool IsCompleted { get { return asyncOp.isDone; } }

public void GetResult() { }
public void GetResult() { }

public void OnCompleted(Action continuation)
{
this.continuation = continuation;
}
public void OnCompleted(Action continuation)
{
this.continuation = continuation;
}

private void OnRequestCompleted(AsyncOperation obj)
{
continuation();
}
private void OnRequestCompleted(AsyncOperation obj)
{
continuation();
}
}
internal static class ExtensionMethods
{
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
{
return new UnityWebRequestAwaiter(asyncOp);
}
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
{
return new UnityWebRequestAwaiter(asyncOp);
}
}


69 changes: 69 additions & 0 deletions UnityAssets/UnityWebSocketClient.cs.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

public class AppMessage
{

}

public class WebSocketManager : MonoBehaviour
{
private Principal devCanisterId = Principal.FromText("{devCanisterId}");
private Uri devGatewayUri = new Uri("ws://localhost:8080");
private Uri devBoundryNodeUri = new Uri("http://localhost:4943");

private Principal prodCanisterId = Principal.FromText("{prodCanisterId}");
private Uri prodGatewayUri = new Uri("wss://icwebsocketgateway.app.runonflux.io");

private IWebSocket<AppMessage>? websocket;
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

async void Start()
{
bool development = true;
Principal canisterId;
Uri gatewayUri;
if (development)
{
canisterId = devCanisterId;
gatewayUri = devGatewayUri;
}
else
{
canisterId = prodCanisterId;
gatewayUri = prodGatewayUri;
}
var builder = new WebSocketBuilder<AppMessage>(canisterId, gatewayUri)
.OnMessage(this.OnMessage)
.OnOpen(this.OnOpen)
.OnError(this.OnError)
.OnClose(this.OnClose);
if (development)
{
// Set the root key as the dev network key
SubjectPublicKeyInfo devRootKey = await new HttpAgent(
httpBoundryNodeUrl: devBoundryNodeUri
).GetRootKeyAsync();
builder = builder.WithRootKey(devRootKey);
}
this.websocket = await builder.BuildAsync(cancellationToken: cancellationTokenSource.Token);
await this.websocket.ReceiveAllAsync(cancellationTokenSource.Token);
}

void OnOpen()
{
}
void OnMessage(AppMessage message)
{
}
void OnError(Exception ex)
{
}
void OnClose()
{
}

void OnDestroy()
{
cancellationTokenSource.Cancel(); // Cancel any ongoing operations
webSocket?.Dispose();
}
}
Loading

0 comments on commit 2793bba

Please sign in to comment.