public void ProcessNewContact(NewContact newContact) { ContactSectionViewModel.ProcessNewContact(newContact); }
private static void ProcessPacket(Socket clientSocket, PacketType PacketType, Stream stream) { switch (PacketType) { case PacketType.Message: { NetworkPackets.Packet.Message msg = NetworkPackets.Packet.Message.Deserialize<NetworkPackets.Packet.Message>(stream, true); if (msg != null) { lock (_clientSocketDictionary) { msg.SenderID = _clientSocketDictionary[clientSocket]; if (_clientSocketDictionary.ContainsValue(msg.RecipientID)) { Socket recipientSocket = _clientSocketDictionary.FirstOrDefault(s => s.Value == msg.RecipientID).Key; if (recipientSocket != null) { Send(recipientSocket, msg.CreateTransferablePacket()); } else { lock (_unsentMessages) { _unsentMessages.Add(msg); } } } else { lock (_unsentMessages) { _unsentMessages.Add(msg); } } } } else { Console.WriteLine("Message deserialization failure!"); } } break; case PacketType.AuthenticationRequest: { AuthenticationRequest authentication = AuthenticationRequest.Deserialize<AuthenticationRequest>(stream, true); if (authentication != null) { AuthenticationResponse ar = UserDAO.Authenticate(authentication.Login, authentication.Password); if (ar.HasError == false) { ContactIsOnline contactIsOnline = new ContactIsOnline(); contactIsOnline.ContactID = ar.Profile.Id; byte[] byteArr = contactIsOnline.CreateTransferablePacket(); Socket recipientSocket = null; lock (_clientSocketDictionary) { _clientSocketDictionary[clientSocket] = ar.Profile.Id; ar.ContactList.ForEach((i) => { if (_clientSocketDictionary.ContainsValue(i.Id)) { i.IsOnline = true; recipientSocket = _clientSocketDictionary.FirstOrDefault(s => s.Value == i.Id).Key; if (recipientSocket != null) { Task.Factory.StartNew(() => { Send(recipientSocket, byteArr); }); } } }); } int clientID = ar.Profile.Id; try { ar.UnreceivedMessages = new List<NetworkPackets.Packet.Message>(); foreach (NetworkPackets.Packet.Message msg in _unsentMessages) { if (msg.RecipientID == clientID) ar.UnreceivedMessages.Add(msg); //Send(clientSocket, PacketHelper.Serialize(PacketType.Message, msg)); } Send(clientSocket, ar.CreateTransferablePacket()); lock (_unsentMessages) { _unsentMessages.RemoveAll((i) => { return i.RecipientID == clientID ? true : false; }); } Console.WriteLine("Authenticated! " + ar.Profile.Id.ToString()); } catch { Console.WriteLine("Authentication failure!"); } } else { Send(clientSocket, ar.CreateTransferablePacket()); Console.WriteLine("Invalid password or login!"); } } else { Console.WriteLine("Authentication deserialization failure!"); } } break; case PacketType.RegistrationRequest: { RegistrationRequest registrationRequest = Packet.Deserialize<RegistrationRequest>(stream, true); if (registrationRequest != null) { string newImgName = null; if (registrationRequest.ImageBytes != null) { newImgName = Path.GetRandomFileName(); File.WriteAllBytes(UserDAO.ImagePath + newImgName, registrationRequest.ImageBytes); } RegistrationResponse registrationResponse = UserDAO.Register(registrationRequest.Login, registrationRequest.Password, registrationRequest.Email, newImgName); byte[] bytyData = registrationResponse.CreateTransferablePacket(); Send(clientSocket, bytyData); } else { Console.WriteLine("Registration request deserialization failure!"); } } break; case PacketType.SearchRequest: { SearchRequest searchRequest = Packet.Deserialize<SearchRequest>(stream, true); if (searchRequest != null) { SearchResponse searchResponse = UserDAO.GetContactListBySearchStr(searchRequest.SearchStr); int requesterID = _clientSocketDictionary[clientSocket]; byte[] byteData = null; searchResponse.ContactList.Remove(new NetworkPackets.Model.User() { Id = requesterID }); // Removing requester from search result byteData = searchResponse.CreateTransferablePacket(); Send(clientSocket, byteData); } else { Console.WriteLine("Search request deserialization failure!"); } } break; case PacketType.ContactRequest: { ContactRequest contactRequest = Packet.Deserialize<ContactRequest>(stream, true); if (contactRequest != null) { Console.WriteLine("Server: contact request received!"); int clientID = 0; lock(_clientSocketDictionary) { clientID = _clientSocketDictionary[clientSocket]; } NetworkPackets.Model.User contact = UserDAO.GetContactByUserID(clientID); if (contact != null) { ContactResponse contactResponse = new ContactResponse(); contactResponse.Contact = contact; if (_clientSocketDictionary.ContainsValue(contactRequest.ContactID)) { Socket recipientSocket = null; lock (_clientSocketDictionary) { recipientSocket = _clientSocketDictionary.FirstOrDefault(s => s.Value == contactRequest.ContactID).Key; } if (recipientSocket != null) { Send(recipientSocket, contactResponse.CreateTransferablePacket()); } } } else { Console.WriteLine("Invalid UserID!"); } } else { Console.WriteLine("Contact request deserialization failure!"); } } break; case PacketType.AddingContact: { AddingContact addingContact = Packet.Deserialize<AddingContact>(stream, true); if (addingContact != null) { Console.WriteLine("Adding friend!"); int clientID = 0; lock (_clientSocketDictionary) { clientID = _clientSocketDictionary[clientSocket]; } if (UserDAO.AddContact(clientID, addingContact.ContactID) && UserDAO.AddContact(addingContact.ContactID, clientID)) { NetworkPackets.Model.User contact = UserDAO.GetContactByUserID(clientID); if (contact != null) { NewContact newContact = new NewContact(); newContact.Contact = contact; if (_clientSocketDictionary.ContainsValue(addingContact.ContactID)) { Socket recipientSocket = null; lock (_clientSocketDictionary) { recipientSocket = _clientSocketDictionary.FirstOrDefault(s => s.Value == addingContact.ContactID).Key; } if (recipientSocket != null) { Send(recipientSocket, newContact.CreateTransferablePacket()); } } } else { Console.WriteLine("Invalid UserID!"); } } } else { Console.WriteLine("Contact request deserialization failure!"); } } break; case PacketType.RemovingContactRequest: { RemovingContactRequest removingContactRequest = Packet.Deserialize<RemovingContactRequest>(stream, true); if (removingContactRequest != null) { Console.WriteLine("Removing contact!"); lock (_clientSocketDictionary) { removingContactRequest.SenderID = _clientSocketDictionary[clientSocket]; } if (UserDAO.RemoveContactPair(removingContactRequest.SenderID, removingContactRequest.ContactID)) { if (_clientSocketDictionary.ContainsValue(removingContactRequest.ContactID)) { Socket recipientSocket = null; lock (_clientSocketDictionary) { recipientSocket = _clientSocketDictionary.FirstOrDefault(s => s.Value == removingContactRequest.ContactID).Key; } if (recipientSocket != null) { RemovingContactResponse removingContactResponse = new RemovingContactResponse(); removingContactResponse.ContactID = removingContactRequest.SenderID; Send(recipientSocket, removingContactResponse.CreateTransferablePacket()); } } } } else { Console.WriteLine("Contact request deserialization failure!"); } } break; } }
public void ProcessNewContact(NewContact newContact) { _contactsLoaded.WaitOne(); lock (_lock) { Contacts.Add(new Contact { Id = newContact.Contact.Id, Login = newContact.Contact.Login, Email = newContact.Contact.Email, AvatarBytes = (newContact.Contact.ImageBytes == null ? App.GetDefaultAvatar() : newContact.Contact.ImageBytes), StatusLogoBytes = App.GetStatusLogo(true) }); } }