Search in sources :

Example 16 with ProtobaseFuture

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.

the class TestListenSimpleServlet method doAccept.

@Override
protected void doAccept(SocketSession session, ParamedProtobaseFuture future) throws Exception {
    String test = future.getReadText();
    if (StringUtil.isNullOrBlank(test)) {
        test = "test";
    }
    future.write(test);
    future.write("$");
    session.flush(future);
    for (int i = 0; i < 5; i++) {
        ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), future.getFutureId(), future.getFutureName());
        f.write(test);
        f.write("$");
        session.flush(f);
    }
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)

Example 17 with ProtobaseFuture

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.

the class FileSendUtil method sendFile.

public void sendFile(SocketSession session, String serviceName, File file, int cacheSize) throws Exception {
    FileInputStream inputStream = new FileInputStream(file);
    int available = inputStream.available();
    int time = (available + cacheSize) / cacheSize - 1;
    byte[] cache = new byte[cacheSize];
    JSONObject json = new JSONObject();
    json.put(FileReceiveUtil.FILE_NAME, file.getName());
    json.put(FileReceiveUtil.IS_END, false);
    String jsonString = json.toJSONString();
    for (int i = 0; i < time; i++) {
        FileUtil.readInputStream(inputStream, cache);
        ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
        f.write(jsonString);
        f.writeBinary(cache);
        session.flush(f);
    }
    int r = FileUtil.readInputStream(inputStream, cache);
    json.put(FileReceiveUtil.IS_END, true);
    ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
    f.write(json.toJSONString());
    f.writeBinary(cache, 0, r);
    session.flush(f);
    CloseUtil.close(inputStream);
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) JSONObject(com.alibaba.fastjson.JSONObject) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) FileInputStream(java.io.FileInputStream)

Example 18 with ProtobaseFuture

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.

the class ProtobaseProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    if (future.isHeartbeat()) {
        ByteBuf buf = future.isPING() ? PING.duplicate() : PONG.duplicate();
        future.setByteBuf(buf);
        return;
    }
    ProtobaseFuture f = (ProtobaseFuture) future;
    String futureName = f.getFutureName();
    if (StringUtil.isNullOrBlank(futureName)) {
        throw new ProtocolException("future name is empty");
    }
    byte[] futureNameBytes = futureName.getBytes(channel.getEncoding());
    if (futureNameBytes.length > Byte.MAX_VALUE) {
        throw new ProtocolException("future name max length 127");
    }
    byte futureNameLength = (byte) futureNameBytes.length;
    int allLen = 6 + futureNameLength;
    int textWriteSize = f.getWriteSize();
    int binaryWriteSize = f.getWriteBinarySize();
    byte h1 = 0b01000000;
    if (f.isBroadcast()) {
        h1 |= 0b00100000;
    }
    if (f.getFutureId() > 0) {
        h1 |= 0b00010000;
        allLen += 4;
    }
    if (f.getSessionId() > 0) {
        h1 |= 0b00001000;
        allLen += 4;
    }
    if (f.getHashCode() > 0) {
        h1 |= 0b00000100;
        allLen += 4;
    }
    if (f.getWriteBinarySize() > 0) {
        h1 |= 0b00000010;
        allLen += 4;
        allLen += f.getWriteBinarySize();
    }
    if (textWriteSize > 0) {
        allLen += textWriteSize;
    }
    ByteBuf buf = allocator.allocate(allLen);
    buf.putByte(h1);
    buf.putByte(futureNameLength);
    buf.putInt(textWriteSize);
    if (f.getFutureId() > 0) {
        buf.putInt(f.getFutureId());
    }
    if (f.getSessionId() > 0) {
        buf.putInt(f.getSessionId());
    }
    if (f.getHashCode() > 0) {
        buf.putInt(f.getHashCode());
    }
    if (binaryWriteSize > 0) {
        buf.putInt(binaryWriteSize);
    }
    buf.put(futureNameBytes);
    if (textWriteSize > 0) {
        buf.put(f.getWriteBuffer(), 0, textWriteSize);
    }
    if (binaryWriteSize > 0) {
        buf.put(f.getWriteBinary(), 0, binaryWriteSize);
    }
    future.setByteBuf(buf.flip());
}
Also used : UnpooledByteBufAllocator(com.generallycloud.baseio.buffer.UnpooledByteBufAllocator) ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ProtocolException(com.generallycloud.baseio.protocol.ProtocolException) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 19 with ProtobaseFuture

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.

the class SimpleTestProtobaseClient 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("localhost", 18300));
    context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    connector.setTimeout(99999999);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    SocketSession session = connector.connect();
    ProtobaseFuture future = new ProtobaseFutureImpl(context, "test222");
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 20 with ProtobaseFuture

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.

the class Test404 method main.

public static void main(String[] args) throws Exception {
    String serviceKey = "22";
    LoggerFactory.configure();
    SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
    ServerConfiguration configuration = new ServerConfiguration(8300);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandle);
    context.setProtocolFactory(new ParamedProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    FixedSession session = new FixedSession(connector.connect());
    ProtobaseFuture future = session.request(serviceKey, null);
    System.out.println(future.getReadText());
    Future future1 = new ProtobaseFutureImpl(connector.getContext()).setPING();
    session.getSession().flush(future1);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) FixedSession(com.generallycloud.baseio.container.FixedSession) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ParamedProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Aggregations

ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)27 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)17 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)16 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)16 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)16 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)15 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)15 SocketSession (com.generallycloud.baseio.component.SocketSession)12 Future (com.generallycloud.baseio.protocol.Future)11 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)10 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)8 FixedSession (com.generallycloud.baseio.container.FixedSession)8 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)8 IOException (java.io.IOException)6 MQException (com.generallycloud.baseio.container.jms.MQException)4 ParamedProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ProtobufUtil (com.generallycloud.baseio.codec.protobuf.ProtobufUtil)2 OnFuture (com.generallycloud.baseio.container.OnFuture)2 BytedMessage (com.generallycloud.baseio.container.jms.BytedMessage)2