Search in sources :

Example 6 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class WebSocketProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    WebSocketFuture f = (WebSocketFuture) future;
    byte[] header;
    byte[] data = f.getWriteBuffer();
    int size = f.getWriteSize();
    byte header0 = (byte) (0x8f & (f.getType() | 0xf0));
    if (size < 126) {
        header = new byte[2];
        header[0] = header0;
        header[1] = (byte) size;
    } else if (size <= MAX_UNSIGNED_SHORT) {
        header = new byte[4];
        header[0] = header0;
        header[1] = 126;
        MathUtil.unsignedShort2Byte(header, size, 2);
    } else {
        header = new byte[6];
        header[0] = header0;
        header[1] = 127;
        MathUtil.int2Byte(header, size, 2);
    }
    ByteBuf buf = allocator.allocate(header.length + size);
    buf.put(header);
    buf.put(data, 0, size);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Aggregations

WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)6 SocketSession (com.generallycloud.baseio.component.SocketSession)4 JSONObject (com.alibaba.fastjson.JSONObject)3 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)3 WebSocketFutureImpl (com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl)3 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)1 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)1 WebSocketBeatFutureFactory (com.generallycloud.baseio.codec.http11.future.WebSocketBeatFutureFactory)1 WebSocketUpgradeRequestFuture (com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture)1 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)1 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)1 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)1 Future (com.generallycloud.baseio.protocol.Future)1