예제 #1
0
        /// <summary>
        /// 重新连接方法
        /// </summary>
        internal void Reconnect()
        {
            string temp = string.Format("TCP主机地址:{0},端口号:{1}", ServerIp, ServerPort); //TCP连接的主机地址与端口

            LastErrorMessage = "TCP连接意外断开,正在尝试重连。" + temp;
            //FileClient.WriteFailureInfo(LastErrorMessage);
            try
            {
                BaseClient.Close();
                BaseClient = HoldLocalPort ? new TcpClient(LocalEndPoint) : new TcpClient();
                BaseClient.Connect(ServerIp, ServerPort);
                SetName();

                //重连次数+1,同时调用事件委托
                ReconnTimer++;
                if (ReconnTimerChanged != null)
                {
                    ReconnTimerChanged.BeginInvoke(Name, ReconnTimer, null, null);
                }

                NetStream = BaseClient.GetStream();
                if (AutoReceive)
                {
                    NetStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(TcpCallBack), this);
                }
                //FileClient.WriteFailureInfo("TCP重新连接成功。" + temp);
            }
            //假如出现异常,将错误信息写入日志并进入下一次循环
            catch (Exception e)
            {
                LastErrorMessage = string.Format("TCP重新连接失败:{0}。", e.Message) + temp;
            }
        }
예제 #2
0
 public void Close()
 {
     if (Connected)
     {
         Disconnect();
     }
     BaseClient.Client.Close();
     BaseClient.Close();
 }
예제 #3
0
        /// <summary>
        /// UDP重新连接方法
        /// </summary>
        private void TcpAutoReconnect()
        {
            while (true)
            {
                Thread.Sleep(1000);

                //假如属性提示未连接,则UDP重连线程挂起
                if (!IsConnected)
                {
                    Auto_UdpReconnect.WaitOne();
                }
                //假如属性提示已连接但实际上连接已断开,尝试重连
                //else if (IsConnected && !IsSocketConnected())
                else if (IsConnected && !IsConnected_Socket)
                {
                    string temp = string.Format("UDP主机地址:{0},端口号:{1}", ServerIp, ServerPort); //UDP连接的主机地址与端口
                    LastErrorMessage = "UDP连接意外断开,正在尝试重连。" + temp;
                    FileClient.WriteFailureInfo(LastErrorMessage);
                    try
                    {
                        BaseClient.Close();
                        BaseClient = HoldLocalPort ? new UdpClient(LocalEndPoint) : new UdpClient();
                        BaseClient.Connect(ServerIp, ServerPort);
                        SetName();

                        //重连次数+1,同时调用事件委托
                        ReconnTimer++;
                        if (ReconnTimerChanged != null)
                        {
                            ReconnTimerChanged.BeginInvoke(Name, ReconnTimer, null, null);
                        }
                        if (Connected != null)
                        {
                            Connected.BeginInvoke(Name, new EventArgs(), null, null);
                        }

                        //NetStream = BaseClient.GetStream();
                        if (AutoReceive)
                        {
                            BaseClient.BeginReceive(new AsyncCallback(ReceiveCallBack), this);
                        }
                        //NetStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(TcpCallBack), this);
                        //IsSocketConnected();
                        FileClient.WriteFailureInfo("UDP重新连接成功。" + temp);
                    }
                    //假如出现异常,将错误信息写入日志并进入下一次循环
                    catch (Exception e)
                    {
                        LastErrorMessage = string.Format("UDP重新连接失败:{0}。", e.Message) + temp;
                        FileClient.WriteExceptionInfo(e, LastErrorMessage, false);
                        continue;
                        //TODO: 是否抛出异常?
                        //throw; //假如不需要抛出异常,注释此句
                    }
                }
            }
        }
예제 #4
0
        ///// <summary>
        ///// 更新并返回TcpClient的连接状态
        ///// </summary>
        ///// <returns>假如处于连接状态,返回true,否则返回false</returns>
        //public bool IsSocketConnected()
        //{
        //    //return IsConnected_Socket = (BaseClient != null && BaseClient.IsSocketConnected());
        //    return IsConnected_Socket;
        //}

        /// <summary>
        /// 关闭UDP连接
        /// </summary>
        /// <returns>假如关闭成功,返回1,否则返回0</returns>
        public int Close()
        {
            LastErrorMessage = string.Empty;
            int result = 1;

            try
            {
                if (BaseClient != null)
                {
                    ThreadAbort(); //终止重连线程

                    //关闭流并释放资源
                    //NetStream.Close();
                    //NetStream.Dispose();
                    BaseClient.Close();
                    IsStartListening = false;
                    ServerIp         = null;
                    ServerPort       = 0;
                    IsConnected      = false;
                    //IsConnected_Socket = false;
                    //调用连接断开事件委托
                    if (Disconnected != null)
                    {
                        Disconnected.BeginInvoke(Name, new EventArgs(), null, null);
                    }

                    BaseClient.Close();
                    BaseClient = null;
                }
            }
            catch (Exception e)
            {
                LastErrorMessage = string.Format("关闭UDP连接{0}失败:{1}", Name, e.Message);
                FileClient.WriteExceptionInfo(e, LastErrorMessage, false);
                result = 0;
                throw; //假如不需要抛出异常,注释此行
            }

            return(result);
        }
예제 #5
0
        public void User_KeyDown(object sender, KeyEventArgs e)
        {
            if (model.ModelIsLoaded)
            {
                switch (e.Key)
                {
                case Key.Left:
                    direction.Horisontal = DirectionHorisontal.Left;
                    break;

                case Key.Right:
                    direction.Horisontal = DirectionHorisontal.Right;
                    break;

                case Key.Up:
                    direction.Vertical = DirectionVertical.Up;
                    break;

                case Key.Down:
                    direction.Vertical = DirectionVertical.Down;
                    break;

                case Key.F:
                    client.SendMessage(new TryPickUp(model.IDPlayer));
                    break;

                case Key.Oem1:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.Gun));
                    break;

                case Key.Oem2:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.ShotGun));
                    break;

                case Key.Oem3:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.AssaultRifle));
                    break;

                case Key.Oem4:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.GrenadeCollection));
                    break;

                case Key.R:
                    client.SendMessage(new MakeReloadWeapon(model.IDPlayer));
                    break;

                case Key.Escape:
                    client.Close();
                    break;

                case Key.NumPad1:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.Gun));
                    break;

                case Key.NumPad2:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.ShotGun));
                    break;

                case Key.NumPad3:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.AssaultRifle));
                    break;

                case Key.NumPad4:
                    client.SendMessage(new ChoiceWeapon(model.IDPlayer, TypesWeapon.GrenadeCollection));
                    break;
                }

                if (e.Key == Key.Up ||
                    e.Key == Key.Down ||
                    e.Key == Key.Left ||
                    e.Key == Key.Right)
                {
                    client.SendMessage(new GoTo(model.IDPlayer, direction));
                }
            }
        }