San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 1 | #ifndef _SOCKET_CLIENT_H |
| 2 | #define _SOCKET_CLIENT_H |
| 3 | |
| 4 | #include "../../../frameworks/base/include/utils/List.h" |
| 5 | |
| 6 | #include <pthread.h> |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 7 | #include <sys/types.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 8 | |
| 9 | class SocketClient { |
| 10 | int mSocket; |
| 11 | pthread_mutex_t mWriteMutex; |
| 12 | |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 13 | /* Peer process ID */ |
| 14 | pid_t mPid; |
| 15 | |
| 16 | /* Peer user ID */ |
| 17 | uid_t mUid; |
| 18 | |
| 19 | /* Peer group ID */ |
| 20 | gid_t mGid; |
| 21 | |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 22 | /* Reference count (starts at 1) */ |
| 23 | pthread_mutex_t mRefCountMutex; |
| 24 | int mRefCount; |
| 25 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 26 | public: |
| 27 | SocketClient(int sock); |
| 28 | virtual ~SocketClient() {} |
| 29 | |
| 30 | int getSocket() { return mSocket; } |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 31 | pid_t getPid() const { return mPid; } |
| 32 | uid_t getUid() const { return mUid; } |
| 33 | gid_t getGid() const { return mGid; } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 34 | |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 35 | // Send null-terminated C strings: |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 36 | int sendMsg(int code, const char *msg, bool addErrno); |
| 37 | int sendMsg(const char *msg); |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 38 | |
| 39 | // Sending binary data: |
| 40 | int sendData(const void *data, int len); |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 41 | |
| 42 | // Optional reference counting. Reference count starts at 1. If |
| 43 | // it's decremented to 0, it deletes itself. |
| 44 | // SocketListener creates a SocketClient (at refcount 1) and calls |
| 45 | // decRef() when it's done with the client. |
| 46 | void incRef(); |
Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 47 | bool decRef(); // returns true at 0 (but note: SocketClient already deleted) |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | typedef android::List<SocketClient *> SocketClientCollection; |
| 51 | #endif |