blob: 6473b3ae7c1b9f5441d2a0d9b0bf59563fa4bcd3 [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#ifndef _SOCKET_CLIENT_H
2#define _SOCKET_CLIENT_H
3
4#include "../../../frameworks/base/include/utils/List.h"
5
6#include <pthread.h>
Robert Greenwalt8702bb12012-02-07 12:23:14 -08007#include <cutils/atomic.h>
Kenny Root30abb722010-09-14 14:26:12 -07008#include <sys/types.h>
San Mehatfa644ff2009-05-08 11:15:53 -07009
10class SocketClient {
11 int mSocket;
Xianzhu Wang45202462011-09-29 12:59:55 +080012 bool mSocketOwned;
San Mehatfa644ff2009-05-08 11:15:53 -070013 pthread_mutex_t mWriteMutex;
14
Kenny Root30abb722010-09-14 14:26:12 -070015 /* 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 Fitzpatrick648ebad2011-03-17 15:41:20 -070024 /* Reference count (starts at 1) */
25 pthread_mutex_t mRefCountMutex;
26 int mRefCount;
27
Robert Greenwalt8702bb12012-02-07 12:23:14 -080028 int mCmdNum;
29
30 bool mUseCmdNum;
31
San Mehatfa644ff2009-05-08 11:15:53 -070032public:
Xianzhu Wang45202462011-09-29 12:59:55 +080033 SocketClient(int sock, bool owned);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080034 SocketClient(int sock, bool owned, bool useCmdNum);
Xianzhu Wang45202462011-09-29 12:59:55 +080035 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070036
37 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070038 pid_t getPid() const { return mPid; }
39 uid_t getUid() const { return mUid; }
40 gid_t getGid() const { return mGid; }
Robert Greenwalt8702bb12012-02-07 12:23:14 -080041 void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); }
42 int getCmdNum() { return mCmdNum; }
San Mehatfa644ff2009-05-08 11:15:53 -070043
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070044 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070045 int sendMsg(int code, const char *msg, bool addErrno);
Robert Greenwalt8702bb12012-02-07 12:23:14 -080046 int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070047
Robert Greenwalt8702bb12012-02-07 12:23:14 -080048 //Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070049 int sendData(const void *data, int len);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070050
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();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070056 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
Robert Greenwalt8702bb12012-02-07 12:23:14 -080057
58private:
59 // Send null-terminated C strings
60 int sendMsg(const char *msg);
61 void init(int socket, bool owned, bool useCmdNum);
San Mehatfa644ff2009-05-08 11:15:53 -070062};
63
64typedef android::List<SocketClient *> SocketClientCollection;
65#endif