File size: 3,352 Bytes
e1191aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | /*
* Free RIL implementation for Samsung Android-based smartphones.
* Copyright (C) 2012 Sergey Gridasov <grindars@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include "IPCWorkerThread.h"
#include "IIPCTransport.h"
#include "IIPCSocket.h"
#include "SocketHandler.h"
#include "Log.h"
#include "IPCSocketHandler.h"
using namespace SamsungIPC;
IPCWorkerThread::IPCWorkerThread() {
if(pipe(m_controlPipe) == -1)
Log::panicErrno("pipe");
}
IPCWorkerThread::~IPCWorkerThread() {
close(m_controlPipe[0]);
close(m_controlPipe[1]);
}
int IPCWorkerThread::run() {
dispatchEvents();
for(std::list<SocketHandler *>::iterator it = m_handlers.begin(), end = m_handlers.end(); it != end; it++) {
delete *it;
}
return 0;
}
void IPCWorkerThread::addHandler(SocketHandler *handler) {
m_handlers.push_back(handler);
}
void IPCWorkerThread::removeHandler(SocketHandler *handler) {
m_handlers.remove(handler);
}
void IPCWorkerThread::dispatchEvents() {
while(1) {
int maxfd = 0;
fd_set read_set, write_set;
for(std::list<SocketHandler *>::iterator it = m_handlers.begin(), end = m_handlers.end(); it != end; it++) {
SocketHandler *handler = *it;
int fd = handler->fd();
maxfd = std::max<int>(maxfd, fd);
if(handler->wantRead())
FD_SET(fd, &read_set);
if(handler->wantWrite())
FD_SET(fd, &write_set);
}
FD_SET(m_controlPipe[0], &read_set);
int ret = select(maxfd + 1, &read_set, &write_set, NULL, NULL);
if(ret == -1)
Log::panicErrno("select");
for(std::list<SocketHandler *>::iterator it = m_handlers.begin(), end = m_handlers.end(); it != end; it++) {
SocketHandler *handler = *it;
int fd = handler->fd();
if(FD_ISSET(fd, &read_set))
handler->readable();
if(FD_ISSET(fd, &write_set))
handler->writable();
}
if(FD_ISSET(m_controlPipe[0], &read_set)) {
Message *msg;
int bytes = read(m_controlPipe[0], &msg, sizeof(msg));
if(bytes == -1)
Log::panicErrno("read(controlPipe)");
if(msg == NULL) {
Log::info("Shutdown requested");
break;
} else {
static_cast<IPCSocketHandler *>(m_handlers.front())->submit(msg);
}
}
}
}
void IPCWorkerThread::submit(Message *message) {
int bytes = write(m_controlPipe[1], &message, sizeof(message));
if(bytes != sizeof(message))
Log::panicErrno("write(controlPipe)");
}
|