當前位置: 首頁>>代碼示例>>Java>>正文


Java IoBuffer.compact方法代碼示例

本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.compact方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.compact方法的具體用法?Java IoBuffer.compact怎麽用?Java IoBuffer.compact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.mina.core.buffer.IoBuffer的用法示例。


在下文中一共展示了IoBuffer.compact方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: decode

import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
	//必須保證每一個數據包的字節緩存都和session綁定在一起,不然就讀取不了上一次剩餘的數據了
	CodecContext context = SessionManager.INSTANCE.getSessionAttr(session, SessionProperties.CODEC_CONTEXT, CodecContext.class);
	if (context == null) {
		context = new CodecContext();
		session.setAttribute(SessionProperties.CODEC_CONTEXT, context);
	}

	IoBuffer ioBuffer = context.getBuffer();
	ioBuffer.put(in);

	//在循環裏迭代,以處理數據粘包
	for (; ;) {
		ioBuffer.flip();
		//消息元信息常量4表示消息body前麵的兩個short字段,一個表示moduel,一個表示cmd,
		final int METE_SIZE = 4;
		if (ioBuffer.remaining() < METE_SIZE) {
			ioBuffer.compact();
			return;
		}
		//----------------消息協議格式-------------------------
		// packetLength | moduleId | cmd   |  body
		//       int       short     short    byte[]
		int length = ioBuffer.getInt();
		//int packLen = length + 4;
		//大於消息body長度,說明至少有一條完整的message消息
		if (ioBuffer.remaining() >= length) {
			short moduleId =  ioBuffer.getShort();
			short cmd = ioBuffer.getShort();
			byte[] body = new byte[length-METE_SIZE];
			ioBuffer.get(body);
			Message msg = readMessage(moduleId, cmd, body);

			if (moduleId > 0) {
				out.write(msg);
			} else { //屬於組合包
				CombineMessage combineMessage = (CombineMessage)msg;
				List<Packet> packets = combineMessage.getPackets();
				for (Packet packet :packets) {
					//依次拆包反序列化為具體的Message
					out.write(Packet.asMessage(packet));
				}
			}
			if (ioBuffer.remaining() == 0) {
				ioBuffer.clear();
				break;
			}
			ioBuffer.compact();
		} else{
			//數據包不完整,繼續等待數據到達
			ioBuffer.rewind();
			ioBuffer.compact();
			break;
		}
	}
}
 
開發者ID:kingston-csj,項目名稱:jforgame,代碼行數:57,代碼來源:MessageDecoder.java

示例2: messageReceived

import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception
{
    if ( ! ( message instanceof IoBuffer ) )
    {
        nextFilter.messageReceived ( session, message );
        return;
    }

    final IoBuffer in = (IoBuffer)message;
    final IoBuffer sessionBuffer = getSessionBuffer ( session );

    // first add to the session buffer (may be optimized later)
    sessionBuffer.position ( sessionBuffer.limit () );
    sessionBuffer.put ( in );
    sessionBuffer.flip ();

    while ( sessionBuffer.remaining () >= 4 )
    {
        final int len = sessionBuffer.getUnsignedShort ( 2 );
        if ( sessionBuffer.remaining () < len )
        {
            logger.debug ( "Next packet requires {} bytes", new Object[] { len } );
            // not enough data for body
            return;
        }

        // convert
        final IoBuffer data = IoBuffer.allocate ( len - 4 );
        sessionBuffer.get (); // version
        sessionBuffer.get (); // reserved
        sessionBuffer.getUnsignedShort (); // length

        sessionBuffer.get ( data.array () );

        nextFilter.messageReceived ( session, data );

        logger.debug ( "{} bytes left in session buffer", sessionBuffer.remaining () );
    }

    if ( sessionBuffer.hasRemaining () )
    {
        sessionBuffer.compact ();
    }
    else
    {
        sessionBuffer.clear ().flip ();
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:50,代碼來源:TPKTFilter.java


注:本文中的org.apache.mina.core.buffer.IoBuffer.compact方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。