static public void ReceiveConnectionError(MyTabManagement Item) { MessageBoxResult res = MessageBox.Show("Il Server " + Item.MyTab.TabServerIP + " non risponde, vuoi riprovare?", "Attenzione!", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (res == MessageBoxResult.No) { Item.MyTab.MainWndw.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { Item.MyTab.MainWndw.Error = true; Item.MyTab.MainWndw.CloseTab(Item.MyTab); })); } if (res == MessageBoxResult.Yes) { Item.MyTab.MainWndw.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { //codice di connect NewConnection C = new NewConnection(); C.Show(); Item.MyTab.MainWndw.Error = true; //codice di disconnect if (Item != null) { Item.MyTab.MainWndw.CloseTab(Item.MyTab); } })); } }
// Funzione che chiude un singolo tab nel caso si preme Disconnetti public void CloseTab(MyTabItem tab) { // Caso in cui il tab da chiudere sia l'ultimo attivo: chiusura dell'intera finestra if (TabItemList.Count == 1) { this.Close(); return; } MyTabManagement servertab = tab.TabManager; // In caso di chiusura della main window, la funzione di chiusura di questo tab non andrà più eseguita dal delegato ClosingEvent -= servertab.ServerTabClose; // Chiusura del tab servertab.ServerTabClose(); // Rimozione di questo tab dalla lista dei tab dei server TabItemList.Remove(tab); // Rimozione dei questa connessione dalla lista di connessioni attive ActiveConnectionsIPList.Remove(tab.TabServerIP); //rimuove dalla list box l'app in focus della tab in chiusura int index = AppsInFocus.IndexOf(new AppInFocus(tab.AppInFocus)); if (index != -1) { AppsInFocus.RemoveAt(index); } }
static public void SendError(MyTabManagement Item) { MessageBoxResult res = MessageBox.Show("Errore durante l'invio del comando. Chiudere la connessione?", "Attenzione!", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (res == MessageBoxResult.Yes) { Item.MyTab.MainWndw.Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() => { Item.MyTab.MainWndw.CloseTab(Item.MyTab); })); } }
// Funzione associata al tasto del Menù File->Disconnetti private void Menu_Disconnect_Click(object sender, RoutedEventArgs e) { /*non ho un riferimento diretto alla MyTabItem quindi, dall'oggetto grafico ServerTabControl * /mi faccio dare l'attuale tab selezionata e da questa prendo l'oggetto MyTabItem da passare a CloseTab */ MyTabManagement s = ServerTabControl.SelectedContent as MyTabManagement; if (s != null) { CloseTab(s.MyTab); } }
} // KeyReleased closing bracket // Metodo che gestisce la terminazione dell'invio di dati al server (invocato da KeyPressed) private void SendToServer(IAsyncResult a) { MyTabManagement s = (MyTabManagement)a.AsyncState; try { s.Stream.EndWrite(a); } catch (IOException) { ExceptionHandler.SendError(s); } }
//Funzione che crea un nuovo tab public void NewTab(TcpClient client, NetworkStream stream, String address) { MyTabItem tab = new MyTabItem(this); MyTabManagement s = new MyTabManagement(tab); //assegno la proprietà Header(ereditata da TabItem) e la proprietà "copia" interna di MyTabItem tab.Header = tab.TabServerIP = address; s.Connection = client; s.Stream = stream; s.StartServerDataExchange(); //assegno la proprietà Content(ereditata da TabItem) e la proprietà "copia" interna di MyTabItem tab.Content = tab.TabManager = s; // Aggiunta del nuovo tab alla lista TabItemList.Add(tab); // Evidenziazione dell'ultimo tab creato ServerTabControl.SelectedIndex = TabItemList.Count - 1; }
// Funzione invocata dall'evento PreviewKeyDown (registrato da SendCommands_Start) // Cattura e registra i modificatori e il tasto premuto e li invia al server private void KeyPressed(object sender, KeyEventArgs e) { //estrae l'attuale app in focus dalla AppsInFocusBox AppInFocus appinfocus = AppsInFocusBox.SelectedItem as AppInFocus; //Controlla se l'app in focus è il client stesso (per evitare cicli infiniti) if (appinfocus.Name == System.AppDomain.CurrentDomain.FriendlyName || appinfocus == null) { e.Handled = true; return; } Key key; if (e.Key == Key.System) { key = e.SystemKey; } else { key = e.Key; } switch (key) { case Key.LeftShift: case Key.RightShift: mod.shift = 'y'; e.Handled = true; break; case Key.LeftCtrl: case Key.RightCtrl: mod.ctrl = 'y'; e.Handled = true; break; case Key.LeftAlt: case Key.RightAlt: mod.alt = 'y'; e.Handled = true; break; default: break; } // Switch closing bracket // Nel caso in cui il tasto premuto non sia un modificatore if (e.Handled == false) { // Preparazione dei dati da inviare byte[] buffer = new byte[3 + sizeof(int)]; // Struttura che conterra Modificatori + tasto buffer[0] = Convert.ToByte(mod.ctrl); buffer[1] = Convert.ToByte(mod.alt); buffer[2] = Convert.ToByte(mod.shift); int conv_key = KeyInterop.VirtualKeyFromKey(key); BitConverter.GetBytes(IPAddress.HostToNetworkOrder(conv_key)).CopyTo(buffer, 3); // Recupero l'applicazione che è in focus dall'elemento selezionato nella combobox // Se c'è almeno un app in focus, cerchiamo il tab o server a cui appartiene foreach (MyTabItem tab in TabItemList) { if (tab.AppInFocus == appinfocus.Name) { MyTabManagement s = tab.Content as MyTabManagement; if (s != null) { try { //invia i tasti sul socket relativo a questa tab (server) s.Stream.BeginWrite(buffer, 0, 3 + sizeof(int), new AsyncCallback(SendToServer), s); } catch (IOException) { ExceptionHandler.SendError(s); } } } } e.Handled = true; } } // KeyPressed closing bracket
private MyTabManagement TabManager; //riferimento al TabManager che sta usando questo socket. // Costruttore della classe SocketManagement public SocketManagement(MyTabManagement s) { TabManager = s; Stream = s.Stream; }