Java tutorial
/** * Copyright 20151121 Hxms. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ package org.server.core.netty.tcp; import java.net.InetSocketAddress; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.socket.nio.NioSocketChannel; /** * * tcp * * @author Hxms * */ class BaseConnector extends BaseSession { /** * ? */ ChannelFuture connectFuture; /** * ? * * @return ? */ protected ChannelFuture validConnectTask() { if (connectFuture == null) return null; ChannelFuture future = connectFuture; connectFuture = null; return future; } /** * ?? */ protected void connectFutureFinish(ChannelFuture future) { future = validConnectTask(); if (future != null) { connectFinalCallback(future, 0); } } /** * * ? * * @param future * ? * * @param retryCount * ? */ protected void connectFinalCallback(ChannelFuture future, int retryCount) { // close before channel closeChannel(); // assignment new channel channel = (NioSocketChannel) future.channel(); // call the event safeCallHandle(h -> h.onConnectFinish(future.isSuccess(), channel, future.cause(), retryCount)); } /** * * * @param hostname * ?? * @param port * ? */ public boolean connect(String hostname, int port) { return connect(new InetSocketAddress(hostname, port)); } /** * * * @param socketAddress * ? * @return ??? */ public boolean connect(InetSocketAddress socketAddress) { if (connectFuture != null) return false; ChannelFutureListener listener = this::connectFutureFinish; connectFuture = bootstrap.connect(socketAddress); connectFuture.addListener(listener); return true; } /** * ? * * @throws InterruptedException */ public void waitConnect() throws InterruptedException { if (connectFuture != null) connectFuture.sync(); } /* * (non-Javadoc) * * @see java.lang.AutoCloseable#close() */ @Override public void close() throws Exception { closeChannel(); } }