Hi guys, I present the newest version of my RPC library
http://idgames.net/uploads/RPC_Release.rar
It features a client and server hub which can interact via RPC messages.
To understand why this is useful, you need to understand how RMI works.
In RMI, you call a remote function by extending UnicastRemoteObject and implementing each function that you require in RMI, for example:
The same code in RPC would look like so:Code:public interface MyRemote extends Remote { public void myMethod() throws RemoteException; } public class MyRemoteObject extends UnicastRemoteObject implements MyRemote { public void myMethod() throws RemoteException { System.out.println("Remote method called"); } }
The above implementations of RMI and RPC will do the same thing, however the RMI version blocks the thread (the thread stops) until a response is received. The RPC version on the other hand, simply sets up a listener and when a response is received the listener is notified asynchronously.Code:Server: RPCServer server = new RPCServer(1919, new DefaultRPCMessageEncoder(), new DefaultRPCMessageDecoder()); server.registerRequestHandler(new RPCRequestHandler() { public void handleRequest(RPCClientContext client, RPCMessage request, SeekableLittleEndianAccessor slea) { System.out.println("Remote method called"); } }, 1); Client: RPCClient client = new RPCClient(new InetSocketAddress(InetAddress.getLocalHost(), 1919), new DefaultRPCMessageEncoder(), new DefaultRPCMessageDecoder()); DefaultRPCMessage request= new DefaultRPCMessage(); request.setRequestID(RequestIDGenerator.getRequestID()); request.setProcedureID(1); client.callRequest(request);
Now imagine if we had to send a character's info across servers.
In RMI:
in RPC:Code:myRemoteObj.sendCharacterInfo(myCharacter);
Now, the problem here is that the RMI version sends a lot of useless info, such as class names and each of the other values in myCharacter that aren't marked as transient.Code:PacketWriter pw = new PacketWriter(); pw.writeString(myCharacter.getName()); pw.writeInt(myCharacter.getID()); DefaultRPCMessage req = new DefaultRPCMessage(); req.setBytes(pw.getBytes()); req.setProcedureID(2); client.callRequest(req);
The RPC version in contrast only sends the required info, such as the name and the ID.
The other downside to the RMI version is that an object of the exact same class will have to be constructed on the receivers end. The RPC version is not so. For example, if you send myCharacter on the senders end in RMI you have to also have the same class available on the receivers end or else you'd get a ClassNotFoundException. In RPC, you can just read the name and ID and construct a smaller object that doesn't require so much space.
An example is included in the download:
The example shows how to interact between RPC clients and RPC servers and how to handle responses and requests.Code:net.idgames.server.rpc.test.TestRPCClient net.idgames.server.rpc.test.TestRPCServer
Well I hope at least somebody makes use of this.
- David



Reply With Quote




