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

Valve sockets networking #465

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5506291
First go
Yamashi Oct 27, 2021
959e353
Fix weirdness with C++ callbacks
Yamashi Oct 27, 2021
d7eb965
Almost working fragmentation code
Yamashi Oct 27, 2021
ecf76e4
Fix bad fragmentation logic
Yamashi Oct 27, 2021
50a7c90
Update NetworkProvider.cs
Yamashi Oct 27, 2021
f08f700
Update NetworkProvider.cs
Yamashi Oct 27, 2021
3cb014e
Native DLLs now load correctly
Yamashi Oct 28, 2021
95a5aec
Clean up TODOs
Yamashi Oct 28, 2021
95c4ce6
Display ping as computed by the network layer
Yamashi Oct 28, 2021
8792ee5
Add reliable/unreliable packet handling
Yamashi Oct 28, 2021
8c3c2d5
Exception thrown when packet did not have the Network attributes
Yamashi Oct 28, 2021
5aa7658
FirstOrDefault's arg is the predicate not the value you want to return!
Yamashi Oct 28, 2021
e2af67e
Fix callback error when switching between Client and Server mode
Yamashi Oct 28, 2021
de3c05d
Delete websocket-sharp
Yamashi Oct 28, 2021
eef16c3
Release poll group when stopping the server
Yamashi Oct 28, 2021
ebc54a4
Some explanation regarding why we store a provider instance
Yamashi Oct 28, 2021
68665a1
Code convention & cleanup
Yamashi Oct 28, 2021
3309b7d
Cleanup
PhantomGamers Oct 28, 2021
fdb2a68
Show time it took to join in debug messages
PhantomGamers Sep 7, 2021
fdf96a1
enable VALVESOCKETS_SPAN
PhantomGamers Oct 28, 2021
a4ec19f
Fix reliable packets being reassembled unordered
Yamashi Oct 28, 2021
e02e6e9
Update GameNetworkingSockets prebuilt libs to 1.3.0
PhantomGamers Oct 29, 2021
3321783
Merge branch 'master' into pr/465
PhantomGamers Oct 29, 2021
c4f72c2
Run polling in a background thread
Yamashi Oct 29, 2021
91d2b85
Remove unused code
Yamashi Oct 29, 2021
cf984f2
abort worker thread on stop
PhantomGamers Oct 29, 2021
4c048f1
Revert "abort worker thread on stop"
PhantomGamers Oct 29, 2021
4bdd0ef
don't poll after client or server exits
PhantomGamers Oct 29, 2021
a812d43
sleep for longer if polling is disabled
PhantomGamers Oct 29, 2021
895eb14
Merge remote-tracking branch 'upstream/master' into valve-sockets
Yamashi Oct 31, 2021
46d3412
Merge branch 'master' into pr/465
PhantomGamers Nov 26, 2021
89966e4
update GameNetworkingSockets
PhantomGamers Nov 26, 2021
d23888f
Merge remote-tracking branch 'upstream/master' into valve-sockets
Yamashi Nov 28, 2021
4edc3f1
Fix packet decode failure
Yamashi Nov 28, 2021
b0005e4
Merge branch 'master' into pr/465
PhantomGamers Jan 6, 2022
2cc55e8
Merge commit '250968aa1847ab79970cf0601c95c047fa92604e' into valve-so…
starfi5h Feb 18, 2022
91f1801
merge-fix
starfi5h Feb 18, 2022
e322399
hide debug message
starfi5h Feb 18, 2022
b82a140
merge main 0.8.4
starfi5h Mar 19, 2022
2f30c54
Set SendRateMin 5 Mb/s, show progress during login
starfi5h Apr 20, 2022
c66a58d
Merge branch 'master' into @valve
starfi5h Apr 24, 2022
57387b0
Merge branch 'master' (0.8.6) into valve-socket
starfi5h Apr 27, 2022
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
22 changes: 11 additions & 11 deletions Nebula.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NebulaWorld", "NebulaWorld\NebulaWorld.csproj", "{28AEA139-FB22-4672-AF51-28B728CF2978}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "websocket-sharp", "dep\websocket-sharp\websocket-sharp\websocket-sharp.csproj", "{B357BAC7-529E-4D81-A0D2-71041B19C8DE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NebulaNetwork", "NebulaNetwork\NebulaNetwork.csproj", "{36A3D8AE-2A0A-4A1F-8A65-4FB61B01F779}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NebulaAPI", "NebulaAPI\NebulaAPI.csproj", "{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NebulaAPI", "NebulaAPI\NebulaAPI.csproj", "{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ValveSockets", "dep\valve.sockets\ValveSockets.csproj", "{D4EEDD8E-0ED6-4426-A3F0-3642A04434D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Release|Any CPU.Build.0 = Release|Any CPU
{C6237195-F77C-40C0-B06A-4AD51CAD314D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6237195-F77C-40C0-B06A-4AD51CAD314D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6237195-F77C-40C0-B06A-4AD51CAD314D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -39,22 +43,18 @@ Global
{28AEA139-FB22-4672-AF51-28B728CF2978}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28AEA139-FB22-4672-AF51-28B728CF2978}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28AEA139-FB22-4672-AF51-28B728CF2978}.Release|Any CPU.Build.0 = Release|Any CPU
{B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B357BAC7-529E-4D81-A0D2-71041B19C8DE}.Release|Any CPU.Build.0 = Release|Any CPU
{36A3D8AE-2A0A-4A1F-8A65-4FB61B01F779}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36A3D8AE-2A0A-4A1F-8A65-4FB61B01F779}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36A3D8AE-2A0A-4A1F-8A65-4FB61B01F779}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36A3D8AE-2A0A-4A1F-8A65-4FB61B01F779}.Release|Any CPU.Build.0 = Release|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47B9362C-E91B-4AE4-979B-7D811AB4D1EA}.Release|Any CPU.Build.0 = Release|Any CPU
{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B59DB3BB-DBA2-42AF-B4F1-32A9A6A273D2}.Release|Any CPU.Build.0 = Release|Any CPU
{D4EEDD8E-0ED6-4426-A3F0-3642A04434D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4EEDD8E-0ED6-4426-A3F0-3642A04434D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4EEDD8E-0ED6-4426-A3F0-3642A04434D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4EEDD8E-0ED6-4426-A3F0-3642A04434D6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion NebulaModel/NebulaModel.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\dep\websocket-sharp\websocket-sharp\websocket-sharp.csproj" />
<ProjectReference Include="..\dep\valve.sockets\ValveSockets.csproj" />
<ProjectReference Include="..\NebulaAPI\NebulaAPI.csproj" Private="false" />
</ItemGroup>

Expand Down
47 changes: 47 additions & 0 deletions NebulaModel/NetworkProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using NebulaAPI;
using NebulaModel.Logger;
using NebulaModel.Networking.Serialization;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Valve.Sockets;

namespace NebulaModel
{
Expand All @@ -10,10 +15,52 @@ public abstract class NetworkProvider : IDisposable, INetworkProvider

public IPlayerManager PlayerManager { get; }

protected static NetworkingSockets sockets = null;
protected static NetworkingUtils utils = null;

protected NetworkProvider(IPlayerManager playerManager)
{
PacketProcessor = new NetPacketProcessor();
PlayerManager = playerManager;
if (sockets == null)
{
InitializeValveSockets();
}
}

private void InitializeValveSockets()
{
Library.Initialize();

sockets = new NetworkingSockets();
utils = new NetworkingUtils();

sockets.SetManualPollMode(true);

utils.SetDebugCallback(DebugType.Everything, (DebugType type, string message) =>
{
Log.Info(message);
});

// Set high speeds
Configuration configSendRateMax = new Configuration();
configSendRateMax.data.Int32 = 0x10000000;
configSendRateMax.dataType = ConfigurationDataType.Int32;
configSendRateMax.value = ConfigurationValue.SendRateMax;

Configuration configSendRateMin = new Configuration();
configSendRateMin.data.Int32 = 0x10000000;
configSendRateMin.dataType = ConfigurationDataType.Int32;
configSendRateMin.value = ConfigurationValue.SendRateMin;

Configuration configSendBuffer = new Configuration();
configSendBuffer.data.Int32 = 0x100000;
configSendBuffer.dataType = ConfigurationDataType.Int32;
configSendBuffer.value = ConfigurationValue.SendBufferSize;

utils.SetConfigurationValue(configSendRateMax, ConfigurationScope.Global, new IntPtr());
utils.SetConfigurationValue(configSendRateMin, ConfigurationScope.Global, new IntPtr());
utils.SetConfigurationValue(configSendBuffer, ConfigurationScope.Global, new IntPtr());
}

public abstract void Start();
Expand Down
173 changes: 160 additions & 13 deletions NebulaModel/Networking/NebulaConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,204 @@
using NebulaModel.Logger;
using NebulaModel.Networking.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using WebSocketSharp;
using Valve.Sockets;

namespace NebulaModel.Networking
{
public class NebulaConnection : INebulaConnection
{
private class Fragment
{
public int Remaining;
public byte[] Data;
}

private readonly NetworkingSockets sockets;
private readonly EndPoint peerEndpoint;
private readonly WebSocket peerSocket;
private readonly uint peerSocket;
private readonly NetPacketProcessor packetProcessor;
private readonly Queue<byte[]> sendQueue = new Queue<byte[]>();
private Dictionary<uint, Fragment> fragments = new Dictionary<uint, Fragment>();
private uint nextFragmentId = 0;

private const int KMaxPacketSize = Library.maxMessageSize - 1;
private const int KMaxFragmentSize = 1 << 16;

public bool IsAlive => peerSocket?.IsAlive ?? false;
public bool IsAlive
{
get
{
ConnectionStatus status = new ConnectionStatus();
sockets.GetQuickConnectionStatus(peerSocket, ref status);

return status.state == ConnectionState.Connected;
}
}

public NebulaConnection(WebSocket peerSocket, EndPoint peerEndpoint, NetPacketProcessor packetProcessor)
public NebulaConnection(NetworkingSockets sockets, uint peerSocket, EndPoint peerEndpoint, NetPacketProcessor packetProcessor)
{
this.sockets = sockets;
this.peerEndpoint = peerEndpoint;
this.peerSocket = peerSocket;
this.packetProcessor = packetProcessor;
}

public void Update()
{
while(sendQueue.Count > 0)
{
if (SendImmediateRawPacket(sendQueue.Dequeue()) == false)
break;
}
}

public void SendPacket<T>(T packet) where T : class, new()
{
if (peerSocket.ReadyState == WebSocketState.Open)
if (IsAlive)
{
peerSocket.Send(packetProcessor.Write(packet));
SendRawPacket(packetProcessor.Write(packet));
}
else
{
Log.Warn($"Cannot send packet {packet?.GetType()} to a closed connection {peerEndpoint}");
}
}

public void SendRawPacket(byte[] rawData)
public bool SendRawPacket(byte[] rawData)
{
if (peerSocket.ReadyState == WebSocketState.Open)
if (IsAlive)
{
peerSocket.Send(rawData);
if (rawData.Length >= KMaxPacketSize)
{
FragmentPacket(rawData);
}
else
{
byte[] data = new byte[rawData.Length + 1];
data[0] = 0;
Array.Copy(rawData, 0, data, 1, rawData.Length);
var result = sockets.SendMessageToConnection(peerSocket, data);
if (result == Result.LimitExceeded)
{
sendQueue.Enqueue(data);
}
else if (result != Result.OK)
{
Log.Error($"Cannot send raw data because of error {result}");
}
else
return true;
}
}
else
{
Log.Warn($"Cannot send raw packet to a closed connection {peerSocket?.Url}");
Log.Warn($"Cannot send raw packet to a closed connection {peerEndpoint}");
}

return false;
}

private bool SendImmediateRawPacket(byte[] rawData)
{
var result = sockets.SendMessageToConnection(peerSocket, rawData);
if (result == Result.LimitExceeded)
{
sendQueue.Enqueue(rawData);
}
else if (result != Result.OK)
{
Log.Error($"Cannot send raw data because of error {result}");
}
else
return true;
return false;
}

private void FragmentPacket(byte[] rawData)
{
NetDataWriter writer = new NetDataWriter();
var fragmentId = nextFragmentId++;

for (var index = 0; index < rawData.Length; index += KMaxFragmentSize)
{
writer.Reset();

writer.Put((byte)1);
writer.Put(fragmentId);
writer.Put((int)rawData.Length);
writer.Put((uint)index);

var dataToSend = rawData.Length - index;
var dataChunk = dataToSend > KMaxFragmentSize ? KMaxFragmentSize : dataToSend;

writer.PutBytesWithLength(rawData, index, dataChunk);

SendImmediateRawPacket(writer.CopyData());
}
}

public byte[] Receive(byte[] rawData)
{
byte[] payload = rawData.Skip(1).ToArray();

if (rawData[0] == 0)
{
return payload;
}
else
{
var data = ProcessFragment(payload);
if (data != null)
{
return data;
}
}

return null;
}

private byte[] ProcessFragment(byte[] payload)
{
NetDataReader reader = new NetDataReader(payload);
var fragId = reader.GetUInt();
var totalLength = reader.GetInt();
var offset = reader.GetUInt();
var data = reader.GetBytesWithLength();

Fragment frag;
if(!fragments.TryGetValue(fragId, out frag))
{
frag = new Fragment();
frag.Data = new byte[totalLength];
frag.Remaining = totalLength;
fragments.Add(fragId, frag);
}

frag.Remaining -= data.Length;
Array.Copy(data, 0, frag.Data, offset, data.Length);

if (frag.Remaining > 0)
return null;

fragments.Remove(fragId);

return frag.Data;
}

public void Disconnect(DisconnectionReason reason = DisconnectionReason.Normal, string reasonString = null)
{
if (string.IsNullOrEmpty(reasonString))
{
peerSocket.Close((ushort)reason);
sockets.CloseConnection(peerSocket, (int)reason, "", true);
}
else
{
if (System.Text.Encoding.UTF8.GetBytes(reasonString).Length <= 123)
if (System.Text.Encoding.UTF8.GetBytes(reasonString).Length <= Library.maxCloseMessageLength)
{
peerSocket.Close((ushort)reason, reasonString);
sockets.CloseConnection(peerSocket, (int)reason, reasonString, true);
}
else
{
Expand All @@ -70,6 +213,8 @@ public void Disconnect(DisconnectionReason reason = DisconnectionReason.Normal,
return Equals(left, right);
}



public static bool operator !=(NebulaConnection left, NebulaConnection right)
{
return !Equals(left, right);
Expand All @@ -96,5 +241,7 @@ public override int GetHashCode()
{
return peerEndpoint?.GetHashCode() ?? 0;
}


}
}
Loading