SocketClient.h revision dc58e73071aa829a5038caf37211f6b3e2d7b275
1#ifndef _SOCKET_CLIENT_H 2#define _SOCKET_CLIENT_H 3 4#include "../../../frameworks/base/include/utils/List.h" 5 6#include <pthread.h> 7#include <sys/types.h> 8 9class SocketClient { 10 int mSocket; 11 bool mSocketOwned; 12 pthread_mutex_t mWriteMutex; 13 14 /* Peer process ID */ 15 pid_t mPid; 16 17 /* Peer user ID */ 18 uid_t mUid; 19 20 /* Peer group ID */ 21 gid_t mGid; 22 23 /* Reference count (starts at 1) */ 24 pthread_mutex_t mRefCountMutex; 25 int mRefCount; 26 27 pthread_mutex_t mCmdNumMutex; 28 int mCmdNum; 29 30public: 31 SocketClient(int sock, bool owned); 32 virtual ~SocketClient(); 33 34 int getSocket() { return mSocket; } 35 pid_t getPid() const { return mPid; } 36 uid_t getUid() const { return mUid; } 37 gid_t getGid() const { return mGid; } 38 void setCmdNum(int cmdNum); 39 int getCmdNum(); 40 41 // Send null-terminated C strings: 42 int sendMsg(int code, const char *msg, bool addErrno); 43 44 //Sending binary data: 45 int sendData(const void *data, int len); 46 47 // Optional reference counting. Reference count starts at 1. If 48 // it's decremented to 0, it deletes itself. 49 // SocketListener creates a SocketClient (at refcount 1) and calls 50 // decRef() when it's done with the client. 51 void incRef(); 52 bool decRef(); // returns true at 0 (but note: SocketClient already deleted) 53 54private: 55 // Send null-terminated C strings 56 int sendMsg(const char *msg); 57}; 58 59typedef android::List<SocketClient *> SocketClientCollection; 60#endif 61