// public ConcurrentDictionary<string, int> scores = new Dictionary<string, int>(); public void ConnectNewPlayingClient() { try { while (true) { Console.WriteLine("Checking for playing clients"); try { tasks[0] = Task.Run(() => { foreach (var client in ServerSide.ConnectClients.ConnectedClients.Values) { if (client.Connected) { try { NetworkStream ns = client.GetStream(); //byte[] b = new byte[sizeof(short)]; //ns.Read(b, 0, b.Length); //var message = (GameAction)BitConverter.ToInt16(b, 0); var message = ServerSide.RecvCommand(ns); if (message == GameAction.Ready) { var key = ServerSide.RecvShortString(ns); client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); Console.WriteLine("client address: " + client.Client.RemoteEndPoint.ToString() + "name:" + key + " playing"); PlayingClients.TryAdd(key, new ClientMember(client, key, client.GetStream())); Console.WriteLine("initing " + key + " background transfer"); InitClient(key); break; } } catch { Console.WriteLine("problem with user playing"); } } } }); } catch { Console.WriteLine("error"); } Thread.Sleep(5000); } } catch { Console.WriteLine("Problem with while"); } }
public override void Update(GameTime gameTime) { foreach (var client in ServerSide.ConnectClients.ConnectedClients.Values) { if (client.Connected) { try { NetworkStream ns = client.GetStream(); var message = ServerSide.RecvCommand(ns); if (message == GameAction.Ready) { var key = ServerSide.RecvShortString(ns); if (!PlayingClients.ContainsKey(key)) { client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); Console.WriteLine("client address: " + client.Client.RemoteEndPoint.ToString() + "name:" + key + " playing"); PlayingClients.TryAdd(key, new ClientMember(client, key, client.GetStream())); //, sw, sr)); Console.WriteLine("initing " + key + " background transfer"); InitClient(key); var pt = Parallel.ForEach <ClientMember>(PlayingClients.Values, (cl) => { if (fighter.Value != null) { var datString = key + "," + fighter.Value.Angle + "," + fighter.Value.Location.X + "," + fighter.Value.Location.Y + ","; gameObjects.TryAdd(key, fighter.Value); ServerSide.SendData(GameAction.NewPlayer, datString, cl.Ns); } }); while (!pt.IsCompleted) { } break; } } // ns.Close(); } catch { Console.WriteLine("problem"); } } } var p = Parallel.ForEach <ClientMember>(PlayingClients.Values, (client) => { if (client.status == "loaded") { try { client.Ns = client.Client.GetStream(); } catch { } if (client.Ns.DataAvailable) { try { var message = ServerSide.RecvCommand(client.Ns); var key = ServerSide.RecvShortString(client.Ns); if (message == GameAction.Accelerate) { ((StarFighter)gameObjects[key]).Accelerate(); } if (message == GameAction.Deaccelerate) { ((StarFighter)gameObjects[key]).Deaccelerate(); } if (message == GameAction.TurnLeft) { ((StarFighter)gameObjects[key]).TurnLeft(); } if (message == GameAction.TurnRight) { ((StarFighter)gameObjects[key]).TurnRight(); } client.ns.Read(new byte[ClientSide.Client.ReceiveBufferSize], 0, ClientSide.Client.ReceiveBufferSize); } catch { } } var fighters = gameObjects.Keys.OfType <StarFighter>(); foreach (var fighter in fighters) { var datString = fighter.ID + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y + ","; ServerSide.SendData(GameAction.Update, datString, client.Ns); } } }); while (!p.IsCompleted) { } newPlayer = false; foreach (var obj in gameObjects) { obj.Value.Update(gameTime); } //foreach (var player in gameObjects.Values.OfType<StarFighter>()) //{ // foreach (var bullet in gameObjects.Values.OfType<Projectile>()) // { // var delx = player.Size.Width; // var dely = player.Size.Height; // if (bullet.InBounds(new BoundF(player.Location.X - delx, player.Location.Y - dely, player.Location.X + delx, player.Location.Y + dely))) // { // if (!bullet.ID.Contains(player.ID)) // { // var id = bullet.ID.Split(',').Last(); // bullet.ToBeDestroyed = true; // if (player.IsDead.IsNotTrue()) // { // player.IsDead = true; // scores[id]++; // } // } // } // } //} //var keys = gameObjects.Keys.ToArray<string>(); //for (int i = 0; i < keys.Count<string>(); i++) //{ // var ass = gameObjects[keys[i]]; // if (ass.GetType() == typeof(Asteroid)) // { // // ((IGame2DProperties)ass).Location = ((IGame2DProperties)ass).KeepInBounds(bound).Location; // } //} //var fighters = gameObjects.Values.OfType<StarFighter>(); //foreach (var client in PlayingClients) //{ // if (client.Value.status == "loaded") // { // foreach (var fighter in fighters) // { // SendString("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // // SendObject("upfi", client.Value.key, client.Value.key + "," + fighter.Angle + "," + fighter.Location.X + "," + fighter.Location.Y); // } // } //} // if (delClient) { var lst = new List <string>(); foreach (var client in PlayingClients) { if (!client.Value.Client.Connected) { lst.Add(client.Key); client.Value.status = "disconnected"; delClient = true; } } if (delClient) { foreach (var key in lst) { var c = new ClientMember(null, "", null); var f = default(IGameBehavior); PlayingClients.TryRemove(key, out c); if (gameObjects.ContainsKey(key)) { gameObjects.TryRemove(key, out f); } } // delClient = false; // tasks[1] = Task.Run(() =>{ for (int i = 0; i < ServerSide.ConnectClients.ConnectedClients.Count; i++) { try { if (!ServerSide.ConnectClients.ConnectedClients[i].Connected) { // disconnect clients var key = ServerSide.ConnectClients.ConnectedClients[i].Client.RemoteEndPoint.ToString(); var t = new TcpClient(); ServerSide.ConnectClients.ConnectedClients.TryRemove(i, out t);//.TryTake(out ServerSide.ConnectClients.ConnectedClients.ElementAt(i) ); i--; Console.WriteLine("client " + key + " disconnected"); } } catch { } } delClient = false; } // Console.WriteLine("ConnectReadyCheck Close.."); // }); } if (GameInput.isKeyDown(Keys.Escape)) { RunAgain = false; GameWindow.AudioPlayer.Item1.StopTrack("background"); GameWindow.CurrentScene = NextScene; GameWindow.View.Init(); gameObjects.Clear(); } }