Example #1
0
        private void TcpClientOnReceiveEvent (object sender, int tid, Stream data)
        {
            try
            {
                var quitMesage = string.Empty;

                
                if(data.Length == 0) return;
                var response = StreamConvert.StreamToString (data, false, true);
                Console.WriteLine ($"[LOG] Mo service message received! command={(MoLoginStatus) tid} response={response}");
                string[] loginResult = response.Split ('#');
                var moLogin = loginResult[0];
                var command = (MoLoginStatus) tid;

                ///KickOut 需修改MoService增加回傳值,目前無法作用
                // switch (tid)
                // {
                //     case -2:
                //         ReceivedInvoke (new MoLoginResponse(){MoLogin=moLogin}, command, "", "You have been logged out by the system due to multiple login. " +
                //             "This session will be terminated!");
                //        return;
                // }

                var moLoginResponse = userResponse.FirstOrDefault (x => x.MoLogin == moLogin);
                if (moLoginResponse == null) return;
                else
                {
                    userResponse.Remove (moLoginResponse);
                }
                
                switch (command)
                {
                    case MoLoginStatus.Success:
                        if (loginResult.Length < 4)
                        {
                            ReceivedInvoke (moLoginResponse, MoLoginStatus.Error, "MOService Error Message",
                                "An error happened during login. Error message below " +
                                $"{Environment.NewLine} msg={response}");
                            break;
                        }

                        var userLevels = loginResult[1].Split (',', StringSplitOptions.RemoveEmptyEntries);

                        if (userLevels.Length > 0)
                        {
                            moLoginResponse.UserLevels.Clear ();
                            moLoginResponse.UserLevels.AddRange (userLevels);
                            GetMeta (moLoginResponse);
                            break;
                        }

                        var errorMessage = loginResult[2];
                        ReceivedInvoke (moLoginResponse, command, "MOService Error Message",
                            "An error happened during login. Error message below " +
                            $"{Environment.NewLine} msg={errorMessage}");
                        break;
                    case MoLoginStatus.WrongPassword:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            "Your password is incorrect.");
                        break;
                    case MoLoginStatus.AccountInactive:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            "Your account has been closed/locked.");
                        break;
                    case MoLoginStatus.LoginAreaLimit:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            $"You are not authorized to login. tid={tid}");
                        break;
                    case MoLoginStatus.UserNotExists:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            "Your login is unknown.");
                        break;
                    case MoLoginStatus.InvalidArguments:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            "The login request to MOService was not well formatted. " +
                            "Please contact IT NOC");
                        break;
                    case MoLoginStatus.NoResponse:
                        ReceivedInvoke (moLoginResponse, command, "MOService Notification", "The server is not responding, please try again later!");
                        break;
                    default:
                        ReceivedInvoke (moLoginResponse, command, "MOService Credentials errors",
                            "Unmanaged/Undefined login response. Please contact IT NOC. " +
                            $"tid={command}");
                        break;
                }
            }
            catch (Exception ex) { }
        }