public static void Disconnect() { initRetryCounter = 0; if (state == ConnectionState.Init) { return; } #if UNITY_WEBGL WebSocketDisconnect(); #else if (hSocketListen.handle != Baselib_Socket_Handle_Invalid.handle) { Baselib_Socket_Close(hSocketListen); hSocketListen = Baselib_Socket_Handle_Invalid; } if (hSocket.handle != Baselib_Socket_Handle_Invalid.handle) { Baselib_Socket_Close(hSocket); hSocket = Baselib_Socket_Handle_Invalid; } errState.code = Baselib_ErrorCode.Success; #endif state = ConnectionState.Init; MessageStreamManager.RecycleAll(); receiveStream.RecycleStreamAndFreeExtra(); }
public static void TransmitAndReceive() { #if ENABLE_MULTICAST Multicast.Broadcast(); #endif Connect(); if (!Connected) { if (state == ConnectionState.Invalid) { MessageStreamManager.RecycleAll(); } return; } // Check if got disconnected #if UNITY_WEBGL if (WebSocketLostConnection() == 1) #else Baselib_Socket_PollFd pollFd = new Baselib_Socket_PollFd(); unsafe { pollFd.handle.handle = hSocket.handle; pollFd.errorState = (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState); pollFd.requestedEvents = Baselib_Socket_PollEvents.Connected; Baselib_Socket_Poll(&pollFd, 1, 0, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState)); }; if (errState.code != Baselib_ErrorCode.Success) #endif { Disconnect(); return; } // Disconnection didn't occur, but we could still be waiting on a connection #if UNITY_WEBGL if (WebSocketIsConnecting() == 1) #else if (pollFd.resultEvents != Baselib_Socket_PollEvents.Connected) #endif { return; } #if ENABLE_PROFILER // While the profiler is generally lock-free, this ensures no new threads or markers are initialized and used // after sending the session stream (which contains the identification for new threads and markers) // but just before sending the related stream using this new thread or marker. The timing is pretty specific // but does happen - especially with threads unsynchronized from the main thread - such as the render thread. // // This will not have any performance implications once a marker or thread is intialized so typically we'll // stall for a couple microseconds on, say, the first frame or two and then no longer have contention. Profiler.PlayerConnectionMt_LockProfilerHashTables(); ProfilerProtocolSession.SendNewMarkersAndThreads(); ProfilerProtocolSession.SendProfilerStats(); // Calculated per frame ProfilerStats.GatheredStats = ProfilerModes.ProfileDisabled; unsafe { // It's ugly here, but this needs to be before other profiler data that is sent - so we do it manually // and only if we know we're going to TrySubmitAll() after other checks above ProfilerProtocolSession.streamSession.buffer->TrySubmitStream(true); } #endif MessageStreamManager.TrySubmitAll(); #if ENABLE_PROFILER Profiler.PlayerConnectionMt_UnlockProfilerHashTables(); #endif Receive(); if (!Connected) { return; } Transmit(); }