void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
 {
     this.ErrorGuid = Guid.NewGuid();
     FaultException<ExceptionDetail> faultException = new FaultException<ExceptionDetail>(new ExceptionDetail(error) { Message = this.ErrorGuid.ToString() }, new FaultReason("ServiceError"));
     MessageFault messageFault = faultException.CreateMessageFault();
     fault = Message.CreateMessage(version, messageFault, faultException.Action);
 }
Example #2
0
 public void SendMessage(Message message)
 {
     foreach (var toId in message.ToList)
     {
         Publish(toId, message);
     }
 }
 public override void Consume(Message msg)
 {
     if (msg is WorkMessage)
     {
         works.Enqueue((msg as WorkMessage).WorkId);
         if (!this.IsPlanning)
             this.MoveToPlanningJobs();
     }
 }
Example #4
0
        public void SendMessage(Message message)
        {
            var canSend = message != null && message.ToList != null;
            if (!canSend) return;

            foreach (var toId in message.ToList)
            {
                Publish(toId, message);
            }
        }
Example #5
0
        public void receive_observer_message()
        {
            // Setup
            var viewModel = new ViewModel();
            viewModel.Observer = new Observer();
            var observer = viewModel.Observer;
            var provider = viewModel.Provider;
            var message = new Message(observer.Id, new List<string>() { provider.Id }, SOME_TEXT);

            // Test
            observer.Send(message);

            // Verify
            var expected = provider.Messages.Count() == 1;
            Assert.IsTrue(expected);
        }
Example #6
0
 public void Send(string destination, Message msg)
 {
     Send(new TransportMessage(destination, msg));
 }
 public void Send(Message message)
 {
     _messageServer.SendMessage(message);
 }
Example #8
0
        /// <summary>
        /// 插入新的消息,插入消息后将查询m_Listeners中是否有符合条件的监听器,如存在,同时将消息发送出去
        /// </summary>
        public Message NewMessage(int receiver, int sender, String content, Hashtable data, bool isTemp)
        {
            AccountInfo senderInfo = AccountImpl.Instance.GetUserInfo(sender);
            AccountInfo receiverInfo = AccountImpl.Instance.GetUserInfo(receiver);
            if (senderInfo.ID == AccountImpl.AdminID || receiverInfo.ID == AccountImpl.AdminID) throw new Exception("系统管理员(admin) 不能发送或接受即时消息!");
            if (senderInfo == null || receiverInfo == null) throw new Exception("无效的消息接收者,该用户可能已被删除!");
            int key = 0;
            lock (m_Lock)
            {
                key = ++m_MaxKey;
            }
            if ((senderInfo.IsTemp || receiverInfo.IsTemp) || Custom.ApplicationInfo.FilterHtml) content = HtmlUtil.ReplaceHtml(content);
            MsgAccessoryEval eval = new MsgAccessoryEval(key, receiverInfo.ID, senderInfo.ID, data);
            Regex reg = new Regex("{Accessory [^\f\n\r\t\v<>]+}");
            content = reg.Replace(content, eval.Replace);
            lock (m_Lock)
            {
                Message message = new Message(
                    senderInfo, receiverInfo,
                    content, new DateTime((DateTime.Now.Ticks / 10000) * 10000), key
                );

                List<Message> messages = new List<Message>();
                messages.Add(message);

                if (isTemp)
                {
                    message.IsTemp = isTemp;
                }

                if (receiverInfo.Type == 1 && receiverInfo.SubType == 3)
                {
                    string cmdData = Utility.RenderHashJson(
                        "Peer", senderInfo.ID,
                        "Message", message,
                        "Users", Utility.GetRelUsers(message)
                    );

                    AccountInfo[] users = GetChatRoomUsers(receiverInfo.ID);

                    if (senderInfo.ID != receiverInfo.Creator)
                    {
                        AccountInfo creatorInfo = AccountImpl.Instance.GetUserInfo(receiverInfo.Creator);
                        SessionManagement.Instance.Send(creatorInfo.ID, "GLOBAL:IM_MESSAGE_NOTIFY", cmdData);
                    }

                    foreach (AccountInfo acc in users)
                    {
                        if (acc.ID != senderInfo.ID) SessionManagement.Instance.Send(acc.ID, "GLOBAL:IM_MESSAGE_NOTIFY", cmdData);
                    }
                }
                else
                {
                    if (receiverInfo.Type == 0)
                    {
                        if (SessionManagement.Instance.IsOnline(receiverInfo.ID))
                        {
                            try
                            {
                                string cmdData = Utility.RenderHashJson(
                                    "Peer", senderInfo.ID,
                                    "Message", message,
                                    "Users", Utility.GetRelUsers(message)
                                );

                                SessionManagement.Instance.Send(receiverInfo.ID, "GLOBAL:IM_MESSAGE_NOTIFY", cmdData);
                            }
                            catch
                            {
                            }
                        }
                    }
                    else
                    {
                        AccountInfo groupInfo = receiverInfo;
                        foreach (int member in groupInfo.Friends)
                        {
                            try
                            {
                                AccountInfo memberInfo = AccountImpl.Instance.GetUserInfo(member);
                                if (senderInfo.ID != AccountImpl.AdminstratorID && memberInfo.ID == senderInfo.ID) continue;
                                if (memberInfo.ID != 3)
                                {
                                    if (SessionManagement.Instance.IsOnline(memberInfo.ID))
                                    {
                                        string cmdData = Utility.RenderHashJson(
                                            "Peer", groupInfo.ID,
                                            "Message", message,
                                            "Users", Utility.GetRelUsers(message)
                                        );

                                        SessionManagement.Instance.Send(memberInfo.ID, "GLOBAL:IM_MESSAGE_NOTIFY", cmdData);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }

                    if (!isTemp)
                    {
                        MessageCacheManagement.Instance.Insert(receiver, message);

                        if (MessageCacheManagement.Instance.Count >= MAX_CACHE_COUNT)
                        {
                            WriteCache();
                        }
                    }
                }
                return message;
            }
        }
Example #9
0
        /// <summary>
        /// 在缓存中插入一条消息
        /// </summary>
        /// <param name="user"></param>
        /// <param name="msg"></param>
        public void Insert(int user, Message msg)
        {
            List<Message> userMsgs = null;

            lock (m_Cache)
            {
                userMsgs = GetUserMessageCache(user);
            }

            lock (userMsgs)
            {
                userMsgs.Add(msg);
                m_Count++;
            }
        }
Example #10
0
		List<Message> IMessageStorage.GetMessages(int user, int peer, DateTime from, DateTime to, ref int page, int pagesize, out int pagecount, int msgid, string content)
		{
			SqlConnection conn = new SqlConnection(ConnectionString);
			conn.Open();
			try
			{

				if (from == null) from = new DateTime(2000, 1, 1);
				SqlCommand cmd = new SqlCommand("GetMessages", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("user", DbType.Int32).Value = user;
				cmd.Parameters.Add("peer", DbType.Int32).Value = peer;
				cmd.Parameters.Add("from", DbType.DateTime).Value = from;
				cmd.Parameters.Add("to", DbType.DateTime).Value = to;
				cmd.Parameters.Add("pagesize", DbType.Int32).Value = pagesize;
				cmd.Parameters.Add("msgid", DbType.Int32).Value = msgid;
				cmd.Parameters.Add("content", SqlDbType.NVarChar, 1024).Value = content;

				SqlParameter param_page = cmd.Parameters.Add("page", DbType.Int32);
				param_page.Direction = ParameterDirection.InputOutput;
				param_page.Value = page;

				SqlParameter param_pagecount = cmd.Parameters.Add("pagecount", DbType.Int32);
				param_pagecount.Direction = ParameterDirection.Output;

				List<Message> messages = new List<Message>();
				SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleResult);
				try
				{
					while (reader.Read())
					{
						Message msg = new Message(
							AccountImpl.Instance.GetUserInfo(Convert.ToInt32(reader[2])),
							AccountImpl.Instance.GetUserInfo(Convert.ToInt32(reader[1])),
							reader.GetString(3), Convert.ToDateTime(reader[4]), Convert.ToInt32(reader[0])
						);
						messages.Add(msg);
					}
				}
				finally
				{
					reader.Close();
				}
				page = Convert.ToInt32(param_page.Value);
				pagecount = Convert.ToInt32(param_pagecount.Value);
				return messages;
			}
			finally
			{
				conn.Close();
			}
		}
Example #11
0
		List<Message> IMessageStorage.Find(int receiver, int sender, Nullable<DateTime> from)
		{
			SqlConnection conn = new SqlConnection(ConnectionString);
			conn.Open();
			try
			{

				if (from == null) from = new DateTime(2000, 1, 1);
				SqlCommand cmd = new SqlCommand("FindMessages", conn);
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("user", DbType.Int32).Value = receiver;
				cmd.Parameters.Add("peer", DbType.Int32).Value = sender;
				cmd.Parameters.Add("from", DbType.DateTime).Value = from.Value;

				List<Message> messages = new List<Message>();
				SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleResult);
				try
				{
					while (reader.Read())
					{
						Message msg = new Message(
							AccountImpl.Instance.GetUserInfo(Convert.ToInt32(reader[2])),
							AccountImpl.Instance.GetUserInfo(Convert.ToInt32(reader[1])),
							reader.GetString(3), Convert.ToDateTime(reader[4]), Convert.ToInt32(reader[0])
						);
						messages.Add(msg);
					}
				}
				finally
				{
					reader.Close();
				}
				return messages;
			}
			finally
			{
				conn.Close();
			}
		}
Example #12
0
        private void Handle(Message message)
        {
            if (message == null || CurrentTerm > message.Term)
                return;

            if (CurrentTerm < message.Term && message.From != Host)
            {
                CurrentTerm = message.Term;
                
                votedFor = string.Empty;
                votesResponded.Clear();
                votesGranted.Clear();

                TransitionTo(NodeRole.Follower);

                _lastLeaderHeartbeatTime = DateTime.UtcNow;
                _pipeline.RequestMissingLeaderTimeout(_lastLeaderHeartbeatTime);
            }
            
            if (message is RequestVoteRequest)
            {
                if (string.IsNullOrEmpty(votedFor))
                {
                    votedFor = message.From;
                    _fll.Send(message.From, new RequestVoteResponse() { Term = CurrentTerm, Granted = VoteGranted.Granted });
                }
                else
                {
                    _logger.Info(Host + " Rejected " + message.From + " already voted for " + votedFor + " term " + message.Term);
                    _fll.Send(message.From, new RequestVoteResponse() { Term = CurrentTerm, Granted = VoteGranted.TermStale });
                }
            }

            if (message is RequestVoteResponse)
            {
                votesResponded.Add(message.From);

                var vote = (RequestVoteResponse) message;

                if (vote.Granted == VoteGranted.Granted)
                {
                    _logger.Info(Host + " Granted by " + message.From);
                    votesGranted.Add(message.From);
                }

                if (State == NodeRole.Leader)
                    return;

                if (votesGranted.Count >= N + 1)
                {
                    TransitionTo(NodeRole.Leader);

                    foreach (var member in _members)
                    {
                        _logger.Info(Host + " Send hartbeat to " + member + " term " + CurrentTerm);
                        _fll.Send(member, new AppendEntriesRequest() { Term = CurrentTerm });
                    }

                    _pipeline.RequestHartbeatTimeout();
                }
            }

            if (message is AppendEntriesRequest)
            {
                _fll.Send(message.From, new AppendEntriesResponse() { Term = CurrentTerm });
                
                _lastLeaderHeartbeatTime = DateTime.UtcNow;
                _pipeline.RequestMissingLeaderTimeout(_lastLeaderHeartbeatTime);
            }

            if (message is AppendEntriesResponse)
            {
                appendEntryReplay.Add(message.From);
                _logger.Debug(Host + " Count reply " + String.Join(" ", appendEntryReplay));
            }
        }
 /// <summary>
 /// This method is called after processing a method on the server side and just
 /// before sending the response to the client.
 /// </summary>
 /// <param name="reply"></param>
 /// <param name="correlationState"></param>
 public void BeforeSendReply(ref Message reply, object correlationState)
 {
 }
 /// <summary>
 /// This method is called on the server when a request is received from the client.
 /// </summary>
 /// <param name="request"></param>
 /// <param name="channel"></param>
 /// <param name="instanceContext"></param>
 /// <returns></returns>
 public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
 {
     var test = OperationContext.Current.IncomingMessageProperties;
     return null;
 }