CSelector.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "CSelector.h"
  32. #include "ICommunicationSocket.h"
  33. #include <sys/epoll.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36. #include <unistd.h>
  37. CSelector::CSelector(IServerSocket* serverSock) :
  38. m_serverSock(serverSock)
  39. {
  40. m_connectedSockets.clear();
  41. FD_ZERO(&m_workingSet);
  42. }
  43. CSelector::~CSelector()
  44. {
  45. }
  46. void CSelector::addSocket(ICommunicationSocket* socket)
  47. {
  48. m_connectedSockets.push_back(socket);
  49. }
  50. void CSelector::removeSocket(ICommunicationSocket* socket)
  51. {
  52. m_connectedSockets.remove(socket);
  53. }
  54. bool CSelector::select(int fd_term)
  55. {
  56. int max_sd;
  57. std::list<ICommunicationSocket*>::const_iterator it = m_connectedSockets.begin();
  58. FD_ZERO(&m_workingSet);
  59. FD_SET(m_serverSock->getSockDescriptor(), &m_workingSet);
  60. max_sd = m_serverSock->getSockDescriptor();
  61. if (fd_term != -1) {
  62. // a pipe is setup to prevent select from blocking current thread
  63. FD_SET(fd_term, &m_workingSet);
  64. if (fd_term > max_sd)
  65. max_sd = fd_term;
  66. }
  67. for(; it!=m_connectedSockets.end(); ++it) {
  68. int sock_fd = (*it)->getSockDescriptor();
  69. if (sock_fd > max_sd)
  70. max_sd = sock_fd;
  71. FD_SET(sock_fd, &m_workingSet);
  72. }
  73. int rc = (int) TEMP_FAILURE_RETRY(::select(max_sd + 1, &m_workingSet, NULL, NULL, NULL));
  74. if (rc < 0) {
  75. throw "Select failed";
  76. }
  77. if (fd_term != -1 && FD_ISSET(fd_term, &m_workingSet))
  78. return false;
  79. return true;
  80. }
  81. bool CSelector::canAcceptConnection()
  82. {
  83. if (FD_ISSET(m_serverSock->getSockDescriptor(), &m_workingSet)) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. std::list<ICommunicationSocket*> CSelector::getSocsWithNewContent()
  90. {
  91. std::list<ICommunicationSocket*> socketswithContent;
  92. std::list<ICommunicationSocket*>::iterator it = m_connectedSockets.begin();
  93. while( it!=m_connectedSockets.end())
  94. {
  95. if (FD_ISSET((*it)->getSockDescriptor(), &m_workingSet)) {
  96. socketswithContent.push_back(*it);
  97. m_connectedSockets.erase(it++);
  98. } else {
  99. ++it;
  100. }
  101. }
  102. return socketswithContent;
  103. }