-
Notifications
You must be signed in to change notification settings - Fork 1
update to use litenetlib #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| <Solution> | ||
| <Project Path="DllNetwork/DllNetwork.csproj" /> | ||
| <Project Path="DllSocket.Test/DllSocket.Test.csproj" Id="e3f318a5-fdf9-4087-be7e-55e9c56bcd4c" /> | ||
| <Project Path="DllSocket/DllSocket.csproj" Id="0d950387-53f6-4ad3-877a-d232eb9cdb31" /> | ||
| <Project Path="NetworkTest/NetworkTest.csproj" Id="5481435e-8aaf-4636-a930-404e91d4fd22" /> | ||
| </Solution> |
This file contains hidden or 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 hidden or 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,87 @@ | ||
| using DllNetwork.Json; | ||
| using DllNetwork.Managers; | ||
| using Serilog; | ||
| using System.Text.Json; | ||
|
|
||
| namespace DllNetwork.Broadcast; | ||
|
|
||
| public static class BroadcastCustom | ||
| { | ||
| static readonly HttpClient? client; | ||
| static BroadcastCustom() | ||
| { | ||
| string endPoint = NetworkSettings.Instance.Broadcast.CustomBroadcastServerEndpoint; | ||
| if (string.IsNullOrEmpty(endPoint)) | ||
| return; | ||
|
|
||
| client = new() | ||
| { | ||
| BaseAddress = new Uri(endPoint) | ||
| }; | ||
| } | ||
|
|
||
| public static void Start() | ||
| { | ||
| if (client == null) | ||
| return; | ||
|
|
||
| BroadcastJson startJson = new() | ||
| { | ||
| AccountId = NetworkSettings.Instance.Account.AccountId, | ||
| Addresses = [.. AddressHelper.Addresses.Select(static x => x.ToString())], | ||
| Port = ServerManager.Instance.Port, | ||
| }; | ||
|
|
||
| string data = JsonSerializer.Serialize(startJson, SourceGenerationContext.Default.BroadcastJson); | ||
| using var content = new StringContent(data); | ||
|
|
||
| var response = client.PostAsync("/start", content).Result; | ||
| if (response.StatusCode == System.Net.HttpStatusCode.OK) | ||
| return; | ||
|
|
||
| try | ||
| { | ||
| string rsp = response.Content.ReadAsStringAsync().Result; | ||
| Log.Warning("[BroadcastCustom.Start] Error: {code} {response}", response.StatusCode, rsp); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Warning("[BroadcastCustom.Start] Error {ex}", ex); | ||
| } | ||
|
Detanup01 marked this conversation as resolved.
Dismissed
|
||
|
|
||
| } | ||
|
|
||
| public static void Stop() | ||
| { | ||
| if (client == null) | ||
| return; | ||
|
|
||
| // We not really care about if account doesnt exists. | ||
| client.DeleteAsync($"/stop?accountId={NetworkSettings.Instance.Account.AccountId}"); | ||
| } | ||
|
|
||
| public static List<BroadcastJson> GetList() | ||
| { | ||
| List<BroadcastJson> broadcasts = []; | ||
|
|
||
| if (client == null) | ||
| return broadcasts; | ||
|
|
||
| var httpResponse = client.GetAsync("/list").Result; | ||
|
|
||
| if (httpResponse.StatusCode != System.Net.HttpStatusCode.OK) | ||
| return broadcasts; | ||
|
|
||
| try | ||
| { | ||
| string rsp = httpResponse.Content.ReadAsStringAsync().Result; | ||
| broadcasts = JsonSerializer.Deserialize(rsp, SourceGenerationContext.Default.ListBroadcastJson) ?? []; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Warning("[BroadcastCustom.GetList] Error {ex}", ex); | ||
| } | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
Detanup01 marked this conversation as resolved.
Dismissed
|
||
|
|
||
| return broadcasts; | ||
| } | ||
| } | ||
This file contains hidden or 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,124 @@ | ||
| using LiteNetLib.Utils; | ||
| using DllNetwork.Json; | ||
| using DllNetwork.Managers; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
|
|
||
| namespace DllNetwork.Broadcast; | ||
|
|
||
| public static class BroadcastUdp | ||
| { | ||
| static readonly UdpClient udp; | ||
| static readonly UdpClient? udpv6; | ||
| static readonly NetDataWriter netDataWriter = new(); | ||
| public static readonly List<BroadcastJson> AcceptedBroadcasts = []; | ||
|
|
||
| static BroadcastUdp() | ||
| { | ||
| // UDP v4 | ||
| udp = new() | ||
| { | ||
| EnableBroadcast = true, | ||
| }; | ||
|
|
||
|
|
||
| int broadcastPort = AddressHelper.GetPort(NetworkSettings.Instance.Broadcast.BroadcastPort, NetworkSettings.Instance.Broadcast.EndRangeBroadcastPort, false); | ||
| IPEndPoint broadCastEndPoint = new(IPAddress.Any, broadcastPort); | ||
|
|
||
| udp.Client.Bind(broadCastEndPoint); | ||
|
|
||
| // UDP v6 | ||
| if (!NetworkSettings.Instance.Manager.EnableIpv6) | ||
| return; | ||
|
|
||
| udpv6 = new() | ||
| { | ||
| EnableBroadcast = true, | ||
| }; | ||
|
|
||
| broadcastPort = AddressHelper.GetPort(NetworkSettings.Instance.Broadcast.BroadcastPort, NetworkSettings.Instance.Broadcast.EndRangeBroadcastPort, false); | ||
| broadCastEndPoint = new(IPAddress.IPv6Any, broadcastPort); | ||
|
|
||
| udpv6.Client.Bind(broadCastEndPoint); | ||
| } | ||
|
|
||
| public static void Start() | ||
| { | ||
| BroadcastPacket packet = new() | ||
| { | ||
| Id = NetworkSettings.Instance.Account.AccountId, | ||
| Addresses = [.. AddressHelper.Addresses.Select(static x => x.ToString())], | ||
| ConnectPort = ServerManager.Instance.Port, | ||
| }; | ||
|
|
||
| PacketProcessor.Processor.WriteNetSerializable(netDataWriter, ref packet); | ||
|
|
||
| var span = netDataWriter.AsReadOnlySpan(); | ||
|
|
||
| for (int port = NetworkSettings.Instance.Broadcast.BroadcastPort; port < NetworkSettings.Instance.Broadcast.EndRangeBroadcastPort; port++) | ||
| { | ||
| IPEndPoint address = new(IPAddress.Broadcast, port); | ||
| udp.Send(span, address); | ||
| udpv6?.Send(span, address); | ||
| } | ||
| } | ||
|
|
||
| public static void Stop() | ||
| { | ||
|
|
||
| } | ||
|
|
||
| public static List<BroadcastJson> GetList() | ||
| { | ||
| foreach (var item in AcceptedBroadcasts) | ||
| { | ||
| PingHelper.ClearPingedAccount(item.AccountId); | ||
| } | ||
|
|
||
| List<BroadcastJson> normalized = []; | ||
|
|
||
| foreach (var item in AcceptedBroadcasts) | ||
| { | ||
| if (!normalized.Exists(x => item.AccountId == x.AccountId)) | ||
| { | ||
| normalized.Add(item); | ||
| continue; | ||
| } | ||
|
|
||
| var found = normalized.FirstOrDefault(x => item.AccountId == x.AccountId); | ||
| if (found == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| found.Addresses.AddRange(item.Addresses); | ||
| } | ||
|
|
||
| AcceptedBroadcasts.Clear(); | ||
| AcceptedBroadcasts.AddRange(normalized); | ||
|
|
||
| return AcceptedBroadcasts; | ||
| } | ||
|
|
||
|
|
||
| public static void UdpUpdate() | ||
| { | ||
| udp.ReceiveAsync().ContinueWith(task => | ||
| { | ||
| if (!task.IsCompletedSuccessfully) | ||
| return; | ||
|
|
||
| NetDataReader reader = new(task.Result.Buffer); | ||
| PacketProcessor.Processor.ReadPacket(reader, task.Result.RemoteEndPoint); | ||
| }); | ||
|
|
||
| udpv6?.ReceiveAsync().ContinueWith(task => | ||
| { | ||
| if (!task.IsCompletedSuccessfully) | ||
| return; | ||
|
|
||
| NetDataReader reader = new(task.Result.Buffer); | ||
| PacketProcessor.Processor.ReadPacket(reader, task.Result.RemoteEndPoint); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,79 @@ | ||
| # Communication (broadcast) with APPNAME via Web | ||
| This communication exist for accounts that cannot use normal broadcast on the PC | ||
|
|
||
| ## ErrorCodes | ||
| There are many errors whenever using this app can happen to break the software.\ | ||
| 1 -> Account already exists | ||
| 2 -> Account does not exists | ||
|
|
||
| ## Start | ||
| Your server must store this account until its calls "stop".\ | ||
| The app automaticly tries to call the `/start` endpoint once `BroadcastCustom.Start()` called.\ | ||
| The request: | ||
| ``` | ||
| POST /start | ||
|
|
||
| { | ||
| "accountId": "1", | ||
| "addresses": | ||
| [ | ||
| "192.168.1.50", | ||
| "192.168.3.50", | ||
| "26.525.151.51", | ||
| "::1" | ||
| ], | ||
| "port": 35666 | ||
| } | ||
| ``` | ||
| Whenever the account already exist respond with: | ||
| ``` | ||
| 400 | ||
|
|
||
| 1 | ||
| ``` | ||
|
|
||
| ## Stop | ||
| Your server must remove this account from the storage.\ | ||
| The app will call `/stop?accountId=<ID>` when `BroadcastCustom.Stop()` called. | ||
| The request: | ||
| ``` | ||
| DELETE /stop?accountId=<ID> | ||
| ``` | ||
|
|
||
| Whenever the account does not exist respond with: | ||
| ``` | ||
| 400 | ||
|
|
||
| 2 | ||
| ``` | ||
|
|
||
| ## List | ||
| Your server must respond with a list of accounts to able to show all available users they can connect to. | ||
| ``` | ||
| GET /list | ||
|
|
||
| [ | ||
| { | ||
| "accountId": "1", | ||
| "addresses": | ||
| [ | ||
| "192.168.1.50", | ||
| "192.168.3.50", | ||
| "26.525.151.51", | ||
| "::1" | ||
| ], | ||
| "port": 35666 | ||
| }, | ||
| { | ||
| "accountId": "2", | ||
| "addresses": | ||
| [ | ||
| "192.168.1.70", | ||
| "192.168.3.95", | ||
| "26.525.151.151", | ||
| "::1" | ||
| ], | ||
| "port": 84643 | ||
| } | ||
| ] | ||
| ``` |
This file contains hidden or 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,6 @@ | ||
| using LiteNetLib; | ||
|
|
||
| namespace DllNetwork; | ||
|
|
||
| public delegate void ConnectedDelegate(NetPeer peer); | ||
| public delegate void DisconnectedDelegate(NetPeer peer, DisconnectInfo disconnectInfo); |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.