Esempio n. 1
0
        public Shell(IEventAggregator eventAggregator, IModuleManager moduleManager)
        {
            InitializeComponent();

            this.receive_Aggregator = eventAggregator;
            this.send_Aggregator    = eventAggregator;

            this.moduleManager = moduleManager;

            //定义消息结构
            receiveMsgOrder = new ReceiveMsgOrder();
            //获取服务器端参数
            configModel = ConfigAccess.GetConfig();
            //获取客户端版本信息
            versionModel = VersionAccess.GetConfig();
            //获取连接信息类的单例
            connectParam = ConnectParam.GetInstance();
            //获取日志记录实例
            this.ilogger = ILogger.GetInstance();

            //订阅接收信息事件
            RequestEvent();

            //定义接收消息线程
            connectParam.ListenerMsgThread = new Thread(ListenerMsgThreadMethod);
            connectParam.ListenerMsgThread.IsBackground = true;

            //先建立连接及SSL通道,再发送
            ConnectToServer(MessageTypes.UPD + versionModel.AppVs + MessageTypes.NSP + versionModel.UpdateTime, //确认更新
                            configModel.LoginIP, int.Parse(configModel.LoginPort));
        }
Esempio n. 2
0
        /// <summary>
        /// 连接服务器异步回调
        /// </summary>
        /// <param name="message">发送的信息</param>
        public void ConnectCallBackF(IAsyncResult ar)
        {
            ConnectParam connectParam = (ConnectParam)ar.AsyncState;

            try
            {
                connectParam.tcpClient.EndConnect(ar);
                connectParam.IsConnect = true;

                connectParam.GetTcpClient();

                //连接成功,然后开启线程等待接收消息
                connectParam.ListenerMsgThread.Start(connectParam);

                //先建立SSL通道(利用公钥将对称key加密发给服务器)
                AsyncSendMessage(connectParam, RSAProcessor.RSAEncrypt(configModel.PublicKey, connectParam.RandomKey) + MessageTypes.KEY);

                //发送第一条用对称key加密的信息
                AsyncSendMessage(connectParam, RijndaelProcessor.EncryptString(connectParam.Msg, connectParam.RandomKey));
            }
            catch
            {
                App.Current.Dispatcher.Invoke(new Action(() =>
                {
                    StoryboardManager.StopStoryboard("Story_Login");
                    XWT.MessageBox.Show("未能连接到服务器,请检查网络状态!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }), System.Windows.Threading.DispatcherPriority.Normal);

                if (connectParam.tcpClient != null)
                {
                    connectParam.tcpClient = null;
                }
                connectParam.IsConnect = false;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static ConnectParam GetInstance()
        {
            if (instance == null)
            {
                instance = new ConnectParam();
            }

            return(instance);
        }
Esempio n. 4
0
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConnect(ConnectParam connectParam)
        {
            connectParam.Close();

            connectParam.ListenerMsgThread = new Thread(ListenerMsgThreadMethod);
            connectParam.ListenerMsgThread.IsBackground = true;
            //ListenerMsgThread.Start();

            connectParam.IsExit = false;
        }
Esempio n. 5
0
 /// <summary>
 /// 向服务器发送数据
 /// </summary>
 /// <param name="message">发送的信息</param>
 public void AsyncSendMessage(ConnectParam connectParam, string message)
 {
     try
     {
         connectParam.sw.WriteLine(message);
         connectParam.sw.Flush();
     }
     catch
     {
         App.Current.Dispatcher.Invoke(new Action(() =>
         {
             StoryboardManager.StopStoryboard("Story_Login");
             XWT.MessageBox.Show("数据发送失败,请重试!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
         }), System.Windows.Threading.DispatcherPriority.Normal);
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 侦听信息命令线程方法
        /// </summary>
        private void ListenerMsgThreadMethod(object connectState)
        {
            ConnectParam connectParam  = connectState as ConnectParam;
            string       receiveString = null;

            while (connectParam.IsExit == false)
            {
                try
                {
                    receiveString = connectParam.sr.ReadLine();
                }
                catch (Exception e)
                {
                    if (connectParam.IsExit == false)
                    {
                        App.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            //关闭连接
                            CloseConnect(connectParam);

                            XWT.MessageBox.Show("与服务器断开连接!\n错误:" + e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }), System.Windows.Threading.DispatcherPriority.Normal);
                    }
                    break;
                }
                if (receiveString != null)
                {
                    receiveString = RijndaelProcessor.DencryptString(receiveString, connectParam.RandomKey);

                    //  ilogger.Logger(string.Format("接收到的数据:{0}", receiveString));

                    //解密、分割
                    string[] Msg = receiveString.Split(MessageTypes.PSP.ToCharArray());

                    //存储
                    receiveMsgOrder.ModuleType = Msg[0];
                    receiveMsgOrder.Sign       = int.Parse(Msg[1]);
                    receiveMsgOrder.MsgContent = Msg[2];

                    if (receiveMsgOrder.ModuleType == MessageTypes.UPD)
                    {
                        DealUpdate(receiveMsgOrder.MsgContent);
                    }
                    else
                    {
                        //广播
                        receive_Aggregator.GetEvent <ReceiveMsgEvent>().Publish(new ReceiveMsgOrder {
                            ModuleType = Msg[0], Sign = int.Parse(Msg[1]), MsgContent = Msg[2]
                        });

                        //连接成功,但操作异常
                        if (receiveMsgOrder.Sign == 0 && receiveMsgOrder.ModuleType == MessageTypes.CON)
                        {
                            break;
                        }

                        //登陆成功,返回“账号+昵称+头像”,并切换到大厅服务器
                        if (receiveMsgOrder.Sign == 1 && receiveMsgOrder.ModuleType == MessageTypes.LOG)
                        {
                            CloseConnect(connectParam);
                            //建立新的连接及SSL通道,然后发送信息(CON++ID+账号)
                            ConnectToServer(MessageTypes.CON + receiveMsgOrder.MsgContent.Split(MessageTypes.NSP.ToCharArray())[0] + MessageTypes.NSP + receiveMsgOrder.MsgContent.Split(MessageTypes.NSP.ToCharArray())[1], configModel.HallIP, int.Parse(configModel.HallPort));

                            break;
                        }
                    }
                }
            }
        }