⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coordinator.cs

📁 ICE3.3.0--聊天程序服务器端demo
💻 CS
📖 第 1 页 / 共 2 页
字号:
                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Glacier2.CannotCreateSessionException):\n" + ex.reason);
                throw ex;            }            catch(Glacier2.PermissionDeniedException ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Glacier2.PermissionDeniedException):\n" + ex.reason);
                throw ex;            }            catch(Ice.ConnectionRefusedException ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Ice.ConnectionRefusedException).\n" +                         "Please check your server:\n" +                         "a Glacier2 router should be running on " + info.Host + " and\n" +                         "listening on port 4064, and the server firewall needs\n" +                         "to be configured to allow TCP connections to this port.");
                throw ex;            }            catch(Ice.TimeoutException ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Ice.TimeoutException)\n." +                         "Please check your server:\n" +                         "a Glacier2 router should be running on " + info.Host + " and\n" +                         "listening on port 4064, and the server firewall needs\n" +                         "to be configured to allow TCP connections to this port.");
                throw ex;            }            catch(Ice.SocketException ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Ice.SocketException ).\n" +                         "Please check your server:\n" +                         "a Glacier2 router should be running on " + info.Host + " and\n" +                         "listening on port 4064, and the server firewall needs\n" +                         "to be configured to allow TCP connections to this port.");
                throw ex;            }            catch(Ice.DNSException ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed (Ice.DNSException ).\n" +                         "Please check your DNS configuration:\n" +                         "Host \"" + info.Host + "\" should point to the ip address of a Glacier2 router\n" +                         "that should be listening on port 4064, and the server firewall needs\n" +                         "to be configured to allow TCP connections to this port.");
                throw ex;            }            catch(Exception ex)            {                if(_communicator != null)                {                    _communicator.destroy();                    _communicator = null;                }                setError("Login failed:\n" + ex.ToString());
                throw ex;
            }
        }

        private void loginCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)delegate()
                    {
                        setState(ClientState.Connected);
                    });
            }
        }

        private void logoutDoWork(object sender, DoWorkEventArgs e)
        {
            lock(this)
            {
                if(_session != null)
                {
                    _session.close();
                    _session = null;
                }

                if(_communicator != null)
                {
                    _communicator.destroy();
                    _communicator = null;
                }
            }
        }

        public void closeSession()
        {

            if(_session != null) {
            _session.close();
            _session = null;
            }

            if(_communicator != null) {
            _communicator.destroy();
            _communicator = null;
            }
        }

        private void logoutCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Error != null)
            {
                setError(e.Error.ToString());
            }
            else
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)delegate()
                {
                    setState(ClientState.Disconnected);
                });
            }
        }

        public void setState(ClientState state)
        {
            if(state == ClientState.Disconnected)
            {
                _users.Clear();
            }

            if(state == ClientState.Connecting || state == ClientState.Disconnecting)
            {
                Mouse.OverrideCursor = Cursors.Wait;
            }
            else
            {
                Mouse.OverrideCursor = Cursors.Arrow;
            }
            _model.State = state;
        }

        public void setError(string error)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                if(_model.State == ClientState.Connected || _model.State == ClientState.ConnectionLost)
                {
                    _chatView.appendError(error);
                    setState(ClientState.ConnectionLost);   
                }
                else
                {
                    setState(ClientState.Disconnected);
                    ErrorView errorView = new ErrorView();
                    errorView.ResizeMode = ResizeMode.NoResize;
                    errorView.setError(error);
                    ChatUtils.centerWindow(errorView,_mainView);
                    errorView.ShowDialog();
                }
            });
        }

        public string getUsername()
        {
            return _username;
        }

        public void appendMessage(string message)
        {
            if(_chatView != null)
            {
                _chatView.appendMessage(message);
            }
        }

        public void setMainView(MainView mainView)
        {
            _mainView = mainView;
        }

        public void storeWindowPrefs()
        {
            _mainView.storeWindowPrefs();
        }

        //
        // AMI implementation of callback send operation.
        // 
        public class AMI_ChatSession_sendI : Chat.AMI_ChatSession_send
        {
            public AMI_ChatSession_sendI(Coordinator coordinator, string name, string message)
            {
                _coordinator = coordinator;
                _name = name;
                _message = message;
            }

            public override void ice_response(long timestampt)
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
                {
                    _coordinator.userSayEvent(timestampt, _name, _message);
                });
            }

            public override void ice_exception(Ice.Exception ex)
            {
                if(ex is Chat.InvalidMessageException)
                {
                    Chat.InvalidMessageException e = (Chat.InvalidMessageException)ex;
                    System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                                                                              (Action)delegate()
                    {
                        _coordinator.appendMessage("<system-message> - " + e.reason + Environment.NewLine);
                    });
                }
                else
                {
                    System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                              (Action) delegate()
                    {
                        _coordinator.closeSession();
                        _coordinator.setError("<system-message> - The connection with the server was unexpectedly lost.\n" +  ex.ToString() +                             "\nYou can try login again from the File menu.");
                    });
                }
            }
            private Coordinator _coordinator = null;
            private string _name = "";
            private string _message = "";
        }

        private App _app = null;
        private string[] _args = null;
        private Ice.Communicator _communicator = null;
        private ChatView _chatView = null;
        private Session _session = null;
        private UserList _users = null;
        private ChatModel _model = null;
        private MainView _mainView = null;
        private BackgroundWorker _loginWorker = null;
        private BackgroundWorker _logoutWorker = null;
        private string _username = "";
    }
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -