Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        private void SendChunkDataTo(EndPoint ep, GenericMessage msg)
        {
            var chp   = new ChunkPos((int)msg.ReadSignedInteger(), (int)msg.ReadSignedInteger());
            var chunk = _world.GetChunk(chp);

            if (!chunk.HasData)
            {
                _world.LoadChunk(chp);
            }

            var raw = chunk.GetRaw();

            var m = new GenericMessage();

            m.WriteUnsignedInteger(1);
            m.WriteSignedInteger((uint)chp.x);
            m.WriteSignedInteger((uint)chp.z);

            var data = new byte[raw.Length];

            Buffer.BlockCopy(raw, 0, data, 0, data.Length);

            m.WriteUnsignedInteger((uint)data.Length);
            m.WriteByteArray(data);

            _server.SendToAsync(ep, m);
        }
Beispiel #3
0
        public void Tick()
        {
            if (client == null)
            {
                return;
            }

            if (SharpCraft.Instance.Player != null)
            {
                var m = new GenericMessage();
                m.WriteUnsignedInteger(1);

                var pos = SharpCraft.Instance.Player.Pos;
                var dir = SharpCraft.Instance.Camera.GetLookVec();

                m.WriteFloat(pos.X);
                m.WriteFloat(pos.Y);
                m.WriteFloat(pos.Z);

                m.WriteFloat(dir.X);
                m.WriteFloat(dir.Y);
                m.WriteFloat(dir.Z);

                client?.Send(m);
            }
        }
Beispiel #4
0
        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();
        }
Beispiel #5
0
        public void RequestChunk(ChunkPos pos)
        {
            var m = new GenericMessage();

            m.WriteUnsignedInteger(2);

            m.WriteSignedInteger(pos.x);
            m.WriteSignedInteger(pos.z);

            client.Send(m);
        }