2009-05-08 20:15:53 +02:00
|
|
|
#ifndef _SOCKET_CLIENT_H
|
|
|
|
#define _SOCKET_CLIENT_H
|
|
|
|
|
2012-03-06 01:45:55 +01:00
|
|
|
#include "List.h"
|
2009-05-08 20:15:53 +02:00
|
|
|
|
|
|
|
#include <pthread.h>
|
2012-02-07 21:23:14 +01:00
|
|
|
#include <cutils/atomic.h>
|
2010-09-14 23:26:12 +02:00
|
|
|
#include <sys/types.h>
|
2012-01-25 05:30:10 +01:00
|
|
|
#include <sys/uio.h>
|
2009-05-08 20:15:53 +02:00
|
|
|
|
|
|
|
class SocketClient {
|
|
|
|
int mSocket;
|
2011-09-29 06:59:55 +02:00
|
|
|
bool mSocketOwned;
|
2009-05-08 20:15:53 +02:00
|
|
|
pthread_mutex_t mWriteMutex;
|
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// Peer process ID
|
2010-09-14 23:26:12 +02:00
|
|
|
pid_t mPid;
|
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// Peer user ID
|
2010-09-14 23:26:12 +02:00
|
|
|
uid_t mUid;
|
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// Peer group ID
|
2010-09-14 23:26:12 +02:00
|
|
|
gid_t mGid;
|
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// Reference count (starts at 1)
|
2011-03-17 23:41:20 +01:00
|
|
|
pthread_mutex_t mRefCountMutex;
|
|
|
|
int mRefCount;
|
|
|
|
|
2012-02-07 21:23:14 +01:00
|
|
|
int mCmdNum;
|
|
|
|
|
|
|
|
bool mUseCmdNum;
|
|
|
|
|
2009-05-08 20:15:53 +02:00
|
|
|
public:
|
2011-09-29 06:59:55 +02:00
|
|
|
SocketClient(int sock, bool owned);
|
2012-02-07 21:23:14 +01:00
|
|
|
SocketClient(int sock, bool owned, bool useCmdNum);
|
2011-09-29 06:59:55 +02:00
|
|
|
virtual ~SocketClient();
|
2009-05-08 20:15:53 +02:00
|
|
|
|
|
|
|
int getSocket() { return mSocket; }
|
2010-09-14 23:26:12 +02:00
|
|
|
pid_t getPid() const { return mPid; }
|
|
|
|
uid_t getUid() const { return mUid; }
|
|
|
|
gid_t getGid() const { return mGid; }
|
2012-01-25 05:30:10 +01:00
|
|
|
void setCmdNum(int cmdNum) {
|
|
|
|
android_atomic_release_store(cmdNum, &mCmdNum);
|
|
|
|
}
|
2012-02-07 21:23:14 +01:00
|
|
|
int getCmdNum() { return mCmdNum; }
|
2009-05-08 20:15:53 +02:00
|
|
|
|
2010-10-27 19:23:16 +02:00
|
|
|
// Send null-terminated C strings:
|
2009-05-21 00:27:14 +02:00
|
|
|
int sendMsg(int code, const char *msg, bool addErrno);
|
2012-02-07 21:23:14 +01:00
|
|
|
int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);
|
2012-01-25 05:30:10 +01:00
|
|
|
int sendMsg(const char *msg);
|
2010-10-27 19:23:16 +02:00
|
|
|
|
2012-03-09 01:10:06 +01:00
|
|
|
// Provides a mechanism to send a response code to the client.
|
|
|
|
// Sends the code and a null character.
|
2012-02-28 01:04:37 +01:00
|
|
|
int sendCode(int code);
|
|
|
|
|
2012-03-09 01:10:06 +01:00
|
|
|
// Provides a mechanism to send binary data to client.
|
|
|
|
// Sends the code and a null character, followed by 4 bytes of
|
2012-02-28 01:04:37 +01:00
|
|
|
// big-endian length, and the data.
|
|
|
|
int sendBinaryMsg(int code, const void *data, int len);
|
|
|
|
|
|
|
|
// Sending binary data:
|
2010-10-27 19:23:16 +02:00
|
|
|
int sendData(const void *data, int len);
|
2012-01-25 05:30:10 +01:00
|
|
|
// iovec contents not preserved through call
|
|
|
|
int sendDatav(struct iovec *iov, int iovcnt);
|
2011-03-17 23:41:20 +01: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 01:14:46 +01:00
|
|
|
bool decRef(); // returns true at 0 (but note: SocketClient already deleted)
|
2012-02-07 21:23:14 +01:00
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// return a new string in quotes with '\\' and '\"' escaped for "my arg"
|
|
|
|
// transmissions
|
2012-04-21 00:21:07 +02:00
|
|
|
static char *quoteArg(const char *arg);
|
|
|
|
|
2012-02-07 21:23:14 +01:00
|
|
|
private:
|
|
|
|
void init(int socket, bool owned, bool useCmdNum);
|
2012-02-28 01:04:37 +01:00
|
|
|
|
2012-01-25 05:30:10 +01:00
|
|
|
// Sending binary data. The caller should make sure this is protected
|
2012-02-28 01:04:37 +01:00
|
|
|
// from multiple threads entering simultaneously.
|
2012-01-25 05:30:10 +01:00
|
|
|
// returns 0 if successful, -1 if there is a 0 byte write or if any
|
|
|
|
// other error occurred (use errno to get the error)
|
|
|
|
int sendDataLockedv(struct iovec *iov, int iovcnt);
|
2009-05-08 20:15:53 +02:00
|
|
|
};
|
|
|
|
|
2012-03-06 01:45:55 +01:00
|
|
|
typedef android::sysutils::List<SocketClient *> SocketClientCollection;
|
2009-05-08 20:15:53 +02:00
|
|
|
#endif
|