PageRenderTime 46ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/google/protobuf-c/protobuf-c-dispatch.h

http://protobuf-c.googlecode.com/
C Header | 167 lines | 83 code | 27 blank | 57 comment | 0 complexity | 3aa2b832762cfdbd007d0b337fe3960b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2008-2011, Dave Benson.
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with
  7. * or without modification, are permitted provided that the
  8. * following conditions are met:
  9. *
  10. * Redistributions of source code must retain the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer.
  13. * Redistributions in binary form must reproduce
  14. * the above copyright notice, this list of conditions and
  15. * the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * Neither the name
  19. * of "protobuf-c" nor the names of its contributors
  20. * may be used to endorse or promote products derived from
  21. * this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
  28. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  31. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #ifndef __PROTOBUF_C_DISPATCH_H_
  39. #define __PROTOBUF_C_DISPATCH_H_
  40. typedef struct _ProtobufCDispatch ProtobufCDispatch;
  41. typedef struct _ProtobufCDispatchTimer ProtobufCDispatchTimer;
  42. typedef struct _ProtobufCDispatchIdle ProtobufCDispatchIdle;
  43. #include "protobuf-c.h"
  44. typedef enum
  45. {
  46. PROTOBUF_C_EVENT_READABLE = (1<<0),
  47. PROTOBUF_C_EVENT_WRITABLE = (1<<1)
  48. } ProtobufC_Events;
  49. #ifdef WIN32
  50. typedef SOCKET ProtobufC_FD;
  51. #else
  52. typedef int ProtobufC_FD;
  53. #endif
  54. /* Create or destroy a Dispatch */
  55. ProtobufCDispatch *protobuf_c_dispatch_new (ProtobufCAllocator *allocator);
  56. void protobuf_c_dispatch_free(ProtobufCDispatch *dispatch);
  57. ProtobufCDispatch *protobuf_c_dispatch_default (void);
  58. ProtobufCAllocator *protobuf_c_dispatch_peek_allocator (ProtobufCDispatch *);
  59. typedef void (*ProtobufCDispatchCallback) (ProtobufC_FD fd,
  60. unsigned events,
  61. void *callback_data);
  62. /* Registering file-descriptors to watch. */
  63. void protobuf_c_dispatch_watch_fd (ProtobufCDispatch *dispatch,
  64. ProtobufC_FD fd,
  65. unsigned events,
  66. ProtobufCDispatchCallback callback,
  67. void *callback_data);
  68. void protobuf_c_dispatch_close_fd (ProtobufCDispatch *dispatch,
  69. ProtobufC_FD fd);
  70. void protobuf_c_dispatch_fd_closed(ProtobufCDispatch *dispatch,
  71. ProtobufC_FD fd);
  72. /* Timers */
  73. typedef void (*ProtobufCDispatchTimerFunc) (ProtobufCDispatch *dispatch,
  74. void *func_data);
  75. ProtobufCDispatchTimer *
  76. protobuf_c_dispatch_add_timer(ProtobufCDispatch *dispatch,
  77. unsigned timeout_secs,
  78. unsigned timeout_usecs,
  79. ProtobufCDispatchTimerFunc func,
  80. void *func_data);
  81. ProtobufCDispatchTimer *
  82. protobuf_c_dispatch_add_timer_millis
  83. (ProtobufCDispatch *dispatch,
  84. unsigned milliseconds,
  85. ProtobufCDispatchTimerFunc func,
  86. void *func_data);
  87. void protobuf_c_dispatch_remove_timer (ProtobufCDispatchTimer *);
  88. /* Idle functions */
  89. typedef void (*ProtobufCDispatchIdleFunc) (ProtobufCDispatch *dispatch,
  90. void *func_data);
  91. ProtobufCDispatchIdle *
  92. protobuf_c_dispatch_add_idle (ProtobufCDispatch *dispatch,
  93. ProtobufCDispatchIdleFunc func,
  94. void *func_data);
  95. void protobuf_c_dispatch_remove_idle (ProtobufCDispatchIdle *);
  96. /* --- API for use in standalone application --- */
  97. /* Where you are happy just to run poll(2). */
  98. /* protobuf_c_dispatch_run()
  99. * Run one main-loop iteration, using poll(2) (or some system-level event system);
  100. * 'timeout' is in milliseconds, -1 for no timeout.
  101. */
  102. void protobuf_c_dispatch_run (ProtobufCDispatch *dispatch);
  103. /* --- API for those who want to embed a dispatch into their own main-loop --- */
  104. typedef struct {
  105. ProtobufC_FD fd;
  106. ProtobufC_Events events;
  107. } ProtobufC_FDNotify;
  108. typedef struct {
  109. ProtobufC_FD fd;
  110. ProtobufC_Events old_events;
  111. ProtobufC_Events events;
  112. } ProtobufC_FDNotifyChange;
  113. void protobuf_c_dispatch_dispatch (ProtobufCDispatch *dispatch,
  114. size_t n_notifies,
  115. ProtobufC_FDNotify *notifies);
  116. void protobuf_c_dispatch_clear_changes (ProtobufCDispatch *);
  117. struct _ProtobufCDispatch
  118. {
  119. /* changes to the events you are interested in. */
  120. /* (this handles closed file-descriptors
  121. in a manner agreeable to epoll(2) and kqueue(2)) */
  122. size_t n_changes;
  123. ProtobufC_FDNotifyChange *changes;
  124. /* the complete set of events you are interested in. */
  125. size_t n_notifies_desired;
  126. ProtobufC_FDNotify *notifies_desired;
  127. /* number of milliseconds to wait if no events occur */
  128. protobuf_c_boolean has_timeout;
  129. unsigned long timeout_secs;
  130. unsigned timeout_usecs;
  131. /* true if there is an idle function, in which case polling with
  132. timeout 0 is appropriate */
  133. protobuf_c_boolean has_idle;
  134. unsigned long last_dispatch_secs;
  135. unsigned last_dispatch_usecs;
  136. /* private data follows (see RealDispatch structure in .c file) */
  137. };
  138. void protobuf_c_dispatch_destroy_default (void);
  139. #endif