Diff of /wBattle.xaml.cs [cf67b8] .. [11cf18] Maximize Restore

  Switch to side-by-side view

--- a/wBattle.xaml.cs
+++ b/wBattle.xaml.cs
@@ -7,14 +7,14 @@
 using System.Windows.Threading;
 
 namespace YuGiOh_Ultra {
-    public enum GameAction { Draw = 0, Summon, Place, Set, HandDiscard, HandBanish, HandToDeck, FieldToGrave, FiledBanish, MonsterChangePos, Lose, Surrendered };
+    public enum GameAction { Draw = 0, Summon, Place, Set, HandDiscard, HandBanish, HandToDeck, FieldToGrave, FiledBanish, MonsterChangePos, Lose, Surrendered, Disconnected };
 
     public partial class wBattle : Window {
         private bool iscon = false;
 
         private readonly DispatcherTimer Datatmr = new DispatcherTimer(), Connectioncontroltmr = new DispatcherTimer();
 
-        private readonly TcpListener Listener = new TcpListener( Com.port );
+        private TcpListener Listener;
         private TcpClient Client = new TcpClient();
         private NetworkStream NetStr;
 
@@ -43,28 +43,34 @@
             int len = NetStr.Read( Bytes, 0, Bytes.Length );
             var msg = Encoding.ASCII.GetString( Bytes, 0, len ).Trim();
 
-            Debug.WriteLine( "wrote: " + msg );
-
-            if ( msg == "disconnected" ) {
-                Datatmr.Stop();
-                Connectioncontroltmr.Stop();
-                Listener.Stop();
-                Client.Close();
-
-                btnclientcon.IsEnabled = btnservercon.IsEnabled = true;
-                btnclientcon.Content = "Client Connect";
-                btnservercon.Content = "Server Connect";
-                return;
-            }
-
+            Debug.WriteLine( "received: " + msg );
             if ( !msg.Contains( "|" ) ) return;
+
             var a = msg.Split( '|' );
 
             var action = (GameAction) int.Parse(a[0]);
+            Debug.WriteLine( "action: " + action.ToString() );
 
             switch ( action ) {
-                
-            }
+                case GameAction.Disconnected:
+                    Disconnect();
+                break;
+            }
+        }
+
+        private void Disconnect() {
+            SendNetAction(GameAction.Disconnected);
+            Datatmr.Stop();
+            Connectioncontroltmr.Stop();
+            NetStr.Close();
+            if( Listener != null ) Listener.Stop();
+            if( Client != null ) Client.Close();
+            Client = null;
+
+            iscon = false;
+            btnclientcon.IsEnabled = btnservercon.IsEnabled = true;
+            btnclientcon.Content = "Client Connect";
+            btnservercon.Content = "Server Connect";
         }
 
         private void ConnectionControl( object sender = null, object e = null ) {
@@ -85,50 +91,37 @@
         }
 
         private void btnclientcon_Click( object sender, RoutedEventArgs e ) {
-            if ( !iscon ) {
-                iscon = true;
-                btnclientcon.Content = "Disconnect client";
-                btnservercon.IsEnabled = false;
-
+            if ( !iscon ) { //connect as client
                 try {
+                    Listener = null;
+                    if( Client == null ) Client = new TcpClient();
                     Client.Connect( enemyip, Com.port );
                     NetStr = Client.GetStream();
                     SendNetMsg( Com.me.Id + " connected" );
+
+                    iscon = true;
+                    btnclientcon.Content = "Disconnect client";
+                    btnservercon.IsEnabled = false;
                 }
                 catch ( Exception ex ) {
                     MessageBox.Show( ex.Message );
                 }
             }
-            else {
-                SendNetMsg( "Disconnected" );
-                iscon = false;
-                btnclientcon.Content = "Connect client";
-                btnservercon.IsEnabled = true;
-
-                Client.Close();
-                Datatmr.Stop();
-                NetStr.Close();
-            }
+            else Disconnect();
         }
 
         private void btnservercon_Click( object sender, RoutedEventArgs e ) {
-            if ( !iscon ) {
+            if ( !iscon ) { //connect as server
                 iscon = true;
                 btnservercon.Content = "Diconnect server";
                 btnclientcon.IsEnabled = false;
 
+                if( Client == null ) Client = new TcpClient();
+                Listener = new TcpListener( Com.port );
                 Listener.Start();
                 Connectioncontroltmr.Start();
                 Datatmr.Start();
-            }
-            else {
-                iscon = false;
-                btnservercon.Content = "Connect server";
-                btnclientcon.IsEnabled = true;
-
-                Listener.Stop();
-                Connectioncontroltmr.Stop();
-            }
+            } else Disconnect();
         }
 
         private void btnsendnetmsg_Click( object sender, RoutedEventArgs e ) {
@@ -185,11 +178,7 @@
         }
 
         private void Window_Closing( object sender, CancelEventArgs e ) {
-            SendNetMsg( "disconnected" );
-            Datatmr.Stop();
-            Connectioncontroltmr.Stop();
-            Listener.Stop();
-            Client.Close();
+            if( iscon ) Disconnect();
 
             new wMenu().Show();
         }