예제 #1
0
        private OperationResponse HandleRegisterGameServerRequest(OperationRequest operationRequest)
        {
            var registerRequest = new RegisterGameServer(this.Protocol, operationRequest);

            if (!registerRequest.IsValid)
            {
                string msg = registerRequest.GetErrorMessage();
                log.ErrorFormat("RegisterGameServer contract error: {0}", msg);
                return(new OperationResponse(operationRequest.OperationCode)
                {
                    DebugMessage = msg, ReturnCode = (short)ReturnCode.OperationInvalid
                });
            }

            IPAddress masterAddress = this.application.GetInternalMasterNodeIpAddress();
            var       contract      = new RegisterGameServerResponse {
                InternalAddress = masterAddress.GetAddressBytes()
            };

            if (this.application.IsMaster)
            {
                if (log.IsErrorEnabled)
                {
                    log.DebugFormat(
                        "Received register request: Address={0}, UdpPort={1}, TcpPort={2}, WebSocketPort={3}, State={4}",
                        registerRequest.GameServerAddress,
                        registerRequest.UdpPort,
                        registerRequest.TcpPort,
                        registerRequest.WebSocketPort,
                        (ServerState)registerRequest.ServerState);
                }
            }

            if (registerRequest.UdpPort.HasValue)
            {
                this.UdpAddress = registerRequest.GameServerAddress + ":" + registerRequest.UdpPort;
            }
            if (registerRequest.TcpPort.HasValue)
            {
                this.TcpAddress = registerRequest.GameServerAddress + ":" + registerRequest.TcpPort;
            }
            if (registerRequest.WebSocketPort.HasValue && registerRequest.WebSocketPort != 0)
            {
                this.WebSocketAddress = registerRequest.GameServerAddress + ":" + registerRequest.WebSocketPort;
            }

            this.ServerId   = new Guid(registerRequest.ServerId);
            this.State      = (ServerState)registerRequest.ServerState;
            this.Key        = string.Format("{0}-{1}-{2}", registerRequest.GameServerAddress, registerRequest.UdpPort, registerRequest.TcpPort);
            this.ServerType = (ServerType)registerRequest.ServerType;

            this.application.GameServers.OnConnect(this);
            //if(this.State == ServerState.Normal) {
            //    this.application.LoadBalancer.TryAddServer(this, 0);
            //}

            contract.AuthList  = this.GetAuthlist();
            contract.SharedKey = this.SharedKey;
            return(new OperationResponse(operationRequest.OperationCode, contract));
        }
예제 #2
0
 public void OnRegisteredAtMaster(RegisterGameServerResponse registerResponse)
 {
     if (log.IsInfoEnabled)
     {
         log.InfoFormat("Master connection registered on master: address:{0}", this.Address);
     }
     this.Application.OnRegisteredAtMaster(this, registerResponse);
 }
        protected virtual void HandleRegisterGameServerResponse(OperationResponse operationResponse)
        {
            var contract = new RegisterGameServerResponse(this.Protocol, operationResponse);

            if (!contract.IsValid)
            {
                if (operationResponse.ReturnCode != (short)ErrorCode.Ok)
                {
                    log.ErrorFormat("RegisterGameServer returned with err {0}: {1}", operationResponse.ReturnCode, operationResponse.DebugMessage);
                }

                log.Error("RegisterGameServerResponse contract invalid: " + contract.GetErrorMessage());
                this.Disconnect();
                return;
            }

            switch (operationResponse.ReturnCode)
            {
            case (short)ErrorCode.Ok:
            {
                log.InfoFormat("Successfully registered at master server: serverId={0}", this.application.ServerId);
                this.IsRegistered = true;
                this.masterServerConnection.UpdateAllGameStates();
                this.StartUpdateLoop();
                this.OnRegisteredAtMaster(contract);
                break;
            }

            case (short)ErrorCode.RedirectRepeat:
            {
                // TODO: decide whether to connect to internal or external address (config)
                // use a new peer since we otherwise might get confused with callbacks like disconnect
                var address = new IPAddress(contract.InternalAddress);
                log.InfoFormat("Connected master server is not the leader; Reconnecting to master at IP {0}...", address);
                this.Reconnect(address);         // don't use proxy for direct connections

                // enable for external address connections
                //// var address = new IPAddress(contract.ExternalAddress);
                //// log.InfoFormat("Connected master server is not the leader; Reconnecting to node {0} at IP {1}...", contract.MasterNode, address);
                //// this.Reconnect(address, contract.MasterNode);
                break;
            }

            default:
            {
                log.WarnFormat("Failed to register at master: err={0}, msg={1}, serverid={2}", operationResponse.ReturnCode, operationResponse.DebugMessage, this.application.ServerId);
                this.Disconnect();
                break;
            }
            }
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            RegisterGameServerResponse response = new RegisterGameServerResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("GameServer", targetDepth))
                {
                    var unmarshaller = GameServerUnmarshaller.Instance;
                    response.GameServer = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
예제 #5
0
    private void HandleRegisterGameServerResponse(OperationResponse operationResponse)
    {
        var contract = new RegisterGameServerResponse(this.Protocol, operationResponse);

        if (!contract.IsValid)
        {
            if (operationResponse.ReturnCode != (short)ReturnCode.Ok)
            {
                log.ErrorFormat("RegisterGameServer returned with err {0}: {1}", operationResponse.ReturnCode, operationResponse.DebugMessage);
            }
            log.Error("RegisterGameServerResponse contract invalid: " + contract.GetErrorMessage());
            this.Disconnect();
            return;
        }

        switch (operationResponse.ReturnCode)
        {
        case (short)ReturnCode.Ok:
        {
            log.InfoFormat("Successfully registered at master server: serverId={0}", GameApplication.ServerId);
            this.IsRegistered = true;
            this.StartUpdateLoop();
            m_App.updater.Start();
            break;
        }

        case (short)ReturnCode.RedirectRepeat:
        {
            var address = new IPAddress(contract.InternalAddress);
            log.InfoFormat("Connected master server is not the leader; Reconnecting to master at IP {0}...", address);
            this.Reconnect(address);
            break;
        }

        default:
        {
            log.WarnFormat("Failed to register at master: err={0}, msg={1}, serverid={2}", operationResponse.ReturnCode, operationResponse.DebugMessage, GameApplication.ServerId);
            this.Disconnect();
            break;
        }
        }
    }
예제 #6
0
        protected virtual OperationResponse HandleRegisterGameServerRequest(OperationRequest request)
        {
            try
            {
                var registerRequest = new RegisterGameServer(this.Protocol, request);

                if (registerRequest.IsValid == false)
                {
                    string msg = registerRequest.GetErrorMessage();
                    log.ErrorFormat("RegisterGameServer contract error: {0}", msg);

                    return(new OperationResponse(request.OperationCode)
                    {
                        DebugMessage = msg, ReturnCode = (short)ErrorCode.OperationInvalid
                    });
                }

                IPAddress masterAddress = this.application.GetInternalMasterNodeIpAddress();
                var       contract      = new RegisterGameServerResponse {
                    InternalAddress = masterAddress.GetAddressBytes()
                };

                // is master
                if (!this.application.IsMaster)
                {
                    return(new OperationResponse(request.OperationCode, contract)
                    {
                        ReturnCode = (short)ErrorCode.RedirectRepeat,
                        DebugMessage = "RedirectRepeat"
                    });
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat(
                        "Received register request: Address={0}, UdpPort={1}, TcpPort={2}, WebSocketPort={3}, SecureWebSocketPort={4}, HttpPort={5}, State={6}, Hostname={7}, IPv6Address={8}",
                        registerRequest.GameServerAddress,
                        registerRequest.UdpPort,
                        registerRequest.TcpPort,
                        registerRequest.WebSocketPort,
                        registerRequest.SecureWebSocketPort,
                        registerRequest.HttpPort,
                        (ServerState)registerRequest.ServerState,
                        registerRequest.GameServerHostName,
                        registerRequest.GameServerAddressIPv6);
                }

                this.Address = registerRequest.GameServerAddress;
                if (registerRequest.GameServerAddressIPv6 != null &&
                    IPAddress.Parse(registerRequest.GameServerAddressIPv6).AddressFamily == AddressFamily.InterNetworkV6)
                {
                    this.AddressIPv6 = string.Format("[{0}]", IPAddress.Parse(registerRequest.GameServerAddressIPv6));
                }
                this.Hostname = registerRequest.GameServerHostName;

                if (registerRequest.UdpPort.HasValue)
                {
                    this.UdpAddress     = string.IsNullOrEmpty(this.Address) ? null : string.Format("{0}:{1}", this.Address, registerRequest.UdpPort);
                    this.UdpAddressIPv6 = string.IsNullOrEmpty(this.AddressIPv6) ? null : string.Format("{0}:{1}", this.AddressIPv6, registerRequest.UdpPort);
                    this.UdpHostname    = string.IsNullOrEmpty(this.Hostname) ? null : string.Format("{0}:{1}", this.Hostname, registerRequest.UdpPort);
                }

                if (registerRequest.TcpPort.HasValue)
                {
                    this.TcpAddress     = string.IsNullOrEmpty(this.Address) ? null : string.Format("{0}:{1}", this.Address, registerRequest.TcpPort);
                    this.TcpAddressIPv6 = string.IsNullOrEmpty(this.AddressIPv6) ? null : string.Format("{0}:{1}", this.AddressIPv6, registerRequest.TcpPort);
                    this.TcpHostname    = string.IsNullOrEmpty(this.Hostname) ? null : string.Format("{0}:{1}", this.Hostname, registerRequest.TcpPort);
                }

                if (registerRequest.WebSocketPort.HasValue && registerRequest.WebSocketPort != 0)
                {
                    this.WebSocketAddress = string.IsNullOrEmpty(this.Address)
                                                ? null
                                                : string.Format("ws://{0}:{1}", this.Address, registerRequest.WebSocketPort);

                    this.WebSocketAddressIPv6 = string.IsNullOrEmpty(this.AddressIPv6)
                                                    ? null
                                                    : string.Format("ws://{0}:{1}", this.AddressIPv6, registerRequest.WebSocketPort);

                    this.WebSocketHostname = string.IsNullOrEmpty(this.Hostname)
                                                 ? null
                                                 : string.Format("ws://{0}:{1}", this.Hostname, registerRequest.WebSocketPort);
                }

                if (registerRequest.HttpPort.HasValue && registerRequest.HttpPort != 0)
                {
                    this.HttpAddress = string.IsNullOrEmpty(this.Address)
                                           ? null
                                           : string.Format("http://{0}:{1}{2}", this.Address, registerRequest.HttpPort, registerRequest.HttpPath);

                    this.HttpAddressIPv6 = string.IsNullOrEmpty(this.AddressIPv6)
                                               ? null
                                               : string.Format("http://{0}:{1}{2}", this.AddressIPv6, registerRequest.HttpPort, registerRequest.HttpPath);

                    this.HttpHostname = string.IsNullOrEmpty(this.Hostname)
                                            ? null
                                            : string.Format("http://{0}:{1}{2}", this.Hostname, registerRequest.HttpPort, registerRequest.HttpPath);
                }

                // HTTP & WebSockets require a proper domain name (especially for certificate validation on secure Websocket & HTTPS connections):
                if (string.IsNullOrEmpty(this.Hostname))
                {
                    log.WarnFormat("HTTPs & Secure WebSockets not supported. GameServer {0} does not have a public hostname.", this.Address);
                }
                else
                {
                    if (registerRequest.SecureWebSocketPort.HasValue && registerRequest.SecureWebSocketPort != 0)
                    {
                        this.SecureWebSocketHostname = string.Format("wss://{0}:{1}", this.Hostname, registerRequest.SecureWebSocketPort);
                    }

                    if (registerRequest.SecureHttpPort.HasValue && registerRequest.SecureHttpPort != 0)
                    {
                        this.SecureHttpHostname = string.Format("https://{0}:{1}{2}", this.Hostname, registerRequest.SecureHttpPort, registerRequest.HttpPath);
                    }
                }

                this.ServerId = new Guid(registerRequest.ServerId);
                this.State    = (ServerState)registerRequest.ServerState;

                this.Key = string.Format("{0}-{1}-{2}", registerRequest.GameServerAddress, registerRequest.UdpPort, registerRequest.TcpPort);

                log.Debug(
                    string.Format(
                        "Registered GameServerAddress={0} GameServerAddressIPv6={1}" + " TcpAddress={2} TcpAddressIPv6={3} UdpAddress={4} UdpAddressIPv6={5}"
                        + " WebSocketAddress={6} WebSocketAddressIPv6={7} HttpAddress={8} HttpAddressIPv6={9}"
                        + " SecureWebSocketAddress={10} SecureHttpAddress={11}",
                        this.Address,
                        this.AddressIPv6,
                        this.TcpAddress,
                        this.TcpAddressIPv6,
                        this.UdpAddress,
                        this.UdpAddressIPv6,
                        this.WebSocketAddress,
                        this.WebSocketAddressIPv6,
                        this.HttpAddress,
                        this.HttpAddressIPv6,
                        this.SecureWebSocketHostname,
                        this.SecureHttpHostname));

                this.application.GameServers.OnConnect(this);

                if (this.State == ServerState.Normal)
                {
                    this.application.LoadBalancer.TryAddServer(this, 0);
                }

                contract.AuthList = this.GetAuthlist();

                return(new OperationResponse(request.OperationCode, contract));
            }
            catch (Exception e)
            {
                log.Error(e);
                return(new OperationResponse(request.OperationCode)
                {
                    DebugMessage = e.Message, ReturnCode = (short)ErrorCode.InternalServerError
                });
            }
        }
 private void OnRegisteredAtMaster(RegisterGameServerResponse registerResponse)
 {
     this.masterServerConnection.OnRegisteredAtMaster(registerResponse);
 }
        private void HandleRegisterGameServerResponse(short returnCode, string debugMessage, RegisterGameServerResponse response)
        {
            switch (returnCode)
            {
            case (short)ErrorCode.Ok:
            {
                if (!response.IsValid)
                {
                    log.Error("RegisterGameServerInitResponse contract invalid: " + response.GetErrorMessage());
                    this.Disconnect();
                    return;
                }

                log.InfoFormat("Successfully registered at master server: serverId={0}", this.application.ServerId);
                this.IsRegistered = true;
                this.StartUpdateLoop();
                this.StartSaveStatsLoop();
                this.OnRegisteredAtMaster(response);
                break;
            }

            default:
            {
                log.ErrorFormat(this.masterServerConnection + ": Failed to register at master: err={0}, msg={1}, serverid={2}", returnCode,
                                debugMessage, this.application.ServerId);
                this.Disconnect();
                break;
            }
            }
        }
        private void HandleRegisterGameServerResponse(OperationResponse operationResponse)
        {
            var contract = new RegisterGameServerResponse(this.Protocol, operationResponse);

            this.HandleRegisterGameServerResponse(operationResponse.ReturnCode, operationResponse.DebugMessage, contract);
        }
 protected virtual void OnRegisteredAtMaster(RegisterGameServerResponse registerResponse)
 {
 }
        private OperationResponse HandleRegisterGameServerRequest(OperationRequest request)
        {
            try
            {
                var registerRequest = new RegisterGameServer(this.Protocol, request);

                if (registerRequest.IsValid == false)
                {
                    string msg = registerRequest.GetErrorMessage();
                    log.ErrorFormat("RegisterGameServer contract error: {0}", msg);

                    return(new OperationResponse(request.OperationCode)
                    {
                        DebugMessage = msg, ReturnCode = (short)ErrorCode.OperationInvalid
                    });
                }

                IPAddress masterAddress = this.application.GetInternalMasterNodeIpAddress();
                var       contract      = new RegisterGameServerResponse {
                    InternalAddress = masterAddress.GetAddressBytes()
                };

                // is master
                if (!this.application.IsMaster)
                {
                    return(new OperationResponse(request.OperationCode, contract)
                    {
                        ReturnCode = (short)ErrorCode.RedirectRepeat,
                        DebugMessage = "RedirectRepeat"
                    });
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat(
                        "Received register request: Address={0}, UdpPort={1}, TcpPort={2}, WebSocketPort={3}, SecureWebSocketPort={4}, HttpPort={5}, State={6}, Hostname={7}, IPv6Address={8}, WebRTCPort={9}",
                        registerRequest.GameServerAddress,
                        registerRequest.UdpPort,
                        registerRequest.TcpPort,
                        registerRequest.WebSocketPort,
                        registerRequest.SecureWebSocketPort,
                        registerRequest.HttpPort,
                        (ServerState)registerRequest.ServerState,
                        registerRequest.GameServerHostName,
                        registerRequest.GameServerAddressIPv6,
                        registerRequest.WebRTCPort);
                }

                this.application.GameServers.RegisterGameServer(registerRequest, this);

                var addrInfo = Context.AddressInfo;
                if (log.IsDebugEnabled)
                {
                    log.Debug(
                        string.Format(
                            "Registered GameServerAddress={0} GameServerAddressIPv6={1}" + " TcpAddress={2} TcpAddressIPv6={3} UdpAddress={4} UdpAddressIPv6={5}"
                            + " WebSocketAddress={6} WebSocketAddressIPv6={7} HttpAddress={8} HttpAddressIPv6={9}"
                            + " SecureWebSocketAddress={10} SecureHttpAddress={11}",
                            addrInfo.Address,
                            addrInfo.AddressIPv6,
                            addrInfo.TcpAddress,
                            addrInfo.TcpAddressIPv6,
                            addrInfo.UdpAddress,
                            addrInfo.UdpAddressIPv6,
                            addrInfo.WebSocketAddress,
                            addrInfo.WebSocketAddressIPv6,
                            addrInfo.HttpAddress,
                            addrInfo.HttpAddressIPv6,
                            addrInfo.SecureWebSocketHostname,
                            addrInfo.SecureHttpHostname));
                }

                return(new OperationResponse(request.OperationCode, contract));
            }
            catch (Exception e)
            {
                log.Error(e);
                return(new OperationResponse(request.OperationCode)
                {
                    DebugMessage = e.Message, ReturnCode = (short)ErrorCode.InternalServerError
                });
            }
        }
 public virtual void OnRegisteredAtMaster(MasterServerConnectionBase masterServerConnectionBase, RegisterGameServerResponse registerResponse)
 {
     masterServerConnectionBase.UpdateAllGameStates();
 }