Esempio n. 1
0
        public uint RequestAppServerRegister(CentralServerPeerInfo peerInfo, AckMessageCallback callback)
        {
            RequestAppServerRegisterMessage message = new RequestAppServerRegisterMessage();

            message.peerInfo = peerInfo;
            return(ClientSendRequest(MMOMessageTypes.RequestAppServerRegister, message, callback));
        }
Esempio n. 2
0
        private void HandleResponseAppServerAddress(LiteNetLibMessageHandler messageHandler)
        {
            ResponseAppServerAddressMessage message = messageHandler.ReadMessage <ResponseAppServerAddressMessage>();

            if (message.responseCode == AckResponseCode.Success)
            {
                CentralServerPeerInfo peerInfo = message.peerInfo;
                switch (peerInfo.peerType)
                {
                case CentralServerPeerType.MapServer:
                    if (!string.IsNullOrEmpty(peerInfo.extra))
                    {
                        if (LogInfo)
                        {
                            Debug.Log("Register map server: " + peerInfo.extra);
                        }
                        mapServerConnectionIdsBySceneName[peerInfo.extra] = peerInfo;
                    }
                    break;

                case CentralServerPeerType.Chat:
                    if (!ChatNetworkManager.IsClientConnected)
                    {
                        if (LogInfo)
                        {
                            Debug.Log("Connecting to chat server");
                        }
                        ChatNetworkManager.StartClient(this, peerInfo.networkAddress, peerInfo.networkPort, peerInfo.connectKey);
                    }
                    break;
                }
            }
        }
Esempio n. 3
0
        public uint RequestAppServerRegister(CentralServerPeerInfo peerInfo, AckMessageCallback callback)
        {
            RequestAppServerRegisterMessage message = new RequestAppServerRegisterMessage();

            message.peerInfo = peerInfo;
            return(Client.ClientSendAckPacket(DeliveryMethod.ReliableOrdered, MMOMessageTypes.RequestAppServerRegister, message, callback));
        }
Esempio n. 4
0
 public bool RequestAppServerRegister(CentralServerPeerInfo peerInfo)
 {
     return(ClientSendRequest(MMORequestTypes.RequestAppServerRegister, new RequestAppServerRegisterMessage()
     {
         peerInfo = peerInfo,
     }));
 }
 public override void DeserializeData(NetDataReader reader)
 {
     peerInfo = new CentralServerPeerInfo();
     peerInfo.Deserialize(reader);
     time = reader.GetInt();
     hash = reader.GetString();
 }
Esempio n. 6
0
 /// <summary>
 /// This function will be used to send connection information to connected map servers and chat servers
 /// </summary>
 /// <param name="connectionId"></param>
 /// <param name="peerInfo"></param>
 protected void BroadcastAppServers(long connectionId, CentralServerPeerInfo peerInfo)
 {
     // Send map peer info to other map server
     foreach (CentralServerPeerInfo mapServerPeer in mapServerPeers.Values)
     {
         // Send other info to current peer
         ResponseAppServerAddressMessage responseMapAddressMessage = new ResponseAppServerAddressMessage();
         responseMapAddressMessage.responseCode = AckResponseCode.Success;
         responseMapAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
         responseMapAddressMessage.peerInfo     = mapServerPeer;
         ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseAppServerAddress, responseMapAddressMessage);
         // Send current info to other peer
         responseMapAddressMessage = new ResponseAppServerAddressMessage();
         responseMapAddressMessage.responseCode = AckResponseCode.Success;
         responseMapAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
         responseMapAddressMessage.peerInfo     = peerInfo;
         ServerSendPacket(mapServerPeer.connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseAppServerAddress, responseMapAddressMessage);
     }
     // Send chat peer info to new map server
     if (chatServerPeers.Count > 0)
     {
         CentralServerPeerInfo           chatPeerInfo = chatServerPeers.Values.First();
         ResponseAppServerAddressMessage responseChatAddressMessage = new ResponseAppServerAddressMessage();
         responseChatAddressMessage.responseCode = AckResponseCode.Success;
         responseChatAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
         responseChatAddressMessage.peerInfo     = chatPeerInfo;
         ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseAppServerAddress, responseChatAddressMessage);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// This function will be used to send connection information to connected map servers and chat servers
        /// </summary>
        /// <param name="connectionId"></param>
        /// <param name="peerInfo"></param>
        protected void BroadcastAppServers(long connectionId, CentralServerPeerInfo peerInfo)
        {
            ResponseAppServerAddressMessage appServerAddressMessage;

            // Send map peer info to other map server
            foreach (CentralServerPeerInfo mapServerPeer in mapServerPeers.Values)
            {
                // Send other info to current peer
                appServerAddressMessage = new ResponseAppServerAddressMessage();
                appServerAddressMessage.responseCode = AckResponseCode.Success;
                appServerAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
                appServerAddressMessage.peerInfo     = mapServerPeer;
                ServerSendResponse(connectionId, MMOMessageTypes.ResponseAppServerAddress, appServerAddressMessage);
                // Send current info to other peer
                appServerAddressMessage = new ResponseAppServerAddressMessage();
                appServerAddressMessage.responseCode = AckResponseCode.Success;
                appServerAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
                appServerAddressMessage.peerInfo     = peerInfo;
                ServerSendResponse(mapServerPeer.connectionId, MMOMessageTypes.ResponseAppServerAddress, appServerAddressMessage);
            }
            // Send chat peer info to new map server
            if (chatServerPeers.Count > 0)
            {
                CentralServerPeerInfo chatPeerInfo = chatServerPeers.Values.First();
                appServerAddressMessage = new ResponseAppServerAddressMessage();
                appServerAddressMessage.responseCode = AckResponseCode.Success;
                appServerAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
                appServerAddressMessage.peerInfo     = chatPeerInfo;
                ServerSendResponse(connectionId, MMOMessageTypes.ResponseAppServerAddress, appServerAddressMessage);
            }
        }
Esempio n. 8
0
 public bool RequestAppServerRegister(CentralServerPeerInfo peerInfo)
 {
     Logging.Log(LogTag, "App Register is requesting");
     return(SendRequest(MMORequestTypes.RequestAppServerRegister, new RequestAppServerRegisterMessage()
     {
         peerInfo = peerInfo,
     }));
 }
Esempio n. 9
0
 public override void SerializeData(NetDataWriter writer)
 {
     writer.Put((byte)error);
     if (peerInfo == null)
     {
         peerInfo = new CentralServerPeerInfo();
     }
     peerInfo.Serialize(writer);
 }
Esempio n. 10
0
        protected async UniTaskVoid HandleRequestSelectCharacter(
            RequestHandlerData requestHandler,
            RequestSelectCharacterMessage request,
            RequestProceedResultDelegate <ResponseSelectCharacterMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            long connectionId = requestHandler.ConnectionId;
            CentralServerPeerInfo mapServerPeerInfo = default;
            CentralUserPeerInfo   userPeerInfo;
            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                result.InvokeError(new ResponseSelectCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }
            AsyncResponseData <CharacterResp> characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq()
            {
                UserId      = userPeerInfo.userId,
                CharacterId = request.characterId
            });

            if (!characterResp.IsSuccess)
            {
                result.InvokeError(new ResponseSelectCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            PlayerCharacterData character = characterResp.Response.CharacterData;
            if (character == null)
            {
                result.InvokeError(new ResponseSelectCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_CHARACTER_DATA,
                });
                return;
            }
            if (!ClusterServer.MapServerPeersByMapId.TryGetValue(character.CurrentMapName, out mapServerPeerInfo))
            {
                result.InvokeError(new ResponseSelectCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_MAP_SERVER_NOT_READY,
                });
                return;
            }
            // Response
            result.InvokeSuccess(new ResponseSelectCharacterMessage()
            {
                sceneName      = mapServerPeerInfo.extra,
                networkAddress = mapServerPeerInfo.networkAddress,
                networkPort    = mapServerPeerInfo.networkPort,
            });
#endif
        }
 public override void SerializeData(NetDataWriter writer)
 {
     if (peerInfo == null)
         peerInfo = new CentralServerPeerInfo();
     time = (int)(System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond);
     hash = CentralNetworkManager.GetAppServerRegisterHash(peerInfo.peerType, time);
     peerInfo.Serialize(writer);
     writer.Put(time);
     writer.Put(hash);
 }
        private void HandleResponseAppServerAddress(MessageHandlerData messageHandler)
        {
            ResponseAppServerAddressMessage message  = messageHandler.ReadMessage <ResponseAppServerAddressMessage>();
            CentralServerPeerInfo           peerInfo = message.peerInfo;

            switch (peerInfo.peerType)
            {
            case CentralServerPeerType.MapServer:
                if (!string.IsNullOrEmpty(peerInfo.extra))
                {
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register map server: " + peerInfo.extra);
                    }
                    mapServerConnectionIdsBySceneName[peerInfo.extra] = peerInfo;
                }
                break;

            case CentralServerPeerType.InstanceMapServer:
                if (!string.IsNullOrEmpty(peerInfo.extra))
                {
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register instance map server: " + peerInfo.extra);
                    }
                    instanceMapServerConnectionIdsByInstanceId[peerInfo.extra] = peerInfo;
                    // Warp characters
                    HashSet <uint> warpingCharacters;
                    if (instanceMapWarpingCharactersByInstanceId.TryGetValue(peerInfo.extra, out warpingCharacters))
                    {
                        BasePlayerCharacterEntity warpingCharacterEntity;
                        foreach (uint warpingCharacter in warpingCharacters)
                        {
                            if (!Assets.TryGetSpawnedObject(warpingCharacter, out warpingCharacterEntity))
                            {
                                continue;
                            }
                            WarpCharacterToInstanceRoutine(warpingCharacterEntity, peerInfo.extra).Forget();
                        }
                    }
                }
                break;

            case CentralServerPeerType.Chat:
                if (!ChatNetworkManager.IsClientConnected)
                {
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Connecting to chat server");
                    }
                    ChatNetworkManager.StartClient(this, peerInfo.networkAddress, peerInfo.networkPort);
                }
                break;
            }
        }
Esempio n. 13
0
        private async UniTaskVoid HandleRequestAppServerAddress(
            RequestHandlerData requestHandler,
            RequestAppServerAddressMessage request,
            RequestProceedResultDelegate <ResponseAppServerAddressMessage> result)
        {
            long                  connectionId = requestHandler.ConnectionId;
            UITextKeys            message      = UITextKeys.NONE;
            CentralServerPeerInfo peerInfo     = new CentralServerPeerInfo();

            switch (request.peerType)
            {
            // TODO: Balancing servers when there are multiple servers with same type
            case CentralServerPeerType.MapSpawnServer:
                if (MapSpawnServerPeers.Count > 0)
                {
                    peerInfo = MapSpawnServerPeers.Values.First();
                    Logging.Log(LogTag, "Request Map Spawn Address: [" + connectionId + "]");
                }
                else
                {
                    message = UITextKeys.UI_ERROR_SERVER_NOT_FOUND;
                    Logging.Log(LogTag, "Request Map Spawn Address: [" + connectionId + "] [" + message + "]");
                }
                break;

            case CentralServerPeerType.MapServer:
                string mapName = request.extra;
                if (!MapServerPeersByMapId.TryGetValue(mapName, out peerInfo))
                {
                    message = UITextKeys.UI_ERROR_SERVER_NOT_FOUND;
                    Logging.Log(LogTag, "Request Map Address: [" + connectionId + "] [" + mapName + "] [" + message + "]");
                }
                break;

            case CentralServerPeerType.InstanceMapServer:
                string instanceId = request.extra;
                if (!MapServerPeersByInstanceId.TryGetValue(instanceId, out peerInfo))
                {
                    message = UITextKeys.UI_ERROR_SERVER_NOT_FOUND;
                    Logging.Log(LogTag, "Request Map Address: [" + connectionId + "] [" + instanceId + "] [" + message + "]");
                }
                break;
            }
            // Response
            result.Invoke(
                message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseAppServerAddressMessage()
            {
                message  = message,
                peerInfo = peerInfo,
            });
            await UniTask.Yield();
        }
        protected void HandleRequestAppServerAddress(LiteNetLibMessageHandler messageHandler)
        {
            var connectionId = messageHandler.connectionId;
            var message      = messageHandler.ReadMessage <RequestAppServerAddressMessage>();
            var error        = ResponseAppServerAddressMessage.Error.None;
            var peerInfo     = new CentralServerPeerInfo();

            switch (message.peerType)
            {
            // TODO: Balancing servers when there are multiple servers with same type
            case CentralServerPeerType.MapSpawnServer:
                if (mapSpawnServerPeers.Count > 0)
                {
                    peerInfo = mapSpawnServerPeers.Values.First();
                    Debug.Log("[Central] Request Map Spawn Address: [" + connectionId + "]");
                }
                else
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    Debug.Log("[Central] Request Map Spawn Address: [" + connectionId + "] [" + error + "]");
                }
                break;

            case CentralServerPeerType.MapServer:
                var mapName = message.extra;
                if (!mapServerPeersBySceneName.TryGetValue(mapName, out peerInfo))
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    Debug.Log("[Central] Request Map Address: [" + connectionId + "] [" + mapName + "] [" + error + "]");
                }
                break;

            case CentralServerPeerType.Chat:
                if (chatServerPeers.Count > 0)
                {
                    peerInfo = chatServerPeers.Values.First();
                    Debug.Log("[Central] Request Chat Address: [" + connectionId + "]");
                }
                else
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    Debug.Log("[Central] Request Chat Address: [" + connectionId + "] [" + error + "]");
                }
                break;
            }
            var responseMessage = new ResponseAppServerAddressMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseAppServerAddressMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.peerInfo     = peerInfo;
            ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MMOMessageTypes.ResponseAppServerAddress, responseMessage);
        }
        public void OnCentralServerConnected()
        {
            Debug.Log("[" + appServer.PeerType + "] Connected to Central Server");
            CentralServerPeerInfo peerInfo = new CentralServerPeerInfo();

            peerInfo.peerType       = appServer.PeerType;
            peerInfo.networkAddress = appServer.AppAddress;
            peerInfo.networkPort    = appServer.AppPort;
            peerInfo.extra          = appServer.AppExtra;
            // Send Request
            RequestAppServerRegisterMessage message = new RequestAppServerRegisterMessage();

            message.peerInfo = peerInfo;
            ClientSendAckPacket(DeliveryMethod.ReliableOrdered, MMOMessageTypes.RequestAppServerRegister, message, OnAppServerRegistered);
        }
Esempio n. 16
0
        public void OnCentralServerConnected()
        {
            Logging.Log(LogTag, "[" + appServer.PeerType + "] Connected to Central Server");
            CentralServerPeerInfo peerInfo = new CentralServerPeerInfo();

            peerInfo.peerType       = appServer.PeerType;
            peerInfo.networkAddress = appServer.AppAddress;
            peerInfo.networkPort    = appServer.AppPort;
            peerInfo.extra          = appServer.AppExtra;
            // Send Request
            SendRequest(MMORequestTypes.RequestAppServerRegister, new RequestAppServerRegisterMessage()
            {
                peerInfo = peerInfo,
            });
        }
        protected async UniTaskVoid HandleRequestSelectCharacter(
            RequestHandlerData requestHandler,
            RequestSelectCharacterMessage request,
            RequestProceedResultDelegate <ResponseSelectCharacterMessage> result)
        {
            long connectionId = requestHandler.ConnectionId;

            ResponseSelectCharacterMessage.Error error = ResponseSelectCharacterMessage.Error.None;
            CentralServerPeerInfo mapServerPeerInfo    = default;
            CentralUserPeerInfo   userPeerInfo;

            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                error = ResponseSelectCharacterMessage.Error.NotLoggedin;
            }
            else
            {
                CharacterResp characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq()
                {
                    UserId      = userPeerInfo.userId,
                    CharacterId = request.characterId
                });

                PlayerCharacterData character = characterResp.CharacterData.FromByteString <PlayerCharacterData>();
                if (character == null)
                {
                    error = ResponseSelectCharacterMessage.Error.InvalidCharacterData;
                }
                else if (!mapServerPeersBySceneName.TryGetValue(character.CurrentMapName, out mapServerPeerInfo))
                {
                    error = ResponseSelectCharacterMessage.Error.MapNotReady;
                }
            }
            AckResponseCode responseCode            = error == ResponseSelectCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            ResponseSelectCharacterMessage response = new ResponseSelectCharacterMessage();

            response.error = error;
            if (error != ResponseSelectCharacterMessage.Error.MapNotReady)
            {
                response.sceneName      = mapServerPeerInfo.extra;
                response.networkAddress = mapServerPeerInfo.networkAddress;
                response.networkPort    = mapServerPeerInfo.networkPort;
            }
            // Response
            result.Invoke(responseCode, response);
        }
Esempio n. 18
0
        protected async UniTaskVoid HandleRequestSelectCharacter(
            RequestHandlerData requestHandler,
            RequestSelectCharacterMessage request,
            RequestProceedResultDelegate <ResponseSelectCharacterMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            long                  connectionId      = requestHandler.ConnectionId;
            UITextKeys            message           = UITextKeys.NONE;
            CentralServerPeerInfo mapServerPeerInfo = default;
            CentralUserPeerInfo   userPeerInfo;
            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                message = UITextKeys.UI_ERROR_NOT_LOGGED_IN;
            }
            else
            {
                CharacterResp characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq()
                {
                    UserId      = userPeerInfo.userId,
                    CharacterId = request.characterId
                });

                PlayerCharacterData character = characterResp.CharacterData;
                if (character == null)
                {
                    message = UITextKeys.UI_ERROR_INVALID_CHARACTER_DATA;
                }
                else if (!mapServerPeersBySceneName.TryGetValue(character.CurrentMapName, out mapServerPeerInfo))
                {
                    message = UITextKeys.UI_ERROR_MAP_SERVER_NOT_READY;
                }
            }
            AckResponseCode responseCode            = message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error;
            ResponseSelectCharacterMessage response = new ResponseSelectCharacterMessage();
            response.message = message;
            if (message != UITextKeys.UI_ERROR_MAP_SERVER_NOT_READY)
            {
                response.sceneName      = mapServerPeerInfo.extra;
                response.networkAddress = mapServerPeerInfo.networkAddress;
                response.networkPort    = mapServerPeerInfo.networkPort;
            }
            // Response
            result.Invoke(responseCode, response);
#endif
        }
Esempio n. 19
0
        private async UniTaskVoid HandleRequestSelectCharacterRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestSelectCharacterMessage message = messageHandler.ReadMessage <RequestSelectCharacterMessage>();

            ResponseSelectCharacterMessage.Error error = ResponseSelectCharacterMessage.Error.None;
            CentralServerPeerInfo mapServerPeerInfo    = default(CentralServerPeerInfo);
            CentralUserPeerInfo   userPeerInfo;

            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                error = ResponseSelectCharacterMessage.Error.NotLoggedin;
            }
            else
            {
                CharacterResp characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq()
                {
                    UserId      = userPeerInfo.userId,
                    CharacterId = message.characterId
                });

                PlayerCharacterData character = characterResp.CharacterData.FromByteString <PlayerCharacterData>();
                if (character == null)
                {
                    error = ResponseSelectCharacterMessage.Error.InvalidCharacterData;
                }
                else if (!mapServerPeersBySceneName.TryGetValue(character.CurrentMapName, out mapServerPeerInfo))
                {
                    error = ResponseSelectCharacterMessage.Error.MapNotReady;
                }
            }
            ResponseSelectCharacterMessage responseMessage = new ResponseSelectCharacterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseSelectCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            if (error != ResponseSelectCharacterMessage.Error.MapNotReady)
            {
                responseMessage.sceneName      = mapServerPeerInfo.extra;
                responseMessage.networkAddress = mapServerPeerInfo.networkAddress;
                responseMessage.networkPort    = mapServerPeerInfo.networkPort;
            }
            ServerSendResponse(connectionId, MMOMessageTypes.ResponseSelectCharacter, responseMessage);
        }
Esempio n. 20
0
        private IEnumerator HandleRequestSelectCharacterRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestSelectCharacterMessage message = messageHandler.ReadMessage <RequestSelectCharacterMessage>();

            ResponseSelectCharacterMessage.Error error = ResponseSelectCharacterMessage.Error.None;
            CentralServerPeerInfo mapServerPeerInfo    = null;
            CentralUserPeerInfo   userPeerInfo;

            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                error = ResponseSelectCharacterMessage.Error.NotLoggedin;
            }
            else
            {
                ReadCharacterJob job = new ReadCharacterJob(Database, userPeerInfo.userId, message.characterId, false, false, false, false, false, false, false, false, false);
                job.Start();
                yield return(StartCoroutine(job.WaitFor()));

                PlayerCharacterData character = job.result;
                if (character == null)
                {
                    error = ResponseSelectCharacterMessage.Error.InvalidCharacterData;
                }
                else if (!mapServerPeersBySceneName.TryGetValue(character.CurrentMapName, out mapServerPeerInfo))
                {
                    error = ResponseSelectCharacterMessage.Error.MapNotReady;
                }
            }
            ResponseSelectCharacterMessage responseMessage = new ResponseSelectCharacterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseSelectCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            if (mapServerPeerInfo != null)
            {
                responseMessage.sceneName      = mapServerPeerInfo.extra;
                responseMessage.networkAddress = mapServerPeerInfo.networkAddress;
                responseMessage.networkPort    = mapServerPeerInfo.networkPort;
                responseMessage.connectKey     = mapServerPeerInfo.connectKey;
            }
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseSelectCharacter, responseMessage);
        }
Esempio n. 21
0
 /// <summary>
 /// This function will be used to send connection information to connected map servers and cluster servers
 /// </summary>
 /// <param name="connectionId"></param>
 /// <param name="broadcastPeerInfo"></param>
 private void BroadcastAppServers(long connectionId, CentralServerPeerInfo broadcastPeerInfo)
 {
     // Send map peer info to other map server
     foreach (CentralServerPeerInfo mapPeerInfo in MapServerPeers.Values)
     {
         // Send other info to current peer
         SendPacket(connectionId, 0, DeliveryMethod.ReliableOrdered, MMOMessageTypes.AppServerAddress, (writer) => writer.PutValue(new ResponseAppServerAddressMessage()
         {
             message  = UITextKeys.NONE,
             peerInfo = mapPeerInfo,
         }));
         // Send current info to other peer
         SendPacket(mapPeerInfo.connectionId, 0, DeliveryMethod.ReliableOrdered, MMOMessageTypes.AppServerAddress, (writer) => writer.PutValue(new ResponseAppServerAddressMessage()
         {
             message  = UITextKeys.NONE,
             peerInfo = broadcastPeerInfo,
         }));
     }
 }
Esempio n. 22
0
        private async UniTaskVoid HandleRequestAppServerRegister(
            RequestHandlerData requestHandler,
            RequestAppServerRegisterMessage request,
            RequestProceedResultDelegate <ResponseAppServerRegisterMessage> result)
        {
            long       connectionId = requestHandler.ConnectionId;
            UITextKeys message      = UITextKeys.NONE;

            if (request.ValidateHash())
            {
                CentralServerPeerInfo peerInfo = request.peerInfo;
                peerInfo.connectionId = connectionId;
                switch (request.peerInfo.peerType)
                {
                case CentralServerPeerType.MapSpawnServer:
                    MapSpawnServerPeers[connectionId] = peerInfo;
                    Logging.Log(LogTag, "Register Map Spawn Server: [" + connectionId + "]");
                    break;

                case CentralServerPeerType.MapServer:
                    // Extra is map ID
                    if (!MapServerPeersByMapId.ContainsKey(peerInfo.extra))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        MapServerPeersByMapId[peerInfo.extra] = peerInfo;
                        MapServerPeers[connectionId]          = peerInfo;
                        Logging.Log(LogTag, "Register Map Server: [" + connectionId + "] [" + peerInfo.extra + "]");
                    }
                    else
                    {
                        message = UITextKeys.UI_ERROR_MAP_EXISTED;
                        Logging.Log(LogTag, "Register Map Server Failed: [" + connectionId + "] [" + peerInfo.extra + "] [" + message + "]");
                    }
                    break;

                case CentralServerPeerType.InstanceMapServer:
                    // Extra is instance ID
                    if (!MapServerPeersByInstanceId.ContainsKey(peerInfo.extra))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        MapServerPeersByInstanceId[peerInfo.extra] = peerInfo;
                        MapServerPeers[connectionId] = peerInfo;
                        Logging.Log(LogTag, "Register Instance Map Server: [" + connectionId + "] [" + peerInfo.extra + "]");
                    }
                    else
                    {
                        message = UITextKeys.UI_ERROR_EVENT_EXISTED;
                        Logging.Log(LogTag, "Register Instance Map Server Failed: [" + connectionId + "] [" + peerInfo.extra + "] [" + message + "]");
                    }
                    break;
                }
            }
            else
            {
                message = UITextKeys.UI_ERROR_INVALID_SERVER_HASH;
                Logging.Log(LogTag, "Register Server Failed: [" + connectionId + "] [" + message + "]");
            }
            // Response
            result.Invoke(
                message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseAppServerRegisterMessage()
            {
                message = message,
            });
            await UniTask.Yield();
        }
 public void Deserialize(NetDataReader reader)
 {
     message  = (UITextKeys)reader.GetPackedUShort();
     peerInfo = new CentralServerPeerInfo();
     peerInfo.Deserialize(reader);
 }
 public override void DeserializeData(NetDataReader reader)
 {
     error    = (Error)reader.GetByte();
     peerInfo = new CentralServerPeerInfo();
     peerInfo.Deserialize(reader);
 }
Esempio n. 25
0
        protected void HandleRequestAppServerAddress(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestAppServerAddressMessage message = messageHandler.ReadMessage <RequestAppServerAddressMessage>();

            ResponseAppServerAddressMessage.Error error = ResponseAppServerAddressMessage.Error.None;
            CentralServerPeerInfo peerInfo = new CentralServerPeerInfo();

            switch (message.peerType)
            {
            // TODO: Balancing servers when there are multiple servers with same type
            case CentralServerPeerType.MapSpawnServer:
                if (mapSpawnServerPeers.Count > 0)
                {
                    peerInfo = mapSpawnServerPeers.Values.First();
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Request Map Spawn Address: [" + connectionId + "]");
                    }
                }
                else
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Request Map Spawn Address: [" + connectionId + "] [" + error + "]");
                    }
                }
                break;

            case CentralServerPeerType.MapServer:
                string mapName = message.extra;
                if (!mapServerPeersBySceneName.TryGetValue(mapName, out peerInfo))
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Request Map Address: [" + connectionId + "] [" + mapName + "] [" + error + "]");
                    }
                }
                break;

            case CentralServerPeerType.Chat:
                if (chatServerPeers.Count > 0)
                {
                    peerInfo = chatServerPeers.Values.First();
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Request Chat Address: [" + connectionId + "]");
                    }
                }
                else
                {
                    error = ResponseAppServerAddressMessage.Error.ServerNotFound;
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Request Chat Address: [" + connectionId + "] [" + error + "]");
                    }
                }
                break;
            }
            ResponseAppServerAddressMessage responseMessage = new ResponseAppServerAddressMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseAppServerAddressMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.peerInfo     = peerInfo;
            ServerSendResponse(connectionId, MMOMessageTypes.ResponseAppServerAddress, responseMessage);
        }
        protected UniTaskVoid HandleRequestAppServerRegister(
            RequestHandlerData requestHandler,
            RequestAppServerRegisterMessage request,
            RequestProceedResultDelegate <ResponseAppServerRegisterMessage> result)
        {
            long connectionId = requestHandler.ConnectionId;

            ResponseAppServerRegisterMessage.Error error = ResponseAppServerRegisterMessage.Error.None;
            if (request.ValidateHash())
            {
                ResponseAppServerAddressMessage responseAppServerAddressMessage;
                CentralServerPeerInfo           peerInfo = request.peerInfo;
                peerInfo.connectionId = connectionId;
                switch (request.peerInfo.peerType)
                {
                case CentralServerPeerType.MapSpawnServer:
                    mapSpawnServerPeers[connectionId] = peerInfo;
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register Map Spawn Server: [" + connectionId + "]");
                    }
                    break;

                case CentralServerPeerType.MapServer:
                    string sceneName = peerInfo.extra;
                    if (!mapServerPeersBySceneName.ContainsKey(sceneName))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        mapServerPeersBySceneName[sceneName] = peerInfo;
                        mapServerPeers[connectionId]         = peerInfo;
                        mapUserIds[connectionId]             = new HashSet <string>();
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Map Server: [" + connectionId + "] [" + sceneName + "]");
                        }
                    }
                    else
                    {
                        error = ResponseAppServerRegisterMessage.Error.MapAlreadyExisted;
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Map Server Failed: [" + connectionId + "] [" + sceneName + "] [" + error + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.InstanceMapServer:
                    string instanceId = peerInfo.extra;
                    if (!instanceMapServerPeersByInstanceId.ContainsKey(instanceId))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        instanceMapServerPeersByInstanceId[instanceId] = peerInfo;
                        instanceMapServerPeers[connectionId]           = peerInfo;
                        mapUserIds[connectionId] = new HashSet <string>();
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Instance Map Server: [" + connectionId + "] [" + instanceId + "]");
                        }
                    }
                    else
                    {
                        error = ResponseAppServerRegisterMessage.Error.EventAlreadyExisted;
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Instance Map Server Failed: [" + connectionId + "] [" + instanceId + "] [" + error + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.Chat:
                    chatServerPeers[connectionId] = peerInfo;
                    // Send chat peer info to map servers
                    responseAppServerAddressMessage = new ResponseAppServerAddressMessage()
                    {
                        error    = ResponseAppServerAddressMessage.Error.None,
                        peerInfo = peerInfo,
                    };
                    foreach (CentralServerPeerInfo mapServerPeer in mapServerPeers.Values)
                    {
                        ServerSendPacket(mapServerPeer.connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.AppServerAddress, responseAppServerAddressMessage);
                    }
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register Chat Server: [" + connectionId + "]");
                    }
                    break;
                }
            }
            else
            {
                error = ResponseAppServerRegisterMessage.Error.InvalidHash;
                if (LogInfo)
                {
                    Logging.Log(LogTag, "Register Server Failed: [" + connectionId + "] [" + error + "]");
                }
            }
            // Response
            result.Invoke(
                error == ResponseAppServerRegisterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseAppServerRegisterMessage()
            {
                error = error,
            });
            return(default);
Esempio n. 27
0
        protected void HandleRequestAppServerRegister(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestAppServerRegisterMessage message = messageHandler.ReadMessage <RequestAppServerRegisterMessage>();

            ResponseAppServerRegisterMessage.Error error = ResponseAppServerRegisterMessage.Error.None;
            if (message.ValidateHash())
            {
                CentralServerPeerInfo peerInfo = message.peerInfo;
                peerInfo.connectionId = connectionId;
                switch (message.peerInfo.peerType)
                {
                case CentralServerPeerType.MapSpawnServer:
                    mapSpawnServerPeers[connectionId] = peerInfo;
                    if (LogInfo)
                    {
                        Debug.Log("[Central] Register Map Spawn Server: [" + connectionId + "]");
                    }
                    break;

                case CentralServerPeerType.MapServer:
                    string sceneName = peerInfo.extra;
                    if (!mapServerPeersBySceneName.ContainsKey(sceneName))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        mapServerPeersBySceneName[sceneName] = peerInfo;
                        mapServerPeers[connectionId]         = peerInfo;
                        mapUserIds[connectionId]             = new HashSet <string>();
                        if (LogInfo)
                        {
                            Debug.Log("[Central] Register Map Server: [" + connectionId + "] [" + sceneName + "]");
                        }
                    }
                    else
                    {
                        error = ResponseAppServerRegisterMessage.Error.MapAlreadyExisted;
                        if (LogInfo)
                        {
                            Debug.Log("[Central] Register Map Server Failed: [" + connectionId + "] [" + sceneName + "] [" + error + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.InstanceMapServer:
                    string instanceId = peerInfo.extra;
                    if (!instanceMapServerPeersByInstanceId.ContainsKey(instanceId))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        instanceMapServerPeersByInstanceId[instanceId] = peerInfo;
                        instanceMapServerPeers[connectionId]           = peerInfo;
                        mapUserIds[connectionId] = new HashSet <string>();
                        if (LogInfo)
                        {
                            Debug.Log("[Central] Register Instance Map Server: [" + connectionId + "] [" + instanceId + "]");
                        }
                    }
                    else
                    {
                        error = ResponseAppServerRegisterMessage.Error.EventAlreadyExisted;
                        if (LogInfo)
                        {
                            Debug.Log("[Central] Register Instance Map Server Failed: [" + connectionId + "] [" + instanceId + "] [" + error + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.Chat:
                    chatServerPeers[connectionId] = peerInfo;
                    // Send chat peer info to map servers
                    foreach (CentralServerPeerInfo mapServerPeer in mapServerPeers.Values)
                    {
                        ResponseAppServerAddressMessage responseChatAddressMessage = new ResponseAppServerAddressMessage();
                        responseChatAddressMessage.responseCode = AckResponseCode.Success;
                        responseChatAddressMessage.error        = ResponseAppServerAddressMessage.Error.None;
                        responseChatAddressMessage.peerInfo     = peerInfo;
                        ServerSendPacket(mapServerPeer.connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseAppServerAddress, responseChatAddressMessage);
                    }
                    if (LogInfo)
                    {
                        Debug.Log("[Central] Register Chat Server: [" + connectionId + "]");
                    }
                    break;
                }
            }
            else
            {
                error = ResponseAppServerRegisterMessage.Error.InvalidHash;
                if (LogInfo)
                {
                    Debug.Log("[Central] Register Server Failed: [" + connectionId + "] [" + error + "]");
                }
            }

            ResponseAppServerRegisterMessage responseMessage = new ResponseAppServerRegisterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseAppServerRegisterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseAppServerRegister, responseMessage);
        }
Esempio n. 28
0
        protected UniTaskVoid HandleRequestAppServerRegister(
            RequestHandlerData requestHandler,
            RequestAppServerRegisterMessage request,
            RequestProceedResultDelegate <ResponseAppServerRegisterMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            long       connectionId = requestHandler.ConnectionId;
            UITextKeys message      = UITextKeys.NONE;
            if (request.ValidateHash())
            {
                ResponseAppServerAddressMessage responseAppServerAddressMessage;
                CentralServerPeerInfo           peerInfo = request.peerInfo;
                peerInfo.connectionId = connectionId;
                switch (request.peerInfo.peerType)
                {
                case CentralServerPeerType.MapSpawnServer:
                    mapSpawnServerPeers[connectionId] = peerInfo;
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register Map Spawn Server: [" + connectionId + "]");
                    }
                    break;

                case CentralServerPeerType.MapServer:
                    string sceneName = peerInfo.extra;
                    if (!mapServerPeersBySceneName.ContainsKey(sceneName))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        mapServerPeersBySceneName[sceneName] = peerInfo;
                        mapServerPeers[connectionId]         = peerInfo;
                        mapUserIds[connectionId]             = new HashSet <string>();
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Map Server: [" + connectionId + "] [" + sceneName + "]");
                        }
                    }
                    else
                    {
                        message = UITextKeys.UI_ERROR_MAP_EXISTED;
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Map Server Failed: [" + connectionId + "] [" + sceneName + "] [" + message + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.InstanceMapServer:
                    string instanceId = peerInfo.extra;
                    if (!instanceMapServerPeersByInstanceId.ContainsKey(instanceId))
                    {
                        BroadcastAppServers(connectionId, peerInfo);
                        // Collects server data
                        instanceMapServerPeersByInstanceId[instanceId] = peerInfo;
                        instanceMapServerPeers[connectionId]           = peerInfo;
                        mapUserIds[connectionId] = new HashSet <string>();
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Instance Map Server: [" + connectionId + "] [" + instanceId + "]");
                        }
                    }
                    else
                    {
                        message = UITextKeys.UI_ERROR_EVENT_EXISTED;
                        if (LogInfo)
                        {
                            Logging.Log(LogTag, "Register Instance Map Server Failed: [" + connectionId + "] [" + instanceId + "] [" + message + "]");
                        }
                    }
                    break;

                case CentralServerPeerType.Chat:
                    chatServerPeers[connectionId] = peerInfo;
                    // Send chat peer info to map servers
                    responseAppServerAddressMessage = new ResponseAppServerAddressMessage()
                    {
                        message  = UITextKeys.NONE,
                        peerInfo = peerInfo,
                    };
                    foreach (CentralServerPeerInfo mapServerPeer in mapServerPeers.Values)
                    {
                        ServerSendPacket(mapServerPeer.connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.AppServerAddress, responseAppServerAddressMessage);
                    }
                    if (LogInfo)
                    {
                        Logging.Log(LogTag, "Register Chat Server: [" + connectionId + "]");
                    }
                    break;
                }
            }
            else
            {
                message = UITextKeys.UI_ERROR_INVALID_SERVER_HASH;
                if (LogInfo)
                {
                    Logging.Log(LogTag, "Register Server Failed: [" + connectionId + "] [" + message + "]");
                }
            }
            // Response
            result.Invoke(
                message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseAppServerRegisterMessage()
            {
                message = message,
            });
#endif
            return(default);