public void AppendChatBoxListViewSeparateThread(T2SClientMessage message) { if (!string.IsNullOrEmpty(message.Message)) { T2SUser sender = null; //Find profile picture from list of users foreach (T2SUser user in ConnectedUsers) { if (user.MacAddr == message.MacAddr) { sender = user; } } ChatBox.Items.Add(new MessageTemplate { ProfilePicture = BitmapToImageSource(GetBitmapFromBytes(sender.ProfilePicture)), Message = message.Message }); } }
//Closed private void Window_Closed(object sender, EventArgs e) { MainWindow.LoadHotkeys(); if (MainWindow.ClientSocket != null && MainWindow.ClientSocket.Connected) { instance.SendMessage("", false, true); //Send update message //Update self T2SUser user = new T2SUser() { MacAddr = instance.MacAddr, ProfilePicture = this.profilePicture, Username = Username.Text }; instance.ListView_ConnectedUsers.Dispatcher.Invoke(new MainWindow.UpdateUserListViewConnectedUsersSeparateThreadCallback(instance.UpdateUserListViewConnecteduseresSeparateThread), new object[] { user }); instance.ConnectedUsers[instance.ConnectedUsers.FindIndex((x) => x.MacAddr == user.MacAddr)] = user; ICollectionView view = CollectionViewSource.GetDefaultView(instance.ConnectedUsers); view.Refresh(); } }
public MainWindow() { InitializeComponent(); DisconnectMenuItem.IsEnabled = false; LabelIP.Content = "Disconnected"; ConnectedUsers = new List <T2SUser>(); ListView_ConnectedUsers.ItemsSource = ConnectedUsers; //Setup text to speech synth speech = new SpeechSynthesizer(); speech.Volume = 80; speech.Rate = 1; keyboard.KeyPressed += new EventHandler <KeyPressedEventArgs>(Keyboard_KeyPressed); //Load user information MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault(); string json = ""; //Load profile data if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\profile.json")) { json = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\profile.json"); T2SUser user = JsonConvert.DeserializeObject <T2SUser>(json); ProfilePicture = user.ProfilePicture; Username = user.Username; } else { //Create and load default Username = Environment.UserName; //Just use their username from their PC ProfilePicture = GetBytesFromBitmap(Properties.Resources.blank_profile); //convert image to string json = JsonConvert.SerializeObject(new T2SUser() { Username = this.Username, ProfilePicture = this.ProfilePicture, MacAddr = this.MacAddr }); Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming"); //Will create a directory if doesnt exist File.WriteAllText(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\profile.json", json); } json = ""; //Load hotkeyMute and hotkeyDisplay, then register them if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\settings.json")) { json = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\settings.json"); Keys[] keys = JsonConvert.DeserializeObject <Keys[]>(json); hotkeyMute = keys[0]; hotkeyDisplay = keys[1]; hotkeyDisableHotkeys = keys[2]; } else { //Create and load default hotkeyMute = Keys.M; hotkeyDisplay = Keys.U; hotkeyDisableHotkeys = Keys.End; Keys[] keys = { Keys.M, Keys.U, Keys.End }; json = JsonConvert.SerializeObject(keys); Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming"); //Will create a directory if doesnt exist File.WriteAllText(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Local\\T2S Gaming\\settings.json", json); } LoadHotkeys(); }
public void UpdateUserListViewConnecteduseresSeparateThread(T2SUser user) { CollectionViewSource.GetDefaultView(ConnectedUsers).Refresh(); }
/// <summary> /// Receives byte array from server, converts to ASCII encoding to display /// Note that this is occurring on a separate thread /// /// HEADER AND BODY /// Header is defined as the zeroth index of a BAR | string split /// The header may also never be larger than 32 bytes (if it is, then this implies the message is SUPER long and we should probably just ignore it. /// The server should also recognize this failure and send an error message to the sending client that the message sent is too large (like, honestly, 32 bytes is a 32 digit integer. /// Who even would send something that big? /// RIP that person's bandwidth lol)) /// The header will represent the TOTAL LENGTH EXCLUSIVE of the header, thereby representing the buffer length of the NEXT receive, which should by protocol be a json file. /// The header will also always be length 32, append pipe separator to be 33 /// /// The first index will be a byte array that is the object T2SClientMessage /// /// </summary> private void ReceiveResponse() { if (!IsConnected(ClientSocket)) { Console.WriteLine("here"); Disconnect(); } int received = 0; var buffer = new byte[33]; try { received = ClientSocket.Receive(buffer, 33, SocketFlags.None); } catch (Exception e) { Console.WriteLine("EXCEPTION"); return; } if (received == 0) { return; //Nothing to receive } string text = Encoding.ASCII.GetString(buffer); //Hopefully the header Console.WriteLine(text); //If full header not grabbed, append the new bytes until header is grabbed while (received < 33) { byte[] tempBuffer = new byte[33 - received]; int appendableBytes = ClientSocket.Receive(tempBuffer, 33 - received, SocketFlags.None); Array.Copy(tempBuffer, 0, buffer, received, tempBuffer.Length); received += appendableBytes; } Console.WriteLine("Received"); int header; //Successfully got header if (Int32.TryParse(text.Split('|')[0], out header)) { buffer = new byte[header]; received = ClientSocket.Receive(buffer, header, SocketFlags.None); while (received < header) { byte[] tempBuffer = new byte[header - received]; int appendableBytes = ClientSocket.Receive(tempBuffer, header - received, SocketFlags.None); Array.Copy(tempBuffer, 0, buffer, received, tempBuffer.Length); received += appendableBytes; } T2SClientMessage message = (T2SClientMessage)ByteArrayToObject(buffer); //Perform logic //In order to do anything, the client must first be connected if (message.Connected) { //If this is the first connection, we're not going to display anything new in the chat if (message.FirstConnect) { ConnectedUsers.Add(new T2SUser() { MacAddr = message.MacAddr, Username = message.Username, ProfilePicture = message.ProfilePicture }); ListView_ConnectedUsers.Dispatcher.Invoke(new AppendListViewConnectedUsersSeparateThreadCallback(this.AppendListViewConnecteduseresSeparateThread), new object[] { message }); } else { if (message.UpdateProfile) { bool found = false; //Modify user if macaddr exists. If not, add foreach (T2SUser user in ConnectedUsers) { if (user.MacAddr == message.MacAddr) { //exists. So update the user user.ProfilePicture = (message.ProfilePicture == null) ? user.ProfilePicture : message.ProfilePicture; user.Username = message.Username; //Should never be null found = true; //Found, therefore look for the user in the ListView_ConnectedUsers and update the user ListView_ConnectedUsers.Dispatcher.Invoke(new UpdateUserListViewConnectedUsersSeparateThreadCallback(this.UpdateUserListViewConnecteduseresSeparateThread), new object[] { user }); break; } } //Not found, therefore add the user to ConnectedUser list and thingy if (!found) { ConnectedUsers.Add(new T2SUser() { MacAddr = message.MacAddr, Username = message.Username, ProfilePicture = message.ProfilePicture }); } } //Append to visual and do TTS speech.SpeakAsync(message.Message); ChatBox.Dispatcher.Invoke(new AppendRTBSeparateThreadCallback(this.AppendChatBoxListViewSeparateThread), new object[] { message }); } } else { T2SUser user = ConnectedUsers.Find(x => x.MacAddr == message.MacAddr); if (user != null) { ConnectedUsers.Remove(user); } ListView_ConnectedUsers.Dispatcher.Invoke(new RemoveListViewConnectedUsersSeparateThreadCallback(this.RemoveListViewConnecteduseresSeparateThread), new object[] { message }); } } }