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

org.rx.net.rpc.RemotingContext Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.rpc;

import io.netty.util.concurrent.FastThreadLocal;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.rx.net.transport.TcpClient;
import org.rx.net.transport.TcpServer;
import org.rx.util.function.Func;

import static org.rx.core.Extends.require;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class RemotingContext {
    static final FastThreadLocal CTX = new FastThreadLocal<>();

    public static RemotingContext context() {
        RemotingContext ctx = CTX.getIfExists();
        require(ctx);
        return ctx;
    }

    @SneakyThrows
    static  T invoke(Func fn, TcpServer rs, TcpClient rc) {
        CTX.set(new RemotingContext(rs, rc));
        try {
            return fn.invoke();
        } finally {
            CTX.remove();
        }
    }

    final TcpServer server;
    final TcpClient client;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy