From 1e05e78e3d82fb2ef39e08a9e6fb3ca1cd918fcc Mon Sep 17 00:00:00 2001 From: zhengliming Date: Thu, 20 Oct 2022 16:59:01 +0800 Subject: [PATCH] Seperate sockets into different clients. Using the handle of client to generate different socket address. Now different clients in the same process can use different socket address to transmit data at the same time. Signed-off-by: zhengliming --- include/ipc_base.h | 2 ++ include/ipc_object_proxy.h | 2 +- include/ipc_skeleton.h | 1 + ipc_object_proxy.cpp | 3 ++- ipc_object_stub.cpp | 4 +++- ipc_skeleton.cpp | 6 ++++++ 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/ipc_base.h b/include/ipc_base.h index 9be4654..833fb55 100644 --- a/include/ipc_base.h +++ b/include/ipc_base.h @@ -20,6 +20,8 @@ const int IPC_SHM_FLAG = IPC_CREAT | 0666; const size_t DATA_SIZE = 0x20000; +const unsigned int MAX_SOCKET_ADDR_LEN = 128; + const int32_t GET_SA_REQUEST_CODE = 2; const int32_t WIFI_DEVICE_ABILITY_ID = 1125; const int32_t WIFI_P2P_ABILITY_ID = 1128; diff --git a/include/ipc_object_proxy.h b/include/ipc_object_proxy.h index a130e69..5148b11 100644 --- a/include/ipc_object_proxy.h +++ b/include/ipc_object_proxy.h @@ -15,7 +15,7 @@ public: void SendObituary(); private: key_t sendShmKey_; - const char *socketAddr_; + char socketAddr_[MAX_SOCKET_ADDR_LEN]; int recvFd_; sptr< DeathRecipient > deathRecipient_; }; diff --git a/include/ipc_skeleton.h b/include/ipc_skeleton.h index 3106243..ae9899b 100644 --- a/include/ipc_skeleton.h +++ b/include/ipc_skeleton.h @@ -24,6 +24,7 @@ public: static bool SocketWriteFd(const char *addr, int fd); static bool SetDeviceAuthObj(sptr< IRemoteObject > obj); static sptr< IRemoteObject > GetDeviceAuthObj(); + static int HandleToPid(unsigned long long handle); private: static sptr< IRemoteObject > obj_; diff --git a/ipc_object_proxy.cpp b/ipc_object_proxy.cpp index 8368e95..c03cf6c 100644 --- a/ipc_object_proxy.cpp +++ b/ipc_object_proxy.cpp @@ -7,9 +7,10 @@ namespace OHOS { static const char *IPC_CLIENT_SOCKET_ADDR = "/tmp/ipc.socket.client"; IPCObjectProxy::IPCObjectProxy(unsigned long long handle) : - socketAddr_(IPC_CLIENT_SOCKET_ADDR), recvFd_(-1), deathRecipient_(nullptr) + recvFd_(-1), deathRecipient_(nullptr) { IPC_LOG("INSERT PROXY with handle=%llx\n", handle); + sprintf(socketAddr_, "%s.%llx", IPC_CLIENT_SOCKET_ADDR, handle); handle_ = handle; sendShmKey_ = HandleToKey(handle); } diff --git a/ipc_object_stub.cpp b/ipc_object_stub.cpp index 4f74983..86514c2 100644 --- a/ipc_object_stub.cpp +++ b/ipc_object_stub.cpp @@ -39,7 +39,9 @@ void IPCObjectStub::CreateThread(key_t shmKey) IpcCenter::threadNum_++; std::thread new_thread(std::bind(IpcCenter::ProcessHandle, shmKey, this)); new_thread.detach(); - recvFd_ = IPCSkeleton::SocketListening(IPC_CLIENT_SOCKET_ADDR); + char cliend_socket_addr[MAX_SOCKET_ADDR_LEN]; + sprintf(cliend_socket_addr, "%s.%llx", IPC_CLIENT_SOCKET_ADDR, handle_); + recvFd_ = IPCSkeleton::SocketListening(cliend_socket_addr); IPCSocketManager::InsertSocketFd(1, recvFd_); if (recvFd_ < 0) { IPC_LOG("Stub socket listen failed\n"); diff --git a/ipc_skeleton.cpp b/ipc_skeleton.cpp index 7259cf9..291d035 100644 --- a/ipc_skeleton.cpp +++ b/ipc_skeleton.cpp @@ -175,4 +175,10 @@ bool IPCSkeleton::SocketWriteFd(const char *addr, int fd) return ret >= 0; } +int IPCSkeleton::HandleToPid(unsigned long long handle) +{ + handle >>= 32; + return (int)handle; +} + } //namespace OHOS -- Gitee