File tree 8 files changed +57
-13
lines changed
ipc-app-test/src/main/java/com/demo/ipc
ipc-core/src/main/java/com/zclever/ipc/core 8 files changed +57
-13
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ class CommonActivity : AppCompatActivity() {
22
22
override fun onCreate (savedInstanceState : Bundle ? ) {
23
23
super .onCreate(savedInstanceState)
24
24
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())
26
26
IpcManager .init (this )
27
27
IpcManager .open(" com.demo.ipcdemo" ) {
28
28
runOnUiThread {
Original file line number Diff line number Diff line change 1
1
package com.demo.ipc
2
2
3
+ import android.os.Binder
3
4
import android.util.Log
4
5
import com.ipc.extend.test.*
5
6
import com.zclever.ipc.core.*
6
7
import kotlin.concurrent.thread
8
+ import kotlin.random.Random
7
9
8
10
9
11
object InfoServiceManager : InfoService {
@@ -39,7 +41,6 @@ object InfoServiceManager : InfoService {
39
41
private var mCallBack: Result <Event >? = null
40
42
41
43
init {
42
-
43
44
thread {
44
45
while (true ) {
45
46
mCallBack?.onData(Event (count++ ))
@@ -71,15 +72,19 @@ object InfoServiceManager : InfoService {
71
72
}
72
73
73
74
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
+ })
75
78
}
76
79
77
80
private var asyncBigByteArrayCallback: Result <ByteArray >? = null
78
81
79
82
override fun asyncGetBigByteArray (callBack : Result <ByteArray >) {
80
83
asyncBigByteArrayCallback= callBack
81
84
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
+ }))
83
88
}
84
89
}
85
90
}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import android.os.Process
5
5
6
6
const val REQUEST_TYPE_CREATE = 1
7
7
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,这里需要写小一点
9
9
10
10
/* *
11
11
* 请求对象
Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ class Config private constructor(builder: Builder) {
12
12
val mediaMemoryCapacity = builder.mediaMemoryCapacity
13
13
14
14
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数据
17
17
fun builder () = Builder ()
18
18
}
19
19
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -55,10 +55,12 @@ internal class ServiceInvocationHandler(
55
55
56
56
val paramByteArray = requestParamJson.encodeToByteArray()
57
57
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 " )
59
61
60
62
val response =
61
- if (paramByteArray.size < BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE ) {// binder传输
63
+ if (parcelSize < BINDER_MAX_TRANSFORM_PARCEL_SIZE ) {// binder传输
62
64
63
65
synchronized(ClientCache .serverResponseSharedMemory!! ) {// 确保同步
64
66
Original file line number Diff line number Diff line change @@ -22,11 +22,13 @@ internal class ServerCallBack(
22
22
23
23
val dataJson = GsonInstance .toJson(data)
24
24
25
+ val parcelSize= ParcelSizeHelper .getStringParcelSize(dataJson)
26
+
25
27
val dataJsonByteArray= dataJson.encodeToByteArray()
26
28
27
- debugD(" onData: $callbackKey ,-------${ServiceCache .remoteClients.getClientByPid(pid)} ,size->${dataJsonByteArray.size} " )
29
+ debugD(" onData: $callbackKey ,-------${ServiceCache .remoteClients.getClientByPid(pid)} ,size->${dataJsonByteArray.size} ,parcelSize-> ${parcelSize} " )
28
30
29
- if (dataJsonByteArray.size < BINDER_MAX_TRANSFORM_JSON_BYTE_ARRAY_SIZE ) {
31
+ if (parcelSize < BINDER_MAX_TRANSFORM_PARCEL_SIZE ) {
30
32
debugD(" onData use binder" )
31
33
ServiceCache .remoteClients.getClientByPid(pid)
32
34
?.onReceive(GsonInstance .toJson(CallbackResponse (callbackKey, dataJson)))
Original file line number Diff line number Diff line change @@ -102,13 +102,17 @@ class ServiceCenter : Service() {
102
102
103
103
val resultByteArray = resultJson.encodeToByteArray()
104
104
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 ) {
106
108
debugD(" return use binder" )
107
109
Response (resultJson).toJson()
108
110
} else {
109
111
debugD(" return use shared memory" )
110
112
111
- ServiceCache .serverResponseMemoryMap[requestBase.pid]!! .writeByteArray(resultByteArray)
113
+ ServiceCache .serverResponseMemoryMap[requestBase.pid]!! .writeByteArray(
114
+ resultByteArray
115
+ )
112
116
113
117
Response (null , resultByteArray.size).toJson()
114
118
}
You can’t perform that action at this time.
0 commit comments