Re: CFSocket & CFRunLoop
Re: CFSocket & CFRunLoop
- Subject: Re: CFSocket & CFRunLoop
- From: Douglas Davidson <email@hidden>
- Date: Thu, 16 May 2002 12:10:25 -0700
On Thursday, May 16, 2002, at 11:15 AM, Avi Drissman wrote:
At 6:53 PM -0400 5/9/02, Matt Jarjoura wrote:
http://wodeveloper.com/omniLists/macosx-dev/2001/January/msg00439.html
When the client disconnects, the server gets a kCFSocketDataCallBack
notification with a data size of zero. Is that what a disconnection
notice looks like? Shouldn't the server do some more cleanup?
It appears that that's what a disconnection notice looks like, and it's
appearing that the socket is properly disposing of itself, since OOM
shows that the objects are going away. Is that indeed the case?
Is there any real documentation for CFSocket?
Yes, that is what a disconnection looks like, and yes, I believe the
socket is properly disposing of itself in that example. To clean up a
CFSocket, you generally should (a) invalidate it, and (b) release any
references you may have to it (and to its associated source). Part (b)
is standard CF practice; part (a) is common for various run loop
sources. Invalidation closes the underlying socket (unless you choose
otherwise, although this option is not available in 10.1.x) and removes
the source from run loops.
As for documentation, I would suggest reading the header, which has a
fair bit of information in it; there was also a session at the recent
WWDC, called "Managing I/O: CFRunLoop and CFStream", which covered
CFSocket. I believe formal documentation is still forthcoming.
Here again is the (rather simple-minded) example presented in that
talk. You will notice that in the interests of brevity the first,
accepting socket is never cleaned up; since there is only one such, and
its lifetime is the same as that of the process, it's not really
leaked. The socket for each connection is cleaned up, immediately after
use.
Douglas Davidson
compile with cc -framework CoreFoundation:
#import <CoreFoundation/CoreFoundation.h>
#include <sys/socket.h>
#include <netinet/in.h>
void receiveData(CFSocketRef child, CFSocketCallBackType type, CFDataRef
address, const void *data, void *info) {
static char helloWorld[] = "HTTP/1.0 200 OK\r\n\r\nhello, world\r\n";
CFDataRef response = CFDataCreate(NULL, helloWorld,
strlen(helloWorld));
CFSocketSendData(child, NULL, response, 0.0);
CFRelease(response);
CFSocketInvalidate(child);
CFRelease(child);
}
void acceptConnection(CFSocketRef socket, CFSocketCallBackType type,
CFDataRef address, const void *data, void *info) {
CFSocketRef child = CFSocketCreateWithNative(NULL,
*(CFSocketNativeHandle *)data, kCFSocketDataCallBack, receiveData, NULL);
CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL,
child, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode);
CFRelease(childSource);
}
int main (int argc, const char *argv[]) {
struct sockaddr_in a = {0, AF_INET, 1234, 0};
CFDataRef d = CFDataCreate(NULL, (UInt8 *)&a, sizeof(struct
sockaddr_in));
CFSocketSignature signature = {PF_INET, SOCK_STREAM, IPPROTO_TCP, d};
CFSocketRef socket = CFSocketCreateWithSocketSignature(NULL,
&signature, kCFSocketAcceptCallBack, acceptConnection, NULL);
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL,
socket, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
CFRunLoopRun();
}
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.