Do not ever close the socket until we are definitely closing the connection. We can...
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 31 Oct 2010 23:28:20 +0000 (19:28 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 31 Oct 2010 23:28:20 +0000 (19:28 -0400)
Source/DebuggerConnection.h
Source/DebuggerConnection.m
Source/DebuggerProcessor.h
Source/DebuggerProcessor.m

index 01c2bb1a03621fe3d399ec0b000a873532133055..87029a1e4ccaca47b638b1b7d493b82a16b4a999 100644 (file)
@@ -31,6 +31,9 @@
   // If the connection to the debugger engine is currently active.
   BOOL connected_;
 
+  // Whether or not in reconnect mode.
+  BOOL reconnect_;
+
   // The thread on which network operations are performed. Weak.
   NSThread* thread_;
 
@@ -81,6 +84,7 @@
 
 @property (readonly) NSUInteger port;
 @property (readonly) BOOL connected;
+@property (readonly, getter=inReconnectMode) BOOL reconnect;
 @property (assign) id <DebuggerConnectionDelegate> delegate;
 
 - (id)initWithPort:(NSUInteger)aPort;
 - (void)connect;
 - (void)close;
 
+// Marks the connection as being in "reconnect mode," which sets the expectation
+// of receiving another <init> message.
+- (void)reconnect;
+
 - (void)send:(NSString*)command;
 
 // This sends the given command format to the debugger. This method is thread
index 1149d567c6173538ef68a25807467c59a2680708..3b282505be03cdc65ae85663860cff7fc98793f1 100644 (file)
@@ -123,7 +123,9 @@ void SocketAcceptCallback(CFSocketRef socket,
   NSLog(@"SocketAcceptCallback()");
   
   DebuggerConnection* connection = (DebuggerConnection*)connectionRaw;
-  
+  if (![connection inReconnectMode])
+    return;
+
   CFReadStreamRef readStream;
   CFWriteStreamRef writeStream;
   
@@ -200,6 +202,7 @@ void PerformQuitSignal(void* info)
 
 @synthesize port = port_;
 @synthesize connected = connected_;
+@synthesize reconnect = reconnect_;
 @synthesize delegate = delegate_;
 
 @synthesize socket = socket_;
@@ -215,6 +218,7 @@ void PerformQuitSignal(void* info)
   if (self = [super init])
   {
     port_ = aPort;
+    reconnect_ = YES;
   }
   return self;
 }
@@ -225,6 +229,12 @@ void PerformQuitSignal(void* info)
   [super dealloc];
 }
 
+- (void)reconnect
+{
+  connected_ = NO;
+  reconnect_ = YES;
+}
+
 /**
  * Kicks off the socket on another thread.
  */
@@ -373,7 +383,7 @@ void PerformQuitSignal(void* info)
     // through normal disconnected procedure (a call to |-close|, followed by
     // the downing of the socket and the stream, which also produces this
     // messsage). Instead, the stream callbacks encountered EOF unexpectedly.
-    [self close];
+    //[self close];
   }
   if ([delegate_ respondsToSelector:@selector(connectionDidClose:)])
     [delegate_ connectionDidClose:self];
@@ -618,13 +628,10 @@ void PerformQuitSignal(void* info)
     
     // Otherwise, assume +1 and hope it works.
     ++lastReadTransaction_;
-  }
-  else
-  {
+  } else if (!reconnect_) {
     // See if the transaction can be parsed out.
     NSInteger transaction = [self transactionIDFromResponse:xmlTest];
-    if (transaction < lastReadTransaction_)
-    {
+    if (transaction < lastReadTransaction_) {
       NSLog(@"tx = %d vs %d", transaction, lastReadTransaction_);
       NSLog(@"out of date transaction %@", packet);
       return;
@@ -655,8 +662,9 @@ void PerformQuitSignal(void* info)
     [self errorEncountered:errorMessage];
   }
   
-  if ([[[response rootElement] name] isEqualToString:@"init"])
-  {
+  if ([[[response rootElement] name] isEqualToString:@"init"]) {
+    connected_ = YES;
+    reconnect_ = NO;
     [delegate_ performSelectorOnMainThread:@selector(handleInitialResponse:)
                                 withObject:response
                              waitUntilDone:NO];
index dee8a2ef60fb7195ff14183c2d193c03ec963e32..1059a338e4ece6633f287b87b646e92291723d20 100644 (file)
@@ -36,6 +36,7 @@
   
   // Human-readable status of the connection.
   NSString* status;
+  BOOL active_;
   
   // The connection's delegate.
   id <DebuggerProcessorDelegate> delegate;
index c392d32fbff58f992b602da4f0d9acfb1893167b..8af726bc050d567606b85f5e9ffd974f386de3d2 100644 (file)
  */
 - (BOOL)isConnected
 {
-  return [connection_ connected];
+  return [connection_ connected] && active_;
 }
 
 // Commands ////////////////////////////////////////////////////////////////////
  */
 - (void)reconnect
 {
-  if (connection_.connected)
-    [connection_ close];
   self.status = @"Connecting";
-  [connection_ connect];
+  active_ = NO;
+  [connection_ reconnect];
 }
 
 /**
  */
 - (void)handleInitialResponse:(NSXMLDocument*)response
 {
+  active_ = YES;
+
   // Register any breakpoints that exist offline.
   for (Breakpoint* bp in [[BreakpointManager sharedManager] breakpoints])
     [self addBreakpoint:bp];
 - (void)updateStatus:(NSXMLDocument*)response
 {
   self.status = [[[[response rootElement] attributeForName:@"status"] stringValue] capitalizedString];
-  if (status == nil || [status isEqualToString:@"Stopped"] || [status isEqualToString:@"Stopping"])
-  {
-    [connection_ close];
+  active_ = YES;
+  if (!status || [status isEqualToString:@"Stopped"]) {
     [delegate debuggerDisconnected];
-    
-    self.status = @"Stopped";
+    active_ = NO;
+  } else if ([status isEqualToString:@"Stopping"]) {
+    [connection_ sendCommandWithFormat:@"stop"];
+    active_ = NO;
   }
 }
 
 - (void)debuggerStep:(NSXMLDocument*)response
 {
   [self updateStatus:response];
-  if (![connection_ connected])
+  if (![self isConnected])
     return;
-  
+
   // If this is the run command, tell the delegate that a bunch of updates
   // are coming. Also remove all existing stack routes and request a new stack.
-  // TODO: figure out if we can not clobber the stack every time.
-  NSString* command = [[[response rootElement] attributeForName:@"command"] stringValue];
-  if (YES || [command isEqualToString:@"run"])
-  {
-    if ([delegate respondsToSelector:@selector(clobberStack)])
-      [delegate clobberStack];
-    [stackFrames_ removeAllObjects];
-    NSNumber* tx = [connection_ sendCommandWithFormat:@"stack_depth"];
-    [self recordCallback:@selector(rebuildStack:) forTransaction:tx];
-    stackFirstTransactionID_ = [tx intValue];
-  }
+  if ([delegate respondsToSelector:@selector(clobberStack)])
+    [delegate clobberStack];
+  [stackFrames_ removeAllObjects];
+  NSNumber* tx = [connection_ sendCommandWithFormat:@"stack_depth"];
+  [self recordCallback:@selector(rebuildStack:) forTransaction:tx];
+  stackFirstTransactionID_ = [tx intValue];
 }
 
 /**