public void SendImage(string path) { try { byte[] img = System.IO.File.ReadAllBytes(path); DataProtocol imgMessage = new DataProtocol("Image", myUsername, "null", img); string jsonMessage = JsonConvert.SerializeObject(imgMessage); byte[] msg = System.Text.Encoding.UTF8.GetBytes(jsonMessage); HistoryDB.AddImage(img, DateTime.Now, connectedUsername); int bytesSent = s.Send(msg); } catch (ArgumentNullException aex) { p2p.MainWindow.AppWindow.ShowMessage("An error occured while trying to send the image"); } }
public void SendMessage(string message) { try { DataProtocol DP = new DataProtocol("Message", myUsername, message, new byte[1]); string jsonMessage = JsonConvert.SerializeObject(DP); byte[] msg = System.Text.Encoding.UTF8.GetBytes(jsonMessage); HistoryDB.AddMessage(message, DateTime.Now, connectedUsername, "Me"); int bytesSent = s.Send(msg); } catch (ArgumentNullException aex) { p2p.MainWindow.AppWindow.ShowMessage("An error occured while trying to send the message"); } catch (SocketException sex) { connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.ConnectionBroken(); } }
private void searchResultBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { chatHistoryBox.Items.Clear(); string selectedUser = searchResultBox.SelectedItem.ToString(); List <Tuple <int, string> > chatHistory = HistoryDB.GetHistory(selectedUser); foreach (Tuple <int, string> tuple in chatHistory) { if (tuple.Item1 == 1) // if item is text { chatHistoryBox.Items.Add(tuple.Item2); } else // if item is an image { byte[] img = System.Text.Encoding.Default.GetBytes(tuple.Item2); using (var ms = new System.IO.MemoryStream(img)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = ms; image.DecodePixelHeight = 150; image.DecodePixelWidth = 150; image.EndInit(); Image imger = new Image(); imger.Source = image; chatHistoryBox.Items.Add(imger); } } } } catch (NullReferenceException ex) { p2p.MainWindow.AppWindow.ShowMessage("Search for a username to display your chat history"); } }
private void searchButton_Click(object sender, RoutedEventArgs e) { searchResultBox.Items.Clear(); List <string> tmp = HistoryDB.UpdateUserList(); if (tmp.Any()) { string search = searchBox.Text; var query_where1 = from a in tmp //LINQ where a.Contains(search) select a; foreach (var a in query_where1) { searchResultBox.Items.Add(a); } } else { searchResultBox.Items.Add("You have no conversations yet"); } }
private void ListenCallback(IAsyncResult ar) { try { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); s = handler; byte[] bytes = new byte[1024 * 5000]; int bytesRec = handler.Receive(bytes); string currUser = Encoding.UTF8.GetString(bytes, 0, bytesRec); connectedUsername = currUser; convoDT = DateTime.Now; /* Accept or decline incoming connection request */ if (!p2p.MainWindow.AppWindow.AcceptRequestBox(currUser)) // if not accepted connection { connectionAccepted = false; DataProtocol declineRequest = new DataProtocol("connectionDeclined", myUsername, "null", new byte[1]); string jsonDeclineRequest = JsonConvert.SerializeObject(declineRequest); byte[] msg = System.Text.Encoding.UTF8.GetBytes(jsonDeclineRequest); int bytesSent = s.Send(msg); s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.DisconnectCallback(); } else { connectionAccepted = true; HistoryDB.InitConvo(currUser); HistoryDB.AddMessage("New conversation started", convoDT, currUser, "System"); p2p.MainWindow.AppWindow.EnableDisconnectButton(); p2p.MainWindow.AppWindow.AddMessage("System", "New conversation started", convoDT); DataProtocol acceptRequest = new DataProtocol("connectionAccepted", myUsername, "null", new byte[1]); string jsonAcceptRequest = JsonConvert.SerializeObject(acceptRequest); byte[] msg = System.Text.Encoding.UTF8.GetBytes(jsonAcceptRequest); int bytesSent = s.Send(msg); } String data = null; while (connectionAccepted) { byte[] rbytes = new byte[1024 * 5000]; int rbytesRec = handler.Receive(rbytes); if (handler.Connected && rbytesRec != 0) { data = Encoding.UTF8.GetString(rbytes, 0, rbytesRec); DataProtocol responseMessage = JsonConvert.DeserializeObject <DataProtocol>(data); DateTime timestamp = DateTime.Now; if (responseMessage.Type == "disconnect") { DataProtocol disconnect = new DataProtocol("disconnect", myUsername, " disconnected", new byte[1]); string jsonDisconnect = JsonConvert.SerializeObject(disconnect); byte[] disconnectMsg = System.Text.Encoding.UTF8.GetBytes(jsonDisconnect); int bytesSen = s.Send(disconnectMsg); HistoryDB.AddMessage(connectedUsername + " disconnected", timestamp, connectedUsername, "System"); p2p.MainWindow.AppWindow.AddMessage("System", connectedUsername + " disconnected", convoDT); connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.DisconnectCallback(); } else if (responseMessage.Type == "Image") { byte[] img = responseMessage.imgByte; HistoryDB.AddImage(img, DateTime.Now, connectedUsername); using (var ms = new System.IO.MemoryStream(img)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; // here image.StreamSource = ms; image.DecodePixelHeight = 150; image.DecodePixelWidth = 150; image.EndInit(); image.Freeze(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); string photolocation = "img.jpg"; //file name encoder.Frames.Add(BitmapFrame.Create((BitmapImage)image)); using (var filestream = new FileStream(photolocation, FileMode.Create)) encoder.Save(filestream); p2p.MainWindow.AppWindow.DisplayImg(responseMessage.Username, timestamp, image); } } else { HistoryDB.AddMessage(responseMessage.Message, timestamp, responseMessage.Username, responseMessage.Username); p2p.MainWindow.AppWindow.AddMessage(responseMessage.Username, responseMessage.Message, timestamp); } } else { break; } } } catch (SocketException se) { connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.ConnectionBroken(); p2p.MainWindow.AppWindow.DisconnectCallback(); } }
private void ConnectCallback(IAsyncResult ar) { try { Socket connector = (Socket)ar.AsyncState; connector.EndConnect(ar); byte[] msg = System.Text.Encoding.UTF8.GetBytes(myUsername); int bytesSent = s.Send(msg); byte[] acceptDecline = new byte[1024 * 5000]; int acceptDeclineRec = connector.Receive(acceptDecline); String data = Encoding.UTF8.GetString(acceptDecline, 0, acceptDeclineRec); DataProtocol response = JsonConvert.DeserializeObject <DataProtocol>(data); if (response.Type == "connectionDeclined") { p2p.MainWindow.AppWindow.ShowMessageBoxCLientDecline(); connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.DisconnectCallback(); return; } else { connectionAccepted = true; HistoryDB.InitConvo(response.Username); connectedUsername = response.Username; convoDT = DateTime.Now; HistoryDB.AddMessage("New conversation started", convoDT, response.Username, "System"); p2p.MainWindow.AppWindow.AddMessage("System", "New conversation started", convoDT); p2p.MainWindow.AppWindow.AcceptedRequest(response.Username); } while (connectionAccepted) { byte[] bytes = new byte[1024 * 5000]; int bytesRec = connector.Receive(bytes); if (connector.Connected && bytesRec != 0) { data = Encoding.UTF8.GetString(bytes, 0, bytesRec); DataProtocol responseMessage = JsonConvert.DeserializeObject <DataProtocol>(data); DateTime timestamp = DateTime.Now; if (responseMessage.Type == "disconnect") { DataProtocol disconnect = new DataProtocol("disconnect", myUsername, " disconnected", new byte[1]); string jsonDisconnect = JsonConvert.SerializeObject(disconnect); byte[] disconnectMsg = System.Text.Encoding.UTF8.GetBytes(jsonDisconnect); HistoryDB.AddMessage(connectedUsername + " disconnected", timestamp, connectedUsername, connectedUsername); p2p.MainWindow.AppWindow.AddMessage("System", connectedUsername + " disconnected", convoDT); int byteSent = s.Send(disconnectMsg); connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.DisconnectCallback(); } else if (responseMessage.Type == "Image") { byte[] img = responseMessage.imgByte; HistoryDB.AddImage(img, DateTime.Now, connectedUsername); using (var ms = new System.IO.MemoryStream(img)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; // here image.StreamSource = ms; image.DecodePixelHeight = 150; image.DecodePixelWidth = 150; image.EndInit(); image.Freeze(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); string photolocation = "tmper.jpg"; //file name encoder.Frames.Add(BitmapFrame.Create((BitmapImage)image)); using (var filestream = new FileStream(photolocation, FileMode.Create)) encoder.Save(filestream); p2p.MainWindow.AppWindow.DisplayImg(responseMessage.Username, timestamp, image); } } else { HistoryDB.AddMessage(responseMessage.Message, timestamp, responseMessage.Username, responseMessage.Username); p2p.MainWindow.AppWindow.AddMessage(responseMessage.Username, responseMessage.Message, timestamp); } } else { break; } } } catch (SocketException se) { if (connectionAccepted) { connectionAccepted = false; s.Shutdown(SocketShutdown.Both); s.Close(); p2p.MainWindow.AppWindow.ConnectionBroken(); } else { p2p.MainWindow.AppWindow.ShowMessage("There is no friend listening on that port!"); p2p.MainWindow.AppWindow.DisconnectCallback(); } } }