2011-03-15 19:14:43 +00:00
|
|
|
/*
|
2013-11-18 20:25:34 -05:00
|
|
|
* Copyright (c) 2008-2013, Dave Benson. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2011-03-15 19:14:43 +00:00
|
|
|
*
|
2013-11-18 20:25:34 -05:00
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following disclaimer
|
|
|
|
* in the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2011-03-15 19:14:43 +00:00
|
|
|
*
|
2013-11-18 20:25:34 -05:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-03-15 19:14:43 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
#ifndef __PROTOBUF_C_DISPATCH_H_
|
|
|
|
#define __PROTOBUF_C_DISPATCH_H_
|
|
|
|
|
|
|
|
typedef struct _ProtobufCDispatch ProtobufCDispatch;
|
|
|
|
typedef struct _ProtobufCDispatchTimer ProtobufCDispatchTimer;
|
|
|
|
typedef struct _ProtobufCDispatchIdle ProtobufCDispatchIdle;
|
|
|
|
|
|
|
|
#include "protobuf-c.h"
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PROTOBUF_C_EVENT_READABLE = (1<<0),
|
|
|
|
PROTOBUF_C_EVENT_WRITABLE = (1<<1)
|
|
|
|
} ProtobufC_Events;
|
|
|
|
|
2009-01-29 02:57:17 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
typedef SOCKET ProtobufC_FD;
|
|
|
|
#else
|
|
|
|
typedef int ProtobufC_FD;
|
|
|
|
#endif
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
/* Create or destroy a Dispatch */
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatch *protobuf_c_dispatch_new (ProtobufCAllocator *allocator);
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_free(ProtobufCDispatch *dispatch);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-25 04:49:12 +00:00
|
|
|
ProtobufCDispatch *protobuf_c_dispatch_default (void);
|
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
ProtobufCAllocator *protobuf_c_dispatch_peek_allocator (ProtobufCDispatch *);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-29 02:57:17 +00:00
|
|
|
typedef void (*ProtobufCDispatchCallback) (ProtobufC_FD fd,
|
|
|
|
unsigned events,
|
|
|
|
void *callback_data);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
/* Registering file-descriptors to watch. */
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_watch_fd (ProtobufCDispatch *dispatch,
|
2009-01-29 02:57:17 +00:00
|
|
|
ProtobufC_FD fd,
|
2009-01-21 08:57:24 +00:00
|
|
|
unsigned events,
|
2009-01-21 16:35:53 +00:00
|
|
|
ProtobufCDispatchCallback callback,
|
2009-01-21 08:57:24 +00:00
|
|
|
void *callback_data);
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_close_fd (ProtobufCDispatch *dispatch,
|
2009-01-29 02:57:17 +00:00
|
|
|
ProtobufC_FD fd);
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_fd_closed(ProtobufCDispatch *dispatch,
|
2009-01-29 02:57:17 +00:00
|
|
|
ProtobufC_FD fd);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
/* Timers */
|
|
|
|
typedef void (*ProtobufCDispatchTimerFunc) (ProtobufCDispatch *dispatch,
|
|
|
|
void *func_data);
|
|
|
|
ProtobufCDispatchTimer *
|
|
|
|
protobuf_c_dispatch_add_timer(ProtobufCDispatch *dispatch,
|
|
|
|
unsigned timeout_secs,
|
|
|
|
unsigned timeout_usecs,
|
|
|
|
ProtobufCDispatchTimerFunc func,
|
|
|
|
void *func_data);
|
2009-01-22 15:27:27 +00:00
|
|
|
ProtobufCDispatchTimer *
|
|
|
|
protobuf_c_dispatch_add_timer_millis
|
|
|
|
(ProtobufCDispatch *dispatch,
|
|
|
|
unsigned milliseconds,
|
|
|
|
ProtobufCDispatchTimerFunc func,
|
|
|
|
void *func_data);
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_remove_timer (ProtobufCDispatchTimer *);
|
|
|
|
|
|
|
|
/* Idle functions */
|
|
|
|
typedef void (*ProtobufCDispatchIdleFunc) (ProtobufCDispatch *dispatch,
|
|
|
|
void *func_data);
|
|
|
|
ProtobufCDispatchIdle *
|
|
|
|
protobuf_c_dispatch_add_idle (ProtobufCDispatch *dispatch,
|
|
|
|
ProtobufCDispatchIdleFunc func,
|
|
|
|
void *func_data);
|
|
|
|
void protobuf_c_dispatch_remove_idle (ProtobufCDispatchIdle *);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
/* --- API for use in standalone application --- */
|
|
|
|
/* Where you are happy just to run poll(2). */
|
|
|
|
|
|
|
|
/* protobuf_c_dispatch_run()
|
|
|
|
* Run one main-loop iteration, using poll(2) (or some system-level event system);
|
|
|
|
* 'timeout' is in milliseconds, -1 for no timeout.
|
|
|
|
*/
|
2009-01-21 16:35:53 +00:00
|
|
|
void protobuf_c_dispatch_run (ProtobufCDispatch *dispatch);
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* --- API for those who want to embed a dispatch into their own main-loop --- */
|
|
|
|
typedef struct {
|
|
|
|
ProtobufC_FD fd;
|
|
|
|
ProtobufC_Events events;
|
|
|
|
} ProtobufC_FDNotify;
|
|
|
|
|
2009-01-29 02:57:17 +00:00
|
|
|
typedef struct {
|
|
|
|
ProtobufC_FD fd;
|
|
|
|
ProtobufC_Events old_events;
|
|
|
|
ProtobufC_Events events;
|
|
|
|
} ProtobufC_FDNotifyChange;
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
void protobuf_c_dispatch_dispatch (ProtobufCDispatch *dispatch,
|
|
|
|
size_t n_notifies,
|
|
|
|
ProtobufC_FDNotify *notifies);
|
|
|
|
void protobuf_c_dispatch_clear_changes (ProtobufCDispatch *);
|
|
|
|
|
2009-01-21 08:57:24 +00:00
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
struct _ProtobufCDispatch
|
2009-01-21 08:57:24 +00:00
|
|
|
{
|
|
|
|
/* changes to the events you are interested in. */
|
2009-01-29 02:57:17 +00:00
|
|
|
/* (this handles closed file-descriptors
|
|
|
|
in a manner agreeable to epoll(2) and kqueue(2)) */
|
2009-01-21 08:57:24 +00:00
|
|
|
size_t n_changes;
|
2009-01-29 02:57:17 +00:00
|
|
|
ProtobufC_FDNotifyChange *changes;
|
2009-01-21 08:57:24 +00:00
|
|
|
|
|
|
|
/* the complete set of events you are interested in. */
|
|
|
|
size_t n_notifies_desired;
|
|
|
|
ProtobufC_FDNotify *notifies_desired;
|
|
|
|
|
2009-01-21 16:35:53 +00:00
|
|
|
/* number of milliseconds to wait if no events occur */
|
|
|
|
protobuf_c_boolean has_timeout;
|
|
|
|
unsigned long timeout_secs;
|
|
|
|
unsigned timeout_usecs;
|
|
|
|
|
|
|
|
/* true if there is an idle function, in which case polling with
|
|
|
|
timeout 0 is appropriate */
|
|
|
|
protobuf_c_boolean has_idle;
|
|
|
|
|
2009-01-22 15:27:27 +00:00
|
|
|
unsigned long last_dispatch_secs;
|
|
|
|
unsigned last_dispatch_usecs;
|
|
|
|
|
2009-01-29 02:57:17 +00:00
|
|
|
/* private data follows (see RealDispatch structure in .c file) */
|
2009-01-21 08:57:24 +00:00
|
|
|
};
|
|
|
|
|
2009-01-28 15:16:11 +00:00
|
|
|
void protobuf_c_dispatch_destroy_default (void);
|
2009-01-22 15:27:27 +00:00
|
|
|
|
|
|
|
#endif
|