Beispiel #1
0
        public void OnDisconnect(ICoreServerClient client)
        {
            switch (client.ServerInfo)
            {
            case ClusterServerInfo cluster:
                _logger.LogInformation($"Cluster server '{cluster.Name}' disconnected from core server.");
                cluster.Worlds.Clear();
                break;

            case WorldServerInfo world:
                _logger.LogInformation($"World server '{world.Name}' disconnected from core server.");
                ICoreServerClient clusterClient = _coreServer.GetClusterServer(world.ParentClusterId);
                var clusterServerInfo           = clusterClient.ServerInfo as ClusterServerInfo;

                if (clusterServerInfo != null)
                {
                    clusterServerInfo.Worlds.Remove(world);
                    _corePacketFactory.SendUpdateWorldList(clusterClient, clusterServerInfo.Worlds);
                }

                break;

            default:
                _logger.LogInformation("Unknown server disconnected from core server.");
                break;
            }
        }
        public void OnAuthenticate(CoreServerClient client, INetPacketStream packet)
        {
            var id   = packet.Read <int>();
            var name = packet.Read <string>();
            var host = packet.Read <string>();
            var port = packet.Read <int>();
            var type = (ServerType)packet.Read <byte>();

            if (type == ServerType.Cluster)
            {
                if (_coreServer.HasCluster(id))
                {
                    _logger.LogWarning($"Cluster Server '{name}' incoming connection from {client.Socket.RemoteEndPoint} refused. Reason: An other Cluster server with id '{id}' is already connected.");
                    _corePacketFactory.SendAuthenticationResult(client, CoreAuthenticationResultType.FailedClusterExists);
                    _coreServer.DisconnectClient(client.Id);
                    return;
                }

                client.ServerInfo = new ClusterServerInfo(id, name, host, port);
                _logger.LogInformation($"Cluster server '{name}' connected to ISC server from {client.Socket.RemoteEndPoint}.");
            }
            else if (type == ServerType.World)
            {
                var parentClusterId = packet.Read <int>();

                CoreServerClient parentClusterServer = _coreServer.GetClusterServer(parentClusterId);

                if (parentClusterServer == null)
                {
                    _logger.LogWarning($"World server '{name}' incoming connection from {client.Socket.RemoteEndPoint} refused. Reason: Cluster server with id '{parentClusterId}' is not connected.");

                    _corePacketFactory.SendAuthenticationResult(client, CoreAuthenticationResultType.FailedNoCluster);
                    _coreServer.DisconnectClient(client.Id);
                    return;
                }

                if (_coreServer.HasWorld(parentClusterId, id))
                {
                    _logger.LogWarning($"World server '{name}' incoming connection from {client.Socket.RemoteEndPoint} refused. Reason: An other World server with id '{id}' is already connected to Cluster Server '{parentClusterServer.ServerInfo.Name}'.");
                    _corePacketFactory.SendAuthenticationResult(client, CoreAuthenticationResultType.FailedWorldExists);
                    _coreServer.DisconnectClient(client.Id);
                    return;
                }

                var cluster = parentClusterServer.ServerInfo as ClusterServerInfo;
                if (cluster is null)
                {
                    _logger.LogWarning($"World server '{name}' incoming connection from {client.Socket.RemoteEndPoint} refused. Reason: Parent cluster server is not a cluster server.");
                    return;
                }

                var worldInfo = new WorldServerInfo(id, name, host, port, parentClusterId);

                cluster.Worlds.Add(worldInfo);
                client.ServerInfo = worldInfo;
                _corePacketFactory.SendUpdateWorldList(parentClusterServer, cluster.Worlds);
                _logger.LogInformation($"World server '{name}' join cluster '{cluster.Name}' and is connected to ISC server from {client.Socket.RemoteEndPoint}.");
            }
            else
            {
                _logger.LogWarning($"Incoming core connection from {client.Socket.RemoteEndPoint} refused. Reason: server type is unknown.");
                _corePacketFactory.SendAuthenticationResult(client, CoreAuthenticationResultType.FailedUnknownServer);
                _coreServer.DisconnectClient(client.Id);
                return;
            }

            _corePacketFactory.SendAuthenticationResult(client, CoreAuthenticationResultType.Success);
        }