2009-05-08 18:15:53 +00:00
|
|
|
#ifndef _SOCKET_CLIENT_H
|
|
|
|
#define _SOCKET_CLIENT_H
|
|
|
|
|
2012-03-06 00:45:55 +00:00
|
|
|
#include "List.h"
|
2009-05-08 18:15:53 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
2012-02-07 20:23:14 +00:00
|
|
|
#include <cutils/atomic.h>
|
2010-09-14 21:26:12 +00:00
|
|
|
#include <sys/types.h>
|
2009-05-08 18:15:53 +00:00
|
|
|
|
|
|
|
class SocketClient {
|
|
|
|
int mSocket;
|
2011-09-29 04:59:55 +00:00
|
|
|
bool mSocketOwned;
|
2009-05-08 18:15:53 +00:00
|
|
|
pthread_mutex_t mWriteMutex;
|
|
|
|
|
2010-09-14 21:26:12 +00:00
|
|
|
/* Peer process ID */
|
|
|
|
pid_t mPid;
|
|
|
|
|
|
|
|
/* Peer user ID */
|
|
|
|
uid_t mUid;
|
|
|
|
|
|
|
|
/* Peer group ID */
|
|
|
|
gid_t mGid;
|
|
|
|
|
2011-03-17 22:41:20 +00:00
|
|
|
/* Reference count (starts at 1) */
|
|
|
|
pthread_mutex_t mRefCountMutex;
|
|
|
|
int mRefCount;
|
|
|
|
|
2012-02-07 20:23:14 +00:00
|
|
|
int mCmdNum;
|
|
|
|
|
|
|
|
bool mUseCmdNum;
|
|
|
|
|
2009-05-08 18:15:53 +00:00
|
|
|
public:
|
2011-09-29 04:59:55 +00:00
|
|
|
SocketClient(int sock, bool owned);
|
2012-02-07 20:23:14 +00:00
|
|
|
SocketClient(int sock, bool owned, bool useCmdNum);
|
2011-09-29 04:59:55 +00:00
|
|
|
virtual ~SocketClient();
|
2009-05-08 18:15:53 +00:00
|
|
|
|
|
|
|
int getSocket() { return mSocket; }
|
2010-09-14 21:26:12 +00:00
|
|
|
pid_t getPid() const { return mPid; }
|
|
|
|
uid_t getUid() const { return mUid; }
|
|
|
|
gid_t getGid() const { return mGid; }
|
2012-02-07 20:23:14 +00:00
|
|
|
void setCmdNum(int cmdNum) { android_atomic_release_store(cmdNum, &mCmdNum); }
|
|
|
|
int getCmdNum() { return mCmdNum; }
|
2009-05-08 18:15:53 +00:00
|
|
|
|
2010-10-27 17:23:16 +00:00
|
|
|
// Send null-terminated C strings:
|
2009-05-20 22:27:14 +00:00
|
|
|
int sendMsg(int code, const char *msg, bool addErrno);
|
2012-02-07 20:23:14 +00:00
|
|
|
int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
|
2010-10-27 17:23:16 +00:00
|
|
|
|
2012-02-07 20:23:14 +00:00
|
|
|
//Sending binary data:
|
2010-10-27 17:23:16 +00:00
|
|
|
int sendData(const void *data, int len);
|
2011-03-17 22:41:20 +00:00
|
|
|
|
|
|
|
// Optional reference counting. Reference count starts at 1. If
|
|
|
|
// it's decremented to 0, it deletes itself.
|
|
|
|
// SocketListener creates a SocketClient (at refcount 1) and calls
|
|
|
|
// decRef() when it's done with the client.
|
|
|
|
void incRef();
|
2011-03-18 00:14:46 +00:00
|
|
|
bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
|
2012-02-07 20:23:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Send null-terminated C strings
|
|
|
|
int sendMsg(const char *msg);
|
|
|
|
void init(int socket, bool owned, bool useCmdNum);
|
2009-05-08 18:15:53 +00:00
|
|
|
};
|
|
|
|
|
2012-03-06 00:45:55 +00:00
|
|
|
typedef android::sysutils::List<SocketClient *> SocketClientCollection;
|
2009-05-08 18:15:53 +00:00
|
|
|
#endif
|