之前写了一篇关于 SNMP 修改SNMP4J消息内容 的博客,但是当时只是针对UDP的,后来应用到TCP上后发现有一些问题,现在说一下解决方法
上一篇连接地址:http://cuisuqiang.iteye.com/blog/1584391
使用TCP发送时,由于Socket不知道数据流的长度,所以要增加数据包长度到包上,这才是真正的需求,是我上次理解错了
增加头信息与上一次一样,直接在发送时增加即可
找到类:DefaultTcpTransportMapping
找到发送方法:
public void sendMessage(Address address, byte[] message)
在发送时增加消息头
/**
* Sends a SNMP message to the supplied address.
* @param address
* an <code>TcpAddress</code>. A <code>ClassCastException</code> is thrown
* if <code>address</code> is not a <code>TcpAddress</code> instance.
* @param message byte[]
* the message to sent.
* @throws IOException
*/
public void sendMessage(Address address, byte[] message)
throws java.io.IOException
{
if (server == null) {
listen();
}
// TODO 增加头字段
short length = (short)(message.length + 2);
byte[] btlength = shortToByte(length);
byte[] relmess = new byte[length];
System.arraycopy(btlength, 0, relmess, 0, 2);
System.arraycopy(message, 0, relmess, 2, message.length);
serverThread.sendMessage(address, relmess);
}
但是在接收时就遇到一个问题,底层接收后会直接验证数据流,如果你增加头信息,就不算是标准SNMP包了,SNMP4J就不会接收不会处理
在类:DefaultTcpTransportMapping 中有一个内部类:
class ServerThread implements WorkerTask
方法
private void readMessage(SelectionKey sk, SocketChannel readChannel,
TcpAddress incomingAddress) throws IOException
提供对于数据流的处理,处理后将数据流返回到上层进行解析
在方法中有这样有这样的代码:
ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
byteBuffer.limit(messageLengthDecoder.getMinHeaderLength());
MessageLengthDecoder 接口用于标识SNMP数据包的一些属性解析方法,包括最小头字节和获得 MessageLength 对象
如果数据小于最小字节,那么直接抛弃不解析
同样在TCP发送类中有一个内部类来实现这个接口:
public static class SnmpMesssageLengthDecoder implements MessageLengthDecoder {
public int getMinHeaderLength() {
return MIN_SNMP_HEADER_LENGTH;
}
public MessageLength getMessageLength(ByteBuffer buf) throws IOException {
MutableByte type = new MutableByte();
BERInputStream is = new BERInputStream(buf);
int ml = BER.decodeHeader(is, type);
int hl = (int)is.getPosition();
MessageLength messageLength = new MessageLength(hl, ml);
return messageLength;
}
}
由于我们增加两个字节的长度,所以要重新写一个实现的接口
// TODO 自己定义的头解析
public static class SnmpMesssageLengthDecoderMe implements MessageLengthDecoder {
public int getMinHeaderLength() {
return MIN_SNMP_HEADER_LENGTH + 2;
}
public MessageLength getMessageLength(ByteBuffer buf) throws IOException {
int hl = 4;
int ml = decodeHeader(buf);
MessageLength messageLength = new MessageLength(hl, ml);
return messageLength;
}
public int decodeHeader(ByteBuffer is){
byte[] bt = is.array();
byte[] len = new byte[]{ bt[0], bt[1] };
return byteToShort(len) - 4;
}
/**
* @功能 字节的转换与短整型
* @return 短整型
*/
public synchronized static short byteToShort(byte[] b) {
short s = 0;
short s0 = (short) (b[1] & 0xff);// 最低位
short s1 = (short) (b[0] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}
}
长度就是原来的 +2 ,MessageLength 的属性有两个,按照我的写法写可以解决问题,但是我不明白
int ml = BER.decodeHeader(is, type);
的含义,希望有明白的给说明一下
消息的长度实际上就是我们定义的头表示的长度 -4
这样流读取方法才能把我们发送的数据流完整读取出来
上次在转发到上层解析时的方法也要修改,因为现在修改的只是正常读取到所有的流,而返给上层的数据流是要截取掉这两个字节的
@SuppressWarnings("static-access")
private void dispatchMessage(TcpAddress incomingAddress,
ByteBuffer byteBuffer, long bytesRead) {
byteBuffer.flip();
if (logger.isDebugEnabled()) {
logger.debug("Received message from " + incomingAddress +
" with length " + bytesRead + ": " +
new OctetString(byteBuffer.array(), 0,
(int)bytesRead).toHexString());
}
ByteBuffer bis;
if (isAsyncMsgProcessingSupported()) {
byte[] bytes = new byte[(int)bytesRead - 2];
System.arraycopy(byteBuffer.array(), 2, bytes, 0, (int)bytesRead - 2);
bis = ByteBuffer.wrap(bytes);
}
else {
byte[] bytes = new byte[(int)bytesRead - 2];
System.arraycopy(byteBuffer.array(), 2, bytes, 0, (int)bytesRead - 2);
bis = ByteBuffer.wrap(bytes);
}
// TODO 调用解析数据流
fireProcessMessage(incomingAddress, bis);
}
这样就可以正常解析增加两个字节的数据包
请您到ITEYE看我的原创:http://cuisuqiang.iteye.com
或支持我的个人博客,地址:http://www.javacui.com