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

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

https://gitlab.com/Mr.Tomato/linbox_server
Java | 123 lines | 93 code | 27 blank | 3 comment | 22 complexity | dc985cbc8ba02750eda1e2aa49fe0e7f 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.ByteToMessageDecoder;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import java.util.List;
  11. /**
  12. * Created by lrsec on 6/29/15.
  13. */
  14. public class IMMessageDecoder extends ByteToMessageDecoder{
  15. private static Logger logger = LoggerFactory.getLogger(IMMessageDecoder.class);
  16. private short version = -1;
  17. private RequestResponseType requestResponseType = null;
  18. private int contentSize = -1;
  19. private AES aes;
  20. public IMMessageDecoder(AES aes) {
  21. this.aes = aes;
  22. }
  23. @Override
  24. protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  25. try {
  26. if (version < 0) {
  27. if (in.readableBytes() >= 2) {
  28. version = in.readShort();
  29. if (version < 0) {
  30. logger.error("Unknow IM Message Version for {}", version);
  31. reset();
  32. return;
  33. }
  34. logger.debug("Version: {}", version);
  35. }
  36. }
  37. if (requestResponseType == null) {
  38. if (in.readableBytes() >= 2) {
  39. int value = in.readShort();
  40. requestResponseType = RequestResponseType.parse(value);
  41. if (requestResponseType == null) {
  42. logger.error("Unknown IM message type for {}.", value);
  43. reset();
  44. return;
  45. }
  46. logger.debug("Request type: {}", requestResponseType.getName());
  47. } else {
  48. return;
  49. }
  50. }
  51. if (requestResponseType != null && contentSize < 0) {
  52. if (in.readableBytes() >= 4) {
  53. contentSize = in.readInt();
  54. if(contentSize <= 0) {
  55. logger.error("Get illegal IM message content size: {} for message type: {}", contentSize, requestResponseType.getName());
  56. reset();
  57. return;
  58. }
  59. logger.debug("Request type: {}. Content Length: {}", requestResponseType.getName(), contentSize);
  60. } else {
  61. return;
  62. }
  63. }
  64. if (requestResponseType != null && contentSize > 0) {
  65. if (in.readableBytes() >= contentSize) {
  66. try {
  67. byte[] buf = in.readBytes(contentSize).array();
  68. String json = aes.decrypt(buf);
  69. Object message = JSON.parseObject(json, requestResponseType.getClazz());
  70. if (message == null) {
  71. logger.error("Can not parse message content from json string. Message Type: {}. Message Content Size: {}. Message Content: {}.", requestResponseType.getName());
  72. } else {
  73. logger.debug("Request type: {}. Content Length: {}. Request: {}", requestResponseType.getName(), contentSize, new String(buf));
  74. MessageWrapper wrapper = new MessageWrapper();
  75. wrapper.type = requestResponseType;
  76. wrapper.content = message;
  77. out.add(wrapper);
  78. }
  79. } catch (Exception e) {
  80. logger.error("Parse request body fail. ", e);
  81. } finally {
  82. reset();
  83. }
  84. }
  85. return;
  86. }
  87. } catch (Exception e) {
  88. logger.error("", e);
  89. }
  90. }
  91. private void reset() {
  92. version = -1;
  93. requestResponseType = null;
  94. contentSize = -1;
  95. }
  96. }