Exemple #1
0
        /// <summary>
        /// 監聽伺服器狀態
        /// </summary>
        private void Listener()
        {
            // 取得端點
            EndPoint remoteEp = this.sock.RemoteEndPoint;

            byte[] receiveBytes = new byte[1023]; // 接受用 byte 陣列
            int    msgLength    = 0;              // 接收的位元組數目

            while (true)
            {
                try
                {
                    msgLength =
                        this.sock.ReceiveFrom(receiveBytes, 0, receiveBytes.Length, SocketFlags.None, ref remoteEp);
                }
                catch (Exception ex)
                {
                    this.sock.Close();
                    this.lb_Users.Items.Clear();
                    this.btn_SignIn.Enabled  = true;
                    this.btn_Giveup.Enabled  = false;
                    this.btn_SendMsg.Enabled = false;
                    this.pb_Board.Enabled    = false;
                    this.tb_UserName.Enabled = true;
                    this.th.Abort();

                    MessageBox.Show("與伺服器斷線");
                    return;
                }

                // 將傳來的 byte 陣列讀取成字串
                string msg = Encoding.Default.GetString(receiveBytes, 0, msgLength);

                // 反序列化字串訊息為 Command 物件
                TCPCommand cmd = JsonConvert.DeserializeObject <TCPCommand>(msg);

                switch (cmd.Type)
                {
                case TCPCommandType.SetUserList:
                    this.SetUserListCommand(cmd.JsonString);
                    break;

                case TCPCommandType.Chat:
                    this.tb_MsgBox.Text += $"{cmd.SendFrom} : {cmd.JsonString}" + Environment.NewLine;
                    break;

                case TCPCommandType.GameStart:
                    this.GameStartCommand(cmd.JsonString);
                    break;

                case TCPCommandType.PlaceAPiece:
                    this.PlaceAPieceCommand(cmd.JsonString);
                    break;

                case TCPCommandType.GiveUp:
                    this.GiveUpCommand(cmd.JsonString);
                    break;
                }
            }
        }
 public void ReloadFlies()
 {
     try
     {
         listcomm = new List <CommandItem>();
         String[] strfilelist = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "command");
         foreach (string file in strfilelist)
         {
             Assembly ab    = Assembly.LoadFile(file);
             Type[]   types = ab.GetExportedTypes();
             foreach (Type t in types)
             {
                 if (t.IsSubclassOf(typeof(TCPCommand)))
                 {
                     CommandItem ci  = new CommandItem();
                     object      obj = ab.CreateInstance(t.FullName);
                     TCPCommand  Ic  = obj as TCPCommand;
                     ci.MyICommand = Ic;
                     ci.CommName   = Ic.Getcommand();
                     Ic.SetGlobalQueueTable(qt);
                     GetAttributeInfo(Ic, obj.GetType(), obj);
                     listcomm.Add(ci);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         if (EventMylog != null)
         {
             EventMylog("加载异常", ex.Message);
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// 接收玩家認輸指令
        /// </summary>
        /// <param name="cmd"></param>
        private void GiveUpCommand(TCPCommand cmd)
        {
            Socket sock = this.GetTargetSocket(cmd);

            if (sock == null)
            {
                return;
            }

            string cmdStr = this.GetCommandStr(cmd.Type, cmd.SendFrom, cmd.JsonString, false);

            this.SendTo(cmdStr, sock);
        }
Exemple #4
0
        /// <summary>
        /// 取得序列化的指令物件
        /// </summary>
        /// <param name="type">指令類別</param>
        /// <param name="obj">需要序列化的資料</param>
        /// <param name="needSerial">是否需要序列化</param>
        /// <returns></returns>
        private string GetCommandStr(TCPCommandType type, string sendFrom, object obj, bool needSerial)
        {
            TCPCommand cmd = new TCPCommand();

            cmd.Type       = type;
            cmd.SendFrom   = sendFrom;
            cmd.JsonString = obj.ToString();
            if (needSerial)
            {
                cmd.JsonString = JsonConvert.SerializeObject(obj);
            }

            return(JsonConvert.SerializeObject(cmd));
        }
 public void GetAttributeInfo(TCPCommand Ic, Type t, object obj)
 {
     foreach (MethodInfo mi in t.GetMethods())
     {
         InstallFun myattribute = (InstallFun)Attribute.GetCustomAttribute(mi, typeof(InstallFun));
         if (myattribute == null)
         {
         }
         else
         {
             Delegate del = Delegate.CreateDelegate(typeof(RequestData), obj, mi, true);
             Ic.Bm.AddListen(mi.Name, del as RequestData, myattribute.Type);
         }
     }
 }
Exemple #6
0
        /// <summary>
        /// 客戶端監聽事件
        /// </summary>
        private void ClientListener()
        {
            Socket sock   = this.client;
            Thread thread = this.th_Client;

            while (true)
            {
                try
                {
                    byte[] receiveByte = new byte[1023];

                    int msgLength = sock.Receive(receiveByte);

                    // 將傳來的 byte 陣列讀取成字串
                    string msg = Encoding.Default.GetString(receiveByte, 0, msgLength);

                    // 反序列化字串訊息為 Command 物件
                    TCPCommand cmd = JsonConvert.DeserializeObject <TCPCommand>(msg);

                    switch (cmd.Type)
                    {
                    case TCPCommandType.SignIn:
                        this.SignInCommand(cmd.JsonString, sock);
                        break;

                    case TCPCommandType.SignOut:
                        this.SignOutCommand(cmd.JsonString, sock, thread);
                        break;

                    case TCPCommandType.Chat:
                        this.ChatCommand(cmd);
                        break;

                    case TCPCommandType.PlaceAPiece:
                        this.PlaceAPieceCommand(cmd);
                        break;

                    case TCPCommandType.GiveUp:
                        this.GiveUpCommand(cmd);
                        break;
                    }
                }
                catch (Exception ex) { }
            }
        }
Exemple #7
0
        /// <summary>
        /// 取得對象 Socket
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private Socket GetTargetSocket(TCPCommand cmd)
        {
            string otherUserName = string.Empty;

            foreach (string userName in this.ht.Keys)
            {
                if (userName != cmd.SendFrom)
                {
                    otherUserName = userName;
                }
            }

            if (otherUserName == string.Empty || !this.ht.Contains(otherUserName))
            {
                return(null);
            }

            return((Socket)this.ht[otherUserName]);
        }
        public  void GetAttributeInfo(TCPCommand Ic,Type t,object obj)
        {

            foreach (MethodInfo mi in t.GetMethods())
            {

                InstallFun myattribute = (InstallFun)Attribute.GetCustomAttribute(mi, typeof(InstallFun));
                if (myattribute == null)
                {

                }
                else
                {

                   
                    Delegate del = Delegate.CreateDelegate(typeof(RequestData), obj, mi,true);
                    Ic.Bm.AddListen(mi.Name, del as RequestData, myattribute.Type);


                }
            }
        }