• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CFSocket example?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CFSocket example?


  • Subject: Re: CFSocket example?
  • From: Douglas Davidson <email@hidden>
  • Date: Thu, 5 Sep 2002 12:27:09 -0700

If it helps, here is the text outline for the portion of the talk I presented on CFRunLoop and CFSocket. I have tried to include sufficient information in the CFSocket header to make it usable by someone who understands the basic run loop model. I think there is now or will soon be some conceptual documentation on CFRunLoop, too.

Douglas Davidson

The Run Loop and Its Sources

CFRunLoop

Enables advanced services
Event manager and dispatcher
Where event is intended to be understood broadly
Maintains sets of event sources
Callbacks to registered handlers when events occur

Run Loops

Each thread has exactly one run loop
CFRunLoop is CoreFoundation interface to it
Apps will usually use Carbon Events or AppKit interfaces
Use the highest-level API that meets your needs
CFRunLoop for CoreFoundation-based sources

Run Loop Modes

Run loop event-source sets are called modes
Modes are used to restrict which event sources are monitored
Example: NSModalPanelRunLoopMode
Normally you will use and run the run loop in the default mode
Can also use common modes
kCFRunLoopCommonModes

CFRunLoop Sources

Some provided source types
CFRunLoopTimer
CFMessagePort
CFMachPort
CFSocket
You can create your own, but it can be complex

CFRunLoopTimers

One-shot or repeating
Callback function provided at create time
Add it to a run loop with modes
When date arrives, it will fire
But only when the run loop is running
If one-shot, automatically invalidated

CFMessagePort

Low-overhead, high-performance IPC
Between threads or processes on the same machine
Messages can be sent
One-way and asynchronous
With reply and synchronous

CFMessagePort

Local port is your end
CFMessagePortCreateLocal()
Local ports may be named and advertised with a CFString
Remote port is the other end, looked up by name
CFMessagePortCreateRemote()

Sending Messages

Messages are a CFData and integer message ID
Optional replies are CFData
Server defines and publishes the protocol for messages
The format of the data
What the message IDs mean
If there will be a reply

Messaging Process

Client sends a message
CFMessagePortSendRequest()
Servers callback is called
Server returns optional reply
Client gets reply
CFMessagePortSendRequest() returns
Can optionally receive other messages or events while waiting

CFMachPort

Allows Mach port to serve as run loop source
Access to raw Mach messages
Handles receive end only
CFMachPort
Create with existing Mach port
CFMachPortCreateWithPort()
Or let it create the Mach port for you
CFMachPortCreate()
You get a callback when a message arrives

CFSocket

Allows BSD socket to serve as run loop source
Not a complete socket wrapper
Notifications for read, write, accept, connect

CFSocket

Create with existing socket
CFSocketCreateWithNative()
Or let it create the socket for you
CFSocketCreate()
CFSocketCreateWithSocketSignature()
CFSocketCreateConnectedToSocketSignature()
Choose what callbacks you want:
Read, data, accept, connect, write

Using CFSocket

Create socket
struct sockaddr_in a = {0, AF_INET, 1234, 0};
CFDataRef d = CFDataCreate(0, (UInt8 *)&a, 16);
CFSocketSignature signature =
{PF_INET, SOCK_STREAM, IPPROTO_TCP, d};
CFSocketRef socket = CFSocketCreateWithSocketSignature(0, &signature, kCFSocketAcceptCallBack, acceptConnection, 0);

Using CFSocket

Create source, add to run loop, run
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(0, socket, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
CFRunLoopRun();

Using CFSocket

acceptConnection() callback
CFSocketRef child = CFSocketCreateWithNative(0, *(CFSocketNativeHandle *)data, kCFSocketDataCallBack, receiveData, 0);
CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(0, child, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode);
CFRelease(childSource);

Using CFSocket

receiveData() callback
static char helloWorld[] = HTTP/1.0 200 OK\r\n\r\nhello, world\r\n;
CFDataRef response = CFDataCreate(0, helloWorld, strlen(helloWorld));
CFSocketSendData(child, 0, response, 0);
CFRelease(response);
CFSocketInvalidate(child);
CFRelease(child);
_______________________________________________
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.

  • Prev by Date: RE: CFSocket example?
  • Next by Date: OpenTransport UDP Sample?
  • Previous by thread: RE: CFSocket example?
  • Next by thread: NSL Manager & AFP
  • Index(es):
    • Date
    • Thread