3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "NetworkConnection.h"
18 #import "NetworkConnectionPrivate.h"
20 #import "AppDelegate.h"
21 #import "LoggingController.h"
22 #include "NetworkCallbackController.h"
24 // Other Run Loop Callbacks ////////////////////////////////////////////////////
26 void PerformQuitSignal(void* info)
28 NetworkConnection* obj = (NetworkConnection*)info;
29 [obj performQuitSignal];
32 ////////////////////////////////////////////////////////////////////////////////
34 @implementation NetworkConnection
36 @synthesize port = port_;
37 @synthesize connected = connected_;
38 @synthesize delegate = delegate_;
39 @synthesize lastReadTransaction = lastReadTransaction_;
40 @synthesize currentPacket = currentPacket_;
41 @synthesize lastWrittenTransaction = lastWrittenTransaction_;
42 @synthesize queuedWrites = queuedWrites_;
44 - (id)initWithPort:(NSUInteger)aPort
46 if (self = [super init]) {
54 self.currentPacket = nil;
59 * Kicks off the socket on another thread.
63 if (thread_ && !connected_) {
64 // A thread has been detached but the socket has yet to connect. Do not
65 // spawn a new thread otherwise multiple threads will be blocked on the same
69 [NSThread detachNewThreadSelector:@selector(runNetworkThread) toTarget:self withObject:nil];
73 * Creates, connects to, and schedules a CFSocket.
75 - (void)runNetworkThread
77 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
79 thread_ = [NSThread currentThread];
80 runLoop_ = [NSRunLoop currentRunLoop];
81 callbackController_ = new NetworkCallbackController(self);
83 // Create a source that is used to quit.
84 CFRunLoopSourceContext quitContext = { 0 };
85 quitContext.info = self;
86 quitContext.perform = PerformQuitSignal;
87 quitSource_ = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &quitContext);
88 CFRunLoopAddSource([runLoop_ getCFRunLoop], quitSource_, kCFRunLoopCommonModes);
90 callbackController_->OpenConnection(port_);
96 delete callbackController_;
97 callbackController_ = NULL;
99 CFRunLoopSourceInvalidate(quitSource_);
100 CFRelease(quitSource_);
103 if ([delegate_ respondsToSelector:@selector(connectionDidClose:)])
104 [delegate_ connectionDidClose:self];
110 * Called by SocketWrapper after the connection is successful. This immediately calls
111 * -[SocketWrapper receive] to clear the way for communication, though the information
112 * could be useful server information that we don't use right now.
114 - (void)socketDidAccept
118 lastReadTransaction_ = 0;
119 lastWrittenTransaction_ = 0;
120 self.queuedWrites = [NSMutableArray array];
121 writeQueueLock_ = [NSRecursiveLock new];
122 if ([delegate_ respondsToSelector:@selector(connectionDidAccept:)])
123 [delegate_ performSelectorOnMainThread:@selector(connectionDidAccept:)
129 * Closes a socket and releases the ref.
136 if (runLoop_ && quitSource_) {
137 CFRunLoopSourceSignal(quitSource_);
138 CFRunLoopWakeUp([runLoop_ getCFRunLoop]);
143 * Quits the run loop and stops the thread.
145 - (void)performQuitSignal
147 self.queuedWrites = nil;
149 [writeQueueLock_ release];
152 CFRunLoopStop([runLoop_ getCFRunLoop]);
155 callbackController_->CloseConnection();
159 * Notification that the socket disconnected.
161 - (void)socketDisconnected
167 * Writes a command into the write stream. If the stream is ready for writing,
168 * we do so immediately. If not, the command is queued and will be written
169 * when the stream is ready.
171 - (void)send:(NSString*)command
173 if (lastReadTransaction_ >= lastWrittenTransaction_ && callbackController_->WriteStreamCanAcceptBytes()) {
174 [self performSend:command];
176 [writeQueueLock_ lock];
177 [queuedWrites_ addObject:command];
178 [writeQueueLock_ unlock];
180 [self sendQueuedWrites];
184 * This will send a command to the debugger engine. It will append the
185 * transaction ID automatically. It accepts a NSString command along with a
186 * a variable number of arguments to substitute into the command, a la
187 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
189 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...
191 // Collect varargs and format command.
193 va_start(args, format);
194 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
197 NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++];
198 NSString* taggedCommand = [NSString stringWithFormat:@"%@ -i %@", [command autorelease], callbackKey];
199 [self performSelector:@selector(send:)
201 withObject:taggedCommand
202 waitUntilDone:connected_];
208 * Certain commands expect encoded data to be the the last, unnamed parameter
209 * of the command. In these cases, inserting the transaction ID at the end is
210 * incorrect, so clients use this method to have |{txn}| replaced with the
213 - (NSNumber*)sendCustomCommandWithFormat:(NSString*)format, ...
215 // Collect varargs and format command.
217 va_start(args, format);
218 NSString* command = [[[NSString alloc] initWithFormat:format arguments:args] autorelease];
221 NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++];
222 NSString* taggedCommand = [command stringByReplacingOccurrencesOfString:@"{txn}"
223 withString:[callbackKey stringValue]];
224 [self performSelector:@selector(send:)
226 withObject:taggedCommand
227 waitUntilDone:connected_];
233 * Given a file path, this returns a file:// URI and escapes any spaces for the
236 - (NSString*)escapedURIPath:(NSString*)path
238 // Custon GDBp paths are fine.
239 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
242 // Create a temporary URL that will escape all the nasty characters.
243 NSURL* url = [NSURL fileURLWithPath:path];
244 NSString* urlString = [url absoluteString];
246 // Remove the host because this is a file:// URL;
247 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
249 // Escape % for use in printf-style NSString formatters.
250 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
255 * Returns the transaction_id from an NSXMLDocument.
257 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response
259 return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
263 * Scans a command string for the transaction ID component. If it is not found,
264 * returns NSNotFound.
266 - (NSInteger)transactionIDFromCommand:(NSString*)command
268 NSRange occurrence = [command rangeOfString:@"-i "];
269 if (occurrence.location == NSNotFound)
271 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
272 return [transaction intValue];
275 // Private /////////////////////////////////////////////////////////////////////
278 // Delegate Thread-Safe Wrappers ///////////////////////////////////////////////
281 * Receives errors from the SocketWrapper and updates the display
283 - (void)errorEncountered:(NSString*)error
285 if (![delegate_ respondsToSelector:@selector(errorEncountered:)])
287 [delegate_ performSelectorOnMainThread:@selector(errorEncountered:)
292 - (LogEntry*)recordSend:(NSString*)command
294 LoggingController* logger = [[AppDelegate instance] loggingController];
295 LogEntry* entry = [LogEntry newSendEntry:command];
296 entry.lastReadTransactionID = lastReadTransaction_;
297 entry.lastWrittenTransactionID = lastWrittenTransaction_;
298 [logger performSelectorOnMainThread:@selector(recordEntry:)
301 return [entry autorelease];
304 - (LogEntry*)recordReceive:(NSString*)command
306 LoggingController* logger = [[AppDelegate instance] loggingController];
307 LogEntry* entry = [LogEntry newReceiveEntry:command];
308 entry.lastReadTransactionID = lastReadTransaction_;
309 entry.lastWrittenTransactionID = lastWrittenTransaction_;
310 [logger performSelectorOnMainThread:@selector(recordEntry:)
313 return [entry autorelease];
316 // Stream Managers /////////////////////////////////////////////////////////////
319 * Callback from the CFReadStream that there is data waiting to be read.
321 - (void)readStreamHasData:(CFReadStreamRef)stream
323 const NSUInteger kBufferSize = 1024;
324 UInt8 buffer[kBufferSize];
325 CFIndex bufferOffset = 0; // Starting point in |buffer| to work with.
326 CFIndex bytesRead = CFReadStreamRead(stream, buffer, kBufferSize);
327 const char* charBuffer = (const char*)buffer;
329 // The read loop works by going through the buffer until all the bytes have
331 while (bufferOffset < bytesRead) {
332 // Find the NULL separator, or the end of the string.
333 NSUInteger partLength = 0;
334 for (CFIndex i = bufferOffset; i < bytesRead && charBuffer[i] != '\0'; ++i, ++partLength) ;
336 // If there is not a current packet, set some state.
337 if (!self.currentPacket) {
338 // Read the message header: the size. This will be |partLength| bytes.
339 packetSize_ = atoi(charBuffer + bufferOffset);
340 currentPacketIndex_ = 0;
341 self.currentPacket = [NSMutableString stringWithCapacity:packetSize_];
342 bufferOffset += partLength + 1; // Pass over the NULL byte.
343 continue; // Spin the loop to begin reading actual data.
346 // Substring the byte stream and append it to the packet string.
347 CFStringRef bufferString = CFStringCreateWithBytes(kCFAllocatorDefault,
348 buffer + bufferOffset, // Byte pointer, offset by start index.
349 partLength, // Length.
350 kCFStringEncodingUTF8,
352 [self.currentPacket appendString:(NSString*)bufferString];
353 CFRelease(bufferString);
356 currentPacketIndex_ += partLength;
357 bufferOffset += partLength + 1;
359 // If this read finished the packet, handle it and reset.
360 if (currentPacketIndex_ >= packetSize_) {
361 [self handlePacket:[[currentPacket_ retain] autorelease]];
362 self.currentPacket = nil;
364 currentPacketIndex_ = 0;
370 * Performs the packet handling of a raw string XML packet. From this point on,
371 * the packets are associated with a transaction and are then dispatched.
373 - (void)handlePacket:(NSString*)packet
375 // Test if we can convert it into an NSXMLDocument.
376 NSError* error = nil;
377 NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:currentPacket_
378 options:NSXMLDocumentTidyXML
380 // TODO: Remove this assert before stable release. Flush out any possible
381 // issues during testing.
384 // Validate the transaction.
385 NSInteger transaction = [self transactionIDFromResponse:xml];
386 if (transaction < lastReadTransaction_) {
387 NSLog(@"Transaction #%d is out of date (lastRead = %d). Dropping packet: %@",
388 transaction, lastReadTransaction_, packet);
391 if (transaction != lastWrittenTransaction_) {
392 NSLog(@"Transaction #%d received out of order. lastRead = %d, lastWritten = %d. Continuing.",
393 transaction, lastReadTransaction_, lastWrittenTransaction_);
396 lastReadTransaction_ = transaction;
398 // Log this receive event.
399 LogEntry* log = [self recordReceive:currentPacket_];
402 // Finally, dispatch the handler for this response.
403 [self handleResponse:[xml autorelease]];
406 - (void)handleResponse:(NSXMLDocument*)response
408 // Check and see if there's an error.
409 NSArray* error = [[response rootElement] elementsForName:@"error"];
410 if ([error count] > 0) {
411 NSLog(@"Xdebug error: %@", error);
412 NSString* errorMessage = [[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue];
413 [self errorEncountered:errorMessage];
416 if ([[[response rootElement] name] isEqualToString:@"init"]) {
418 [delegate_ performSelectorOnMainThread:@selector(handleInitialResponse:)
424 if ([delegate_ respondsToSelector:@selector(handleResponse:)])
425 [delegate_ performSelectorOnMainThread:@selector(handleResponse:)
429 [self sendQueuedWrites];
433 * This performs a blocking send. This should ONLY be called when we know we
434 * have write access to the stream. We will busy wait in case we don't do a full
437 - (void)performSend:(NSString*)command
439 // If this is an out-of-date transaction, do not bother sending it.
440 NSInteger transaction = [self transactionIDFromCommand:command];
441 if (transaction != NSNotFound && transaction < lastWrittenTransaction_)
444 if (callbackController_->WriteString(command)) {
445 // We need to scan the string to find the transactionID.
446 if (transaction == NSNotFound) {
447 NSLog(@"sent %@ without a transaction ID", command);
449 lastWrittenTransaction_ = transaction;
452 // Log this trancation.
453 [self recordSend:command];
457 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
458 * them if it's OK to do so. This will not block.
460 - (void)sendQueuedWrites
465 [writeQueueLock_ lock];
466 if (lastReadTransaction_ >= lastWrittenTransaction_ && [queuedWrites_ count] > 0) {
467 NSString* command = [queuedWrites_ objectAtIndex:0];
469 // We don't want to block because this is called from the main thread.
470 // |-performSend:| busy waits when the stream is not ready. Bail out
471 // before we do that becuase busy waiting is BAD.
472 if (callbackController_->WriteStreamCanAcceptBytes()) {
473 [self performSend:command];
474 [queuedWrites_ removeObjectAtIndex:0];
477 [writeQueueLock_ unlock];