PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/connector/tcp/handler/IMMessageEncoder.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 45 lines | 32 code | 10 blank | 3 comment | 0 complexity | 4b0c111f1a545e8796a147206d14e61d MD5 | raw file
  1. package com.linbox.im.server.connector.tcp.handler;
  2. import com.alibaba.fastjson.JSON;
  3. import com.linbox.im.message.MessageWrapper;
  4. import com.linbox.im.message.RequestResponseType;
  5. import io.netty.buffer.ByteBuf;
  6. import io.netty.channel.ChannelHandlerContext;
  7. import io.netty.handler.codec.MessageToByteEncoder;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. /**
  11. * Created by lrsec on 7/10/15.
  12. */
  13. public class IMMessageEncoder extends MessageToByteEncoder<MessageWrapper> {
  14. private static Logger logger = LoggerFactory.getLogger(IMMessageEncoder.class);
  15. private static short CURRENT_VERISON = 1;
  16. private AES aes;
  17. public IMMessageEncoder(AES aes) {
  18. this.aes = aes;
  19. }
  20. @Override
  21. protected void encode(ChannelHandlerContext channelHandlerContext, MessageWrapper messageWrapper, ByteBuf byteBuf) throws Exception {
  22. try {
  23. RequestResponseType type = messageWrapper.type;
  24. long encodeStart = System.currentTimeMillis();
  25. byte[] content = aes.encrypt(JSON.toJSONString(messageWrapper.content));
  26. long encodeEnd = System.currentTimeMillis();
  27. byteBuf.writeShort(CURRENT_VERISON);
  28. byteBuf.writeShort(type.getValue());
  29. byteBuf.writeInt(content.length);
  30. byteBuf.writeBytes(content);
  31. } catch (Exception e) {
  32. logger.error("", e);
  33. }
  34. }
  35. }