Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
0
        public void Dispose()
        {
            var server = new FeatherTcpServer <BinaryMessage>();

            Assert.False(server.IsDisposed);
            server.Listen(13007);
            Assert.False(server.IsDisposed);
            server.Dispose();
            Assert.True(server.IsDisposed);
        }