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 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(); } }