net client
This commit is contained in:
parent
6b7923cf41
commit
193a12b00f
2 changed files with 45 additions and 63 deletions
|
@ -1,31 +1,18 @@
|
|||
package common.network;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import common.future.ThreadFactoryBuilder;
|
||||
import common.log.Log;
|
||||
import common.network.NetHandler.ThreadQuickExitException;
|
||||
import common.util.LazyLoadBase;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelException;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||
import io.netty.handler.timeout.TimeoutException;
|
||||
import io.netty.util.AttributeKey;
|
||||
import io.netty.util.concurrent.Future;
|
||||
|
@ -35,13 +22,6 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
|
|||
{
|
||||
private static final Pattern IP_REPLACER = Pattern.compile("([0-9]*)\\.([0-9]*)\\.[0-9]*\\.[0-9]*");
|
||||
public static final AttributeKey<PacketRegistry> ATTR_STATE = AttributeKey.<PacketRegistry>valueOf("protocol");
|
||||
public static final LazyLoadBase<NioEventLoopGroup> CLIENT_NIO_EVENTLOOP = new LazyLoadBase<NioEventLoopGroup>()
|
||||
{
|
||||
protected NioEventLoopGroup load()
|
||||
{
|
||||
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build());
|
||||
}
|
||||
};
|
||||
private final Queue<NetConnection.InboundHandlerTuplePacketListener> outboundPacketsQueue = new ConcurrentLinkedQueue<InboundHandlerTuplePacketListener>();
|
||||
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
|
||||
|
||||
|
@ -290,48 +270,6 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
|
|||
this.terminationReason = message;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new NetworkManager from the server host and connect it to the server
|
||||
*
|
||||
* @param address The address of the server
|
||||
* @param serverPort The server port
|
||||
*/
|
||||
public static NetConnection createNetworkManagerAndConnect(InetAddress address, int serverPort)
|
||||
{
|
||||
final NetConnection networkmanager = new NetConnection();
|
||||
Class <? extends SocketChannel > oclass;
|
||||
LazyLoadBase <? extends EventLoopGroup > lazyloadbase;
|
||||
|
||||
// if (Epoll.isAvailable())
|
||||
// {
|
||||
// oclass = EpollSocketChannel.class;
|
||||
// lazyloadbase = CLIENT_EPOLL_EVENTLOOP;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
oclass = NioSocketChannel.class;
|
||||
lazyloadbase = CLIENT_NIO_EVENTLOOP;
|
||||
// }
|
||||
|
||||
((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group((EventLoopGroup)lazyloadbase.getValue())).handler(new ChannelInitializer<Channel>()
|
||||
{
|
||||
protected void initChannel(Channel p_initChannel_1_) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true));
|
||||
}
|
||||
catch (ChannelException var3)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"splitter", (ChannelHandler)(new PacketSplitter())).addLast((String)"decoder", (ChannelHandler)(new PacketDecoder(false))).addLast((String)"prepender", (ChannelHandler)(new PacketPrepender())).addLast((String)"encoder", (ChannelHandler)(new PacketEncoder(true))).addLast((String)"packet_handler", (ChannelHandler)networkmanager);
|
||||
}
|
||||
})).channel(oclass)).connect(address, serverPort).syncUninterruptibly();
|
||||
return networkmanager;
|
||||
}
|
||||
|
||||
public boolean isChannelOpen()
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue