Skip to content

Commit 1081c11

Browse files
committed
增加ParcelSize准确计算,处理可能溢出Parcel binder size问题;增大共享内存默认空间
1 parent 5823a43 commit 1081c11

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

ipc-app-test/src/main/java/com/demo/ipc/CommonActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CommonActivity : AppCompatActivity() {
2222
override fun onCreate(savedInstanceState: Bundle?) {
2323
super.onCreate(savedInstanceState)
2424
setContentView(R.layout.activity_common)
25-
IpcManager.config(Config.builder().configDebug(true).configSharedMemoryCapacity(2*1024*1024).build())
25+
IpcManager.config(Config.builder().configDebug(true).build())
2626
IpcManager.init(this)
2727
IpcManager.open("com.demo.ipcdemo") {
2828
runOnUiThread {

ipc-app-test/src/main/java/com/demo/ipc/InfoServiceManager.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.demo.ipc
22

3+
import android.os.Binder
34
import android.util.Log
45
import com.ipc.extend.test.*
56
import com.zclever.ipc.core.*
67
import kotlin.concurrent.thread
8+
import kotlin.random.Random
79

810

911
object InfoServiceManager : InfoService {
@@ -39,7 +41,6 @@ object InfoServiceManager : InfoService {
3941
private var mCallBack: Result<Event>? = null
4042

4143
init {
42-
4344
thread {
4445
while (true) {
4546
mCallBack?.onData(Event(count++))
@@ -71,15 +72,19 @@ object InfoServiceManager : InfoService {
7172
}
7273

7374
override fun getBigByteArray(): ByteArray {
74-
return ByteArray(258_000)
75+
return Random.nextBytes(Random.nextInt(1024*1024).also {
76+
Log.i(TAG, "getBigByteArray size->${it}")
77+
})
7578
}
7679

7780
private var asyncBigByteArrayCallback:Result<ByteArray>?=null
7881

7982
override fun asyncGetBigByteArray(callBack: Result<ByteArray>) {
8083
asyncBigByteArrayCallback=callBack
8184
thread {
82-
asyncBigByteArrayCallback?.onData(ByteArray(254_998))
85+
asyncBigByteArrayCallback?.onData(Random.nextBytes(Random.nextInt(1024*500).also {
86+
Log.i(TAG, "asyncGetBigByteArray size->${it}")
87+
}))
8388
}
8489
}
8590
}

ipc-core/src/main/java/com/zclever/ipc/core/Bridge.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.os.Process
55

66
const val REQUEST_TYPE_CREATE = 1
77
const val REQUEST_TYPE_INVOKE = 2
8-
const val BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE = 510_000 //根据parcel序列化结合接口设计,最大能传输的json字节数据长度
8+
const val BINDER_MAX_TRANSFORM_PARCEL_SIZE = 600_000 //定义最大Parcel传输的size,理论是1M-8k,这里需要写小一点
99

1010
/**
1111
* 请求对象

ipc-core/src/main/java/com/zclever/ipc/core/Config.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Config private constructor(builder: Builder) {
1212
val mediaMemoryCapacity = builder.mediaMemoryCapacity
1313

1414
companion object {
15-
const val DEFAULT_MEDIA_MEMORY_SIZE = 1280 * 720 * 4 //
16-
const val SHARED_MEMORY_DEFAULT_SIZE = 5 * 1024 * 1024 //默认5M数据
15+
const val DEFAULT_MEDIA_MEMORY_SIZE = 8 * 1024 * 1024 //默认8M数据
16+
const val SHARED_MEMORY_DEFAULT_SIZE = 8 * 1024 * 1024 //默认8M数据
1717
fun builder() = Builder()
1818
}
1919

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.zclever.ipc.core
2+
3+
import android.os.Parcel
4+
5+
/*
6+
* *****************************************************************************
7+
* <p>
8+
* Copyright (C),2007-2016, LonBon Technologies Co. Ltd. All Rights Reserved.
9+
* <p>
10+
* *****************************************************************************
11+
*/
12+
object ParcelSizeHelper {
13+
14+
15+
fun getStringParcelSize(data:String):Int{
16+
17+
val parcel = Parcel.obtain()
18+
19+
parcel.writeString(data)
20+
21+
22+
val size=parcel.marshall().size
23+
24+
parcel.recycle()
25+
26+
return size
27+
}
28+
29+
30+
31+
}

ipc-core/src/main/java/com/zclever/ipc/core/client/ServiceInvocationHandler.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ internal class ServiceInvocationHandler(
5555

5656
val paramByteArray = requestParamJson.encodeToByteArray()
5757

58-
debugD("invoke requestParamJson byte size ->${paramByteArray.size}, content->$requestParamJson")
58+
val parcelSize=ParcelSizeHelper.getStringParcelSize(requestParamJson)
59+
60+
debugD("invoke requestParamJson parcelSize ->${parcelSize}, content->$requestParamJson")
5961

6062
val response =
61-
if (paramByteArray.size < BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE) {//binder传输
63+
if (parcelSize < BINDER_MAX_TRANSFORM_PARCEL_SIZE) {//binder传输
6264

6365
synchronized(ClientCache.serverResponseSharedMemory!!) {//确保同步
6466

ipc-core/src/main/java/com/zclever/ipc/core/server/ServerCallBack.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ internal class ServerCallBack(
2222

2323
val dataJson = GsonInstance.toJson(data)
2424

25+
val parcelSize=ParcelSizeHelper.getStringParcelSize(dataJson)
26+
2527
val dataJsonByteArray=dataJson.encodeToByteArray()
2628

27-
debugD("onData: $callbackKey,-------${ServiceCache.remoteClients.getClientByPid(pid)},size->${dataJsonByteArray.size}")
29+
debugD("onData: $callbackKey,-------${ServiceCache.remoteClients.getClientByPid(pid)},size->${dataJsonByteArray.size},parcelSize->${parcelSize}")
2830

29-
if (dataJsonByteArray.size < BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE) {
31+
if (parcelSize < BINDER_MAX_TRANSFORM_PARCEL_SIZE) {
3032
debugD("onData use binder")
3133
ServiceCache.remoteClients.getClientByPid(pid)
3234
?.onReceive(GsonInstance.toJson(CallbackResponse(callbackKey, dataJson)))

ipc-core/src/main/java/com/zclever/ipc/core/server/ServiceCenter.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ class ServiceCenter : Service() {
102102

103103
val resultByteArray = resultJson.encodeToByteArray()
104104

105-
if (resultByteArray.size < BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE) {
105+
val parcelSize = ParcelSizeHelper.getStringParcelSize(resultJson)
106+
107+
if (parcelSize < BINDER_MAX_TRANSFORM_PARCEL_SIZE) {
106108
debugD("return use binder")
107109
Response(resultJson).toJson()
108110
} else {
109111
debugD("return use shared memory")
110112

111-
ServiceCache.serverResponseMemoryMap[requestBase.pid]!!.writeByteArray(resultByteArray)
113+
ServiceCache.serverResponseMemoryMap[requestBase.pid]!!.writeByteArray(
114+
resultByteArray
115+
)
112116

113117
Response(null, resultByteArray.size).toJson()
114118
}

0 commit comments

Comments
 (0)