Esempio n. 1
0
        public void SendMessageToSelectedFriends()
        {
            for (int i = 0; i < users_count; i++)
            {
                if (!users_api[i].IsAuthorized)
                {
                    continue;
                }

                List <FriendsListItem>   items    = applicationContract.GetFriendsListItems();
                List <SendMessageStatus> statuses = new List <SendMessageStatus>();

                MessagesSendParams sendParams = new MessagesSendParams();

                foreach (FriendsListItem item in items)
                {
                    if (!item.SocialNetworkName.Equals(getSocialNetworkName()))
                    {
                        continue;
                    }
                    if (!item.IsChecked)
                    {
                        continue;
                    }
                    if (item.User.ID != Convert.ToString(users_api[i].UserId))
                    {
                        continue;
                    }

                    sendParams.UserId  = Convert.ToInt64(item.Friend.ID);
                    sendParams.Message = applicationContract.GetMessage();

                    SendMessageStatus status = new SendMessageStatus();
                    status.SocialNetworkName = getSocialNetworkName();
                    status.UserNameTo        = item.Friend.Name;
                    status.UserNameFrom      = getUserAccountName(i);
                    status.IsMessageSended   = true;

                    try
                    {
                        users_api[i].Messages.Send(sendParams);
                    }
                    catch (VkApiException ex)
                    {
                        status.IsMessageSended = false;
                    }

                    statuses.Add(status);
                }
                applicationContract.AddSendMessageStatuses(statuses);
            }
        }
Esempio n. 2
0
        public void SendMessageToSelectedFriends()
        {
            foreach (SlackHelper user_helper in users_helpers)
            {
                if (!user_helper.IsAuthorized)
                {
                    continue;
                }

                Responses.SlackIMListResponse slackIMListResponse = user_helper.Im_List();

                if (!slackIMListResponse.Ok)
                {
                    continue;
                }

                List <Models.SlackIM> ims = slackIMListResponse.IMs?.ToList();

                if (ims == null)
                {
                    continue;
                }

                List <FriendsListItem>   friends_items = applicationContract.GetFriendsListItems();
                List <SendMessageStatus> statuses      = new List <SendMessageStatus>();

                foreach (FriendsListItem friend_item in friends_items)
                {
                    if (!friend_item.SocialNetworkName.Equals(getSocialNetworkName()))
                    {
                        continue;
                    }
                    if (!friend_item.IsChecked)
                    {
                        continue;
                    }
                    if (friend_item.User.ID != user_helper.User.ID)
                    {
                        continue;
                    }

                    String channel_id = null;

                    foreach (Models.SlackIM im in ims)
                    {
                        if (friend_item.Friend.ID == im.User)
                        {
                            channel_id = im.ID;
                            break;
                        }
                    }

                    if (channel_id == null)
                    {
                        continue;
                    }

                    SendMessageStatus status = new SendMessageStatus();
                    status.SocialNetworkName = getSocialNetworkName();
                    status.UserNameTo        = friend_item.Friend.Name;
                    status.UserNameFrom      = friend_item.User.Name;
                    status.IsMessageSended   = user_helper.Chat_MeMessage(channel_id, applicationContract.GetMessage()).Ok;
                    statuses.Add(status);
                }

                applicationContract.AddSendMessageStatuses(statuses);
            }
        }
Esempio n. 3
0
        // TODO: build this kind of mechanism into handling a queue of tasks:
        // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/start-multiple-async-tasks-and-process-them-as-they-complete
        // https://devblogs.microsoft.com/pfxteam/processing-tasks-as-they-complete/
        public SendMessageStatus SendMessage(CoapMessage message)
        {
            message.DecomposeUri(message.Uri);

            message.MessageID  = (ushort)(idhash[0] << 8);
            message.MessageID |= (ushort)idhash[1];

            // prepare hasher for next message ID
            idhash = hasher.ComputeHash(idhash);

            SendMessageStatus status = SendMessageStatus.BusyQueued;

            // TODO: move this to something that will check in the background - not as part of send! - check for any messages that are complete
            List <Task> toRemove = new List <Task>();

            foreach (Task t in messagesInProgress)
            {
                if (t.IsCompleted || t.IsCanceled || t.IsFaulted)
                {
                    // TODO: do a bit more than this, handle errors
                    toRemove.Add(t);
                }
            }
            foreach (Task t in toRemove)
            {
                messagesInProgress.Remove(t);
            }

            // TODO: if busy sending enqueue the message, make max configurable
            if (messagesInProgress.Count > 100)
            {
                // too busy, queue the message and move on
                outgoingQueue.Enqueue(message);
                status = SendMessageStatus.BusyQueued;
            }
            else
            {
                // TODO: generate tokens and whatever needed to fulfill the protocol

                // encode the message into a byte array
                byte[] datagram = message.ToByteArray();

                // attempt to send the message
                Task <int> t = null;

                t = endpoint.SendAsync(datagram, datagram.Length, message.Destination);

                if (t.IsCompleted)
                {
                    status = SendMessageStatus.SentImmediately;
                }
                else
                {
                    Console.WriteLine($"Didn't send immediately so putting on progress list to check");
                    status = SendMessageStatus.Sending;
                    messagesInProgress.Add(t);
                }
            }

            // TODO: kick queue

            return(status);
        }
 public SendChatMessageResponse(SendMessageStatus status) : base("send_chat_message")
 {
     GameId = Guid.Empty;
     Status = status;
 }
 public SendChatMessageResponse(Guid gameId, SendMessageStatus status) : base("send_chat_message")
 {
     GameId = gameId;
     Status = status;
 }