blob: 7d2b1d67c5fdb1ca64a07326373cadc52b3bee7d [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
San Mehatfa644ff2009-05-08 11:15:53 -070027public:
Xianzhu Wang45202462011-09-29 12:59:55 +080028 SocketClient(int sock, bool owned);
29 virtual ~SocketClient();
San Mehatfa644ff2009-05-08 11:15:53 -070030
31 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070032 pid_t getPid() const { return mPid; }
33 uid_t getUid() const { return mUid; }
34 gid_t getGid() const { return mGid; }
San Mehatfa644ff2009-05-08 11:15:53 -070035
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070036 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070037 int sendMsg(int code, const char *msg, bool addErrno);
38 int sendMsg(const char *msg);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070039
40 // Sending binary data:
41 int sendData(const void *data, int len);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070042
43 // Optional reference counting. Reference count starts at 1. If
44 // it's decremented to 0, it deletes itself.
45 // SocketListener creates a SocketClient (at refcount 1) and calls
46 // decRef() when it's done with the client.
47 void incRef();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070048 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
San Mehatfa644ff2009-05-08 11:15:53 -070049};
50
51typedef android::List<SocketClient *> SocketClientCollection;
52#endif