Examples

I'm now going to show you a number of "cookbook"
examples you can cut and paste into your code, to use as a basis
for your projects.
These aren't complete resource managers—you'll need to add the thread pool
and dispatch "skeleton" shown immediately below, and ensure that
your versions of the I/O functions are placed into the I/O functions table after
you've done the iofunc_func_init(),
in order to override the defaults!

I'll start with a number of simple examples that show basic functionality
for the various resource manager message handlers:

The basic skeleton of a resource manager
The following can be used as a template for a resource manager with multiple threads. (We've already seen a template that can be used for a single-threaded resource manager above in "The resource manager library," when we discussed a /dev/null resource manager).

A simple io_read() example
To illustrate how your resource manager might return data to a client, consider a simple resource manager that always returns the constant string "Hello, world!\n". There are a number of issues involved, even in this very simple case:

A simple io_write() example
The io_read() example was fairly simple; let's take a look at io_write(). The major hurdle to overcome with the io_write() is to access the data. Since the resource manager library reads in a small portion of the message from the client, the data content that the client sent (immediately after the _IO_WRITE header) may have only partially arrived at the io_write() function. To illustrate this, consider the client writing one megabyte—only the header and a few bytes of the data will get read by the resource manager library. The rest of the megabyte of data is still available on the client side—the resource manager can access it at will.

An io_devctl() example that deals with data
In the previous io_devctl() example, above, we raised the question of how to set arbitrary sampling rates. Obviously, it's not a good solution to create a large number of DCMD_AUDIO_SET_SAMPLE_RATE_* constants—we'd rapidly use up the available bits in the dcmd member.