San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 1 | #ifndef _SOCKET_CLIENT_H |
| 2 | #define _SOCKET_CLIENT_H |
| 3 | |
Mathias Agopian | b7286aa | 2012-03-05 16:45:55 -0800 | [diff] [blame] | 4 | #include "List.h" |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 5 | |
| 6 | #include <pthread.h> |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 7 | #include <cutils/atomic.h> |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 8 | #include <sys/types.h> |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 9 | |
| 10 | class SocketClient { |
| 11 | int mSocket; |
Xianzhu Wang | 4520246 | 2011-09-29 12:59:55 +0800 | [diff] [blame] | 12 | bool mSocketOwned; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 13 | pthread_mutex_t mWriteMutex; |
| 14 | |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 15 | /* Peer process ID */ |
| 16 | pid_t mPid; |
| 17 | |
| 18 | /* Peer user ID */ |
| 19 | uid_t mUid; |
| 20 | |
| 21 | /* Peer group ID */ |
| 22 | gid_t mGid; |
| 23 | |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 24 | /* Reference count (starts at 1) */ |
| 25 | pthread_mutex_t mRefCountMutex; |
| 26 | int mRefCount; |
| 27 | |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 28 | int mCmdNum; |
| 29 | |
| 30 | bool mUseCmdNum; |
| 31 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 32 | public: |
Xianzhu Wang | 4520246 | 2011-09-29 12:59:55 +0800 | [diff] [blame] | 33 | SocketClient(int sock, bool owned); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 34 | SocketClient(int sock, bool owned, bool useCmdNum); |
Xianzhu Wang | 4520246 | 2011-09-29 12:59:55 +0800 | [diff] [blame] | 35 | virtual ~SocketClient(); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 36 | |
| 37 | int getSocket() { return mSocket; } |
Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 38 | pid_t getPid() const { return mPid; } |
| 39 | uid_t getUid() const { return mUid; } |
| 40 | gid_t getGid() const { return mGid; } |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 41 | void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); } |
| 42 | int getCmdNum() { return mCmdNum; } |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 43 | |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 44 | // Send null-terminated C strings: |
San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 45 | int sendMsg(int code, const char *msg, bool addErrno); |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 46 | int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum); |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 47 | |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 48 | // Provides a mechanism to send a response code to the client. |
| 49 | // Sends the code and a null character. |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 50 | int sendCode(int code); |
| 51 | |
Robert Greenwalt | 7599bfc | 2012-03-08 16:10:06 -0800 | [diff] [blame] | 52 | // Provides a mechanism to send binary data to client. |
| 53 | // Sends the code and a null character, followed by 4 bytes of |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 54 | // big-endian length, and the data. |
| 55 | int sendBinaryMsg(int code, const void *data, int len); |
| 56 | |
| 57 | // Sending binary data: |
Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 58 | int sendData(const void *data, int len); |
Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 59 | |
| 60 | // Optional reference counting. Reference count starts at 1. If |
| 61 | // it's decremented to 0, it deletes itself. |
| 62 | // SocketListener creates a SocketClient (at refcount 1) and calls |
| 63 | // decRef() when it's done with the client. |
| 64 | void incRef(); |
Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 65 | bool decRef(); // returns true at 0 (but note: SocketClient already deleted) |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 66 | |
Robert Greenwalt | 5949477 | 2012-04-20 15:21:07 -0700 | [diff] [blame] | 67 | // return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions |
| 68 | static char *quoteArg(const char *arg); |
| 69 | |
Robert Greenwalt | 8702bb1 | 2012-02-07 12:23:14 -0800 | [diff] [blame] | 70 | private: |
| 71 | // Send null-terminated C strings |
| 72 | int sendMsg(const char *msg); |
| 73 | void init(int socket, bool owned, bool useCmdNum); |
Selim Gurun | 7bf4c45 | 2012-02-27 16:04:37 -0800 | [diff] [blame] | 74 | |
| 75 | // Sending binary data. The caller should use make sure this is protected |
| 76 | // from multiple threads entering simultaneously. |
| 77 | // returns 0 if successful, -1 if there is a 0 byte write and -2 if any other |
| 78 | // error occurred (use errno to get the error) |
| 79 | int sendDataLocked(const void *data, int len); |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Mathias Agopian | b7286aa | 2012-03-05 16:45:55 -0800 | [diff] [blame] | 82 | typedef android::sysutils::List<SocketClient *> SocketClientCollection; |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 83 | #endif |