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_;
40 @synthesize readStream = readStream_;
41 @synthesize lastReadTransaction = lastReadTransaction_;
42 @synthesize currentPacket = currentPacket_;
43 @synthesize writeStream = writeStream_;
44 @synthesize lastWrittenTransaction = lastWrittenTransaction_;
45 @synthesize queuedWrites = queuedWrites_;
47 - (id)initWithPort:(NSUInteger)aPort
49 if (self = [super init]) {
57 self.currentPacket = nil;
62 * Kicks off the socket on another thread.
66 if (thread_ && !connected_) {
67 // A thread has been detached but the socket has yet to connect. Do not
68 // spawn a new thread otherwise multiple threads will be blocked on the same
72 [NSThread detachNewThreadSelector:@selector(runNetworkThread) toTarget:self withObject:nil];
76 * Creates, connects to, and schedules a CFSocket.
78 - (void)runNetworkThread
80 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
82 thread_ = [NSThread currentThread];
83 runLoop_ = [NSRunLoop currentRunLoop];
84 callbackController_ = new NetworkCallbackController(self);
86 // Create a source that is used to quit.
87 CFRunLoopSourceContext quitContext = { 0 };
88 quitContext.info = self;
89 quitContext.perform = PerformQuitSignal;
90 quitSource_ = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &quitContext);
91 CFRunLoopAddSource([runLoop_ getCFRunLoop], quitSource_, kCFRunLoopCommonModes);
93 callbackController_->OpenConnection(port_);
99 delete callbackController_;
100 callbackController_ = NULL;
102 CFRunLoopSourceInvalidate(quitSource_);
103 CFRelease(quitSource_);
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
163 if ([delegate_ respondsToSelector:@selector(connectionDidClose:)])
164 [delegate_ connectionDidClose:self];
168 * Writes a command into the write stream. If the stream is ready for writing,
169 * we do so immediately. If not, the command is queued and will be written
170 * when the stream is ready.
172 - (void)send:(NSString*)command
174 if (lastReadTransaction_ >= lastWrittenTransaction_ && CFWriteStreamCanAcceptBytes(writeStream_)) {
175 [self performSend:command];
177 [writeQueueLock_ lock];
178 [queuedWrites_ addObject:command];
179 [writeQueueLock_ unlock];
181 [self sendQueuedWrites];
185 * This will send a command to the debugger engine. It will append the
186 * transaction ID automatically. It accepts a NSString command along with a
187 * a variable number of arguments to substitute into the command, a la
188 * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber.
190 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...
192 // Collect varargs and format command.
194 va_start(args, format);
195 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
198 NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++];
199 NSString* taggedCommand = [NSString stringWithFormat:@"%@ -i %@", [command autorelease], callbackKey];
200 [self performSelector:@selector(send:)
202 withObject:taggedCommand
203 waitUntilDone:connected_];
209 * Certain commands expect encoded data to be the the last, unnamed parameter
210 * of the command. In these cases, inserting the transaction ID at the end is
211 * incorrect, so clients use this method to have |{txn}| replaced with the
214 - (NSNumber*)sendCustomCommandWithFormat:(NSString*)format, ...
216 // Collect varargs and format command.
218 va_start(args, format);
219 NSString* command = [[[NSString alloc] initWithFormat:format arguments:args] autorelease];
222 NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++];
223 NSString* taggedCommand = [command stringByReplacingOccurrencesOfString:@"{txn}"
224 withString:[callbackKey stringValue]];
225 [self performSelector:@selector(send:)
227 withObject:taggedCommand
228 waitUntilDone:connected_];
234 * Given a file path, this returns a file:// URI and escapes any spaces for the
237 - (NSString*)escapedURIPath:(NSString*)path
239 // Custon GDBp paths are fine.
240 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
243 // Create a temporary URL that will escape all the nasty characters.
244 NSURL* url = [NSURL fileURLWithPath:path];
245 NSString* urlString = [url absoluteString];
247 // Remove the host because this is a file:// URL;
248 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
250 // Escape % for use in printf-style NSString formatters.
251 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
256 * Returns the transaction_id from an NSXMLDocument.
258 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response
260 return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
264 * Scans a command string for the transaction ID component. If it is not found,
265 * returns NSNotFound.
267 - (NSInteger)transactionIDFromCommand:(NSString*)command
269 NSRange occurrence = [command rangeOfString:@"-i "];
270 if (occurrence.location == NSNotFound)
272 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
273 return [transaction intValue];
276 // Private /////////////////////////////////////////////////////////////////////
279 // Delegate Thread-Safe Wrappers ///////////////////////////////////////////////
282 * Receives errors from the SocketWrapper and updates the display
284 - (void)errorEncountered:(NSString*)error
286 if (![delegate_ respondsToSelector:@selector(errorEncountered:)])
288 [delegate_ performSelectorOnMainThread:@selector(errorEncountered:)
293 - (LogEntry*)recordSend:(NSString*)command
295 LoggingController* logger = [[AppDelegate instance] loggingController];
296 LogEntry* entry = [LogEntry newSendEntry:command];
297 entry.lastReadTransactionID = lastReadTransaction_;
298 entry.lastWrittenTransactionID = lastWrittenTransaction_;
299 [logger performSelectorOnMainThread:@selector(recordEntry:)
302 return [entry autorelease];
305 - (LogEntry*)recordReceive:(NSString*)command
307 LoggingController* logger = [[AppDelegate instance] loggingController];
308 LogEntry* entry = [LogEntry newReceiveEntry:command];
309 entry.lastReadTransactionID = lastReadTransaction_;
310 entry.lastWrittenTransactionID = lastWrittenTransaction_;
311 [logger performSelectorOnMainThread:@selector(recordEntry:)
314 return [entry autorelease];
317 // Stream Managers /////////////////////////////////////////////////////////////
320 * Callback from the CFReadStream that there is data waiting to be read.
322 - (void)readStreamHasData
324 const NSUInteger kBufferSize = 1024;
325 UInt8 buffer[kBufferSize];
326 CFIndex bufferOffset = 0; // Starting point in |buffer| to work with.
327 CFIndex bytesRead = CFReadStreamRead(readStream_, buffer, kBufferSize);
328 const char* charBuffer = (const char*)buffer;
330 // The read loop works by going through the buffer until all the bytes have
332 while (bufferOffset < bytesRead) {
333 // Find the NULL separator, or the end of the string.
334 NSUInteger partLength = 0;
335 for (CFIndex i = bufferOffset; i < bytesRead && charBuffer[i] != '\0'; ++i, ++partLength) ;
337 // If there is not a current packet, set some state.
338 if (!self.currentPacket) {
339 // Read the message header: the size. This will be |partLength| bytes.
340 packetSize_ = atoi(charBuffer + bufferOffset);
341 currentPacketIndex_ = 0;
342 self.currentPacket = [NSMutableString stringWithCapacity:packetSize_];
343 bufferOffset += partLength + 1; // Pass over the NULL byte.
344 continue; // Spin the loop to begin reading actual data.
347 // Substring the byte stream and append it to the packet string.
348 CFStringRef bufferString = CFStringCreateWithBytes(kCFAllocatorDefault,
349 buffer + bufferOffset, // Byte pointer, offset by start index.
350 partLength, // Length.
351 kCFStringEncodingUTF8,
353 [self.currentPacket appendString:(NSString*)bufferString];
354 CFRelease(bufferString);
357 currentPacketIndex_ += partLength;
358 bufferOffset += partLength + 1;
360 // If this read finished the packet, handle it and reset.
361 if (currentPacketIndex_ >= packetSize_) {
362 [self handlePacket:[[currentPacket_ retain] autorelease]];
363 self.currentPacket = nil;
365 currentPacketIndex_ = 0;
371 * Performs the packet handling of a raw string XML packet. From this point on,
372 * the packets are associated with a transaction and are then dispatched.
374 - (void)handlePacket:(NSString*)packet
376 // Test if we can convert it into an NSXMLDocument.
377 NSError* error = nil;
378 NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:currentPacket_
379 options:NSXMLDocumentTidyXML
381 // TODO: Remove this assert before stable release. Flush out any possible
382 // issues during testing.
385 // Validate the transaction.
386 NSInteger transaction = [self transactionIDFromResponse:xml];
387 if (transaction < lastReadTransaction_) {
388 NSLog(@"Transaction #%d is out of date (lastRead = %d). Dropping packet: %@",
389 transaction, lastReadTransaction_, packet);
392 if (transaction != lastWrittenTransaction_) {
393 NSLog(@"Transaction #%d received out of order. lastRead = %d, lastWritten = %d. Continuing.",
394 transaction, lastReadTransaction_, lastWrittenTransaction_);
397 lastReadTransaction_ = transaction;
399 // Log this receive event.
400 LogEntry* log = [self recordReceive:currentPacket_];
403 // Finally, dispatch the handler for this response.
404 [self handleResponse:[xml autorelease]];
407 - (void)handleResponse:(NSXMLDocument*)response
409 // Check and see if there's an error.
410 NSArray* error = [[response rootElement] elementsForName:@"error"];
411 if ([error count] > 0) {
412 NSLog(@"Xdebug error: %@", error);
413 NSString* errorMessage = [[[[error objectAtIndex:0] children] objectAtIndex:0] stringValue];
414 [self errorEncountered:errorMessage];
417 if ([[[response rootElement] name] isEqualToString:@"init"]) {
419 [delegate_ performSelectorOnMainThread:@selector(handleInitialResponse:)
425 if ([delegate_ respondsToSelector:@selector(handleResponse:)])
426 [delegate_ performSelectorOnMainThread:@selector(handleResponse:)
430 [self sendQueuedWrites];
434 * This performs a blocking send. This should ONLY be called when we know we
435 * have write access to the stream. We will busy wait in case we don't do a full
438 - (void)performSend:(NSString*)command
440 // If this is an out-of-date transaction, do not bother sending it.
441 NSInteger transaction = [self transactionIDFromCommand:command];
442 if (transaction != NSNotFound && transaction < lastWrittenTransaction_)
447 char* string = (char*)[command UTF8String];
448 size_t stringLength = strlen(string);
450 // Busy wait while writing. BAADD. Should background this operation.
452 if (CFWriteStreamCanAcceptBytes(writeStream_)){
453 // Include the NULL byte in the string when we write.
454 CFIndex bytesWritten = CFWriteStreamWrite(writeStream_, (UInt8*)string, stringLength + 1);
455 if (bytesWritten < 0) {
456 CFErrorRef error = CFWriteStreamCopyError(writeStream_);
457 NSLog(@"Write stream error: %@", error);
461 else if (bytesWritten < static_cast<CFIndex>(strlen(string))) {
462 // Adjust the buffer and wait for another chance to write.
463 stringLength -= bytesWritten;
464 memmove(string, string + bytesWritten, stringLength);
468 // We need to scan the string to find the transactionID.
469 if (transaction == NSNotFound) {
470 NSLog(@"sent %@ without a transaction ID", command);
473 lastWrittenTransaction_ = transaction;
478 // Log this trancation.
479 [self recordSend:command];
483 * Checks if there are unsent commands in the |queuedWrites_| queue and sends
484 * them if it's OK to do so. This will not block.
486 - (void)sendQueuedWrites
491 [writeQueueLock_ lock];
492 if (lastReadTransaction_ >= lastWrittenTransaction_ && [queuedWrites_ count] > 0) {
493 NSString* command = [queuedWrites_ objectAtIndex:0];
495 // We don't want to block because this is called from the main thread.
496 // |-performSend:| busy waits when the stream is not ready. Bail out
497 // before we do that becuase busy waiting is BAD.
498 if (CFWriteStreamCanAcceptBytes(writeStream_)) {
499 [self performSend:command];
500 [queuedWrites_ removeObjectAtIndex:0];
503 [writeQueueLock_ unlock];