Skip to content

Commit

Permalink
代码优化,抽离部分通用代码
Browse files Browse the repository at this point in the history
  • Loading branch information
夜色 committed Jun 9, 2018
1 parent 0c2fa13 commit ca290d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
Expand Up @@ -24,14 +24,12 @@
import com.mpush.api.service.Listener; import com.mpush.api.service.Listener;
import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketDecoder;
import com.mpush.netty.codec.PacketEncoder; import com.mpush.netty.codec.PacketEncoder;
import com.mpush.tools.config.CC;
import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.ThreadNames;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator; import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel; import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.epoll.Native;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory; import io.netty.util.concurrent.DefaultThreadFactory;
Expand All @@ -41,6 +39,8 @@
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.channels.spi.SelectorProvider; import java.nio.channels.spi.SelectorProvider;


import static com.mpush.tools.Utils.useNettyEpoll;

public abstract class NettyTCPClient extends BaseService implements Client { public abstract class NettyTCPClient extends BaseService implements Client {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyTCPClient.class); private static final Logger LOGGER = LoggerFactory.getLogger(NettyTCPClient.class);


Expand Down Expand Up @@ -129,18 +129,6 @@ protected void doStart(Listener listener) throws Throwable {
} }
} }


private boolean useNettyEpoll() {
if (CC.mp.core.useNettyEpoll()) {
try {
Native.offsetofEpollData();
return true;
} catch (UnsatisfiedLinkError error) {
LOGGER.warn("can not load netty epoll, switch nio model.");
}
}
return false;
}

@Override @Override
protected void doStop(Listener listener) throws Throwable { protected void doStop(Listener listener) throws Throwable {
if (workerGroup != null) { if (workerGroup != null) {
Expand Down
Expand Up @@ -26,14 +26,12 @@
import com.mpush.netty.codec.PacketDecoder; import com.mpush.netty.codec.PacketDecoder;
import com.mpush.netty.codec.PacketEncoder; import com.mpush.netty.codec.PacketEncoder;
import com.mpush.tools.common.Strings; import com.mpush.tools.common.Strings;
import com.mpush.tools.config.CC;
import com.mpush.tools.thread.ThreadNames; import com.mpush.tools.thread.ThreadNames;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator; import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.epoll.EpollEventLoopGroup; import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.epoll.Native;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory; import io.netty.util.concurrent.DefaultThreadFactory;
Expand All @@ -45,6 +43,8 @@
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;


import static com.mpush.tools.Utils.useNettyEpoll;

/** /**
* Created by ohun on 2015/12/22. * Created by ohun on 2015/12/22.
* *
Expand Down Expand Up @@ -302,18 +302,6 @@ protected int getIoRate() {
return 70; return 70;
} }


protected boolean useNettyEpoll() {
if (CC.mp.core.useNettyEpoll()) {
try {
Native.offsetofEpollData();
return true;
} catch (UnsatisfiedLinkError error) {
logger.warn("can not load netty epoll, switch nio model.");
}
}
return false;
}

public EventLoopGroup getBossGroup() { public EventLoopGroup getBossGroup() {
return bossGroup; return bossGroup;
} }
Expand Down
15 changes: 14 additions & 1 deletion mpush-tools/src/main/java/com/mpush/tools/Utils.java
Expand Up @@ -19,6 +19,7 @@


package com.mpush.tools; package com.mpush.tools;


import com.mpush.tools.config.CC;
import com.mpush.tools.thread.NamedThreadFactory; import com.mpush.tools.thread.NamedThreadFactory;
import io.netty.channel.EventLoopGroup; import io.netty.channel.EventLoopGroup;
import io.netty.channel.SingleThreadEventLoop; import io.netty.channel.SingleThreadEventLoop;
Expand Down Expand Up @@ -55,6 +56,18 @@ public static Thread newThread(String name, Runnable target) {
return NAMED_THREAD_FACTORY.newThread(name, target); return NAMED_THREAD_FACTORY.newThread(name, target);
} }


public static boolean useNettyEpoll() {
if (CC.mp.core.useNettyEpoll()) {
try {
Class.forName("io.netty.channel.epoll.Native");
return true;
} catch (Throwable error) {
LOGGER.warn("can not load netty epoll, switch nio model.");
}
}
return false;
}

public static boolean isLocalHost(String host) { public static boolean isLocalHost(String host) {
return host == null return host == null
|| host.length() == 0 || host.length() == 0
Expand Down Expand Up @@ -118,7 +131,7 @@ public static String getInetAddress(boolean getLocal) {
return address.getHostAddress(); return address.getHostAddress();
} }
} else { } else {
if (!address.isSiteLocalAddress() && !address.isLoopbackAddress()) { if (!address.isSiteLocalAddress()) {
return address.getHostAddress(); return address.getHostAddress();
} }
} }
Expand Down

0 comments on commit ca290d2

Please sign in to comment.