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