public byte[] SerializeGame(int cid) { GameCommon gameClient = game.GetInfo(); gameClient.SetFractionId(cid); Stream str = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(str, gameClient); str.Seek(0, SeekOrigin.Begin); List <byte> buffer = new List <byte>(); while (!(str.Position == str.Length)) { buffer.Add((byte)str.ReadByte()); } str.Close(); return(buffer.ToArray()); }
public void Run() { while (!gameEnd) { Update(); foreach (Connection c in connections.Values) { GameCommon gc = GetInfo(); MemoryStream str = new MemoryStream(); gc.SetFractionId(c.id); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(str, gc); str.Seek(0, SeekOrigin.Begin); List <byte> buffer = new List <byte>(); while (!(str.Position == str.Length)) { buffer.Add((byte)str.ReadByte()); } str.Close(); c.socket.Send(buffer.ToArray()); } Thread.Sleep(1); } }
public void NetworkLoop() { byte[] buffer = new byte[256]; while (tcpclient.Connected) { do { BinaryFormatter bf = new BinaryFormatter(); GameCommon gc = bf.Deserialize(stream) as GameCommon; gm.SetInfo(gc); //size = stream.Read(buffer, 0, buffer.Length); //Console.WriteLine(size); }while (stream.DataAvailable); } }
public async void AcceptAsync() { if (socket != null) { Socket connection = await AsyncTask(); if (connection != null) { Console.WriteLine("Connection from " + (connection.RemoteEndPoint as IPEndPoint)); int connectionId = connectionIds.getId(); Connection c = new Connection(connection, connectionId, connectionId); connections.TryAdd(connectionId, c); c.SetupConnection(ref connections, ref connectionIds); GameCommon gameClient = game.GetInfo(); gameClient.SetFractionId(connectionId); Stream str = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(str, gameClient); str.Seek(0, SeekOrigin.Begin); List <byte> buffer = new List <byte>(); while (!(str.Position == str.Length)) { buffer.Add((byte)str.ReadByte()); } str.Close(); c.socket.Send(buffer.ToArray()); RecieveDataAsync(c); } AcceptAsync(); } }
public GameCommon GetInfo() { GameCommon gc = new GameCommon(); foreach (Fraction f in fractions) { FractionCommon fc = new FractionCommon(); foreach (Entity e in f.Entities) { EntityCommon ec = new EntityCommon(e.X, e.Y, e.Mass, e.id); ec.moving = e.Moving; ec.speed = e.Speed; fc.entities.Add(ec); } gc.AddFraction(fc); } return(gc); }
public void SetInfo(GameCommon gc) { List <Fraction> nf = new List <Fraction>(); int count = 0; foreach (FractionCommon fc in gc.Fractions) { Fraction f = new Fraction(fractionColors[count], this); count++; foreach (EntityCommon ec in fc.entities) { Entity e = new Entity(new Vector2f(ec.x, ec.y), ec.id, ec.mass); e.SetFraction(f); /* if (fractions.Count > 0) * foreach (Entity es in fractions[gc.FractionId].entities) * if (es.Id == e.Id) * { * if (es.Selected) * * if (es.DrawMoving) * e.DrawMoving = true; * }*/ f.AddEntity(e); } nf.Add(f); } fractions = nf; Fraction pf = fractions[gc.FractionId]; ec.SetPlayersFraction(ref pf); }