public static void writeHeader(
     ChannelBuffer buffer, long requestId, byte status, Version version) {
   int index = buffer.readerIndex();
   buffer.setByte(index, 'E');
   index += 1;
   buffer.setByte(index, 'S');
   index += 1;
   // write the size, the size indicates the remaining message size, not including the size int
   buffer.setInt(index, buffer.readableBytes() - 6);
   index += 4;
   buffer.setLong(index, requestId);
   index += 8;
   buffer.setByte(index, status);
   index += 1;
   buffer.setInt(index, version.id);
 }
 @Override
 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
   // 重置channelbuffer, 重设参数length
   ChannelBuffer b = (ChannelBuffer) msg;
   b.setInt(0, b.readableBytes() - 4);
   return b;
 }
  @Override
  public void encode(MessageTree tree, ChannelBuffer buf) {
    Message message = tree.getMessage();

    if (message instanceof Transaction) {
      int count = 0;
      int index = buf.writerIndex();
      BufferHelper helper = m_bufferHelper;
      Transaction t = (Transaction) message;
      Locator locator = new Locator();
      Ruler ruler = new Ruler((int) t.getDurationInMicros());

      ruler.setWidth(1400);
      ruler.setHeight(18 * calculateLines(t) + 10);
      ruler.setOffsetX(200);
      ruler.setOffsetY(10);

      buf.writeInt(0); // place-holder

      count += helper.table1(buf);
      count += helper.crlf(buf);
      count += encodeHeader(tree, buf, ruler);

      count += encodeRuler(buf, locator, ruler);
      count += encodeTransaction(tree, t, buf, locator, ruler);

      count += encodeFooter(tree, buf);
      count += helper.table2(buf);
      buf.setInt(index, count);
    }
  }
 @Override
 public void sendResponse(Throwable error) throws IOException {
   BytesStreamOutput stream;
   try {
     stream = BytesStreamOutput.Cached.cached();
     writeResponseExceptionHeader(stream);
     RemoteTransportException tx =
         new RemoteTransportException(
             transport.nodeName(),
             transport.wrapAddress(channel.getLocalAddress()),
             action,
             error);
     ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream);
     too.writeObject(tx);
     too.close();
   } catch (NotSerializableException e) {
     stream = BytesStreamOutput.Cached.cached();
     writeResponseExceptionHeader(stream);
     RemoteTransportException tx =
         new RemoteTransportException(
             transport.nodeName(),
             transport.wrapAddress(channel.getLocalAddress()),
             action,
             new NotSerializableTransportException(error));
     ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream);
     too.writeObject(tx);
     too.close();
   }
   ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(stream.copiedByteArray());
   buffer.setInt(0, buffer.writerIndex() - 4); // update real size.
   channel.write(buffer);
 }
 @Override
 public void sendResponse(Streamable message) throws IOException {
   HandlesStreamOutput stream = BytesStreamOutput.Cached.cachedHandles();
   stream.writeBytes(LENGTH_PLACEHOLDER); // fake size
   stream.writeLong(requestId);
   byte status = 0;
   status = setResponse(status);
   stream.writeByte(status); // 0 for request, 1 for response.
   message.writeTo(stream);
   byte[] data = ((BytesStreamOutput) stream.wrappedOut()).copiedByteArray();
   ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
   buffer.setInt(0, buffer.writerIndex() - 4); // update real size.
   channel.write(buffer);
 }
Exemple #6
0
  @Override
  public void encode(MessageTree tree, ChannelBuffer buf) {
    int count = 0;
    int index = buf.writerIndex();
    BufferHelper helper = m_bufferHelper;

    buf.writeInt(0); // place-holder

    count += helper.table1(buf);
    count += helper.crlf(buf);
    count += encodeHeader(tree, buf);

    if (tree.getMessage() != null) {
      count += encodeMessage(tree, tree.getMessage(), buf, 0, new LineCounter());
    }

    count += helper.table2(buf);
    buf.setInt(index, count);
  }