mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-21 09:39:56 +00:00
Revert "Revert "cleanup fizzyfacepunch""
This reverts commit c049b94527a16c3ae0f04c644362cf5b3f3f114b.
This commit is contained in:
parent
c049b94527
commit
f7ed57a787
@ -5,8 +5,8 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
public class BidirectionalDictionary<T1, T2> : IEnumerable
|
||||
{
|
||||
private Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
|
||||
private Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();
|
||||
private readonly Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
|
||||
private readonly Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();
|
||||
|
||||
public IEnumerable<T1> FirstTypes => t1ToT2Dict.Keys;
|
||||
public IEnumerable<T2> SecondTypes => t2ToT1Dict.Keys;
|
||||
@ -43,7 +43,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
if (Contains(key))
|
||||
{
|
||||
T2 val = t1ToT2Dict[key];
|
||||
var val = t1ToT2Dict[key];
|
||||
t1ToT2Dict.Remove(key);
|
||||
t2ToT1Dict.Remove(val);
|
||||
}
|
||||
@ -52,7 +52,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
if (Contains(key))
|
||||
{
|
||||
T1 val = t2ToT1Dict[key];
|
||||
var val = t2ToT1Dict[key];
|
||||
t1ToT2Dict.Remove(val);
|
||||
t2ToT1Dict.Remove(key);
|
||||
}
|
||||
|
@ -5,8 +5,5 @@ public class FizzyConnectionManager : ConnectionManager
|
||||
{
|
||||
public Action<IntPtr, int> ForwardMessage;
|
||||
|
||||
public override void OnMessage(IntPtr data, int size, long messageNum, long recvTime, int channel)
|
||||
{
|
||||
ForwardMessage(data, size);
|
||||
}
|
||||
public override void OnMessage(IntPtr data, int size, long messageNum, long recvTime, int channel) => ForwardMessage(data, size);
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using Steamworks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
@ -41,7 +40,7 @@ namespace Mirror.FizzySteam
|
||||
const string fileName = "steam_appid.txt";
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
string content = File.ReadAllText(fileName);
|
||||
var content = File.ReadAllText(fileName);
|
||||
if (content != SteamAppID)
|
||||
{
|
||||
File.WriteAllText(fileName, SteamAppID.ToString());
|
||||
@ -137,14 +136,16 @@ namespace Mirror.FizzySteam
|
||||
public override void ClientConnect(Uri uri)
|
||||
{
|
||||
if (uri.Scheme != STEAM_SCHEME)
|
||||
{
|
||||
throw new ArgumentException($"Invalid url {uri}, use {STEAM_SCHEME}://SteamID instead", nameof(uri));
|
||||
}
|
||||
|
||||
ClientConnect(uri.Host);
|
||||
}
|
||||
|
||||
public override void ClientSend(ArraySegment<byte> segment, int channelId)
|
||||
{
|
||||
byte[] data = new byte[segment.Count];
|
||||
var data = new byte[segment.Count];
|
||||
Array.Copy(segment.Array, segment.Offset, data, 0, segment.Count);
|
||||
client.Send(data, channelId);
|
||||
}
|
||||
@ -211,7 +212,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
if (ServerActive())
|
||||
{
|
||||
byte[] data = new byte[segment.Count];
|
||||
var data = new byte[segment.Count];
|
||||
Array.Copy(segment.Array, segment.Offset, data, 0, segment.Count);
|
||||
server.Send(connectionId, data, channelId);
|
||||
}
|
||||
@ -295,9 +296,6 @@ namespace Mirror.FizzySteam
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
private void OnDestroy() => Shutdown();
|
||||
}
|
||||
}
|
@ -6,8 +6,5 @@ public class FizzySocketManager : SocketManager
|
||||
{
|
||||
public Action<Connection, IntPtr, int> ForwardMessage;
|
||||
|
||||
public override void OnMessage(Connection connection, NetIdentity identity, IntPtr data, int size, long messageNum, long recvTime, int channel)
|
||||
{
|
||||
ForwardMessage(connection, data, size);
|
||||
}
|
||||
public override void OnMessage(Connection connection, NetIdentity identity, IntPtr data, int size, long messageNum, long recvTime, int channel) => ForwardMessage(connection, data, size);
|
||||
}
|
@ -21,14 +21,11 @@ namespace Mirror.FizzySteam
|
||||
private TaskCompletionSource<Task> connectedComplete;
|
||||
private CancellationTokenSource cancelToken;
|
||||
|
||||
private LegacyClient(FizzyFacepunch transport) : base(transport)
|
||||
{
|
||||
ConnectionTimeout = TimeSpan.FromSeconds(Math.Max(1, transport.Timeout));
|
||||
}
|
||||
private LegacyClient(FizzyFacepunch transport) : base(transport) => ConnectionTimeout = TimeSpan.FromSeconds(Math.Max(1, transport.Timeout));
|
||||
|
||||
public static LegacyClient CreateClient(FizzyFacepunch transport, string host)
|
||||
{
|
||||
LegacyClient c = new LegacyClient(transport);
|
||||
var c = new LegacyClient(transport);
|
||||
|
||||
c.OnConnected += () => transport.OnClientConnected.Invoke();
|
||||
c.OnDisconnected += () => transport.OnClientDisconnected.Invoke();
|
||||
@ -58,7 +55,7 @@ namespace Mirror.FizzySteam
|
||||
OnConnected += SetConnectedComplete;
|
||||
SendInternal(hostSteamID, InternalMessages.CONNECT);
|
||||
Task connectedCompleteTask = connectedComplete.Task;
|
||||
Task timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token);
|
||||
var timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token);
|
||||
|
||||
if (await Task.WhenAny(connectedCompleteTask, timeOutTask) != connectedCompleteTask)
|
||||
{
|
||||
|
@ -7,7 +7,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
public abstract class LegacyCommon
|
||||
{
|
||||
private P2PSend[] channels;
|
||||
private readonly P2PSend[] channels;
|
||||
private int internal_ch => channels.Length;
|
||||
|
||||
protected enum InternalMessages : byte
|
||||
@ -91,7 +91,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
try
|
||||
{
|
||||
while (transport.enabled && Receive(out SteamId clientSteamID, out byte[] internalMessage, internal_ch))
|
||||
while (transport.enabled && Receive(out var clientSteamID, out var internalMessage, internal_ch))
|
||||
{
|
||||
if (internalMessage.Length == 1)
|
||||
{
|
||||
@ -104,9 +104,9 @@ namespace Mirror.FizzySteam
|
||||
}
|
||||
}
|
||||
|
||||
for (int chNum = 0; chNum < channels.Length; chNum++)
|
||||
for (var chNum = 0; chNum < channels.Length; chNum++)
|
||||
{
|
||||
while (transport.enabled && Receive(out SteamId clientSteamID, out byte[] receiveBuffer, chNum))
|
||||
while (transport.enabled && Receive(out var clientSteamID, out var receiveBuffer, chNum))
|
||||
{
|
||||
OnReceiveData(receiveBuffer, clientSteamID, chNum);
|
||||
}
|
||||
|
@ -12,13 +12,13 @@ namespace Mirror.FizzySteam
|
||||
private event Action<int> OnDisconnected;
|
||||
private event Action<int, Exception> OnReceivedError;
|
||||
|
||||
private BidirectionalDictionary<SteamId, int> steamToMirrorIds;
|
||||
private int maxConnections;
|
||||
private readonly BidirectionalDictionary<SteamId, int> steamToMirrorIds;
|
||||
private readonly int maxConnections;
|
||||
private int nextConnectionID;
|
||||
|
||||
public static LegacyServer CreateServer(FizzyFacepunch transport, int maxConnections)
|
||||
{
|
||||
LegacyServer s = new LegacyServer(transport, maxConnections);
|
||||
var s = new LegacyServer(transport, maxConnections);
|
||||
|
||||
s.OnConnected += (id) => transport.OnServerConnected.Invoke(id);
|
||||
s.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id);
|
||||
@ -61,13 +61,13 @@ namespace Mirror.FizzySteam
|
||||
|
||||
SendInternal(clientSteamID, InternalMessages.ACCEPT_CONNECT);
|
||||
|
||||
int connectionId = nextConnectionID++;
|
||||
var connectionId = nextConnectionID++;
|
||||
steamToMirrorIds.Add(clientSteamID, connectionId);
|
||||
OnConnected.Invoke(connectionId);
|
||||
Debug.LogError($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}");
|
||||
break;
|
||||
case InternalMessages.DISCONNECT:
|
||||
if (steamToMirrorIds.TryGetValue(clientSteamID, out int connId))
|
||||
if (steamToMirrorIds.TryGetValue(clientSteamID, out var connId))
|
||||
{
|
||||
OnDisconnected.Invoke(connId);
|
||||
CloseP2PSessionWithUser(clientSteamID);
|
||||
@ -88,7 +88,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
protected override void OnReceiveData(byte[] data, SteamId clientSteamID, int channel)
|
||||
{
|
||||
if (steamToMirrorIds.TryGetValue(clientSteamID, out int connectionId))
|
||||
if (steamToMirrorIds.TryGetValue(clientSteamID, out var connectionId))
|
||||
{
|
||||
OnReceivedData.Invoke(connectionId, data, channel);
|
||||
}
|
||||
@ -102,7 +102,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public void Disconnect(int connectionId)
|
||||
{
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out SteamId steamID))
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out var steamID))
|
||||
{
|
||||
SendInternal(steamID, InternalMessages.DISCONNECT);
|
||||
steamToMirrorIds.Remove(connectionId);
|
||||
@ -128,7 +128,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public void Send(int connectionId, byte[] data, int channelId)
|
||||
{
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out SteamId steamId))
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out var steamId))
|
||||
{
|
||||
Send(steamId, data, channelId);
|
||||
}
|
||||
@ -141,7 +141,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public string ServerGetClientAddress(int connectionId)
|
||||
{
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out SteamId steamId))
|
||||
if (steamToMirrorIds.TryGetValue(connectionId, out var steamId))
|
||||
{
|
||||
return steamId.ToString();
|
||||
}
|
||||
@ -155,7 +155,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
protected override void OnConnectionFailed(SteamId remoteId)
|
||||
{
|
||||
int connectionId = steamToMirrorIds.TryGetValue(remoteId, out int connId) ? connId : nextConnectionID++;
|
||||
var connectionId = steamToMirrorIds.TryGetValue(remoteId, out var connId) ? connId : nextConnectionID++;
|
||||
OnDisconnected.Invoke(connectionId);
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ namespace Mirror.FizzySteam
|
||||
private SteamId hostSteamID = 0;
|
||||
private FizzyConnectionManager HostConnectionManager;
|
||||
private Connection HostConnection => HostConnectionManager.Connection;
|
||||
private List<Action> BufferedData;
|
||||
private readonly List<Action> BufferedData;
|
||||
|
||||
private NextClient(FizzyFacepunch transport)
|
||||
{
|
||||
@ -35,7 +35,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public static NextClient CreateClient(FizzyFacepunch transport, string host)
|
||||
{
|
||||
NextClient c = new NextClient(transport);
|
||||
var c = new NextClient(transport);
|
||||
|
||||
c.OnConnected += () => transport.OnClientConnected.Invoke();
|
||||
c.OnDisconnected += () => transport.OnClientDisconnected.Invoke();
|
||||
@ -63,13 +63,13 @@ namespace Mirror.FizzySteam
|
||||
|
||||
try
|
||||
{
|
||||
hostSteamID = UInt64.Parse(host);
|
||||
hostSteamID = ulong.Parse(host);
|
||||
connectedComplete = new TaskCompletionSource<Task>();
|
||||
OnConnected += SetConnectedComplete;
|
||||
HostConnectionManager = SteamNetworkingSockets.ConnectRelay<FizzyConnectionManager>(hostSteamID);
|
||||
HostConnectionManager.ForwardMessage = OnMessageReceived;
|
||||
Task connectedCompleteTask = connectedComplete.Task;
|
||||
Task timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token);
|
||||
var timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token);
|
||||
|
||||
if (await Task.WhenAny(connectedCompleteTask, timeOutTask) != connectedCompleteTask)
|
||||
{
|
||||
@ -116,7 +116,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
private void OnMessageReceived(IntPtr dataPtr, int size)
|
||||
{
|
||||
(byte[] data, int ch) = ProcessMessage(dataPtr, size);
|
||||
(var data, var ch) = ProcessMessage(dataPtr, size);
|
||||
if (Connected)
|
||||
{
|
||||
OnReceivedData(data, ch);
|
||||
@ -140,7 +140,7 @@ namespace Mirror.FizzySteam
|
||||
{
|
||||
Debug.LogError($"{BufferedData.Count} received before connection was established. Processing now.");
|
||||
{
|
||||
foreach (Action a in BufferedData)
|
||||
foreach (var a in BufferedData)
|
||||
{
|
||||
a();
|
||||
}
|
||||
@ -174,14 +174,11 @@ namespace Mirror.FizzySteam
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceiveData()
|
||||
{
|
||||
HostConnectionManager.Receive(MAX_MESSAGES);
|
||||
}
|
||||
public void ReceiveData() => HostConnectionManager.Receive(MAX_MESSAGES);
|
||||
|
||||
public void Send(byte[] data, int channelId)
|
||||
{
|
||||
Result res = SendSocket(HostConnection, data, channelId);
|
||||
var res = SendSocket(HostConnection, data, channelId);
|
||||
|
||||
if (res != Result.OK)
|
||||
{
|
||||
@ -191,9 +188,6 @@ namespace Mirror.FizzySteam
|
||||
|
||||
private void SetConnectedComplete() => connectedComplete.SetResult(connectedComplete.Task);
|
||||
private void OnConnectionFailed() => OnDisconnected.Invoke();
|
||||
public void FlushData()
|
||||
{
|
||||
HostConnection.Flush();
|
||||
}
|
||||
public void FlushData() => HostConnection.Flush();
|
||||
}
|
||||
}
|
@ -14,10 +14,10 @@ public abstract class NextCommon
|
||||
Array.Resize(ref data, data.Length + 1);
|
||||
data[data.Length - 1] = (byte)channelId;
|
||||
|
||||
GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
|
||||
IntPtr pData = pinnedArray.AddrOfPinnedObject();
|
||||
SendType sendFlag = channelId == Channels.Unreliable ? SendType.Unreliable : SendType.Reliable;
|
||||
Result res = conn.SendMessage(pData, data.Length, sendFlag);
|
||||
var pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
|
||||
var pData = pinnedArray.AddrOfPinnedObject();
|
||||
var sendFlag = channelId == Channels.Unreliable ? SendType.Unreliable : SendType.Reliable;
|
||||
var res = conn.SendMessage(pData, data.Length, sendFlag);
|
||||
if (res != Result.OK)
|
||||
{
|
||||
Debug.LogWarning($"Send issue: {res}");
|
||||
@ -29,7 +29,7 @@ public abstract class NextCommon
|
||||
|
||||
protected (byte[], int) ProcessMessage(IntPtr ptrs, int size)
|
||||
{
|
||||
byte[] managedArray = new byte[size];
|
||||
var managedArray = new byte[size];
|
||||
Marshal.Copy(ptrs, managedArray, 0, size);
|
||||
int channel = managedArray[managedArray.Length - 1];
|
||||
Array.Resize(ref managedArray, managedArray.Length - 1);
|
||||
|
@ -12,9 +12,9 @@ namespace Mirror.FizzySteam
|
||||
private event Action<int> OnDisconnected;
|
||||
private event Action<int, Exception> OnReceivedError;
|
||||
|
||||
private BidirectionalDictionary<Connection, int> connToMirrorID;
|
||||
private BidirectionalDictionary<SteamId, int> steamIDToMirrorID;
|
||||
private int maxConnections;
|
||||
private readonly BidirectionalDictionary<Connection, int> connToMirrorID;
|
||||
private readonly BidirectionalDictionary<SteamId, int> steamIDToMirrorID;
|
||||
private readonly int maxConnections;
|
||||
private int nextConnectionID;
|
||||
|
||||
private FizzySocketManager listenSocket;
|
||||
@ -30,7 +30,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public static NextServer CreateServer(FizzyFacepunch transport, int maxConnections)
|
||||
{
|
||||
NextServer s = new NextServer(maxConnections);
|
||||
var s = new NextServer(maxConnections);
|
||||
|
||||
s.OnConnected += (id) => transport.OnServerConnected.Invoke(id);
|
||||
s.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id);
|
||||
@ -78,7 +78,7 @@ namespace Mirror.FizzySteam
|
||||
}
|
||||
else if (info.State == ConnectionState.Connected)
|
||||
{
|
||||
int connectionId = nextConnectionID++;
|
||||
var connectionId = nextConnectionID++;
|
||||
connToMirrorID.Add(conn, connectionId);
|
||||
steamIDToMirrorID.Add(clientSteamID, connectionId);
|
||||
OnConnected.Invoke(connectionId);
|
||||
@ -86,7 +86,7 @@ namespace Mirror.FizzySteam
|
||||
}
|
||||
else if (info.State == ConnectionState.ClosedByPeer)
|
||||
{
|
||||
if (connToMirrorID.TryGetValue(conn, out int connId))
|
||||
if (connToMirrorID.TryGetValue(conn, out var connId))
|
||||
{
|
||||
InternalDisconnect(connId, conn);
|
||||
}
|
||||
@ -108,7 +108,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public void Disconnect(int connectionId)
|
||||
{
|
||||
if (connToMirrorID.TryGetValue(connectionId, out Connection conn))
|
||||
if (connToMirrorID.TryGetValue(connectionId, out var conn))
|
||||
{
|
||||
Debug.LogError($"Connection id {connectionId} disconnected.");
|
||||
conn.Close(false, 0, "Disconnected by server");
|
||||
@ -124,28 +124,25 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public void FlushData()
|
||||
{
|
||||
foreach (Connection conn in connToMirrorID.FirstTypes)
|
||||
foreach (var conn in connToMirrorID.FirstTypes)
|
||||
{
|
||||
conn.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceiveData()
|
||||
{
|
||||
listenSocket.Receive(MAX_MESSAGES);
|
||||
}
|
||||
public void ReceiveData() => listenSocket.Receive(MAX_MESSAGES);
|
||||
|
||||
private void OnMessageReceived(Connection conn, IntPtr dataPtr, int size)
|
||||
{
|
||||
(byte[] data, int ch) = ProcessMessage(dataPtr, size);
|
||||
(var data, var ch) = ProcessMessage(dataPtr, size);
|
||||
OnReceivedData(connToMirrorID[conn], data, ch);
|
||||
}
|
||||
|
||||
public void Send(int connectionId, byte[] data, int channelId)
|
||||
{
|
||||
if (connToMirrorID.TryGetValue(connectionId, out Connection conn))
|
||||
if (connToMirrorID.TryGetValue(connectionId, out var conn))
|
||||
{
|
||||
Result res = SendSocket(conn, data, channelId);
|
||||
var res = SendSocket(conn, data, channelId);
|
||||
|
||||
if (res == Result.NoConnection || res == Result.InvalidParam)
|
||||
{
|
||||
@ -166,7 +163,7 @@ namespace Mirror.FizzySteam
|
||||
|
||||
public string ServerGetClientAddress(int connectionId)
|
||||
{
|
||||
if (steamIDToMirrorID.TryGetValue(connectionId, out SteamId steamId))
|
||||
if (steamIDToMirrorID.TryGetValue(connectionId, out var steamId))
|
||||
{
|
||||
return steamId.ToString();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user