本文整理汇总了Java中io.netty.channel.Channel.close方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.close方法的具体用法?Java Channel.close怎么用?Java Channel.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.Channel
的用法示例。
在下文中一共展示了Channel.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: closeChannelGracefully
import io.netty.channel.Channel; //导入方法依赖的package包/类
static void closeChannelGracefully(InetSocketAddress udpSource) {
Channel socksChannel = removeSocksMapping(udpSource);
Channel udpChannel = removeUdpMapping(udpSource);
Channel tcpChannel = removeTcpMapping(udpSource);
if (tcpChannel.isActive()) {
tcpChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
log.info("\t Proxy << Target \tDisconnect");
}
if (socksChannel.isActive()) {
socksChannel.close();
log.info("\tClient << Proxy \tDisconnect");
}
if (udpChannel.isActive()) {
udpChannel.close();
}
}
示例2: removeChannel
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 从缓存中移除Channel,并且关闭Channel
* @param channel
*/
public void removeChannel(Channel channel){
try{
logger.warn("channel will be remove, address is :{}", NettyUtil.parseChannelRemoteAddr(channel));
rwLock.writeLock().lock();
channel.close();
ChatUser chatUser= chatUserMap.get(channel);
if (chatUser!=null){
ChatUser tmp = chatUserMap.remove(channel);
if (tmp != null && tmp.isAuth()) {
// 减去一个认证用户
userCount.decrementAndGet();
}
}
}finally {
rwLock.writeLock().unlock();
}
}
示例3: registerServerSession
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 注册RPC生产者Session
*
* @param ctx
* @param providerName
* @param rpcServerPort
* @param rpcServerIp
* @return
*/
private ServiceXServerSession registerServerSession(Channel channel, String rpcServerIp, int rpcServerPort,
int providerId) {
if (rpcServers.containsKey(providerId)) {
channel.close();
log.error("服务提供者ID重复,断开链接,IP:" + channel.remoteAddress() + ",服务者ID:" + providerId);
return null;
}
ServiceXServerSession session = new ServiceXServerSession();
session.setSession(channel);
session.setProviderId(providerId);
session.setServerIp(rpcServerIp);
session.setServerPort(rpcServerPort);
rpcServers.put(session.getProviderId(), session);
log.info("RPC生产者[" + providerId + "]注册到服务中心:" + channel.remoteAddress() + ",生产者中心大小:" + rpcServers.size());
return session;
}
示例4: removeChannel
import io.netty.channel.Channel; //导入方法依赖的package包/类
/**
* 从缓存中移除Channel,并且关闭Channel
*
* @param channel
*/
public static void removeChannel(Channel channel) {
try {
logger.warn("channel will be remove, address is :{}", NettyUtil.parseChannelRemoteAddr(channel));
rwLock.writeLock().lock();
channel.close();
UserInfo userInfo = userInfos.get(channel);
if (userInfo != null) {
UserInfo tmp = userInfos.remove(channel);
if (tmp != null && tmp.isAuth()) {
// 减去一个认证用户
userCount.decrementAndGet();
}
}
} finally {
rwLock.writeLock().unlock();
}
}
示例5: preCreateConnections
import io.netty.channel.Channel; //导入方法依赖的package包/类
@Override
public void preCreateConnections(final int count)
throws ConnectException, IllegalArgumentException {
if(count > 0) {
for(int i = 0; i < count; i ++) {
final Channel conn = connectToAnyNode();
if(conn == null) {
throw new ConnectException(
"Failed to pre-create the connections to the target nodes"
);
}
final String nodeAddr = conn.attr(ATTR_KEY_NODE).get();
if(conn.isActive()) {
final Queue<Channel> connQueue = availableConns.get(nodeAddr);
if(connQueue != null) {
connQueue.add(conn);
}
} else {
conn.close();
}
}
LOG.info("Pre-created " + count + " connections");
} else {
throw new IllegalArgumentException("Connection count should be > 0, but got " + count);
}
}
示例6: notifyDied
import io.netty.channel.Channel; //导入方法依赖的package包/类
public void notifyDied(String username, EntityDamageEvent.DamageCause cause) {
webSocketServerThread.log(Level.INFO, "web user "+username+"'s entity died from "+cause);
Channel channel = name2channel.get(username);
if (channel != null) {
String message = "T,You died from "+(cause == null ? "unknown causes" : cause);
if (!dieDisconnect) {
message += ", but remain connected to the server as a ghost";
}
webSocketServerThread.sendLine(channel, message);
if (dieDisconnect) {
channel.close();
clientDisconnected(channel);
}
}
}
示例7: handleNewClient
import io.netty.channel.Channel; //导入方法依赖的package包/类
public void handleNewClient(ChannelHandlerContext ctx, String username, String token) {
Channel channel = ctx.channel();
if (!webPlayerBridge.newPlayer(channel, username, token)) {
channel.close();
return;
}
allUsersGroup.add(channel);
/* Send initial server messages on client connect here, example from Python server for comparison:
U,1,0,0,0,0,0
E,1491627331.01,600
T,Welcome to Craft!
T,Type "/help" for a list of commands.
N,1,guest1
*/
sendLine(channel, "B,0,0,0,30,0,1"); // floating grass block at (0,30,0) in chunk (0,0)
sendLine(channel, "K,0,0,0"); // update chunk key (0,0) to 0
sendLine(channel, "R,0,0"); // refresh chunk (0,0)
blockBridge.sendWorld(channel);
playersBridge.sendPlayers(channel);
}
示例8: handshake
import io.netty.channel.Channel; //导入方法依赖的package包/类
protected void handshake(HelloMessage request, Channel channel) {
try {
switch (getAuthScheme()) {
case CHALLENGE_RESPONSE:
handshakeChallengeResponse(request, channel);
break;
case NO_AUTH:
// shouldn't get here
break;
}
} catch (AuthException e) {
logger.warn("[{}->{}] Failed to authenticate connection: {}",
new Object[]{getLocalNodeIdString(),
getRemoteNodeIdString(),
e.getMessage()});
channel.writeAndFlush(getError(request.getHeader().getTransactionId(),
e, MessageType.HELLO));
channel.close();
}
}
示例9: startDownTask
import io.netty.channel.Channel; //导入方法依赖的package包/类
public static void startDownTask(TaskInfo taskInfo, HttpRequest httpRequest,
HttpResponse httpResponse, Channel clientChannel) {
HttpHeaders httpHeaders = httpResponse.headers();
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest);
HttpDownServer.DOWN_CONTENT.put(taskInfo.getId(), httpDownInfo);
httpHeaders.clear();
httpResponse.setStatus(HttpResponseStatus.OK);
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/html");
String host = HttpDownServer.isDev() ? "localhost"
: ((InetSocketAddress) clientChannel.localAddress()).getHostString();
String js =
"<script>window.top.location.href='http://" + host + ":" + HttpDownServer.VIEW_SERVER_PORT
+ "/#/tasks/new/" + httpDownInfo
.getTaskInfo().getId()
+ "';</script>";
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(js.getBytes());
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, js.getBytes().length);
clientChannel.writeAndFlush(httpResponse);
clientChannel.writeAndFlush(content);
clientChannel.close();
}
示例10: channelRead0
import io.netty.channel.Channel; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
Channel channel = ctx.channel();
Server server = Cloud.getInstance().getServerManager().getChannelToServer().get(channel);
if (packet instanceof PacketPing && server != null) {
PacketPing pingPacket = (PacketPing) packet;
long ping = System.currentTimeMillis() - pingPacket.getTimestamp();
server.setPing(ping);
} else if (packet instanceof PacketServerRegister) {
PacketServerRegister registerPacket = (PacketServerRegister) packet;
switch (registerPacket.getType()) {
case BUNGEECORD:
BungeeServer bungeeServer = new BungeeServer(channel);
bungeeServer.setPrefix("bungee");
Cloud.getInstance().getServerManager().registerServer(bungeeServer);
break;
case SPIGOT:
SpigotServer spigotServer = new SpigotServer(channel);
spigotServer.setPrefix(registerPacket.getPrefix());
spigotServer.setBukkitPort(registerPacket.getBukkitPort());
Cloud.getInstance().getServerManager().registerServer(spigotServer);
break;
case DAEMON:
Daemon daemon = new Daemon(channel);
daemon.setPrefix("daemon");
Cloud.getInstance().getServerManager().registerServer(daemon);
break;
default:
}
} else if (packet instanceof PacketCloseConnection) {
channel.close();
} else if (packet instanceof PacketServerLoad) {
PacketServerLoad serverLoad = (PacketServerLoad) packet;
Cloud.getInstance().getEventManager().callEvent(new DaemonLoadEvent(serverLoad.getCpuLoad(), serverLoad.getFreeRam(), server));
}
Cloud.getInstance().getEventManager().callEvent(new PacketReceivingEvent(packet, server));
}
示例11: closeChannelGracefully
import io.netty.channel.Channel; //导入方法依赖的package包/类
static void closeChannelGracefully(InetSocketAddress udpSource) {
Channel udpChannel = removeUdpMapping(udpSource);
Channel tcpChannel = removeTcpMapping(udpSource);
if (udpChannel.isActive()) {
log.info("\t Proxy << Target \tDisconnect");
udpChannel.close();
}
if (tcpChannel.isActive()) {
tcpChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
log.info("\tClient << Proxy \tDisconnect");
}
}
示例12: clearUnheart
import io.netty.channel.Channel; //导入方法依赖的package包/类
private void clearUnheart() {
if (checkHeartWhenConnectionCount > connectionCount.get()) {
return;
}
long now = System.currentTimeMillis();
for (Channel channel : validatedChannels) {
long last = 0;
long first = 0;
int count = 0;
if (channel.hasAttr(LAST_HEART_TIME)) {
last = channel.attr(LAST_HEART_TIME).get();
}
if (channel.hasAttr(FIRST_HEART_TIME)) {
first = channel.attr(FIRST_HEART_TIME).get();
}
if (channel.hasAttr(HEART_COUNT)) {
count = channel.attr(HEART_COUNT).get();
}
int allow = (int) ((now - first) / (heartIntervalSec * 1000));
if (count - 2 > allow) {
log.error(channel + " heart too quick,might be Game Accelerator,please check!");
channel.pipeline().fireExceptionCaught(new HeartTooQuickException(channel, first, now, count, allow));
channel.attr(FIRST_HEART_TIME).set(now);
channel.attr(HEART_COUNT).set(0);
}
if (count < allow - 2) {
channel.pipeline().fireExceptionCaught(new HeartNotAnswerException(channel, first, last, count));
channel.close();
}
}
}
示例13: channelRead0
import io.netty.channel.Channel; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg)
throws Exception {
Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
return;
}
if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
throw new IllegalStateException(
"Unexpected FullHttpResponse (getStatus=" + response.getStatus() +
", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
} else if (msg instanceof WebSocketFrame) {
WebSocketFrame frame = (WebSocketFrame) msg;
if (msg instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
System.out.println("WebSocket Client received message: " + textFrame.text());
} else if (msg instanceof PongWebSocketFrame) {
System.out.println("WebSocket Client received pong");
} else if (msg instanceof CloseWebSocketFrame) {
System.out.println("WebSocket Client received closing");
ch.close();
}
}
}
示例14: destroy
import io.netty.channel.Channel; //导入方法依赖的package包/类
public void destroy() {
logger.info("MQTT server is stopping...");
for (Channel channel : channels) {
channel.close();
}
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
logger.info("MQTT server is stopped...");
}
示例15: release
import io.netty.channel.Channel; //导入方法依赖的package包/类
@Override
public final void release(final Channel conn) {
final String nodeAddr = conn.attr(ATTR_KEY_NODE).get();
if(conn.isActive()) {
final Queue<Channel> connQueue = availableConns.get(nodeAddr);
if(connQueue != null) {
connQueue.add(conn);
}
concurrencyThrottle.release();
} else {
conn.close();
}
}