예제 #1
0
 /// <summary>
 /// 當接收到訊息之後,進行觸發
 /// </summary>
 /// <param name="msgModel">物件格式的訊息</param>
 protected void TriggerMsgReceived_Model(MQMsgModel msgModel)
 {
     if (MsgReceived_Model != null)
     {
         MsgReceived_Model(msgModel);
     }
 }
예제 #2
0
 private void QueueMsgReceived_Model(MQMsgModel msgModel)
 {
     if (this.MsgReceived_Model != null)
     {
         this.MsgReceived_Model(msgModel);
     }
 }
예제 #3
0
 public void Send(MQMsgModel msg, bool multicast = false)
 {
     if (this.client != null)
     {
         this.client.Send(msg, multicast);
     }
 }
예제 #4
0
파일: MSMQClient.cs 프로젝트: abablin/MQ
        private void MsgReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            if (this.queueReceiveLocal != null)
            {
                this.msmqMessage = this.queueReceiveLocal.EndReceive(e.AsyncResult);
                MQMsgModel msg = this.msmqMessage.Body as MQMsgModel;

                // 根據接收的資料類型,決定要觸發對應的處理程序
                if (msg == null)
                {
                    msg             = JSON.Deserialize <MQMsgModel>((string)this.msmqMessage.Body);
                    msg.ArrivedTime = DateTime.Now;

                    this.TriggerMsgReceived(JSON.SerializeDynamic(msg));
                }
                else
                {
                    msg.ArrivedTime = DateTime.Now;
                    this.TriggerMsgReceived_Model(msg);
                }

                if (!this.stopReceiving)
                {
                    this.queueReceiveLocal.BeginReceive();
                }
            }
        }
예제 #5
0
파일: MSMQClient.cs 프로젝트: abablin/MQ
        internal override void Send(ApplicationPort fromPort, ApplicationPort toPort, string toSiteName, string jsonMsg, bool multicast = false)
        {
            //this.isMulticast = multicast;
            //this.msgToSend.Body = jsonMsg;
            this.isMulticast = multicast;

            MQMsgModel msg = JSON.Deserialize <MQMsgModel>(jsonMsg);

            msg.SentTime = DateTime.Now;

            this.msgToSend.Body = JSON.SerializeDynamic(msg);

            if (this.isMulticast == false)
            {
                // 2018/08/16, 避免 foreach 在多執行緒可能發生的問題, 加上 lock
                lock (thisLock)
                {
                    this.GetTargetPorts(toPort, toSiteName);

                    foreach (KeyValuePair <MessageQueue, string> item in this.dicQueueAndPath)
                    {
                        // 發送之前檢查下是否有設定 IP,因為是內部主機,所以直接寫死192.168
                        if (item.Value.IndexOf("192.168") > -1)
                        {
                            item.Key.Path = item.Value;
                            item.Key.Send(this.msgToSend);
                        }
                        else
                        {
                            throw new Exception("MQ 主機的相關 IP 未設定,請確認");
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.multicastIP))
                {
                    // 改用 Message 物件來傳送
                    this.queueMulticast.Path = string.Format(this.queuePathMulticast, this.multicastIP);
                    this.queueMulticast.Send(this.msgToSend);
                }
                else
                {
                    throw new Exception("Message Queue 主機的 Multicast IP 未設定");
                }
            }
        }
예제 #6
0
파일: MSMQClient.cs 프로젝트: abablin/MQ
        internal override void Send(MQMsgModel msg, bool multicast = false)
        {
            this.isMulticast = multicast;
            msg.SentTime     = DateTime.Now;

            if (this.isMulticast == false)
            {
                // 2018/08/16, 避免 foreach 在多執行緒可能發生的問題, 加上 lock
                lock (thisLock)
                {
                    this.GetTargetPorts(msg.ToPort, msg.ToSiteName, msg.ToServerIP);

                    foreach (KeyValuePair <MessageQueue, string> item in this.dicQueueAndPath)
                    {
                        // 發送之前檢查下是否有設定 IP,因為是內部主機,所以直接寫死192.168
                        if (item.Value.IndexOf("192.168") > -1)
                        {
                            item.Key.Path = item.Value;
                            item.Key.Send(msg);
                        }
                        else
                        {
                            throw new Exception("MQ 主機的相關 IP 未設定,請確認");
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.multicastIP))
                {
                    this.queueMulticast.Path = string.Format(this.queuePathMulticast, this.multicastIP);
                    this.queueMulticast.Send(msg);
                }
                else
                {
                    throw new Exception("Message Queue 主機的 Multicast IP 未設定");
                }
            }
        }
예제 #7
0
        /// <summary>
        /// 發送訊息
        /// </summary>
        /// <param name="jsonMsg">JSON 格式的訊息內容</param>
        /// <param name="multicast">是否為多點傳送,預設為 false</param>
        public void Send(string jsonMsg, bool multicast = false)
        {
            if (this.client != null)
            {
                // 收到前端的訊息後,解析目的地的端口與站台名稱
                MQMsgModel msg = JSON.Deserialize <MQMsgModel>(jsonMsg);

                // 改為另開執行緒去傳送
                //Thread sendThread = new Thread(() => Send(msg.FromPort, msg.ToPort, msg.ToSiteName, jsonMsg, multicast));
                //sendThread.Start();

                // 根據是否傳入目的地主機的 IP 來決定要呼叫的方法
                if (string.IsNullOrEmpty(msg.ToServerIP))
                {
                    this.Send(msg.FromPort, msg.ToPort, msg.ToSiteQueueName, jsonMsg, multicast);
                }
                else
                {
                    this.Send(msg.FromPort, msg.ToPort, msg.ToServerIP, msg.ToSiteQueueName, jsonMsg, multicast);
                }
            }
        }
예제 #8
0
 internal virtual void Send(MQMsgModel msg, bool multicast = false)
 {
 }