blob: 96323ac106bd05918db9070c4cb2bd2fcbd92698 [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>
Kenny Root30abb722010-09-14 14:26:12 -07007#include <sys/types.h>
San Mehatfa644ff2009-05-08 11:15:53 -07008
9class SocketClient {
10 int mSocket;
Xianzhu Wang45202462011-09-29 12:59:55 +080011 bool mSocketOwned;
San Mehatfa644ff2009-05-08 11:15:53 -070012 pthread_mutex_t mWriteMutex;
13
Kenny Root30abb722010-09-14 14:26:12 -070014 /* Peer process ID */
15 pid_t mPid;
16
17 /* Peer user ID */
18 uid_t mUid;
19
20 /* Peer group ID */
21 gid_t mGid;
22
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070023 /* Reference count (starts at 1) */
24 pthread_mutex_t mRefCountMutex;
25 int mRefCount;
26
Robert Greenwaltdc58e732012-02-07 12:23:14 -080027 pthread_mutex_t mCmdNumMutex;
28 int mCmdNum;
29
San Mehatfa644ff2009-05-08 11:15:53 -070030public:
Xianzhu Wang45202462011-09-29 12:59:55 +080031 SocketClient(int sock, bool owned);
32 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070033
34 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070035 pid_t getPid() const { return mPid; }
36 uid_t getUid() const { return mUid; }
37 gid_t getGid() const { return mGid; }
Robert Greenwaltdc58e732012-02-07 12:23:14 -080038 void setCmdNum(int cmdNum);
39 int getCmdNum();
San Mehatfa644ff2009-05-08 11:15:53 -070040
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070041 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070042 int sendMsg(int code, const char *msg, bool addErrno);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070043
Robert Greenwaltdc58e732012-02-07 12:23:14 -080044 //Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070045 int sendData(const void *data, int len);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070046
47 // Optional reference counting. Reference count starts at 1. If
48 // it's decremented to 0, it deletes itself.
49 // SocketListener creates a SocketClient (at refcount 1) and calls
50 // decRef() when it's done with the client.
51 void incRef();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070052 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
Robert Greenwaltdc58e732012-02-07 12:23:14 -080053
54private:
55 // Send null-terminated C strings
56 int sendMsg(const char *msg);
San Mehatfa644ff2009-05-08 11:15:53 -070057};
58
59typedef android::List<SocketClient *> SocketClientCollection;
60#endif