[xSocket-develop] How to create a blocking server using streams?
Status: Inactive
Brought to you by:
grro
|
From: Robert W. <wie...@go...> - 2008-06-15 10:57:03
|
Hi,
how do I create a blocking server using streams? My current try only accepts
one "message" per connection and than hangs!
I just started using xSocket and have the following problem:
I already have some (un-)/marshaling mechanism that I need to use on the
server side. This mechanism unfortunately works with streams so I used
Channels method to create a stream from the connection object
private Object readRequestMessage(IBlockingConnection bc, Class<?>
messageType) throws IOException {
// wrap an InputStream around the incomming data to feed it into
the unmarshaller
InputStream in = Channels.newInputStream(bc);
Unmarshaller unmarshaller = new Unmarshaller(in);
Object result = unmarshaller.unmarshalMessage(messageType);
return result;
}
private void writeReplyMessage(IBlockingConnection bc, Object
replyMessage) throws IOException {
if (replyMessage == null)
return; // nothing to do
OutputStream out = Channels.newOutputStream(bc);
StructuredDataMarsahller marshaller = new
StructuredDataMarsahller(out);
marshaller.marshal(replyMessage);
}
Both messages are called from IDataHandler.onData
class TWPHandler implements IDataHandler, IConnectionScoped {
public boolean onData(INonBlockingConnection nbc) throws IOException,
BufferUnderflowException, MaxReadSizeExceededException {
IBlockingConnection bc = new BlockingConnection(nbc);
switch (this.mode) {
case INIT:
this.init(bc);
this.mode = MESSAGE;
case MESSAGE:
int messageId = this.readMessageId(bc);
Class<?> messageType = classForId.get(messageId);
Object requestMessage = this.readRequestMessage(bc,
messageType);
// handle the message
Object replyMessage =
handlerForClass.get(messageType).onMessage(requestMessage);
this.writeReplyMessage(bc, replyMessage);
}
return true;
}
}
I'm also using mark support because I need to read some bytes that the
unmarshaller need to read again.
private int readMessageId(IBlockingConnection bc) throws IOException {
int messageId;
synchronized (bc) {
bc.markReadPosition();
// read message id
// ...
bc.resetToReadMark(); // so that the id can be read again by
the unmarshaller
}
return messageId;
}
This all works fine if there is only one request and one response message
per connection. But if I try to send another request message on the same
connection the server hangs forever.
So what am I doing wrong?
thanks in advance
robert
|