private void SendEntities() { if (_server == null) { return; } var msg = new GenericMessage(); msg.WriteUnsignedInteger(2); msg.WriteUnsignedInteger((ulong)_entities.Count); foreach (var entity in _entities.Values) { msg.WriteGuid(entity.ID); msg.WriteFloat(entity.Pos.X); msg.WriteFloat(entity.Pos.Y); msg.WriteFloat(entity.Pos.Z); msg.WriteFloat(entity.LookVec.X); msg.WriteFloat(entity.LookVec.Y); msg.WriteFloat(entity.LookVec.Z); } foreach (var ep in _server.RemoteEndPoints) { _server.SendToAsync(ep, msg); } }
public ServerHandler() { _world = new WorldServer(); Thread tickThread = new Thread(TickThread); tickThread.Start(); _server = new FeatherTcpServer <GenericMessage>(); _server.OnClientConnected += (endPoint) => { if (!_entities.Keys.Contains(endPoint)) { var e = new Entity(); _entities.TryAdd(endPoint, e); var gm = new GenericMessage(); gm.WriteUnsignedInteger(0); gm.WriteGuid(e.ID); _server.SendToAsync(endPoint, gm); } Console.WriteLine($"{endPoint} connected."); }; _server.OnClientDisconnected += (endPoint, reason) => { _entities.TryRemove(endPoint, out var removed); Console.WriteLine($"{endPoint} disconnected."); }; _server.OnMessageReceived += (endPoint, message) => { var id = message.ReadUnsignedInteger(); if (_entities.TryGetValue(endPoint, out var entity)) { if (id == 1) { ProcessPlayerData(entity, message); } if (id == 2) { SendChunkDataTo(endPoint, message); } } }; _server.Listen(25566); Console.WriteLine("Server now listening for connections on port 25566. Press any key to halt."); Console.ReadKey(true); _server.Dispose(); }