From 62c09c6a21b1d539a8cc515f6f2aa768e6023df7 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 1 Aug 2007 23:27:02 -0700 Subject: [PATCH] * MacGDBp.xcodeproj: Change the C mode to be C99 * Source/DebuggerConnection.m: ([DebuggerConnection initWithPort:]): If the result of init'ing a SocketWrapper is nil, throw an "error" * Source/SocketWrapper: ([SocketWrapper receive]): New method. Used to fetch a string from a socket ([SocketWrapper send:]): New method. Used to write to a socket --- MacGDBp.xcodeproj/project.pbxproj | 2 ++ Source/DebuggerConnection.m | 7 +++++ Source/SocketWrapper.h | 3 ++ Source/SocketWrapper.m | 50 +++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/MacGDBp.xcodeproj/project.pbxproj b/MacGDBp.xcodeproj/project.pbxproj index bcb9bc4..d0e20a7 100644 --- a/MacGDBp.xcodeproj/project.pbxproj +++ b/MacGDBp.xcodeproj/project.pbxproj @@ -296,6 +296,7 @@ C01FCF4F08A954540054247B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; @@ -306,6 +307,7 @@ C01FCF5008A954540054247B /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + GCC_C_LANGUAGE_STANDARD = c99; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; PREBINDING = NO; diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 38c91ec..df79fad 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -35,6 +35,13 @@ // now that we have our host information, open the socket socket = [[SocketWrapper alloc] initWithPort: port]; + if (socket == nil) + { + // TODO - kill us somehow + NSLog(@"can't proceed further... SocketWrapper is nil"); + } + + [socket receive]; // clean up after ourselves [[NSNotificationCenter defaultCenter] addObserver: self diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index dedbb9d..93732fb 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -24,4 +24,7 @@ - (id)initWithPort: (int)port; +- (NSString *)receive; +- (void)send: (NSString *)data; + @end diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 076b54a..dfad562 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -88,4 +88,54 @@ [super dealloc]; } +/** + * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware + * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this + * is used either in a threaded environment so the interface does not hang, or when you *know* the server + * will return something (which we almost always do). + * + * This function can only read a set amount of bytes (1024 to be exact). If anything is larger, then partial + * data will be returned. Don't shoot the messenger... even if it is our fault. + * + * Data string returned is autorelease'd + */ +- (NSString *)receive +{ + // create a buffer + char buffer[1024]; + + // do our initial recv() call to get (hopefully) all the data and the lengh of the packet + int recvd = recv(_socket, &buffer, sizeof(buffer), 0); + + // the length of the packet + // packet is formatted in lenpacket + int length = atoi(buffer); + + // check if we have a partial packet + if (length + sizeof(length) > sizeof(buffer)) + { + // TODO - another recv() call to get the rest of the packet + NSLog(@"TODO: implement incomplete packet fetching"); + } + + // strip the length from the packet, and clear the null byte + for (int i = recvd - length - 2; i >= 0; i--) + { + buffer[i] = ' '; + } + + // take our buffer read it into NSString! + NSLog(@"data = %@", [[NSString alloc] initWithBytes: buffer length: recvd encoding: NSUTF8StringEncoding]); + + return nil; +} + +/** + * Sends a given NSString over the socket + */ +- (void)send: (NSString *)data +{ + // TODO - implement me +} + @end -- 2.22.5