protected bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer)
        {
            OperationResponse operationResponse;

            if (serverPeer.Registered)
            {
                operationResponse = new OperationResponse(message.Code)
                {
                    ReturnCode   = (short)ErrorCode.InternalServerError,
                    DebugMessage = "Already registered"
                };
            }
            else
            {
                var registerRequest = new RegisterSubServer(serverPeer.protocol, message);

                if (!registerRequest.IsValid)
                {
                    string msg = registerRequest.GetErrorMessage();

                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Invalid register request {0}", msg);
                    }

                    operationResponse = new OperationResponse(message.Code)
                    {
                        DebugMessage = msg, ReturnCode = (short)ErrorCode.OperationInvalid
                    };
                }
                else
                {
                    XmlSerializer mySerializer = new XmlSerializer(typeof(RegisterSubServerData));
                    StringReader  inStream     = new StringReader(registerRequest.RegisterSubServerOperation);
                    var           registerData = (RegisterSubServerData)mySerializer.Deserialize(inStream);

                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Received register request: Address={0}, UdpPort={1}, TcpPort={2}, Type={3}",
                                        registerData.GameServerAddress, registerData.UdpPort, registerData.TcpPort, registerData.ServerType);
                    }

                    var serverData = serverPeer.ServerData <ServerData>();

                    if (serverData == null)
                    {
                        Log.DebugFormat("ServerData is NULL");
                    }

                    if (registerData.UdpPort.HasValue)
                    {
                        serverData.UdpAddress = registerData.GameServerAddress + ":" + registerData.UdpPort;
                    }

                    if (registerData.TcpPort.HasValue)
                    {
                        serverData.TcpAddress = registerData.GameServerAddress + ":" + registerData.TcpPort;
                    }

                    serverData.ServerId = registerData.ServerId;

                    serverData.ServerType = registerData.ServerType;

                    serverPeer.ServerType = _serverType.GetServerType(registerData.ServerType);

                    serverData.ApplicationName = registerData.ServerName;

                    Log.DebugFormat("ServerData: id={0} AppName={1} Type={2} | TCP: {3} UDP: {4}",
                                    serverData.ServerId, serverData.ApplicationName, serverData.ServerType,
                                    serverData.TcpAddress, serverData.UdpAddress);

                    operationResponse = new OperationResponse(message.Code, new Dictionary <byte, object>()
                    {
                        { _serverConfiguration.SubCodeParameterCode, 0 }
                    });

                    serverPeer.Registered = true;
                }
            }

            serverPeer.SendOperationResponse(operationResponse, new SendParameters());

            return(true);
        }
Exemplo n.º 2
0
        protected bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer)
        {
            OperationResponse operationResponse;

            // we are already registered, tell the subserver it tried to reiogster more than once
            if (serverPeer.Registered)
            {
                operationResponse = new OperationResponse(message.Code)
                {
                    ReturnCode = (short)ErrorCode.InternalServerError, DebugMessage = "Already Registered"
                };
            }
            else
            {
                var registerRequest = new RegisterSubServer(serverPeer.Protocol, message);

                // Register sub server operation is bad, something is missing, etc....
                if (!registerRequest.IsValid)
                {
                    string msg = registerRequest.GetErrorMessage();
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Invalid Register request {0}", msg);
                    }

                    operationResponse = new OperationResponse(message.code)
                    {
                        DebugMessage = msg, ReturnCode = (short)ErrorCode.OperationInvalid
                    };
                }
                else
                {
                    // valid message, not registered process the registration.
                    XmlSerializer mySerializer = new XmlSerializer(typeof(RegisterSubServerData));
                    StringReader  inStream     = new StringReader(registerRequest.RegisterSubServerOperation);
                    var           registerData = (RegisterSubServerData)mySerializer.Deserialize((inStream));

                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("Received register request: Address ={0}, Udpport={1}, Tcpport={2}, Type={3}",
                                        registerData.GameServerAddress, registerData.UdpPort, registerData.TcpPort,
                                        registerData.ServerType);
                    }

                    var serverData = serverPeer.ServerData <ServerData>();
                    if (serverData == null)
                    {
                        // Autofac Doesnt have a reference to serverData so it doesnt exist in the server Iserverdata list
                        Log.DebugFormat("ServerData is null...");
                    }

                    if (registerData.UdpPort.HasValue)
                    {
                        serverData.UdpAddress = registerData.GameServerAddress + ":" + registerData.UdpPort;
                    }

                    if (registerData.TcpPort.HasValue)
                    {
                        serverData.TcpAddress = registerData.GameServerAddress + ":" + registerData.TcpPort;
                    }

                    // setting server ID
                    serverData.ServerId = registerData.ServerId;
                    // setting server type
                    serverData.ServerType = registerData.ServerType;
                    // looking up the server type for the server peer
                    serverPeer.ServerType = _serverType.GetServerType(registerData.ServerType);
                    // setting application name
                    serverData.ApplicationName = registerData.ServerName;

                    operationResponse = new OperationResponse(message.Code);

                    serverPeer.Registered = true;
                }
            }

            serverPeer.SendOperationResponse(operationResponse, new SendParameters());
            return(true);
        }