private static void DownloadWorld(MyGuiScreenProgress progress, MyMultiplayerBase multiplayer) { if (progress.Text != null) { progress.Text.Clear(); progress.Text.Append(MyTexts.Get(MySpaceTexts.MultiplayerStateConnectingToServer)); } MyLog.Default.WriteLine("World requested"); const float worldRequestTimeout = 40; // in seconds Stopwatch worldRequestTime = Stopwatch.StartNew(); ulong serverId = multiplayer.GetOwner(); bool connected = false; progress.Tick += () => { P2PSessionState state = default(P2PSessionState); Peer2Peer.GetSessionState(multiplayer.ServerId, ref state); if (!connected && state.ConnectionActive) { MyLog.Default.WriteLine("World requested - connection alive"); connected = true; if (progress.Text != null) { progress.Text.Clear(); progress.Text.Append(MyTexts.Get(MySpaceTexts.MultiplayerStateWaitingForServer)); } } //progress.Text.Clear(); //progress.Text.AppendLine("Connecting: " + state.Connecting); //progress.Text.AppendLine("ConnectionActive: " + state.ConnectionActive); //progress.Text.AppendLine("Relayed: " + state.UsingRelay); //progress.Text.AppendLine("Bytes queued: " + state.BytesQueuedForSend); //progress.Text.AppendLine("Packets queued: " + state.PacketsQueuedForSend); //progress.Text.AppendLine("Last session error: " + state.LastSessionError); //progress.Text.AppendLine("Original server: " + serverId); //progress.Text.AppendLine("Current server: " + multiplayer.Lobby.GetOwner()); //progress.Text.AppendLine("Game version: " + multiplayer.AppVersion); if (serverId != multiplayer.GetOwner()) { MyLog.Default.WriteLine("World requested - failed, server changed"); progress.Cancel(); MyGuiSandbox.Show(MySpaceTexts.MultiplayerErrorServerHasLeft); multiplayer.Dispose(); } if (worldRequestTime.IsRunning && worldRequestTime.Elapsed.TotalSeconds > worldRequestTimeout) { MyLog.Default.WriteLine("World requested - failed, server changed"); progress.Cancel(); MyGuiSandbox.Show(MySpaceTexts.MultiplaterJoin_ServerIsNotResponding); multiplayer.Dispose(); } }; var downloadResult = multiplayer.DownloadWorld(); downloadResult.ProgressChanged += (result) => { worldRequestTime.Stop(); OnDownloadProgressChanged(progress, result, multiplayer); }; progress.ProgressCancelled += () => { downloadResult.Cancel(); multiplayer.Dispose(); //var joinScreen = MyScreenManager.GetScreenWithFocus() as MyGuiScreenJoinGame; //if (joinScreen != null) // joinScreen.ReloadList(); }; }
private static void DownloadWorld(MyGuiScreenProgress progress, MyMultiplayerBase multiplayer) { if (progress.Text != null) { progress.Text.Clear(); progress.Text.Append(MyTexts.Get(MyCommonTexts.MultiplayerStateConnectingToServer)); } MyLog.Default.WriteLine("World requested"); const float worldRequestTimeout = 40; // in seconds Stopwatch worldRequestTime = Stopwatch.StartNew(); ulong serverId = multiplayer.GetOwner(); bool connected = false; progress.Tick += () => { P2PSessionState state = default(P2PSessionState); Peer2Peer.GetSessionState(multiplayer.ServerId, ref state); if (!connected && state.ConnectionActive) { MyLog.Default.WriteLine("World requested - connection alive"); connected = true; if (progress.Text != null) { progress.Text.Clear(); progress.Text.Append(MyTexts.Get(MyCommonTexts.MultiplayerStateWaitingForServer)); } } //progress.Text.Clear(); //progress.Text.AppendLine("Connecting: " + state.Connecting); //progress.Text.AppendLine("ConnectionActive: " + state.ConnectionActive); //progress.Text.AppendLine("Relayed: " + state.UsingRelay); //progress.Text.AppendLine("Bytes queued: " + state.BytesQueuedForSend); //progress.Text.AppendLine("Packets queued: " + state.PacketsQueuedForSend); //progress.Text.AppendLine("Last session error: " + state.LastSessionError); //progress.Text.AppendLine("Original server: " + serverId); //progress.Text.AppendLine("Current server: " + multiplayer.Lobby.GetOwner()); //progress.Text.AppendLine("Game version: " + multiplayer.AppVersion); if (serverId != multiplayer.GetOwner()) { MyLog.Default.WriteLine("World requested - failed, server changed"); progress.Cancel(); MyGuiSandbox.Show(MyCommonTexts.MultiplayerErrorServerHasLeft); multiplayer.Dispose(); } if (worldRequestTime.IsRunning && worldRequestTime.Elapsed.TotalSeconds > worldRequestTimeout) { MyLog.Default.WriteLine("World requested - failed, server changed"); progress.Cancel(); MyGuiSandbox.Show(MyCommonTexts.MultiplaterJoin_ServerIsNotResponding); multiplayer.Dispose(); } }; var downloadResult = multiplayer.DownloadWorld(); downloadResult.ProgressChanged += (result) => { worldRequestTime.Stop(); OnDownloadProgressChanged(progress, result, multiplayer); }; progress.ProgressCancelled += () => { downloadResult.Cancel(); multiplayer.Dispose(); //var joinScreen = MyScreenManager.GetScreenWithFocus() as MyGuiScreenJoinGame; //if (joinScreen != null) // joinScreen.ReloadList(); }; }