1 /* 2 * Copyright (C) 2008-2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef _SOCKETLISTENER_H 17 #define _SOCKETLISTENER_H 18 19 #include <pthread.h> 20 21 #include <sysutils/SocketClient.h> 22 #include "SocketClientCommand.h" 23 24 class SocketListener { 25 bool mListen; 26 const char *mSocketName; 27 int mSock; 28 SocketClientCollection *mClients; 29 pthread_mutex_t mClientsLock; 30 int mCtrlPipe[2]; 31 pthread_t mThread; 32 bool mUseCmdNum; 33 34 public: 35 SocketListener(const char *socketName, bool listen); 36 SocketListener(const char *socketName, bool listen, bool useCmdNum); 37 SocketListener(int socketFd, bool listen); 38 39 virtual ~SocketListener(); 40 int startListener(); 41 int startListener(int backlog); 42 int stopListener(); 43 44 void sendBroadcast(int code, const char *msg, bool addErrno); 45 46 void runOnEachSocket(SocketClientCommand *command); 47 release(SocketClient * c)48 bool release(SocketClient *c) { return release(c, true); } 49 50 protected: 51 virtual bool onDataAvailable(SocketClient *c) = 0; 52 53 private: 54 bool release(SocketClient *c, bool wakeup); 55 static void *threadStart(void *obj); 56 void runListener(); 57 void init(const char *socketName, int socketFd, bool listen, bool useCmdNum); 58 }; 59 #endif 60