public override void DisconnectRemoteClient(ulong clientId) { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - DisconnectRemoteClient clientId: " + clientId); } if (!connectedUsers.ContainsKey(clientId)) { if (NetworkLog.CurrentLogLevel <= LogLevel.Error) { NetworkLog.LogError("SteamP2PTransport - Can't disconect client, client not connected, clientId: " + clientId); } return; } SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, new byte[] { 0 }, 1, (int)InternalChannelType.Disconnect, P2PSend.Reliable); SteamId steamId = connectedUsers[clientId].SteamId; NetworkingManager.Singleton.StartCoroutine(Delay(100, () => { //Need to delay the closing of the p2p sessions to not block the disconect message before it is sent. SteamNetworking.CloseP2PSessionWithUser(steamId); if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - DisconnectRemoteClient - has Closed P2P Session With clientId: " + clientId); } })); connectedUsers.Remove(clientId); }
/// <summary> /// Cleans up our instance count and warns if there instantiation issues /// </summary> public void Dispose() { Shutdown(); #if UNITY_EDITOR || DEVELOPMENT_BUILD if (s_RpcQueueContainerInstances > 0) { if (NetworkLog.CurrentLogLevel == LogLevel.Developer) { NetworkLog.LogInfo($"[Instance : {s_RpcQueueContainerInstances}] {nameof(RpcQueueContainer)} disposed."); } s_RpcQueueContainerInstances--; } else //This should never happen...if so something else has gone very wrong. { if (NetworkLog.CurrentLogLevel >= LogLevel.Normal) { NetworkLog.LogError($"[*** Warning ***] {nameof(RpcQueueContainer)} is being disposed twice?"); } throw new Exception("[*** Warning ***] System state is not stable! Check all references to the Dispose method!"); } #endif }
public override void DisconnectLocalClient() { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - DisconnectLocalClient"); } SteamNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, (int)InternalChannelType.Disconnect, P2PSend.Reliable); }
/// <summary> /// TODO /// </summary> public void ResetParameterOptions() { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogInfo("ResetParameterOptions"); } parameterSendBits = 0; animatorParameters = null; }
private void OnP2PSessionConnectFail(SteamId connectionAttemptFailedClientId, P2PSessionError request) { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - OnP2PSessionConnectFail - m_steamIDRemote: " + connectionAttemptFailedClientId + " Error: " + request.ToString()); } connectionAttemptFailed = true; this.connectionAttemptFailedClientId = connectionAttemptFailedClientId; }
private void OnP2PSessionRequest(SteamId userId) { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - OnP2PSessionRequest - m_steamIDRemote: " + userId); } //Todo: Might want to check if we expect the user before just accepting it. SteamNetworking.AcceptP2PSessionWithUser(userId); }
/// <summary> /// Puts a PooledNetworkWriter back into the pool /// </summary> /// <param name="writer">The writer to put in the pool</param> public static void PutBackInPool(PooledNetworkWriter writer) { if (s_Writers.Count < 64) { s_Writers.Enqueue(writer); } else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo($"{nameof(NetworkWriterPool)} already has 64 queued. Throwing to GC. Did you forget to dispose?"); } }
/// <summary> /// Puts a PooledBitReader back into the pool /// </summary> /// <param name="reader">The reader to put in the pool</param> public static void PutBackInPool(PooledBitReader reader) { if (readers.Count < 64) { readers.Enqueue(reader); } else if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("BitReaderPool already has 64 queued. Throwing to GC. Did you forget to dispose?"); } }
/// <summary> /// Retrieves an expandable PooledBitStream from the pool /// </summary> /// <returns>An expandable PooledBitStream</returns> public static PooledBitStream GetStream() { if (streams.Count == 0) { if (overflowStreams.Count > 0) { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("Retrieving PooledBitStream from overflow pool. Recent burst?"); } object weakStream = null; while (overflowStreams.Count > 0 && ((weakStream = overflowStreams.Dequeue().Target) == null)) { ; } if (weakStream != null) { PooledBitStream strongStream = (PooledBitStream)weakStream; strongStream.SetLength(0); strongStream.Position = 0; return(strongStream); } } if (createdStreams == 254) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("255 streams have been created. Did you forget to dispose?"); } } else if (createdStreams < 255) { createdStreams++; } return(new PooledBitStream()); } PooledBitStream stream = streams.Dequeue(); stream.SetLength(0); stream.Position = 0; return(stream); }
/// <summary> /// Retrieves an expandable PooledNetworkBuffer from the pool /// </summary> /// <returns>An expandable PooledNetworkBuffer</returns> public static PooledNetworkBuffer GetBuffer() { if (s_Buffers.Count == 0) { if (s_OverflowBuffers.Count > 0) { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo($"Retrieving {nameof(PooledNetworkBuffer)} from overflow pool. Recent burst?"); } object weakBuffer = null; while (s_OverflowBuffers.Count > 0 && ((weakBuffer = s_OverflowBuffers.Dequeue().Target) == null)) { ; } if (weakBuffer != null) { PooledNetworkBuffer strongBuffer = (PooledNetworkBuffer)weakBuffer; strongBuffer.SetLength(0); strongBuffer.Position = 0; return(strongBuffer); } } if (s_CreatedBuffers == k_MaxBitPoolBuffers) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning($"{k_MaxBitPoolBuffers} buffers have been created. Did you forget to dispose?"); } } else if (s_CreatedBuffers < k_MaxBitPoolBuffers) { s_CreatedBuffers++; } return(new PooledNetworkBuffer()); } PooledNetworkBuffer buffer = s_Buffers.Dequeue(); buffer.SetLength(0); buffer.Position = 0; return(buffer); }
/// <summary> /// RpcQueueContainer - Constructor /// Note about processExternally: this values determines if it will register with the Network Update Loop /// or not. If not, then most likely unit tests are being preformed on this class. The default value is false. /// </summary> /// <param name="maxFrameHistory"></param> /// <param name="processExternally">determines if it handles processing externally</param> public RpcQueueContainer(NetworkManager networkManager, uint maxFrameHistory = 0, bool processExternally = false) { NetworkManager = networkManager; #if UNITY_EDITOR || DEVELOPMENT_BUILD //Keep track of how many instances we have instantiated s_RpcQueueContainerInstances++; if (NetworkLog.CurrentLogLevel == LogLevel.Developer) { NetworkLog.LogInfo($"[Instance : {s_RpcQueueContainerInstances}] {nameof(RpcQueueContainer)} Initialized"); } #endif m_ProcessUpdateStagesExternally = processExternally; Initialize(maxFrameHistory); }
private void CloseP2PSessions() { foreach (User user in connectedUsers.Values) { SteamNetworking.CloseP2PSessionWithUser(user.SteamId); } if (serverUser != null) { SteamNetworking.CloseP2PSessionWithUser(serverUser.SteamId); } connectedUsers.Clear(); serverUser = null; if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - CloseP2PSessions - has Closed P2P Sessions With all Users"); } }
public override SocketTasks StartServer() { isServer = true; // setup the callback method SteamNetworking.OnP2PSessionRequest += OnP2PSessionRequest; SteamNetworking.OnP2PConnectionFailed += OnP2PSessionConnectFail; OnConnected(); if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - StartServer - ConnectToCSteamID: " + SteamClient.SteamId.ToString()); } return(SocketTask.Done.AsTasks()); }
/// <summary> /// Puts a PooledBitStream back into the pool /// </summary> /// <param name="stream">The stream to put in the pool</param> public static void PutBackInPool(PooledBitStream stream) { if (streams.Count > 16) { // The user just created lots of streams without returning them in between. // Streams are essentially byte array wrappers. This is valuable memory. // Thus we put this stream as a weak reference incase of another burst // But still leave it to GC if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("Putting PooledBitStream into overflow pool. Did you forget to dispose?"); } overflowStreams.Enqueue(new WeakReference(stream)); } else { streams.Enqueue(stream); } }
/// <summary> /// Puts a PooledNetworkBuffer back into the pool /// </summary> /// <param name="buffer">The buffer to put in the pool</param> public static void PutBackInPool(PooledNetworkBuffer buffer) { if (s_Buffers.Count > k_MaxCreatedDelta) { // The user just created lots of buffers without returning them in between. // Buffers are essentially byte array wrappers. This is valuable memory. // Thus we put this buffer as a weak reference incase of another burst // But still leave it to GC if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo($"Putting {nameof(PooledNetworkBuffer)} into overflow pool. Did you forget to dispose?"); } s_OverflowBuffers.Enqueue(new WeakReference(buffer)); } else { s_Buffers.Enqueue(buffer); } }
public override void Shutdown() { if (NetworkLog.CurrentLogLevel <= LogLevel.Developer) { NetworkLog.LogInfo("SteamP2PTransport - Shutdown"); } SteamNetworking.OnP2PSessionRequest -= OnP2PSessionRequest; SteamNetworking.OnP2PConnectionFailed -= OnP2PSessionConnectFail; if (clientOnFail != default) { SteamNetworking.OnP2PConnectionFailed -= clientOnFail; clientOnFail = default; } sendPings = false; isServer = false; connectionAttemptFailed = false; channelSendTypes.Clear(); channelCounter = 0; currentPollChannel = 0; sentPings.Clear(); pingIdCounter = 0; if (NetworkingManager.Singleton != null) { NetworkingManager.Singleton.StartCoroutine(Delay(100, () => {//Need to delay the closing of the p2p sessions to not block the disconect message before it is sent. CloseP2PSessions(); })); } else { CloseP2PSessions(); } }
/// <summary> /// Flushes the internal messages /// Removes itself from the network update loop /// Disposes readers, writers, clears the queue history, and resets any parameters /// </summary> private void Shutdown() { #if UNITY_EDITOR || DEVELOPMENT_BUILD if (NetworkLog.CurrentLogLevel == LogLevel.Developer) { NetworkLog.LogInfo($"[Instance : {s_RpcQueueContainerInstances}] {nameof(RpcQueueContainer)} shutting down."); } #endif //As long as this instance is using the pre-defined update stages if (!m_ProcessUpdateStagesExternally) { //Remove ourself from the network loop update system this.UnregisterAllNetworkUpdates(); } //Make sure any remaining internal messages are sent before completely shutting down. m_RpcQueueProcessor.InternalMessagesSendAndFlush(NetworkManager.IsListening); //Dispose of any readers and writers foreach (var queueHistorySection in m_QueueHistory) { foreach (var queueHistoryItemByStage in queueHistorySection.Value) { foreach (var queueHistoryItem in queueHistoryItemByStage.Value) { queueHistoryItem.Value.QueueWriter?.Dispose(); queueHistoryItem.Value.QueueReader?.Dispose(); queueHistoryItem.Value.QueueBuffer?.Dispose(); } } } //Clear history and parameters m_QueueHistory.Clear(); ClearParameters(); }