更新 ClientMessageDecoder.java

This commit is contained in:
远方夕阳 2017-04-24 09:34:16 +08:00
parent c0bddbb848
commit 98b9c3c33b

View File

@ -1,138 +1,136 @@
/** /**
* Copyright 2013-2023 Xia Jun(3979434@qq.com). * Copyright 2013-2023 Xia Jun(3979434@qq.com).
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
*************************************************************************************** ***************************************************************************************
* * * *
* Website : http://www.farsunset.com * * Website : http://www.farsunset.com *
* * * *
*************************************************************************************** ***************************************************************************************
*/ */
package com.farsunset.cim.sdk.android.filter; package com.farsunset.cim.sdk.android.filter;
import java.util.List; import java.util.List;
import com.farsunset.cim.sdk.android.constant.CIMConstant; import com.farsunset.cim.sdk.android.constant.CIMConstant;
import com.farsunset.cim.sdk.android.model.HeartbeatRequest; import com.farsunset.cim.sdk.android.model.HeartbeatRequest;
import com.farsunset.cim.sdk.android.model.Message; import com.farsunset.cim.sdk.android.model.Message;
import com.farsunset.cim.sdk.android.model.ReplyBody; import com.farsunset.cim.sdk.android.model.ReplyBody;
import com.farsunset.cim.sdk.android.model.proto.MessageProto; import com.farsunset.cim.sdk.android.model.proto.MessageProto;
import com.farsunset.cim.sdk.android.model.proto.ReplyBodyProto; import com.farsunset.cim.sdk.android.model.proto.ReplyBodyProto;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import android.util.Log; import android.util.Log;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
/** /**
* 客户端消息解码 * 客户端消息解码
*/ */
public class ClientMessageDecoder extends ByteToMessageDecoder { public class ClientMessageDecoder extends ByteToMessageDecoder {
final static String TAG = ClientMessageDecoder.class.getSimpleName(); final static String TAG = ClientMessageDecoder.class.getSimpleName();
@Override @Override
protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception { protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {
/** /**
* 消息头3位 * 消息头3位
*/ */
if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) { if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
return; return;
} }
buffer.markReaderIndex(); buffer.markReaderIndex();
buffer.markReaderIndex(); byte conetnType = buffer.readByte();
byte conetnType = buffer.readByte(); byte lv = buffer.readByte();// int 低位
byte hv = buffer.readByte();// int 高位
byte lv = buffer.readByte();// int 低位
byte hv = buffer.readByte();// int 高位 int conetnLength = getContentLength(lv, hv);
int conetnLength = getContentLength(lv, hv); // 如果消息体没有接收完整则重置读取等待下一次重新读取
if (conetnLength > buffer.readableBytes()) {
// 如果消息体没有接收完整则重置读取等待下一次重新读取 buffer.resetReaderIndex();
if (conetnLength > buffer.readableBytes()) { return;
buffer.resetReaderIndex(); }
return;
} byte[] dataBytes = new byte[conetnLength];
buffer.readBytes(dataBytes);
byte[] dataBytes = new byte[conetnLength];
buffer.readBytes(dataBytes); Object message = mappingMessageObject(dataBytes, conetnType);
Object message = mappingMessageObject(dataBytes, conetnType); if(message!=null){
queue.add(message);
if(message!=null){ }
queue.add(message);
} }
} private Object mappingMessageObject(byte[] bytes, byte type) throws InvalidProtocolBufferException {
private Object mappingMessageObject(byte[] bytes, byte type) throws InvalidProtocolBufferException { if (CIMConstant.ProtobufType.S_H_RQ == type) {
HeartbeatRequest request = HeartbeatRequest.getInstance();
if (CIMConstant.ProtobufType.S_H_RQ == type) { Log.i(TAG, request.toString());
HeartbeatRequest request = HeartbeatRequest.getInstance(); return request;
Log.i(TAG, request.toString()); }
return request;
} if (CIMConstant.ProtobufType.REPLYBODY == type) {
ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bytes);
if (CIMConstant.ProtobufType.REPLYBODY == type) { ReplyBody body = new ReplyBody();
ReplyBodyProto.Model bodyProto = ReplyBodyProto.Model.parseFrom(bytes); body.setKey(bodyProto.getKey());
ReplyBody body = new ReplyBody(); body.setTimestamp(bodyProto.getTimestamp());
body.setKey(bodyProto.getKey()); body.putAll(bodyProto.getDataMap());
body.setTimestamp(bodyProto.getTimestamp()); body.setCode(bodyProto.getCode());
body.putAll(bodyProto.getDataMap()); body.setMessage(bodyProto.getMessage());
body.setCode(bodyProto.getCode());
body.setMessage(bodyProto.getMessage()); Log.i(TAG, body.toString());
Log.i(TAG, body.toString()); return body;
}
return body;
} if (CIMConstant.ProtobufType.MESSAGE == type) {
MessageProto.Model bodyProto = MessageProto.Model.parseFrom(bytes);
if (CIMConstant.ProtobufType.MESSAGE == type) { Message message = new Message();
MessageProto.Model bodyProto = MessageProto.Model.parseFrom(bytes); message.setMid(bodyProto.getMid());
Message message = new Message(); message.setAction(bodyProto.getAction());
message.setMid(bodyProto.getMid()); message.setContent(bodyProto.getContent());
message.setAction(bodyProto.getAction()); message.setSender(bodyProto.getSender());
message.setContent(bodyProto.getContent()); message.setReceiver(bodyProto.getReceiver());
message.setSender(bodyProto.getSender()); message.setTitle(bodyProto.getTitle());
message.setReceiver(bodyProto.getReceiver()); message.setExtra(bodyProto.getExtra());
message.setTitle(bodyProto.getTitle()); message.setTimestamp(bodyProto.getTimestamp());
message.setExtra(bodyProto.getExtra()); message.setFormat(bodyProto.getFormat());
message.setTimestamp(bodyProto.getTimestamp());
message.setFormat(bodyProto.getFormat()); Log.i(TAG, message.toString());
return message;
Log.i(TAG, message.toString()); }
return message;
} return null;
return null; }
} /**
* 解析消息体长度
/** *
* 解析消息体长度 * @param type
* * @param length
* @param type * @return
* @param length */
* @return private int getContentLength(byte lv, byte hv) {
*/ int l = (lv & 0xff);
private int getContentLength(byte lv, byte hv) { int h = (hv & 0xff);
int l = (lv & 0xff); return (l | (h <<= 8));
int h = (hv & 0xff); }
return (l | (h <<= 8));
} }
}