blob: cee863fd900bb7bd84d0af31df2f81bb18c19db9 [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#ifndef _SOCKET_CLIENT_H
2#define _SOCKET_CLIENT_H
3
Mathias Agopianb7286aa2012-03-05 16:45:55 -08004#include "List.h"
San Mehatfa644ff2009-05-08 11:15:53 -07005
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);
Guang Zhua8185a62012-02-07 19:13:28 -080038 int sendMsg(const char *msg);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070039
Guang Zhua8185a62012-02-07 19:13:28 -080040 // Sending binary data:
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070041 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
Mathias Agopianb7286aa2012-03-05 16:45:55 -080051typedef android::sysutils::List<SocketClient *> SocketClientCollection;
San Mehatfa644ff2009-05-08 11:15:53 -070052#endif