[xSocket-develop] xSocket (multiplexed) and serialized objects
Status: Inactive
Brought to you by:
grro
|
From: Jeff C. <JC...@sp...> - 2008-05-29 18:54:51
|
Hi there. I have had some trouble using xSocket. 2 questions (with associated followups)
I'm using xSocket to transfer serialized objects and the javadocs hint that there's a leaky buffer in the way that I am using it.
Clientside:
client = new NonBlockingConnection(...);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(genericObj);
out.writeObject(DELIMITER);
client.write(bos.toByteArray());
Server Protocol Handler in onData(...)
//TODO: This is the only way I can get it to read. Leaky?
byte[] test = connection.readBytesByDelimiter(DELIMITER);
ByteArrayInputStream bis = new ByteArrayInputStream(test);
ObjectInputStream incomingObjstream = new ObjectInputStream(bis);
Object incomingObj = null;
try{
incomingObj = incomingObjstream.readObject();
}
catch(ClassNotFoundException e){e.printStackTrace();
}
incomingObjstream.close();
1. How do I safely specify the message length before I read it when I don't know the length until I've written it to stream already?
Some type of byte padding which specifies length at the beginning of the message? I'm not sure how to even read that out correctly.
2. What is the standard practice to index open connections for sending? I use my protocol handler to read the object, handle the object, throw an event. If the server wants to disconnect or send a message, how do I do a lookup? I could do a singleton HashMap or pass a reference in the event thrown (which doesn't help if it's interacting with another connection) or am I missing something in the xSocket interfaces?
Apologies in advance if these questions are inappropriate.
|