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 unsafe PlayerConnection() { #if ENABLE_PLAYERCONNECTION // This builder is tracked so all can be freed during application shutdown - no need to destroy manually buffer = MessageStreamManager.CreateStreamBuilder(); #endif }
public unsafe void CheckShutdownFrees() { // (because of PlayerConnectionTestFixture.PerTestSetUp) MessageStreamManager.DestroyStreamBuilder(messageBuilder); // Kill any buffers already registered from the profiler system MessageStreamManager.Shutdown(); long sizePreInit = UnsafeUtility.GetHeapSize(Collections.Allocator.Persistent); // Start fresh MessageStreamManager.Initialize(); // The pointers returned by these 2 methods will get lost surely... MessageStreamManager.CreateStreamBuilder(); MessageStreamManager.CreateStreamBuilder(); long sizeCreate = UnsafeUtility.GetHeapSize(Collections.Allocator.Persistent); MessageStreamManager.Shutdown(); long sizeShutdown = UnsafeUtility.GetHeapSize(Collections.Allocator.Persistent); // (because of PlayerConnectionTestFixture.PerTestTearDown) // (also do this prior to asserts in case of test failure so tear down doesn't fail and we miss the real issue) MessageStreamManager.Initialize(); messageBuilder = MessageStreamManager.CreateStreamBuilder(); Assert.IsTrue(sizePreInit < sizeCreate); Assert.IsTrue(sizePreInit == sizeShutdown); }
private static unsafe void Transmit() { // Transmit anything in buffers while (HasSendDataQueued) { MessageStream *sendStream = MessageStreamManager.PlayerConnectionMt_DequeSendStream(); if (sendStream == null) // rare return value in multithreading situations when the queue is not empty { continue; } MessageStream.MessageStreamBuffer *bufferRead = sendStream->BufferRead; int offset = 0; while (bufferRead != null) { if (bufferRead->Size > 0) { #if UNITY_WEBGL uint actualRead = WebSocketSend(bufferRead->Buffer + offset, bufferRead->Size - offset); if (actualRead == 0xffffffff) #else uint actualRead = Baselib_Socket_TCP_Send(hSocket, bufferRead->Buffer + offset, (uint)(bufferRead->Size - offset), (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState)); if (errState.code != Baselib_ErrorCode.Success) #endif { // Something bad happened; lost connection maybe? // After cleaning up, next time we will try to re-initialize Disconnect(); initRetryCounter = kInitRetryCounter; return; } if (actualRead == 0) { // Move the data to be sent to the front of this buffer for next time sendStream->RecyclePartialStream(bufferRead, offset); MessageStreamManager.PlayerConnectionMt_QueueSendStream((IntPtr)sendStream); return; } offset += (int)actualRead; } if (offset == bufferRead->Size) { bufferRead = bufferRead->Next; offset = 0; } } MessageStreamManager.RecycleStream(sendStream); } }
public void PerTestSetUp() { TempMemoryScope.EnterScope(); Connection.Initialize(); #if ENABLE_PROFILER Profiler.Initialize(); #endif unsafe { messageBuilder = MessageStreamManager.CreateStreamBuilder(); } }
public void PerTestTearDown() { unsafe { MessageStreamManager.DestroyStreamBuilder(messageBuilder); } #if ENABLE_PROFILER Profiler.Shutdown(); #endif Connection.Shutdown(); SpoofExitInternal(); TempMemoryScope.ExitScope(); }
internal ProfilerProtocolStream(ulong streamId) { threadId = (ulong)streamId; blockIndex = 0; blockSizeStart = 0; blockSizeExpected = 0; profiledFrame = 0; unsafe { // This builder is tracked so all can be freed during application shutdown - no need to destroy manually buffer = MessageStreamManager.CreateStreamBuilder(); } markerStack = new MarkerStack(); }
internal static unsafe void ReceiveConnectMessage() { MessageStreamBuilder *messageStream = MessageStreamManager.CreateStreamBuilder(); messageStream->WriteData(ProfilerModes.ProfileCPU); MessageEventArgs message = new MessageEventArgs(); message.data = messageStream->m_BufferList->ToByteArray(0, messageStream->m_BufferList->TotalBytes); var callback = Connection.GetMessageEvent(EditorMessageIds.kProfilerSetMode); callback.Invoke(message); MessageStreamManager.DestroyStreamBuilder(messageStream); }
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(); }
public static void ShutdownMessageStreamManager() { MessageStreamManager.Shutdown(); }
// This happens in the default Initialize() method. // However, separate to support tests without having to // a) initialize all of player connection if it's not used otherwise // b) make the MessageStream api public when it otherwise doesn't need to be public static void InitializeMessageStreamManager() { MessageStreamManager.Initialize(); }