use of io.netty.util.concurrent.GenericFutureListener in project study-by-myself by Howinfun.
the class NettyServer method main.
public static void main(String[] args) throws Exception {
NioEventLoopGroup boss = new NioEventLoopGroup();
NioEventLoopGroup work = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, work).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel channel) throws Exception {
channel.pipeline().addLast(new ProtobufVarint32FrameDecoder()).addLast(new ProtobufDecoder(JsonMsgProto.Msg.getDefaultInstance())).addLast(new ProtobufProcessorHandler());
}
});
serverBootstrap.bind("127.0.0.1", 8080).addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
if (future.isSuccess()) {
System.out.println("启动成功!");
} else {
System.out.println("启动失败!");
}
}
}).sync();
ThreadUtil.sleep(Integer.MAX_VALUE);
}
use of io.netty.util.concurrent.GenericFutureListener in project Minecraft-SlientClient-Hack by YouNeverKnow00.
the class NetworkSystem method networkTick.
/**
* Will try to process the packets received by each NetworkManager, gracefully manage processing failures and cleans
* up dead connections
*/
public void networkTick() {
synchronized (this.networkManagers) {
Iterator<NetworkManager> iterator = this.networkManagers.iterator();
while (iterator.hasNext()) {
final NetworkManager networkmanager = iterator.next();
if (!networkmanager.hasNoChannel()) {
if (networkmanager.isChannelOpen()) {
try {
networkmanager.processReceivedPackets();
} catch (Exception exception) {
if (networkmanager.isLocalChannel()) {
CrashReport crashreport = CrashReport.makeCrashReport(exception, "Ticking memory connection");
CrashReportCategory crashreportcategory = crashreport.makeCategory("Ticking connection");
crashreportcategory.setDetail("Connection", new ICrashReportDetail<String>() {
public String call() throws Exception {
return networkmanager.toString();
}
});
throw new ReportedException(crashreport);
}
LOGGER.warn("Failed to handle packet for {}", networkmanager.getRemoteAddress(), exception);
final TextComponentString textcomponentstring = new TextComponentString("Internal server error");
networkmanager.sendPacket(new SPacketDisconnect(textcomponentstring), new GenericFutureListener<Future<? super Void>>() {
public void operationComplete(Future<? super Void> p_operationComplete_1_) throws Exception {
networkmanager.closeChannel(textcomponentstring);
}
});
networkmanager.disableAutoRead();
}
} else {
iterator.remove();
networkmanager.checkDisconnected();
}
}
}
}
}
use of io.netty.util.concurrent.GenericFutureListener in project CumServerPro by MCUmbrella.
the class NetworkDispatcher method kickWithMessage.
private void kickWithMessage(String message) {
FMLLog.log.error("Network Disconnect: {}", message);
final TextComponentString TextComponentString = new TextComponentString(message);
if (side == Side.CLIENT) {
manager.closeChannel(TextComponentString);
} else {
manager.sendPacket(new SPacketDisconnect(TextComponentString), new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> result) {
manager.closeChannel(TextComponentString);
}
}, (GenericFutureListener<? extends Future<? super Void>>[]) null);
}
manager.channel().config().setAutoRead(false);
}
use of io.netty.util.concurrent.GenericFutureListener in project pollen by MoonflowerTeam.
the class PollinatedFabricLoginChannel method processClient.
private CompletableFuture<@Nullable FriendlyByteBuf> processClient(Minecraft client, ClientHandshakePacketListenerImpl listener, FriendlyByteBuf data, Consumer<GenericFutureListener<? extends Future<? super Void>>> listenerAdder) {
CompletableFuture<FriendlyByteBuf> future = new CompletableFuture<>();
NetworkRegistry.processMessage(this.deserialize(data, PollinatedPacketDirection.LOGIN_CLIENTBOUND), new PollinatedFabricLoginPacketContext(pkt -> {
try {
future.complete(this.serialize(pkt, PollinatedPacketDirection.LOGIN_SERVERBOUND));
} catch (Throwable t) {
t.printStackTrace();
future.completeExceptionally(t);
}
}, listener.getConnection(), __ -> {
}, PollinatedPacketDirection.LOGIN_CLIENTBOUND), this.clientMessageHandler.get().get());
return future;
}
use of io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse-vertx.
the class NetClientImpl method connectInternal.
public void connectInternal(ProxyOptions proxyOptions, SocketAddress remoteAddress, SocketAddress peerAddress, String serverName, boolean ssl, boolean useAlpn, boolean registerWriteHandlers, Promise<NetSocket> connectHandler, ContextInternal context, int remainingAttempts) {
checkClosed();
EventLoop eventLoop = context.nettyEventLoop();
if (eventLoop.inEventLoop()) {
Objects.requireNonNull(connectHandler, "No null connectHandler accepted");
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoop);
bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
vertx.transport().configure(options, remoteAddress.isDomainSocket(), bootstrap);
ChannelProvider channelProvider = new ChannelProvider(bootstrap, sslHelper, context).proxyOptions(proxyOptions);
channelProvider.handler(ch -> connected(context, ch, connectHandler, remoteAddress, channelProvider.applicationProtocol(), registerWriteHandlers));
io.netty.util.concurrent.Future<Channel> fut = channelProvider.connect(remoteAddress, peerAddress, serverName, ssl, useAlpn);
fut.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Channel>>) future -> {
if (!future.isSuccess()) {
Throwable cause = future.cause();
boolean connectError = cause instanceof ConnectException || cause instanceof FileNotFoundException;
if (connectError && (remainingAttempts > 0 || remainingAttempts == -1)) {
context.emit(v -> {
log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds");
vertx.setTimer(options.getReconnectInterval(), tid -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1));
});
} else {
failed(context, null, cause, connectHandler);
}
}
});
} else {
eventLoop.execute(() -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts));
}
}
Aggregations