A Retrofit-like IPC communication framework that supports Java, Kotlin, and synchronous calls, asynchronous calls, and coroutines
A high availability, high maintenance, high performance, thread safe IPC communication framework. (Android platform support, only 98KB) the core idea is to hook the Handler Messenger implementation; (The server currently needs the ability to reflect the hide API, otherwise functionality will degrade)
- Kotlin 👍
- Java 👍
- Android 4 – Android 12+ 👍
- Synchronous call 👍
- Asynchronous call 👍
- Thread safety 👍
- One server to multiple clients 👍
- Coroutines 👍
- Interface parameters are supported. The returned parameters are basic types or Parcelable or List 👍
- Alert message (WIP)
- Exception Mechanism (WIP)
- Transport Size Detection and Limitation (WIP)
How to use
The client
-
- Define the interface you want IPC on the client first.
interface RaTestInterface {
fun testReturnAModel(testString: String, testNumber: Int): RaTestModel?
fun testReturnAllList(testString: String): List<RaTestModel>?
fun testVoid(a)
}
Copy the code
-
- After the remote service is successfully bound to the client, the
RaClientApi.INSTANCE.create(RaTestInterface::class.java)
Method to obtain the corresponding service, and then call the corresponding interface;
- After the remote service is successfully bound to the client, the
Client example
RaClientApi.INSTANCE.bindRaConnectionService(this, ComponentName("com.softtanck.ramessageservice"."com.softtanck.ramessageservice.RaConnectionService"), object : BindStateListener {
override fun onConnectedToRaServices(a) {
Log.d("~ ~ ~"."connectedToRaServices: $this")
val testInterface = RaClientApi.INSTANCE.create(RaTestInterface::class.java)
val testReturnAModel = testInterface.testReturnAModel("I am from the caller".1)
Log.d("~ ~ ~"."testReturnAModel:${testReturnAModel? .testString}")
val testReturnAllList = testInterface.testReturnAllList("I am from the caller")
Log.d("~ ~ ~"."testReturnAllList:$testReturnAllList")
testInterface.testVoid()
}
override fun onConnectRaServicesFailed(a) {
Log.d("~ ~ ~"."onConnectRaServicesFailed: ")}override fun onDisconnectedFromRaServices(@DisconnectedReason disconnectedReason: Int) {
Log.d("~ ~ ~"."disconnectedFromRaServices: $disconnectedReason")}})Copy the code
The service side
-
- inheritance
BaseConnectionService
- inheritance
-
- implementation
RaTestInterface
interface
- implementation
Server example
class RaConnectionService : BaseConnectionService(), RaTestInterface {
override fun testReturnAModel(testString: String, testNumber: Int): RaTestModel {
Log.d("~ ~ ~"."[SERVER] testReturnAModel: Service is invoked, testString:$testString, testNumber:$testNumber")
return RaTestModel("Server returns new ID")}override fun testReturnAllList(testString: String): List<RaTestModel> {
Log.d("~ ~ ~"."[SERVER] testReturnAllList: Service is invoked")
return arrayListOf(RaTestModel("The server returned by the new interface returns a new ID"))}override fun testVoid(a) {
Log.d("~ ~ ~"."[SERVER] testVoid: Service is invoked")}}Copy the code
Note that the parameter or return value is a primitive type.Don’t need to care about)
- When the argument is an object, the object must implement the Parcelable interface.
- When the client expects an interface return value to be an object, the object must implement the Parcelable interface.
- If the interface has a return value, but if the remote call fails, the return value is null, pay attention to the “null pointer” exception.
Fun testReturnAModel(testString: String, testNumber: Int): RaTestModel in RaTestModel needs to implement Parcelable, and both the server and client need to define classes with the same package name;
Connection:Github.com/Softtanck/R…
In the current project iteration, you are welcome to propose various requirements, PR and issues, etc.