SocketClient.h revision 3549b0dc2829184f9911d27a6ab0cf39b19764f1
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    pthread_mutex_t mWriteMutex;
12
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
22    /* Reference count (starts at 1) */
23    pthread_mutex_t mRefCountMutex;
24    int mRefCount;
25
26public:
27    SocketClient(int sock);
28    virtual ~SocketClient() {}
29
30    int getSocket() { return mSocket; }
31    pid_t getPid() const { return mPid; }
32    uid_t getUid() const { return mUid; }
33    gid_t getGid() const { return mGid; }
34
35    // Send null-terminated C strings:
36    int sendMsg(int code, const char *msg, bool addErrno);
37    int sendMsg(const char *msg);
38
39    // Sending binary data:
40    int sendData(const void *data, int len);
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();
47    bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
48};
49
50typedef android::List<SocketClient *> SocketClientCollection;
51#endif
52