sock_select_symbian.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/sock_select.h>
  20. #include <pj/array.h>
  21. #include <pj/assert.h>
  22. #include <pj/os.h>
  23. #include "os_symbian.h"
  24. struct symbian_fd_set
  25. {
  26. unsigned count;
  27. CPjSocket *sock[PJ_IOQUEUE_MAX_HANDLES];
  28. };
  29. PJ_DEF(void) PJ_FD_ZERO(pj_fd_set_t *fdsetp)
  30. {
  31. symbian_fd_set *fds = (symbian_fd_set *)fdsetp;
  32. fds->count = 0;
  33. }
  34. PJ_DEF(void) PJ_FD_SET(pj_sock_t fd, pj_fd_set_t *fdsetp)
  35. {
  36. symbian_fd_set *fds = (symbian_fd_set *)fdsetp;
  37. PJ_ASSERT_ON_FAIL(fds->count < PJ_IOQUEUE_MAX_HANDLES, return);
  38. fds->sock[fds->count++] = (CPjSocket*)fd;
  39. }
  40. PJ_DEF(void) PJ_FD_CLR(pj_sock_t fd, pj_fd_set_t *fdsetp)
  41. {
  42. symbian_fd_set *fds = (symbian_fd_set *)fdsetp;
  43. unsigned i;
  44. for (i=0; i<fds->count; ++i) {
  45. if (fds->sock[i] == (CPjSocket*)fd) {
  46. pj_array_erase(fds->sock, sizeof(fds->sock[0]), fds->count, i);
  47. --fds->count;
  48. return;
  49. }
  50. }
  51. }
  52. PJ_DEF(pj_bool_t) PJ_FD_ISSET(pj_sock_t fd, const pj_fd_set_t *fdsetp)
  53. {
  54. symbian_fd_set *fds = (symbian_fd_set *)fdsetp;
  55. unsigned i;
  56. for (i=0; i<fds->count; ++i) {
  57. if (fds->sock[i] == (CPjSocket*)fd) {
  58. return PJ_TRUE;
  59. }
  60. }
  61. return PJ_FALSE;
  62. }
  63. PJ_DEF(pj_size_t) PJ_FD_COUNT(const pj_fd_set_t *fdsetp)
  64. {
  65. symbian_fd_set *fds = (symbian_fd_set *)fdsetp;
  66. return fds->count;
  67. }
  68. PJ_DEF(int) pj_sock_select( int n,
  69. pj_fd_set_t *readfds,
  70. pj_fd_set_t *writefds,
  71. pj_fd_set_t *exceptfds,
  72. const pj_time_val *timeout)
  73. {
  74. CPjTimeoutTimer *pjTimer;
  75. unsigned i;
  76. PJ_UNUSED_ARG(n);
  77. PJ_UNUSED_ARG(writefds);
  78. PJ_UNUSED_ARG(exceptfds);
  79. if (timeout) {
  80. pjTimer = PjSymbianOS::Instance()->SelectTimeoutTimer();
  81. pjTimer->StartTimer(timeout->sec*1000 + timeout->msec);
  82. } else {
  83. pjTimer = NULL;
  84. }
  85. /* Scan for readable sockets */
  86. if (readfds) {
  87. symbian_fd_set *fds = (symbian_fd_set *)readfds;
  88. do {
  89. /* Scan sockets for readily available data */
  90. for (i=0; i<fds->count; ++i) {
  91. CPjSocket *pjsock = fds->sock[i];
  92. if (pjsock->Reader()) {
  93. if (pjsock->Reader()->HasData() && !pjsock->Reader()->IsActive()) {
  94. /* Found socket with data ready */
  95. PJ_FD_ZERO(readfds);
  96. PJ_FD_SET((pj_sock_t)pjsock, readfds);
  97. /* Cancel timer, if any */
  98. if (pjTimer) {
  99. pjTimer->Cancel();
  100. }
  101. /* Clear writable and exception fd_set */
  102. if (writefds)
  103. PJ_FD_ZERO(writefds);
  104. if (exceptfds)
  105. PJ_FD_ZERO(exceptfds);
  106. return 1;
  107. } else if (!pjsock->Reader()->IsActive())
  108. pjsock->Reader()->StartRecvFrom();
  109. } else {
  110. pjsock->CreateReader();
  111. pjsock->Reader()->StartRecvFrom();
  112. }
  113. }
  114. PjSymbianOS::Instance()->WaitForActiveObjects();
  115. } while (pjTimer==NULL || !pjTimer->HasTimedOut());
  116. }
  117. /* Timeout */
  118. if (readfds)
  119. PJ_FD_ZERO(readfds);
  120. if (writefds)
  121. PJ_FD_ZERO(writefds);
  122. if (exceptfds)
  123. PJ_FD_ZERO(exceptfds);
  124. return 0;
  125. }