/// <summary> /// This method only runs on the server. It reads tank inputs that /// have been sent over the network by a client machine, storing /// them for later use by the UpdateServer method. /// </summary> void ServerReadInputFromClients(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(packetReader, out sender); if (!sender.IsLocal) { // Look up the tank associated with whoever sent this packet. Tank remoteTank = sender.Tag as Tank; // Read the latest inputs controlling this tank. remoteTank.TankInput = packetReader.ReadVector2(); remoteTank.TurretInput = packetReader.ReadVector2(); } } }
/// <summary> /// This method only runs on client machines. It reads /// tank position data that has been computed by the server. /// </summary> void ClientReadGameStateFromServer(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(packetReader, out sender); // This packet contains data about all the players in the session. // We keep reading from it until we have processed all the data. while (packetReader.Position < packetReader.Length) { // Read the state of one tank from the network packet. byte gamerId = packetReader.ReadByte(); Vector2 position = packetReader.ReadVector2(); float tankRotation = packetReader.ReadSingle(); float turretRotation = packetReader.ReadSingle(); // Look up which gamer this state refers to. NetworkGamer remoteGamer = networkSession.FindGamerById(gamerId); // This might come back null if the gamer left the session after // the host sent the packet but before we received it. If that // happens, we just ignore the data for this gamer. if (remoteGamer != null) { // Update our local state with data from the network packet. Tank tank = remoteGamer.Tag as Tank; tank.Position = position; tank.TankRotation = tankRotation; tank.TurretRotation = turretRotation; } } } }
void ClientReadGameStateFromServer(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(packetReader, out sender); // This packet contains data about all the players in the session. // We keep reading from it until we have processed all the data. while (packetReader.Position < packetReader.Length) { if (packetReader.Length <= 2) { break; } // Read the state of one tank from the network packet. // byte gamerId = packetReader.ReadByte(); //Vector2 position = packetReader.ReadVector2(); //float tankRotation = packetReader.ReadSingle(); //float turretRotation = packetReader.ReadSingle(); QueueHdr qH; qH.inSeqNum = packetReader.ReadInt32(); qH.outSeqNum = packetReader.ReadInt32(); qH.type = (Queue_type)packetReader.ReadInt32(); // int incomingCount = packetReader.ReadInt32(); //Debug.WriteLine("qH.inSeqNum ServerRead = " + qH.inSeqNum); //Debug.WriteLine("qH.outSeqNum ServerRead = " + qH.outSeqNum); //Debug.WriteLine("qH.type ServerRead = " + qH.type); //Debug.WriteLine("incomingCount ServerRead = " + incomingCount); //////////////////////// send to input queue on client //PhysicsBuffer[] physicsBuff2 = new PhysicsBuffer[incomingCount]; switch (qH.type) { case Queue_type.QUEUE_PHYSICS_BUFFER: if (qH.type == Queue_type.QUEUE_PHYSICS_BUFFER) { int incomingCount = packetReader.ReadInt32(); PhysicsBuffer[] physicsBuff2 = new PhysicsBuffer[incomingCount]; for (int y = 0; y < incomingCount; y++) { //PhysicsBuffer_Message localPhysicsBuffer_Message = new PhysicsBuffer_Message((PhysicsBuffer_Message)qH.data); //PhysicsBuffer myPhysicsBuffer = new PhysicsBuffer(); //myPhysicsBuffer.id = packetReader.ReadInt32(); //myPhysicsBuffer.position = packetReader.ReadVector2(); //myPhysicsBuffer.rotation = (float)packetReader.ReadSingle(); //Push to buffer physicsBuff2[y].id = packetReader.ReadInt32(); physicsBuff2[y].position = packetReader.ReadVector2(); physicsBuff2[y].rotation = (float)packetReader.ReadSingle(); } // send this msg to Input Queue inQ so client can read the messages PhysicsBuffer_Message msg = new PhysicsBuffer_Message(ref physicsBuff2); PhysicsBuffer_Message_inQueue.add(msg); } break; case Queue_type.QUEUE_SHIP_MISSILE: Ship_Create_Missile_Message msgMissile = new Ship_Create_Missile_Message(player2); qH.data = msgMissile; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_BOMB: Ship_Create_Bomb_Message msgBomb = new Ship_Create_Bomb_Message(player2); qH.data = msgBomb; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_IMPULSE: Vector2 impulse = packetReader.ReadVector2(); Ship_Impulse_Message msgImpulse = new Ship_Impulse_Message(player2, impulse); qH.data = msgImpulse; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_ROT: float rotation = packetReader.ReadSingle(); Ship_Rot_Message msgRotation = new Ship_Rot_Message(player2, rotation); qH.data = msgRotation; InputQueue.add(qH); break; case Queue_type.QUEUE_EVENT: //msg = new Event_Message((Event_Message)qH.data); int idA = packetReader.ReadInt32(); // Debug.WriteLine("idA =" + idA); int idB = packetReader.ReadInt32(); // Debug.WriteLine("idA =" + idB); Vector2 collisionPt = packetReader.ReadVector2(); // Debug.WriteLine("idA =" + collisionPt); Event_Message eventMsg = new Event_Message(idA, idB, collisionPt); qH.data = eventMsg; InputQueue.add(qH); break; } } } }
void ServerReadInputFromClients(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(packetReader, out sender); if (!sender.IsLocal) { // Look up the tank associated with whoever sent this packet. //Tank remoteTank = sender.Tag as Tank; Ship remoteShip = sender.Tag as Ship; // Read the latest inputs controlling this tank. // remoteTank.TankInput = packetReader.ReadVector2(); // remoteTank.TurretInput = packetReader.ReadVector2(); QueueHdr qH; qH.inSeqNum = packetReader.ReadInt32(); qH.outSeqNum = packetReader.ReadInt32(); qH.type = (Queue_type)packetReader.ReadInt32(); //qH.data = packetReader.ReadVector2(); switch (qH.type) { case Queue_type.QUEUE_SHIP_BOMB: Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player1); qH.data = msg; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_MISSILE: Ship_Create_Missile_Message msg2 = new Ship_Create_Missile_Message(player1); qH.data = msg2; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_IMPULSE: Ship_Impulse_Message sim = new Ship_Impulse_Message(player1, packetReader.ReadVector2()); // qH.data = packetReader.ReadVector2(); qH.data = sim; // Ship_Impulse_Message msg3 = new Ship_Impulse_Message((Ship_Impulse_Message)qH.data); // msg = new Ship_Impulse_Message((Ship_Impulse_Message)qH.data); // qH.data = msg3; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_ROT: Ship_Rot_Message rotMessage = new Ship_Rot_Message(player1, packetReader.ReadSingle()); // float? ReadDouble() // qH.data = packetReader.ReadVector2(); qH.data = rotMessage; // Ship_Rot_Message msg4 = new Ship_Rot_Message((Ship_Rot_Message)qH.data); // qH.data = msg4; InputQueue.add(qH); // msg = new Ship_Rot_Message((Ship_Rot_Message)qH.data); break; case Queue_type.QUEUE_EVENT: break; } } } }
void ReadFromServer(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(Reader, out sender); Player p = gamer.Tag as Player; // This packet contains data about all the players in the session. // We keep reading from it until we have processed all the data. while (Reader.Position < Reader.Length) { qHeader qH; Player player; Ship Player2Ship; qH.type = (QueueType)Reader.ReadInt32(); qH.packetOwner = (PlayerID)Reader.ReadInt32(); qH.inseq = Reader.ReadInt32(); qH.outseq = Reader.ReadInt32(); qH.obj = null; if (qH.packetOwner == PlayerID.one) { Player2Ship = player1.playerShip; player = player1; } else { Player2Ship = player2.playerShip; player = player2; } switch (qH.type) { case QueueType.ship_impulse: float x = Reader.ReadInt32(); float y = Reader.ReadInt32(); player.playerShip.location.X = Reader.ReadInt32(); player.playerShip.location.Y = Reader.ReadInt32(); player.playerShip.rotation = Reader.ReadInt32(); Vector2 direction = new Vector2((float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle()))); direction.Normalize(); direction *= shipSpeed; ship_impulse p1 = new ship_impulse(player, direction); p1.impulse.X = x; p1.impulse.Y = y; qH.obj = p1; break; case QueueType.ship_rot_anti: float x1 = (int)Reader.ReadInt32(); player.playerShip.location.X = Reader.ReadInt32(); player.playerShip.location.Y = Reader.ReadInt32(); player.playerShip.rotation = Reader.ReadInt32(); Ship_rot_message pShip = new Ship_rot_message(player, x1); pShip.rot = x1; qH.obj = pShip; break; case QueueType.ship_rot_clock: float x2 = (int)Reader.ReadInt32(); player.playerShip.location.X = Reader.ReadInt32(); player.playerShip.location.Y = Reader.ReadInt32(); player.playerShip.rotation = Reader.ReadInt32(); Ship_rot_message pShip1 = new Ship_rot_message(player, x2); pShip1.rot = x2; qH.obj = pShip1; break; case QueueType.ship_bomb: player.playerShip.location.X = Reader.ReadInt32(); player.playerShip.location.Y = Reader.ReadInt32(); player.playerShip.rotation = Reader.ReadInt32(); Ship_Create_Bomb_Message p2 = new Ship_Create_Bomb_Message(player); qH.obj = p2; break; case QueueType.ship_missile: player.playerShip.location.X = Reader.ReadInt32(); player.playerShip.location.Y = Reader.ReadInt32(); player.playerShip.rotation = Reader.ReadInt32(); Ship_Create_Missile_Message p3 = new Ship_Create_Missile_Message(player); qH.obj = p3; break; case QueueType.EventMessage: int a = (int)Reader.ReadInt32(); int b = (int)Reader.ReadInt32(); Vector2 pt = (Vector2)Reader.ReadVector2(); EvenMessage e = new EvenMessage(a, b, pt); qH.obj = e; break; } inQueue.add(qH.obj, qH.type, qH.outseq, qH.packetOwner); } } }
void ReadInputFromClients(LocalNetworkGamer gamer) { while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(Reader, out sender); if (sender.IsLocal) { continue; } qHeader qH; Ship Player2Ship; Player player; qH.type = (QueueType)Reader.ReadInt32(); qH.packetOwner = (PlayerID)Reader.ReadInt32(); qH.inseq = Reader.ReadInt32(); qH.outseq = Reader.ReadInt32(); qH.obj = null; if (qH.packetOwner == PlayerID.one) { Player2Ship = player1.playerShip; player = player1; } else { Player2Ship = player2.playerShip; player = player2; } switch (qH.type) { case QueueType.ship_impulse: float x = Reader.ReadInt32(); float y = Reader.ReadInt32(); float m = Reader.ReadInt32(); float n = Reader.ReadInt32(); float r = Reader.ReadInt32(); Vector2 direction = new Vector2((float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle()))); direction.Normalize(); direction *= shipSpeed; ship_impulse p = new ship_impulse(player, direction); //update server values p.rot = player.playerShip.rotation; p.x = player.playerShip.location.X; p.y = player.playerShip.location.Y; p.impulse.X = x; p.impulse.Y = y; qH.obj = p; break; case QueueType.ship_rot_anti: float x1 = (int)Reader.ReadInt32(); Ship_rot_message pShip = new Ship_rot_message(player, x1); pShip.rot = x1; //server values pShip.x = player.playerShip.location.X; pShip.y = player.playerShip.location.Y; pShip.serverRotvalue = player.playerShip.rotation; qH.obj = pShip; break; case QueueType.ship_rot_clock: float x2 = (int)Reader.ReadInt32(); Ship_rot_message pShip1 = new Ship_rot_message(player, x2); pShip1.rot = x2; //server values pShip1.x = player.playerShip.location.X; pShip1.y = player.playerShip.location.Y; pShip1.serverRotvalue = player.playerShip.rotation; qH.obj = pShip1; break; case QueueType.ship_bomb: Ship_Create_Bomb_Message p2 = new Ship_Create_Bomb_Message(player); //server values p2.x = player.playerShip.location.X; p2.y = player.playerShip.location.Y; p2.rot = player.playerShip.rotation; qH.obj = p2; break; case QueueType.ship_missile: Ship_Create_Missile_Message p3 = new Ship_Create_Missile_Message(player); //server values p3.x = player.playerShip.location.X; p3.y = player.playerShip.location.Y; p3.rot = player.playerShip.rotation; qH.obj = p3; break; case QueueType.physicsBuffer: //physics_buffer_message p4 = new physics_buffer_message(); //qH.ob = p3; break; case QueueType.EventMessage: int a = (int)Reader.ReadInt32(); int b = (int)Reader.ReadInt32(); Vector2 pt = (Vector2)Reader.ReadVector2(); EvenMessage e = new EvenMessage(a, b, pt); qH.obj = e; break; } inQueue.add(qH.obj, qH.type, qH.outseq, qH.packetOwner); OutQueue.add(qH.type, qH.obj, qH.packetOwner); } }
public void ClientReadGameStateFromServer(LocalNetworkGamer gamer) { while (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(packetReader, out sender); while (packetReader.Position < packetReader.Length) { game.GameOver = packetReader.ReadBoolean(); int WinnerId = packetReader.ReadInt32(); if (game.GameOver) { foreach (BasicOrb orb in game.Orbs) { if (orb.relevent && orb.ID == WinnerId) { game.Winner = orb; } } } int GameMode = packetReader.ReadInt32(); SetGameMode(GameMode); FutureGameMode = GameMode; int Map = packetReader.ReadInt32(); SetMap(Map); FutureMap = Map; foreach (BasicOrb orb in game.Orbs) { orb.relevent = packetReader.ReadBoolean(); } foreach (BasicOrb orb in game.Orbs) { if (orb.relevent) { byte gamerId = packetReader.ReadByte(); networkSession.FindGamerById(gamerId).Tag = orb; orb.Read(packetReader); } } if (game.gamemode == Game1.GameMode.KeepAway) { game.flag.IsCarried = packetReader.ReadBoolean(); game.flag.Position = packetReader.ReadVector3(); int CarrierID = packetReader.ReadInt32(); if (game.flag.IsCarried) { foreach (BasicOrb orb in game.Orbs) { if (orb.relevent && orb.ID == CarrierID) { game.flag.carrier = orb; } } } } } } }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } if (this.IsActive) { switch (currentGameState) { case GameState.SignIn: { if (Gamer.SignedInGamers.Count < 1) { Guide.ShowSignIn(1, false); log.Add("Opened User SignIn Interface"); } else { currentGameState = GameState.SearchSession; log.Add(Gamer.SignedInGamers[0].Gamertag + " logged in - proceed to SearchSession"); } } break; case GameState.SearchSession: { AvailableNetworkSessionCollection activeSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 4, null); if (activeSessions.Count == 0) { currentGameState = GameState.CreateSession; log.Add("No active sessions found - proceed to CreateSession"); } else { AvailableNetworkSession sessionToJoin = activeSessions[0]; networkSession = NetworkSession.Join(sessionToJoin); string myString = "Joined session hosted by " + sessionToJoin.HostGamertag; myString += " with " + sessionToJoin.CurrentGamerCount.ToString() + " players"; myString += " and " + sessionToJoin.OpenPublicGamerSlots.ToString() + " open player slots."; log.Add(myString); HookSessionEvents(); currentGameState = GameState.InSession; } } break; case GameState.CreateSession: { networkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 4, 16); networkSession.AllowHostMigration = true; networkSession.AllowJoinInProgress = false; log.Add("New session created"); HookSessionEvents(); currentGameState = GameState.InSession; } break; case GameState.InSession: { //send data to all other players in session writer.Write(gameTime.TotalGameTime.Minutes); writer.Write(gameTime.TotalGameTime.Seconds); LocalNetworkGamer localGamer = networkSession.LocalGamers[0]; localGamer.SendData(writer, SendDataOptions.ReliableInOrder); //receive data from all other players in session while (localGamer.IsDataAvailable) { NetworkGamer sender; localGamer.ReceiveData(reader, out sender); string gamerTime = ""; gamerTime += sender.Gamertag + ": "; gamerTime += reader.ReadInt32() + "m "; gamerTime += reader.ReadInt32() + "s"; gamerTimes[sender.Gamertag] = gamerTime; } networkSession.Update(); } break; } } base.Update(gameTime); }
/// <summary> /// Helper for reading incoming network packets. /// </summary> void ReadIncomingPackets(LocalNetworkGamer gamer) { // Keep reading as long as incoming packets are available. while (gamer.IsDataAvailable) { NetworkGamer sender; // Read a single packet from the network. gamer.ReceiveData(packetReader, out sender); // Discard packets sent by local gamers: we already know their state! if (sender.IsLocal) { continue; } MessageType msgType = (MessageType)packetReader.ReadInt32(); switch (msgType) { case MessageType.NewGame: //ReceiveNewNetworkedGame(); break; case MessageType.CatapultAiming: if (isFirstPlayerTurn && !NetworkSession.IsHost) { playerOne.Catapult.CurrentState = CatapultState.Aiming; playerOne.isDragging = true; catapultInfoVector = packetReader.ReadVector3(); playerOne.Catapult.ShotStrength = catapultInfoVector.X; playerOne.Catapult.ShotVelocity = catapultInfoVector.Y; playerOne.ArrowScale = catapultInfoVector.Z; } if (!isFirstPlayerTurn && NetworkSession.IsHost) { playerTwo.Catapult.CurrentState = CatapultState.Aiming; playerTwo.isDragging = true; catapultInfoVector = packetReader.ReadVector3(); playerTwo.Catapult.ShotStrength = catapultInfoVector.X; playerTwo.Catapult.ShotVelocity = catapultInfoVector.Y; playerTwo.ArrowScale = catapultInfoVector.Z; } break; case MessageType.CatapultFiring: if (isFirstPlayerTurn && !NetworkSession.IsHost) { catapultInfoVector = packetReader.ReadVector3(); playerOne.Catapult.Fire(catapultInfoVector.Y); playerOne.Catapult.CurrentState = CatapultState.Firing; playerOne.ResetDragState(); } if (!isFirstPlayerTurn && NetworkSession.IsHost) { catapultInfoVector = packetReader.ReadVector3(); playerTwo.Catapult.Fire(catapultInfoVector.Y); playerTwo.Catapult.CurrentState = CatapultState.Firing; playerTwo.ResetDragState(); } break; case MessageType.UpdateEnvironment: wind = packetReader.ReadVector2(); cloud1Position = packetReader.ReadVector2(); cloud2Position = packetReader.ReadVector2(); // Set new wind value to the players and playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y; break; } } }
public void Update(Character[] c, ParticleManager pMan) { LocalNetworkGamer gamer = GetGamer(); if (gamer == null) { return; } frame -= FrameTime; if (frame < 0f) { frame = .05f; if (netPlay.Hosting) { if (c[0] != null) { writer.Write(MSG_SERVER_DATA); c[0].WriteToNet(writer); for (int i = 2; i < c.Length; i++) { if (c[i] != null) { c[i].WriteToNet(writer); } } pMan.NetWriteParticles(writer); writer.Write(MSG_END); gamer.SendData(writer, SendDataOptions.None); } } if (netPlay.Joined) { if (c[1] != null) { writer.Write(MSG_CLIENT_DATA); c[1].WriteToNet(writer); pMan.NetWriteParticles(writer); writer.Write(MSG_END); gamer.SendData(writer, SendDataOptions.None); } } } if (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(reader, out sender); if (!sender.IsLocal) { byte type = reader.ReadByte(); if (netPlay.Joined) { for (int i = 0; i < c.Length; i++) { if (i != 1) { if (c[i] != null) { c[i].ReceivedNetUpdate = false; } } } } bool end = false; while (!end) { byte msg = reader.ReadByte(); switch (msg) { case MSG_END: end = true; break; case MSG_CHARACTER: int defID = NetPacker.SbyteToInt(reader.ReadSByte()); int team = NetPacker.SbyteToInt(reader.ReadSByte()); int ID = NetPacker.SbyteToInt(reader.ReadSByte()); if (c[ID] == null) { c[ID] = new Character(new Vector2(), Game1.charDef[defID], ID, team); } c[ID].ReadFromNet(reader); c[ID].ReceivedNetUpdate = true; break; case MSG_PARTICLE: byte pType = reader.ReadByte(); bool bg = reader.ReadBoolean(); switch (pType) { case Particle.PARTICLE_NONE: // break; case Particle.PARTICLE_BLOOD: pMan.AddParticle(new Blood(reader), bg, true); break; case Particle.PARTICLE_BLOOD_DUST: pMan.AddParticle(new BloodDust(reader), bg, true); break; case Particle.PARTICLE_BULLET: pMan.AddParticle(new Bullet(reader), bg, true); break; case Particle.PARTICLE_FIRE: pMan.AddParticle(new Fire(reader), bg, true); break; case Particle.PARTICLE_FOG: pMan.AddParticle(new Fog(reader), bg, true); break; case Particle.PARTICLE_HEAT: pMan.AddParticle(new Heat(reader), bg, true); break; case Particle.PARTICLE_HIT: pMan.AddParticle(new Hit(reader), bg, true); break; case Particle.PARTICLE_MUZZLEFLASH: pMan.AddParticle(new MuzzleFlash(reader), bg, true); break; case Particle.PARTICLE_ROCKET: pMan.AddParticle(new Rocket(reader), bg, true); break; case Particle.PARTICLE_SHOCKWAVE: pMan.AddParticle(new Shockwave(reader), bg, true); break; case Particle.PARTICLE_SMOKE: pMan.AddParticle(new Smoke(reader), bg, true); break; default: //Error! break; } break; } } if (netPlay.Joined) { for (int i = 0; i < c.Length; i++) { if (i != 1) { if (c[i] != null) { if (c[i].ReceivedNetUpdate == false) { c[i] = null; } } } } } } } }