public void Run() { while (running) { try { if (mediator.IsConnected()) { String[] messages = mediator.ReceiveMessage(); if (messages != null) { lock (receivedSemaphore) { foreach (String msg in messages) { ReceivedMessageQueue.Enqueue(msg); } } } if (MessageQueue.Count > 0) { String message; lock (receivedSemaphore) { message = MessageQueue.Dequeue(); } mediator.SendMessage(message); } } else { Screens.ScreenManager.GotoScreen("menu", "Lost connection to server"); running = false; } } catch (Exception ex) { Screens.ScreenManager.GotoScreen("menu", ex.Message); running = false; } } }
public MultiplayerGameState(GraphicsDevice device, String name, String ip, int port) { Players = new List <Player>(); Listeners = new List <GameListener>(); Deck = Deck.CreateFullDeck((device.Viewport.Bounds.Width / 2) - Deck.DECK_WIDTH, (device.Viewport.Bounds.Height / 2) - Deck.DECK_HEIGHT); CardPile = new CardPile(Deck.Position.X + 60, Deck.Position.Y); CurrentPlayState = PlayState.DEAL; try { NetworkMediator mediator = new NetworkMediator(); networkThread = new NetworkThread(mediator); thread = new Thread(new ThreadStart(networkThread.Run)); mediator.Connect(ip, port); mediator.SendMessage(name); String handshake = mediator.ReceiveMessageBlocking()[0]; if (!handshake.Equals("NOT_OK")) { Players.Add(new LocalPlayer(PlayerPositions.South, name)); List <String> usernames = MessageParser.ToPlayersConnected(handshake); if (usernames != null) { foreach (String username in usernames) { AddPlayer(username); } } thread.IsBackground = true; thread.Start(); } else { Screens.ScreenManager.GotoScreen("menu", "Handshaking failed"); } } catch (Exception ex) { Screens.ScreenManager.GotoScreen("menu", ex.Message); } }