use of org.jboss.netty.buffer.ChannelBufferOutputStream in project MSEC by Tencent.
the class NettyClient method sendRequestAndWaitResponse.
// ��RPC��
public RpcResponse sendRequestAndWaitResponse(CustomPackageCodec packageCodec, RpcRequest request, int timeoutMillis) {
RpcResponse response = null;
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.dynamicBuffer(1024));
long sequence = request.getSeq();
int ret = 0;
try {
ret = packageCodec.encode(sequence, bout);
} catch (IOException ex) {
response = new RpcResponse();
response.setErrno(-1);
response.setError(ex);
return response;
}
if (ret != 0) {
response = new RpcResponse();
response.setErrno(-1);
response.setError(new Exception("Encode request failed."));
return response;
}
Object[] objects = new Object[3];
objects[0] = bout.buffer();
objects[1] = packageCodec;
objects[2] = null;
if (sendRequest(objects) == null)
return null;
CallbackFuture callback = new CallbackFuture(request, this);
sessions.put(request.getSeq(), callback);
try {
response = callback.getResponse(timeoutMillis);
} catch (Exception ex) {
// TODO: set exception
System.out.println("Exception occurs: " + ex);
response = new RpcResponse();
response.setErrno(-1);
response.setError(ex);
} finally {
sessions.remove(request.getSeq());
}
return response;
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.
the class Response method writeJSON.
public void writeJSON(Object object) throws IOException {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(channelBuffer);
mapper.writeValue(outputStream, object);
outputStream.close();
write(channelBuffer);
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.
the class Response method writeXML.
public void writeXML(Object object) {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
ChannelBufferOutputStream outputStream = new ChannelBufferOutputStream(channelBuffer);
xstreamXML.toXML(object, outputStream);
write(channelBuffer);
}
use of org.jboss.netty.buffer.ChannelBufferOutputStream in project NabAlive by jcheype.
the class DataUtil method compress.
public static ChannelBuffer compress(ChannelBuffer in) throws IOException {
ChannelBufferInputStream channelBufferInputStream = new ChannelBufferInputStream(in);
ChannelBuffer out = ChannelBuffers.dynamicBuffer(in.readableBytes());
ChannelBufferOutputStream channelBufferOutputStream = new ChannelBufferOutputStream(out);
compress(ByteStreams.toByteArray(channelBufferInputStream), channelBufferOutputStream);
return out;
}
Aggregations