/fbus/fbus_source_listen.c

http://ftk.googlecode.com/ · C · 121 lines · 73 code · 19 blank · 29 comment · 9 complexity · 511106b487f0cc39188e86d663fe9578 MD5 · raw file

  1. /*
  2. * File: fbus_source_listen.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: a source to listen on a socket port.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_globals.h"
  31. #include "ftk_main_loop.h"
  32. #include "fbus_stream_socket.h"
  33. #include "fbus_source_listen.h"
  34. #include "fbus_source_service.h"
  35. typedef struct _PrivInfo
  36. {
  37. int sock_no;
  38. FBusService* service;
  39. }PrivInfo;
  40. static int fbus_source_listen_get_fd(FtkSource* thiz)
  41. {
  42. DECL_PRIV(thiz, priv);
  43. return priv->sock_no;
  44. }
  45. static int fbus_source_listen_check(FtkSource* thiz)
  46. {
  47. return -1;
  48. }
  49. Ret fbus_source_listen_dispatch(FtkSource* thiz)
  50. {
  51. FtkSource* source = NULL;
  52. FBusStream* stream = NULL;
  53. DECL_PRIV(thiz, priv);
  54. stream = fbus_socket_accept(priv->sock_no);
  55. return_val_if_fail(stream != NULL, RET_REMOVE);
  56. source = fbus_source_service_create(priv->service, stream);
  57. if(source != NULL)
  58. {
  59. ftk_main_loop_add_source(ftk_default_main_loop(), source);
  60. }
  61. else
  62. {
  63. fbus_stream_destroy(stream);
  64. }
  65. return RET_OK;
  66. }
  67. static void fbus_source_listen_destroy(FtkSource* thiz)
  68. {
  69. if(thiz != NULL)
  70. {
  71. DECL_PRIV(thiz, priv);
  72. ftk_socket_close(priv->sock_no);
  73. fbus_service_destroy(priv->service);
  74. FTK_FREE(thiz);
  75. }
  76. return;
  77. }
  78. FtkSource* fbus_source_listen_create(FBusService* service, int port)
  79. {
  80. int sock_no = 0;
  81. FtkSource* thiz = NULL;
  82. return_val_if_fail(service != NULL && port > 0, NULL);
  83. sock_no = fbus_stream_listen(port);
  84. return_val_if_fail(sock_no > 0, NULL);
  85. thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
  86. if(thiz != NULL)
  87. {
  88. DECL_PRIV(thiz, priv);
  89. priv->sock_no = sock_no;
  90. priv->service = service;
  91. thiz->ref = 1;
  92. thiz->check = fbus_source_listen_check;
  93. thiz->get_fd = fbus_source_listen_get_fd;
  94. thiz->dispatch = fbus_source_listen_dispatch;
  95. thiz->destroy = fbus_source_listen_destroy;
  96. }
  97. else
  98. {
  99. ftk_socket_close(sock_no);
  100. }
  101. return thiz;
  102. }