blob: d6bb7d5dfbd988476a94a8ef885a5050a2385a3a [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;
11 pthread_mutex_t mWriteMutex;
12
Kenny Root30abb722010-09-14 14:26:12 -070013 /* 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
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070022 /* Reference count (starts at 1) */
23 pthread_mutex_t mRefCountMutex;
24 int mRefCount;
25
San Mehatfa644ff2009-05-08 11:15:53 -070026public:
27 SocketClient(int sock);
28 virtual ~SocketClient() {}
29
30 int getSocket() { return mSocket; }
Kenny Root30abb722010-09-14 14:26:12 -070031 pid_t getPid() const { return mPid; }
32 uid_t getUid() const { return mUid; }
33 gid_t getGid() const { return mGid; }
San Mehatfa644ff2009-05-08 11:15:53 -070034
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070035 // Send null-terminated C strings:
San Mehatdb017542009-05-20 15:27:14 -070036 int sendMsg(int code, const char *msg, bool addErrno);
37 int sendMsg(const char *msg);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070038
39 // Sending binary data:
40 int sendData(const void *data, int len);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070041
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();
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -070047 bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
San Mehatfa644ff2009-05-08 11:15:53 -070048};
49
50typedef android::List<SocketClient *> SocketClientCollection;
51#endif