Esempio n. 1
0
        private void HandleHello(IPEndPoint endpoint, Message message)
        {
            var client = GetClient(endpoint);

            if (client != null)
            {
                Debug($"Client with endpoint {endpoint.Address}:{endpoint.Port} already on list ({client.Nickname}).");
                return;
            }

            var nickname = message.GetTextContent();

            using (var database = new RozmawiatorDb())
            {
                var user = database.Users.FirstOrDefault(u => u.UserName == nickname);
                if (user == null)
                {
                    Debug($"Client {nickname} with endpoint {endpoint.Address}:{endpoint.Port} does not exist in database.");
                    return;
                }
            }

            var newClient = AddClient(endpoint, nickname);

            Send(newClient, Message.Hello(Configuration.Host.Name));

            Debug($"{nickname} connected ({endpoint.Address}:{endpoint.Port})");
            ClientConnected?.Invoke(newClient);
        }
Esempio n. 2
0
        private void OnNewMessage(Client client, Message message)
        {
            switch (message.Type)
            {
            case Message.MessageType.Hello:
            case Message.MessageType.Bye:
            case Message.MessageType.KeepAlive:
            case Message.MessageType.Call:
                break;

            case Message.MessageType.DirectText:
                HandleDirectText(message);
                break;

            case Message.MessageType.CallRequest:
            case Message.MessageType.CallResponse:
                break;

            case Message.MessageType.HelloConversation:
            case Message.MessageType.ByeConversation:
            case Message.MessageType.CloseConversation:
            case Message.MessageType.Text:
            case Message.MessageType.Audio:
                break;
            }
        }
Esempio n. 3
0
 public void Send(IEnumerable <Client> receivers, Message message)
 {
     foreach (var client in receivers)
     {
         message.Receiver = client.Id;
         Send(client, message);
     }
 }
Esempio n. 4
0
 private void HandleBye(Client sender, Message message)
 {
     if (sender == null)
     {
         return;
     }
     Debug($"{sender.Nickname} disconnecting ({sender.EndPoint.Address}:{sender.EndPoint.Port})");
     RemoveClient(sender);
 }
Esempio n. 5
0
 public void SendAsClient(Client sender, IEnumerable <Client> receivers, Message message)
 {
     message.Sender = sender.Id;
     foreach (var client in receivers)
     {
         message.Receiver = client.Id;
         Send(client, message);
     }
 }
Esempio n. 6
0
        private void ListenLoop()
        {
            while (State == ListenerState.Listening)
            {
                var endpoint = new IPEndPoint(0, 0);
                var msg      = _listener.Receive(ref endpoint);

                Task.Factory.StartNew(() => HandleMessage(endpoint, Message.FromBytes(msg)));
            }
        }
Esempio n. 7
0
        private void HandleMessage(IPEndPoint endpoint, Message message)
        {
            var sender = GetClient(endpoint);

            message.Sender = sender?.Id ?? 0;
            var conversation = GetConversation(sender);

            switch (message.Type)
            {
            case Message.MessageType.Hello:
                HandleHello(endpoint, message);
                break;

            case Message.MessageType.Bye:
                HandleBye(sender, message);
                break;

            case Message.MessageType.KeepAlive:
                HandleKeepAlive(sender, message);
                break;

            case Message.MessageType.Call:
                HandleCall(sender, message);
                break;

            case Message.MessageType.CallRequest:
                break;

            case Message.MessageType.CallResponse:
                HandleCallResponse(sender, message);
                break;

            case Message.MessageType.Text:
            case Message.MessageType.Audio:
            case Message.MessageType.HelloConversation:
            case Message.MessageType.ByeConversation:
                if (sender != null)
                {
                    conversation?.HandleMessage(message);
                }
                break;

            case Message.MessageType.DirectText:
                HandleDirectText(sender, message);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            NewMessage?.Invoke(endpoint, message);
        }
Esempio n. 8
0
        private async void HandleDirectText(Message message)
        {
            var sender  = message.GetDirectTextNickname();
            var content = message.GetDirectTextContent();

            var conversation =
                PassiveConversationService.Conversations.FirstOrDefault(c => c.Participants.Select(p => p.Nickname).Contains(sender)) ??
                await ConversationService.AddConversation(ConversationType.Passive, UserService.LoggedUser.Nickname, sender);

            var conversationControl = ConversationControls.FirstOrDefault(p => p.PassiveConversation == conversation);

            if (conversationControl == null)
            {
                conversationControl = new ConversationControl
                {
                    PassiveConversation = conversation
                };
                Dispatcher.Invoke(() =>
                {
                    ConversationList.Items.Add(conversationControl);
                });
            }

            if (SelectedConversationControl.PassiveConversation != conversationControl.PassiveConversation)
            {
                conversationControl.Notify();
            }

            var messageDisplay = MessageDisplays.FirstOrDefault(m => m.Conversation == conversation);

            if (messageDisplay == null)
            {
                messageDisplay = new MessageDisplayControl(conversation);
                Dispatcher.Invoke(() =>
                {
                    MessageDisplays.Add(messageDisplay);
                });
                return;
            }

            Dispatcher.Invoke(() =>
            {
                var msg = new TextMessage(content, DateTime.Now, UserService.GetUser(sender));
                conversation.Messages.Add(msg);
                messageDisplay.AddMessageControl(msg);
            });
        }
Esempio n. 9
0
        private void HandleCall(Client sender, Message message)
        {
            var calledNickname = message.GetTextContent();
            var calledUser     = GetClient(calledNickname);

            var callRequest = new Database.Entities.CallRequest
            {
                State = CallRequestState.NoResponse
            };

            using (var database = new RozmawiatorDb())
            {
                var callee =
                    database.Users.Select(u => new { u.Id, u.UserName }).FirstOrDefault(u => u.UserName == calledNickname);
                var caller =
                    database.Users.Select(u => new { u.Id, u.UserName })
                    .FirstOrDefault(u => u.UserName == sender.Nickname);

                callRequest.CalleeId = callee?.Id ?? Guid.Empty;
                callRequest.CallerId = caller?.Id ?? Guid.Empty;
            }

            Debug($"{sender.Nickname} is trying to call {calledNickname} ({sender.EndPoint.Address}:{sender.EndPoint.Port} -> ?)");

            if (calledUser == null)
            {
                Debug($"{sender.Nickname} was trying to call {calledNickname}, but {calledNickname} is offline.");
                Send(sender, Message.CallResponse(0, 0, Message.CallResponseType.TargetIsOffline));
                SaveCall(callRequest);
                return;
            }

            var conversation = GetInitiatedConversation(sender);

            if (conversation == null)
            {
                var currentConversation = GetConversation(sender);
                currentConversation?.Disconnect(sender);
                conversation = NewConversation(sender);
            }

            SaveCall(callRequest);
            conversation.AddRequest(calledUser);
            conversation.SendRequests();
        }
Esempio n. 10
0
 private void HandleKeepAlive(Client sender, Message message)
 {
     //Debug($"{sender.Nickname} is alive ({sender.EndPoint.Address}:{sender.EndPoint.Port})");
     sender?.KeepAlive();
 }
Esempio n. 11
0
        private void HandleDirectText(Client sender, Message message)
        {
            if (sender == null)
            {
                return;
            }

            var targetNickname = message.GetDirectTextNickname();
            var targetClient   = GetClient(targetNickname);

            if (targetClient == null)
            {
                Debug($"Undelivered message from {sender.Nickname} to {targetNickname}, target client offline.");
                return;
            }

            using (var database = new RozmawiatorDb())
            {
                var conversation =
                    database.Conversations.FirstOrDefault(
                        c => c.ConversationParticipants.Any(cp => cp.User.UserName == targetNickname));

                var senderUser = database.Users.FirstOrDefault(u => u.UserName == sender.Nickname);
                if (senderUser == null)
                {
                    SendAsClient(sender, targetClient, message);
                    return;
                }

                if (conversation == null)
                {
                    var targetUser = database.Users.FirstOrDefault(u => u.UserName == targetNickname);
                    if (targetUser == null)
                    {
                        SendAsClient(sender, targetClient, message);
                        return;
                    }

                    conversation = new Database.Entities.Conversation
                    {
                        Type = ConversationType.Passive,
                        ConversationParticipants = new[]
                        {
                            new ConversationParticipant {
                                User = senderUser
                            },
                            new ConversationParticipant {
                                User = targetUser
                            }
                        }
                    };
                    database.Conversations.Add(conversation);
                }

                var msg = new Database.Entities.Message
                {
                    Conversation = conversation,
                    Content      = message.GetDirectTextContent(),
                    Timestamp    = DateTime.Now,
                    Sender       = senderUser
                };

                database.Messages.Add(msg);
                database.SaveChanges();
            }

            var forwardMessage = new Message().DirectText(sender.Nickname, message.GetDirectTextContent());

            //SendAsClient(sender, targetClient, message);
            SendAsClient(sender, targetClient, forwardMessage);
        }
Esempio n. 12
0
        private void OnNewCallRequest(Client client, ClientApi.CallRequest callRequest, Message message)
        {
            var request = new CallRequest
            {
                Id       = Guid.Empty,
                CallerId = message.Sender,
                Caller   = callRequest.CallerNickname,
                Callee   = UserService.LoggedUser.Nickname,
                State    = CallRequestState.NoResponse
            };

            DisplayCallRequest(request);
        }
Esempio n. 13
0
 public void SendAsClient(Client sender, Client receiver, Message message)
 {
     message.Sender   = sender.Id;
     message.Receiver = receiver.Id;
     Send(receiver, message);
 }
Esempio n. 14
0
 public void Send(Client receiver, Message message)
 {
     message.Receiver = receiver.Id;
     Send(receiver, message.GetBytes());
 }
Esempio n. 15
0
 private void OnConversationClose(ClientApi.Conversation conversation, Message message)
 {
     Dispatcher.Invoke(EndConversation);
 }
Esempio n. 16
0
 private void OnNewAudioSamples(ClientApi.Conversation conversation, RemoteClient remoteClient, Message message)
 {
     Player.AddSamples(message.Content, 0, message.Content.Length);
 }
Esempio n. 17
0
        private void HandleCallResponse(Client sender, Message message)
        {
            var callingClient = GetClient(message.Receiver);

            if (callingClient == null)
            {
                Debug($"{sender.Nickname} was trying to respond to call, but caller is offline.");
                Send(sender, Message.CallResponse(0, 0, Message.CallResponseType.TargetIsOffline));
                return;
            }

            var conversation = GetConversation(callingClient);

            if (conversation == null)
            {
                Debug($"{sender.Nickname} was trying to respond to {callingClient.Nickname}'s call, but the conversation no longer exists.");
                Send(sender, Message.CallResponse(0, 0, Message.CallResponseType.ExpiredCall));
                return;
            }

            var request = conversation.GetRequest(callingClient, sender);

            if (request == null)
            {
                Debug($"{sender.Nickname} was trying to respond to {callingClient.Nickname}'s call, but the request no longer exists.");
                Send(sender, Message.CallResponse(0, 0, Message.CallResponseType.ExpiredCall));
                return;
            }

            var response = request.ResolveRequest(message);



            switch (response)
            {
            case Message.CallResponseType.RequestDenied:
                Debug($"{sender.Nickname} declined {callingClient.Nickname}'s call.");
                break;

            case Message.CallResponseType.RequestAccepted:
                var currentConversation = GetConversation(sender);
                currentConversation?.Disconnect(sender);

                conversation.Join(sender);
                Debug($"{sender.Nickname} joined {callingClient.Nickname}'s conversation.");
                break;

            default:
                Debug($"Exception: Caller: {callingClient.Nickname}, Target: {sender.Nickname} - unexpected message {response}");
                break;
            }

            using (var database = new RozmawiatorDb())
            {
                var content       = message.GetTextContent();
                var lastTimestamp =
                    database.CallRequests.Where(
                        c => c.Callee.UserName == sender.Nickname).Max(c => c.Timestamp);

                /*
                 * var callRequest =
                 *  database.CallRequests.LastOrDefault(
                 *      c => c.Callee.UserName == sender.Nickname && c.Caller.UserName == content);
                 */
                var callRequest = database.CallRequests.FirstOrDefault(c => c.Timestamp == lastTimestamp);
                if (callRequest != null)
                {
                    callRequest.State = response == Message.CallResponseType.RequestAccepted
                        ? CallRequestState.RequestAccepted
                        : CallRequestState.RequestDenied;
                    database.SaveChanges();
                }
            }

            SendAsClient(sender, callingClient, Message.CallResponse(0, 0, response));
            conversation.RemoveRequests();
        }
Esempio n. 18
0
 public void Broadcast(Message message)
 {
     message.Sender   = 0;
     message.Receiver = 0;
     Send(_clients, message);
 }