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