All Downloads are FREE. Search and download functionalities are using the official Maven repository.

link.jfire.socket.socketserver.util.DefaultHeadFactory Maven / Gradle / Ivy

Go to download

Jfire - socket is a server-side framework based on AIO. Users only need a simple implementation of a business logic processing interface can be the business data processing. The framework provides the client and server at the same time. Have strong connection capacity. Single server provides tens of thousands of connections.

The newest version!
package link.jfire.socket.socketserver.util;

import java.nio.ByteBuffer;

public final class DefaultHeadFactory implements HeadFactory
{
    /**
     * 第一位为0x1b,第二位是command位的取反,第三位是第二位与第四位的异或,第四位数值随机
     */
    @Override
    public void putHeadInBuffer(ByteBuffer buffer, byte command, int length)
    {
        byte one = 0x1b;
        byte two = (byte) ~command;
        byte four = (byte) length;
        byte three = (byte) (two ^ four);
        buffer.put(one).put(two).put(three).put(four);
    }
    
    @Override
    public boolean fitHeadPtotocol(ByteBuffer buffer, int position)
    {
        byte command = buffer.get(position + 5);
        if (buffer.get(position) != (byte) 0x1b)
        {
            return false;
        }
        if (buffer.get(position + 1) != (byte) ~command)
        {
            return false;
        }
        if (buffer.get(position + 2) != (byte) (buffer.get(position + 1) ^ buffer.get(position + 3)))
        {
            return false;
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy