
#ifndef __TI_SERVERCONTEXT__
#define __TI_SERVERCONTEXT__

#include <CoreFoundation/CoreFoundation.h>


#if defined(__cplusplus)
extern "C" {
#endif


typedef struct __TI_Server* TIServerRef;

typedef struct {
    void *info;
    void *(*retain)(void *info);
    void (*release)(void *info);
    CFStringRef (*copyDescription)(void *info);
} TIServerContext;
typedef void (*TIServerCallBack)( TIServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info, CFDataRef address );


/*
** MyTI_ServerCreate
**
** Create a new instance of a server object.  This will allocate
** only the underlying structures and set aside the resources.
** Use ServerConnect to actually start the server listening.
**
** alloc	Allocator to use for allocating.  NULL indicates
**			the default allocator.
**
** callback	Function to call as incoming connections are accepted
**
** context	Reference to a context block which will be copied into
**			the server context for the callbacks.
*/
TIServerRef MyTI_ServerCreate(CFAllocatorRef alloc, TIServerCallBack callback, TIServerContext* context);


/*
** MyTI_ServerRetain
**
** Increment the retain count on the given server reference.
** This is comparable to CFRetain on a CFTypeRef.  It only
** works on server references.
**
** server Reference to the server.  Must be non-NULL.
*/
TIServerRef MyTI_ServerRetain(TIServerRef server);


/*
** MyTI_ServerRelease
**
** Decrements the retain count on the give server reference.
** If all retained references to this object are released, the
** object with be destroyed.  This is comparable to CFRelease
** on a CFTypeRef.  This only works on server references.
**
** server Reference to the server.  Must be non-NULL.
*/
Boolean MyTI_ServerRelease(TIServerRef server);


/*
** MyTI_ServerShow
**
** Debug utility for examining a server reference.  Comparable to
** CFShow on a CFTypeRef.  It only works on server references.
**
** server Reference to the server.  Must be non-NULL.
*/
void MyTI_ServerShow(TIServerRef server);


/*
** MyTI_ServerConnect
**
** Starts the socket listening on the given port.  Registers
** the service by name and type on the local network.  The
** socket and service will be registered on the current
** run loop in the common modes.
**
** server Reference to the server.  Must be non-NULL.
**
** name		Unique name to register on the network.  NULL indicates
**			to use the computer's name.  The name must be unique in
**			the local domain and for the given service type.
**
** type 	Service type being registered.  Service types can be
**			retrieved from IANA (www.iana.org).  Examples include
**			_http._tcp. and _echo._tcp.
**
** port		TCP port on which to listen.  If a well-known port is
**			not required, set to zero and one will be assigned.
*/
Boolean MyTI_ServerConnect(TIServerRef server, CFStringRef name, CFStringRef type, CFStringRef info, long int port);


/*
** MyTI_ServerInvalidate
**
** Removes the client and its associated data from the server reference.
** This ensures that the client will no longer get callbacks associated
** with this instance of the object.
**
** server Reference to the server.  Must be non-NULL.
*/
void MyTI_ServerInvalidate(TIServerRef server);


#if defined(__cplusplus)
}
#endif

#endif	/* __TI_SERVERCONTEXT__ */