Search in sources :

Example 1 with CharBasedFuture

use of com.generallycloud.baseio.codec.charbased.future.CharBasedFuture in project baseio by generallycloud.

the class CharBasedProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    CharBasedFuture f = (CharBasedFuture) future;
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        throw new IOException("null write buffer");
    }
    ByteBuf buf = allocator.allocate(writeSize + 1);
    buf.put(f.getWriteBuffer(), 0, writeSize);
    buf.putByte(splitor);
    future.setByteBuf(buf.flip());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) IOException(java.io.IOException) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Example 2 with CharBasedFuture

use of com.generallycloud.baseio.codec.charbased.future.CharBasedFuture in project baseio by generallycloud.

the class TestLineBasedClient method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new CharBasedProtocolFactory());
    SocketSession session = connector.connect();
    CharBasedFuture future = new CharBasedFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) CharBasedFutureImpl(com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Aggregations

CharBasedFuture (com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)2 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)1 CharBasedFutureImpl (com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl)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 SocketSession (com.generallycloud.baseio.component.SocketSession)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)1 Future (com.generallycloud.baseio.protocol.Future)1 IOException (java.io.IOException)1