public int addEntity(ref ConcurrentDictionary <int, Connection> connections, float x, float y, int size, string type, int fraction) { int id = entityIdMap.getId(); Entity e = new Entity(id, x, y, size, type, fraction); entities.TryAdd(id, e); Buffer buffer = new Buffer(); foreach (Connection client in connections.Values) { if (client != null) { buffer.SeekStart(); buffer.WriteInt8(7); buffer.WriteInt16((short)id); buffer.WriteFloat(e.x); buffer.WriteFloat(e.y); buffer.WriteInt16((short)e.size); buffer.WriteString(e.type); buffer.WriteInt16((short)e.fraction); client.socket.Send(buffer.GetBytes(), 0, buffer.Tell(), 0); } } buffer.Clear(); return(id); }
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 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.SetupTimeout(ref connections, ref connectionIds); Buffer buff = new Buffer(); buff.SeekStart(); buff.WriteInt8(2); buff.WriteInt16((short)connectionId); // buff.WriteInt16((short)worldMap.width); // buff.WriteInt16((short)worldMap.height); // buff.WriteString(worldMap.mapName); connection.Send(buff.GetBytes(), 0, buff.Tell(), 0); buff.SeekStart(); buff.WriteInt8(3); buff.WriteInt16((short)defaultFraction); connection.Send(buff.GetBytes()); buff.SeekStart(); buff.WriteInt8(4); buff.WriteInt16((short)worldMap.width); buff.WriteInt16((short)worldMap.height); buff.WriteString(worldMap.mapName); connection.Send(buff.GetBytes(), 0, buff.Tell(), 0); string[] motd = File.ReadAllLines("motd.txt"); foreach (string str in motd) { buff.SeekStart(); buff.WriteInt8(10); buff.WriteString(str); connection.Send(buff.GetBytes(), 0, buff.Tell(), 0); } buff.Clear(); //worldMap.entityContainer.sendClient(ref c); entc.sendClient(ref c); RecieveDataAsync(c); } AcceptAsync(); } }