3 * Copyright (c) 2007 - 2008, 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 "SocketWrapper.h"
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 @interface SocketWrapper (Private
)
26 - (void)error
:(NSString
*)msg
;
30 @implementation SocketWrapper
33 * Initializes the socket wrapper with a host and port
35 - (id)initWithConnection
:(DebuggerConnection
*)cnx
37 if (self = [super init
])
39 connection
= [cnx retain
];
40 port
= [connection port
];
56 * Close our socket and clean up anything else
64 * Returns the delegate
72 * Sets the delegate but does *not* retain it
74 - (void)setDelegate
:(id)aDelegate
80 * Returns the name of the host to whom we are currently connected.
82 - (NSString
*)remoteHost
88 * Connects to a socket on the port specified during init. This will dispatch another thread to do the
89 * actual waiting. Delegate notifications are posted along the way to let the client know what is going on.
93 [NSThread detachNewThreadSelector
:@selector(connect
:) toTarget
:self withObject
:nil];
97 * This does the actual dirty work (in a separate thread) of connecting to a socket
99 - (void)connect
:(id)obj
101 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
103 // create an INET socket that we'll be listen()ing on
104 int socketOpen
= socket(PF_INET
, SOCK_STREAM
, 0);
106 // create our address given the port
107 struct sockaddr_in address
;
108 address.sin_family
= AF_INET
;
109 address.sin_port
= htons(port
);
110 address.sin_addr.s_addr
= htonl(INADDR_ANY
);
111 memset(address.sin_zero
, '\0', sizeof(address.sin_zero
));
113 // allow an already-opened socket to be reused
115 setsockopt(socketOpen
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(int));
117 // bind the socket... and don't give up until we've tried for a while
119 while (bind(socketOpen
, (struct sockaddr
*)&address
, sizeof(address
)) < 0)
124 [self error
:@
"Could not bind to socket"];
128 NSLog(@
"couldn't bind to the socket... trying again in 5");
133 // now we just have to keep our ears open
134 if (listen(socketOpen
, 0) == -1)
136 [self error
:@
"Could not use bound socket for listening"];
139 // accept a connection
140 struct sockaddr_in remoteAddress
;
141 socklen_t remoteAddressLen
= sizeof(remoteAddress
);
142 sock
= accept(socketOpen
, (struct sockaddr
*)&remoteAddress
, &remoteAddressLen
);
146 [self error
:@
"Client failed to accept remote socket"];
151 // we're done listening now that we have a connection
154 struct sockaddr_in addr
;
155 socklen_t addrLength
;
156 if (getpeername(sock
, (struct sockaddr
*)&addr
, &addrLength
) < 0)
158 [self error
:@
"Could not get remote hostname."];
160 char *name
= inet_ntoa(addr.sin_addr
);
161 hostname
= [[NSString alloc
] initWithUTF8String
:name
];
163 [connection performSelectorOnMainThread
:@selector(socketDidAccept
:) withObject
:nil waitUntilDone
:NO
];
169 * Reads from the socket and returns the result as a NSString (because it's always going to be XML). Be aware
170 * that the underlying socket recv() call will *wait* for the server to send a message, so be sure that this
171 * is used either in a threaded environment so the interface does not hang, or when you *know* the server
172 * will return something (which we almost always do). Returns the data that was received from the socket.
174 - (NSString
*)receive
179 // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
180 int recvd
= recv(sock
, &buffer
, sizeof(buffer
), 0);
182 // take the received data and put it into an NSData
183 NSMutableString
*str
= [NSMutableString string
];
185 // strip the length from the packet, and clear the null byte then add it to the NSData
186 char packetLength
[8];
187 memset(packetLength
, '\0', sizeof(packetLength
));
189 while (buffer
[i
] != '\0')
191 packetLength
[i
] = buffer
[i
];
195 // we also want the null byte, so move us up 1
198 // the total length of the full transmission
199 int length
= atoi(packetLength
);
201 // move the packet part of the received data into it's own char[]
202 char packet
[sizeof(buffer
)];
203 memset(packet
, '\0', sizeof(packet
));
204 memmove(packet
, &buffer
[i
], recvd
- i
);
206 // convert bytes to NSString
207 [str appendString
:[NSString stringWithCString
:packet length
:recvd
- i
]];
209 // check if we have a partial packet
210 if (length
+ i
> sizeof(buffer
))
212 while (recvd
< length
)
214 int latest
= recv(sock
, &buffer
, sizeof(buffer
), 0);
217 [self error
:@
"Socket closed or could not be read"];
220 [str appendString
:[NSString stringWithCString
:buffer length
:latest
]];
225 NSString
*tmp
= [str stringByTrimmingCharactersInSet
:[NSCharacterSet whitespaceAndNewlineCharacterSet
]]; // strip whitespace
226 return [tmp substringToIndex
:[tmp length
] - 1]; // don't want the null byte
230 * Sends a given NSString over the socket. Returns YES on complete submission.
232 - (BOOL)send
:(NSString
*)data
234 data
= [NSString stringWithFormat
:@
"%@\0", data
];
235 int sent
= send(sock
, [data UTF8String
], [data length
], 0);
238 [self error
:@
"Failed to write data to socket"];
241 if (sent
< [data length
])
243 // TODO - do we really need to worry about partial sends with the lenght of our commands?
244 NSLog(@
"FAIL: only partial packet was sent; sent %d bytes", sent
);
252 * Helper method that just calls -[DebuggerWindowController setError:] on the main thread
254 - (void)error
:(NSString
*)msg
256 [delegate performSelectorOnMainThread
:@selector(errorEncountered
:) withObject
:msg waitUntilDone
:NO
];