package org.voovan.network.messagesplitter;

import org.voovan.network.IoSession;
import org.voovan.network.MessageSplitter;
import org.voovan.network.filter.ByteFilter;

import java.nio.ByteBuffer;

/**
 * Byte数据截断器
 *      255+4位为数据长度+255+数据
 *
 * @author: helyho
 * Project: DBase
 * Create: 2017/11/1 14:38
 */
public class ByteMessageSpliter implements MessageSplitter {
	@Override
	public int canSplite(IoSession session, ByteBuffer byteBuffer) {
		int originPosition = byteBuffer.position();

		try {
			if (byteBuffer.remaining() > ByteFilter.HEAD_LEGNTH && byteBuffer.get() == ByteFilter.SPLITER) {
				int length = byteBuffer.getInt();

				if (byteBuffer.get() == ByteFilter.SPLITER) {
					if (length > 0 && byteBuffer.remaining() >= length) {
						return ByteFilter.HEAD_LEGNTH + length;
					}
				}
			}
		} finally {
			byteBuffer.position(originPosition);
		}

		return -1;
	}
}

@@ -501,16 +501,17 @@
 %09%09try %7B%0A
+%0A
 %09%09%09if (b
@@ -556,20 +556,27 @@
 D_LEGNTH
- && 
+) %7B%0A%09%09%09%09if(
 byteBuff
@@ -605,24 +605,26 @@
 .SPLITER) %7B%0A
+%0A%09
 %09%09%09%09int leng
@@ -650,24 +650,25 @@
 Int();%0A%0A%09%09%09%09
+%09
 if (byteBuff
@@ -697,24 +697,25 @@
 .SPLITER) %7B%0A
+%09
 %09%09%09%09%09if (len
@@ -757,24 +757,25 @@
 = length) %7B%0A
+%09
 %09%09%09%09%09%09return
@@ -809,25 +809,105 @@
 ength;%0A%09%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09session.close();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09session.close();
 %0A%09%09%09%09%7D%0A%09%09%09%7D%0A

优化 ByteMessageSpliter 在报文格式不正确的时候人为是黑客攻击, 断开连接