ManagementServer.java

net.minecraft.server.jsonrpc.ManagementServer

信息

  • 全限定名:net.minecraft.server.jsonrpc.ManagementServer
  • 类型:public class
  • 包:net.minecraft.server.jsonrpc
  • 源码路径:src/main/java/net/minecraft/server/jsonrpc/ManagementServer.java
  • 起始行号:L33
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L34
    • 说明:

      TODO

  • hostAndPort

    • 类型: HostAndPort
    • 修饰符: private final
    • 源码定位: L35
    • 说明:

      TODO

  • authenticationHandler

    • 类型: AuthenticationHandler
    • 修饰符: private final
    • 源码定位: L36
    • 说明:

      TODO

  • serverChannel

    • 类型: Channel
    • 修饰符: private
    • 源码定位: L37
    • 说明:

      TODO

  • nioEventLoopGroup

    • 类型: NioEventLoopGroup
    • 修饰符: private final
    • 源码定位: L38
    • 说明:

      TODO

  • connections

    • 类型: Set<Connection>
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ManagementServer(HostAndPort hostAndPort, AuthenticationHandler authenticationHandler) @ L41

  • 构造器名:ManagementServer
  • 源码定位:L41
  • 修饰符:public

参数:

  • hostAndPort: HostAndPort
  • authenticationHandler: AuthenticationHandler

说明:

TODO

public ManagementServer(HostAndPort hostAndPort, AuthenticationHandler authenticationHandler, NioEventLoopGroup nioEventLoopGroup) @ L47

  • 构造器名:ManagementServer
  • 源码定位:L47
  • 修饰符:public

参数:

  • hostAndPort: HostAndPort
  • authenticationHandler: AuthenticationHandler
  • nioEventLoopGroup: NioEventLoopGroup

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void onConnected(Connection connection) @ L53

  • 方法名:onConnected
  • 源码定位:L53
  • 返回类型:void
  • 修饰符:public

参数:

  • connection: Connection

说明:

TODO

public void onDisconnected(Connection connection) @ L59

  • 方法名:onDisconnected
  • 源码定位:L59
  • 返回类型:void
  • 修饰符:public

参数:

  • connection: Connection

说明:

TODO

public void startWithoutTls(MinecraftApi minecraftApi) @ L65

  • 方法名:startWithoutTls
  • 源码定位:L65
  • 返回类型:void
  • 修饰符:public

参数:

  • minecraftApi: MinecraftApi

说明:

TODO

public void startWithTls(MinecraftApi minecraftApi, SslContext sslContext) @ L69

  • 方法名:startWithTls
  • 源码定位:L69
  • 返回类型:void
  • 修饰符:public

参数:

  • minecraftApi: MinecraftApi
  • sslContext: SslContext

说明:

TODO

private void start(MinecraftApi minecraftApi, SslContext sslContext) @ L73

  • 方法名:start
  • 源码定位:L73
  • 返回类型:void
  • 修饰符:private

参数:

  • minecraftApi: MinecraftApi
  • sslContext: SslContext

说明:

TODO

public void stop(boolean closeNioEventLoopGroup) @ L114

  • 方法名:stop
  • 源码定位:L114
  • 返回类型:void
  • 修饰符:public

参数:

  • closeNioEventLoopGroup: boolean

说明:

TODO

public void tick() @ L126

  • 方法名:tick
  • 源码定位:L126
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public int getPort() @ L130

  • 方法名:getPort
  • 源码定位:L130
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

void forEachConnection(Consumer<Connection> action) @ L134

  • 方法名:forEachConnection
  • 源码定位:L134
  • 返回类型:void
  • 修饰符:package-private

参数:

  • action: Consumer

说明:

TODO

代码

public class ManagementServer {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final HostAndPort hostAndPort;
    private final AuthenticationHandler authenticationHandler;
    private @Nullable Channel serverChannel;
    private final NioEventLoopGroup nioEventLoopGroup;
    private final Set<Connection> connections = Sets.newIdentityHashSet();
 
    public ManagementServer(HostAndPort hostAndPort, AuthenticationHandler authenticationHandler) {
        this.hostAndPort = hostAndPort;
        this.authenticationHandler = authenticationHandler;
        this.nioEventLoopGroup = new NioEventLoopGroup(0, new ThreadFactoryBuilder().setNameFormat("Management server IO #%d").setDaemon(true).build());
    }
 
    public ManagementServer(HostAndPort hostAndPort, AuthenticationHandler authenticationHandler, NioEventLoopGroup nioEventLoopGroup) {
        this.hostAndPort = hostAndPort;
        this.authenticationHandler = authenticationHandler;
        this.nioEventLoopGroup = nioEventLoopGroup;
    }
 
    public void onConnected(Connection connection) {
        synchronized (this.connections) {
            this.connections.add(connection);
        }
    }
 
    public void onDisconnected(Connection connection) {
        synchronized (this.connections) {
            this.connections.remove(connection);
        }
    }
 
    public void startWithoutTls(MinecraftApi minecraftApi) {
        this.start(minecraftApi, null);
    }
 
    public void startWithTls(MinecraftApi minecraftApi, SslContext sslContext) {
        this.start(minecraftApi, sslContext);
    }
 
    private void start(MinecraftApi minecraftApi, @Nullable SslContext sslContext) {
        final JsonRpcLogger jsonrpcLogger = new JsonRpcLogger();
        ChannelFuture channel = new ServerBootstrap()
            .handler(new LoggingHandler(LogLevel.DEBUG))
            .channel(NioServerSocketChannel.class)
            .childHandler(
                new ChannelInitializer<Channel>() {
                    {
                        Objects.requireNonNull(ManagementServer.this);
                    }
 
                    @Override
                    protected void initChannel(Channel channel) {
                        try {
                            channel.config().setOption(ChannelOption.TCP_NODELAY, true);
                        } catch (ChannelException var3) {
                        }
 
                        ChannelPipeline pipeline = channel.pipeline();
                        if (sslContext != null) {
                            pipeline.addLast(sslContext.newHandler(channel.alloc()));
                        }
 
                        pipeline.addLast(new HttpServerCodec())
                            .addLast(new HttpObjectAggregator(65536))
                            .addLast(ManagementServer.this.authenticationHandler)
                            .addLast(new WebSocketServerProtocolHandler("/"))
                            .addLast(new WebSocketToJsonCodec())
                            .addLast(new JsonToWebSocketEncoder())
                            .addLast(new Connection(channel, ManagementServer.this, minecraftApi, jsonrpcLogger));
                    }
                }
            )
            .group(this.nioEventLoopGroup)
            .localAddress(this.hostAndPort.getHost(), this.hostAndPort.getPort())
            .bind();
        this.serverChannel = channel.channel();
        channel.syncUninterruptibly();
        LOGGER.info("Json-RPC Management connection listening on {}:{}", this.hostAndPort.getHost(), this.getPort());
    }
 
    public void stop(boolean closeNioEventLoopGroup) throws InterruptedException {
        if (this.serverChannel != null) {
            this.serverChannel.close().sync();
            this.serverChannel = null;
        }
 
        this.connections.clear();
        if (closeNioEventLoopGroup) {
            this.nioEventLoopGroup.shutdownGracefully().sync();
        }
    }
 
    public void tick() {
        this.forEachConnection(Connection::tick);
    }
 
    public int getPort() {
        return this.serverChannel != null ? ((InetSocketAddress)this.serverChannel.localAddress()).getPort() : this.hostAndPort.getPort();
    }
 
    void forEachConnection(Consumer<Connection> action) {
        synchronized (this.connections) {
            this.connections.forEach(action);
        }
    }
}

引用的其他类