private void NsConnectionOnObjectReceivedEvent(NSBaseMessage nsBaseMessage)
 {
     if (nsBaseMessage is ConnectToFriendResponse)
     {
         var resp = (ConnectToFriendResponse) nsBaseMessage;
         var address = resp.Address.ToPeerAddress();
         var conn = (ClientPeerConnection) _connections.First(c => c.UserId == resp.UserId);
         Task.Factory.StartNew(() => conn.Connect(address))
             .ContinueWith(t => ConnectedToFriendEvent?.Invoke(resp.UserId))
             .ContinueWith(t => conn.ReadMessages());
     }
     else if (nsBaseMessage is AllowFriendToConnectRequest)
     {
         var req = (AllowFriendToConnectRequest)nsBaseMessage;
         var address = req.Address.ToPeerAddress();                
         var conn = new ServerPeerConnection(_superPeerEndPoint, MessageReceivedEvent, ConnectionClosedEvent) {UserId = req.UserId};
         conn.Run();
         _connections.Add(conn);
         Task.Factory.StartNew(() => conn.AllowConnection(address))
             .ContinueWith(t => conn.ReadMessages());
         //MessageBox.Show("P2PConMng - Sending allow friend to connect resp to " + req.UserId);
         _nsConnection.SendAllowFriendToConnectResponse(req.UserId, conn.GetPeerAddress());                    
         //Task.Factory.StartNew(() => conn.ReadMessages());                
     }
 }
Esempio n. 2
0
        private void ProcessMessage(NSBaseMessage obj, IProgress<IJob> progress)
        {            
            if (obj is ConnectResponse)
            {

            }
            else if (obj is KeepAliveResponse)
            {
                MessageBox.Show("keep alive response received");
            }
            else if (obj is GetOnlineContactsResponse)
            {
                var resp = (GetOnlineContactsResponse) obj;
                IJob job = new SetFriendsOnlineJob(resp.Contacts.ToArray());
                progress.Report(job);
            }
            else if (obj is NotifyClientOnlineRequest)
            {
                var req = (NotifyClientOnlineRequest) obj;
                IJob job = new SetFriendsOnlineJob(req.Contact);
                progress.Report(job);
            }
            else if (obj is NotifyClientOfflineRequest)
            {
                var req = (NotifyClientOfflineRequest)obj;
                IJob job = new SetFriendOfflineJob(req.Contact);
                progress.Report(job);
            }
            else
            {
                ObjectReceivedEvent?.Invoke(obj);
            }
        }        
Esempio n. 3
0
        private async void ProcessMessage(NSBaseMessage obj)
        {
            if (obj is ConnectRequest)
            {
                
            }
            else if (obj is KeepAliveRequest)
            {
                SendObject(new KeepAliveResponse());
            }            
            else if (obj is GetOnlineContactsRequest)
            {
                var allContacts = await _usersService.GetFriends(Info.Username);
                var onlineContacts = allContacts.Data
                    .Where(c => ClientsConnectionsManager.IsOnline(c.Id))
                    .Select(c => c.Id);
                var resp = new GetOnlineContactsResponse() {Contacts = onlineContacts};
                SendObject(resp);
            } 
            else if (obj is ConnectToFriendRequest)
            {
                // should check if is a friend
                var req = (ConnectToFriendRequest) obj;
                var contact = ClientsConnectionsManager.GetClientConnection(req.UserId);
                if (contact == null)
                {
                    Console.WriteLine("Client {0} is offline", req.UserId);
                    return; // should send an error message
                }

                //var address = new P2PCommunicationLibrary.PeerAddress(
                //    Info.PeerAddress().PrivateEndPoint, Info.PeerAddress().PublicEndPoint);
                contact.SendObject(new AllowFriendToConnectRequest() {Address = req.Address, UserId = Info.Username});
            }  
            else if (obj is AllowFriendToConnectResponse)
            {
                var resp = (AllowFriendToConnectResponse) obj;
                var contact = ClientsConnectionsManager.GetClientConnection(resp.UserId);
                if (contact == null)
                {
                    Console.WriteLine("Client {0} is offline", resp.UserId);
                    return; // should send an error message
                }

                //var address = new P2PCommunicationLibrary.PeerAddress(
                //    Info.PeerAddress().PrivateEndPoint, Info.PeerAddress().PublicEndPoint);

                contact.SendObject(new ConnectToFriendResponse() {Address = resp.PeerAddress,
                    UserId = Info.Username});
            }         
            else
            {
                Console.WriteLine("Unexpected message received {0}", obj.MessageType);
            }
        }