RPC Impl By Udp in Android(Android基于UDP的进程通信)
- Support mutil-clients and mutil-server exist in same time,but every client can only connect to one server. One serve can have mutil-clients.
- Both support synchronous and asynchronous invoke.
- Very minor performance cost.
- Aginst AIDL:1.more easy to use;2.more efficient,less performance cost;
- Aginst Broadcast:1.support synchronous invoke;2.more efficient,less performance cost;
- In normal use stituation,will extremely rare lose data or in worng order(near nerver).
- Add the udprpcLib src file to your project,or download that module and add it to your project.
- Initialize Server and Client.
In the server process,use ServerManager.getInstance().init(context,cmdDispatcher) to initialize your udp server.
In the client process,use ClientManager.getInstance().init(context,serverProcessName) to initialize your udp client.
Ps:suggest do initialize work when application onCreate and use application context. - Now you can send your data from client process and receive it in server process.
For example:
The client send async invoke
ClientManager.getInstance().sendInvoke(UdpDataFactory.UdpData.CMD_TEST, "testData".getBytes());
The server receive and handle the receive data
UdpServer.ICmdDispatcher mTestCmdDispatcher = new UdpServer.ICmdDispatcher() {
@Override
public UdpDataFactory.UdpData onInvoke(UdpDataFactory.UdpData udpData) {
switch (udpData.cmd){
case UdpDataFactory.UdpData.CMD_TEST:
mTvReceive.post(new Runnable1<byte[]>(udpData.data) {
@Override
public void run() {
if (udpData.data != null && udpData.length > 0) {
mTvReceive.setText(new String(udpData.data));
}
}
});
break;
}
return null;
}
};
the mTestCmdDispatcher is appointed when server initialize.
You can read the TestActivity to learn the useage.
If you have any other questions,connect me: [email protected]