public void ClientHandleConnection(object state) { // TcpClient NetworkStream ns = client.GetStream(); BinaryFormatter bf = new BinaryFormatter(); IntroduceMessage i = new IntroduceMessage(); i.senderID = -1; i.playerNick = GlobalData.me.name; i.house = GlobalData.me.House; bf.Serialize(ns, i); NetworkMessage nm; nm = bf.Deserialize(ns) as NetworkMessage; if (nm != null) { if (nm is ResponseMessage) { GlobalData.me.userID = ((ResponseMessage)nm).NewID; Network.connections.Add(client, GlobalData.me.userID); } } try { while (true) { nm = (NetworkMessage)bf.Deserialize(ns); if (nm == null) { continue; } if (nm is TextMessage) { lock (Network.messages) // tu by³o ; { Network.messages.Enqueue(nm); } if (NewMessage != null) { NewMessage(this, new EventArgs()); } } if (nm is PlayersListMessage) { lock (GlobalData.usersList) { GlobalData.usersList = ((PlayersListMessage)nm).players; } if (PlayersUpdate != null) { PlayersUpdate(this, new EventArgs()); } } if (nm is StartGameMessage) { lock (Network.messages) { Network.messages.Enqueue((StartGameMessage)nm); } GlobalData.playersReadyToPlay = new bool[1]; if (NewMessage != null) { NewMessage(this, new EventArgs()); } } if (nm is CreateMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((CreateMessage)nm); } } } if (nm is DestroyMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((DestroyMessage)nm); } } } if (nm is MoveMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((MoveMessage)nm); } } } if (nm is AttackMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((AttackMessage)nm); } } } } } catch (Exception) { //MessageBox.Show(exc.Message); } finally { ns.Close(); AfterDisconnection(); } }
public void ServerHandleConnection(object state) { NetworkStream ns = client.GetStream(); BinaryFormatter bf = new BinaryFormatter(); NetworkMessage nm; try { while (true) { nm = (NetworkMessage)bf.Deserialize(ns); if (nm == null) { continue; } if (nm is IntroduceMessage) { IntroduceMessage im = nm as IntroduceMessage; int userID = User.GetNewID(); Network.connections.Add(client, userID); ResponseMessage rm = new ResponseMessage(); rm.senderID = GlobalData.me.userID; rm.NewID = userID; bf.Serialize(ns, rm); User u = new User(); u.name = im.playerNick; u.House = im.house; u.userID = userID; GlobalData.usersList.Add(u); //PlayersListMessage playerList = new PlayersListMessage(); //playerList.players = GlobalData.usersList; //bf.Serialize(ns, playerList); PlayersListMessage pl = new PlayersListMessage(); pl.players = GlobalData.usersList; pl.senderID = GlobalData.me.userID; Network.SendMessage(pl); if (PlayersUpdate != null) { PlayersUpdate(this, new EventArgs()); } } if (nm is TextMessage) { lock (Network.messages) { Network.messages.Enqueue(nm); } if (NewMessage != null) { NewMessage(this, new EventArgs()); } Network.SendMessage(nm); } if (nm is PlayersListMessage) { if (((PlayersListMessage)nm).players.Count == 1) { User u = ((PlayersListMessage)nm).players[0]; int i; for (i = 0; i < GlobalData.usersList.Count; i++) { if (GlobalData.usersList[i].userID == u.userID) { GlobalData.usersList[i].name = u.name; GlobalData.usersList[i].House = u.House; break; } } //tego nie powinno byæ if (i == GlobalData.usersList.Count) { client.Close(); Network.connections.Remove(client); throw new Exception(); //Not on the list! } PlayersListMessage pl = new PlayersListMessage(); pl.players = GlobalData.usersList; pl.senderID = GlobalData.me.userID; Network.SendMessage(pl); if (PlayersUpdate != null) { PlayersUpdate(this, new EventArgs()); } } } if (nm is ReadyToPlayMessage) { int u = Network.connections[client]; for (int i = 0; i < GlobalData.usersList.Count; i++) { if (u == GlobalData.usersList[i].userID) { GlobalData.playersReadyToPlay[i - 1] = true; break; } } } if (nm is CreateMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((CreateMessage)nm); } //Network.SendMessage(nm); } } if (nm is DestroyMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((DestroyMessage)nm); } Network.SendMessage(nm); } } if (nm is MoveMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((MoveMessage)nm); } } } if (nm is AttackMessage) { if (GlobalData.isPlaying) { lock (Network.messages) { Network.messages.Enqueue((AttackMessage)nm); } Network.SendMessage(nm); } } } } catch (Exception) { //MessageBox.Show(exc.Message); } finally { ns.Close(); AfterDisconnection(); PlayersListMessage pl = new PlayersListMessage(); pl.players = GlobalData.usersList; pl.senderID = GlobalData.me.userID; Network.SendMessage(pl); } }