Java Code Examples for java.nio.channels.AsynchronousSocketChannel#getRemoteAddress()

The following examples show how to use java.nio.channels.AsynchronousSocketChannel#getRemoteAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultGLSPServerLauncher.java    From graphical-lsp with Eclipse Public License 2.0 6 votes vote down vote up
private void createClientConnection(AsynchronousSocketChannel socketChannel) {
	Injector injector = Guice.createInjector(getGLSPModule());
	GsonConfigurator gsonConf = injector.getInstance(GsonConfigurator.class);

	InputStream in = Channels.newInputStream(socketChannel);
	OutputStream out = Channels.newOutputStream(socketChannel);

	Consumer<GsonBuilder> configureGson = (GsonBuilder builder) -> gsonConf.configureGsonBuilder(builder);
	Function<MessageConsumer, MessageConsumer> wrapper = Function.identity();
	GLSPServer languageServer = injector.getInstance(GLSPServer.class);

	Launcher<GLSPClient> launcher = Launcher.createIoLauncher(languageServer, GLSPClient.class, in, out, threadPool,
			wrapper, configureGson);
	languageServer.connect(launcher.getRemoteProxy());
	launcher.startListening();

	try {
		SocketAddress remoteAddress = socketChannel.getRemoteAddress();
		log.info("Started language server for client " + remoteAddress);
	} catch (IOException ex) {
		log.error("Failed to get the remoteAddress for the new client connection: " + ex.getMessage(), ex);
	}
}
 
Example 2
Source File: AsynchronousSocketStep.java    From coroutines with Apache License 2.0 6 votes vote down vote up
/***************************************
 * {@inheritDoc}
 */
@Override
protected ByteBuffer execute(
	ByteBuffer		rData,
	Continuation<?> rContinuation)
{
	try
	{
		AsynchronousSocketChannel rChannel =
			getSocketChannel(rContinuation);

		if (rChannel.getRemoteAddress() == null)
		{
			rChannel.connect(getSocketAddress(rContinuation)).get();
		}

		performBlockingOperation(rChannel, rData);
	}
	catch (Exception e)
	{
		throw new CoroutineException(e);
	}

	return rData;
}
 
Example 3
Source File: TcpManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void failed(Throwable ex, ConnectParam param)
{
	AsynchronousSocketChannel channel = param.channel;
	try
	{
		SocketAddress addr = (channel.isOpen() ? channel.getRemoteAddress() : null);
		closeChannel(channel);
		onConnectFailed(addr, ex);
	}
	catch (Exception e)
	{
		closeChannel(channel);
		doException(null, e);
	}
}
 
Example 4
Source File: ConnectionHelper.java    From L2jOrg with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean accept(AsynchronousSocketChannel channel) {
    try {
        var socketAddress = (InetSocketAddress) channel.getRemoteAddress();
        return !AuthController.getInstance().isBannedAddress(socketAddress.getAddress().getHostAddress());
    } catch (IOException e) {
        return false;
    }
}
 
Example 5
Source File: AsynchronousSocketStep.java    From coroutines with Apache License 2.0 5 votes vote down vote up
/***************************************
 * Opens and connects a {@link AsynchronousSocketChannel} to the {@link
 * SocketAddress} of this step and then performs the channel operation
 * asynchronously.
 *
 * @param rData       The byte buffer of the data to be processed
 * @param rSuspension The coroutine suspension to be resumed when the
 *                    operation is complete
 */
private void connectAsync(
	ByteBuffer			   rData,
	Suspension<ByteBuffer> rSuspension)
{
	try
	{
		AsynchronousSocketChannel rChannel =
			getSocketChannel(rSuspension.continuation());

		if (rChannel.getRemoteAddress() == null)
		{
			SocketAddress rSocketAddress =
				fGetSocketAddress.apply(rSuspension.continuation());

			rChannel.connect(
				rSocketAddress,
				rData,
				new ChannelCallback<>(
					rChannel,
					rSuspension,
					this::performAsyncOperation));
		}
		else
		{
			performAsyncOperation(
				FIRST_OPERATION,
				rChannel,
				rData,
				new ChannelCallback<>(
					rChannel,
					rSuspension,
					this::performAsyncOperation));
		}
	}
	catch (Exception e)
	{
		rSuspension.fail(e);
	}
}
 
Example 6
Source File: ChannelContext.java    From oxygen with Apache License 2.0 5 votes vote down vote up
public void setChannel(AsynchronousSocketChannel channel) {
  this.channel = channel;
  try {
    if (isServer()) {
      this.address = (InetSocketAddress) channel.getRemoteAddress();
    } else {
      this.address = (InetSocketAddress) channel.getLocalAddress();
    }
  } catch (IOException ignore) {
    //ignore
  }
  this.future = new CompletableFuture<>();
}
 
Example 7
Source File: NetworkUtil.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
public static long getClientIdBySocketChannel(AsynchronousSocketChannel socketChannel) throws IOException{
  	InetSocketAddress address = (InetSocketAddress)socketChannel.getRemoteAddress();
  	byte[] quad = address.getAddress().getAddress();
int port = address.getPort();
long clientId = NetworkUtil.convertIpPortToUniqueId(quad, port);
return clientId;
  }
 
Example 8
Source File: NetworkUtil.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
public static long getClientIdBySocketChannel(AsynchronousSocketChannel socketChannel) throws IOException{
  	InetSocketAddress address = (InetSocketAddress)socketChannel.getRemoteAddress();
  	byte[] quad = address.getAddress().getAddress();
int port = address.getPort();
long clientId = NetworkUtil.convertIpPortToUniqueId(quad, port);
return clientId;
  }
 
Example 9
Source File: NetworkUtil.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
public static long getClientIdBySocketChannel(AsynchronousSocketChannel socketChannel) throws IOException{
  	InetSocketAddress address = (InetSocketAddress)socketChannel.getRemoteAddress();
  	byte[] quad = address.getAddress().getAddress();
int port = address.getPort();
long clientId = NetworkUtil.convertIpPortToUniqueId(quad, port);
return clientId;
  }
 
Example 10
Source File: ServerChannelContext.java    From t-io with Apache License 2.0 3 votes vote down vote up
/**
 * @see org.tio.core.ChannelContext#createClientNode(java.nio.channels.AsynchronousSocketChannel)
 *
 * @param asynchronousSocketChannel
 * @return
 * @throws IOException
 * @author tanyaowu
 * 2016年12月6日 下午12:18:08
 *
 */
@Override
public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
	InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getRemoteAddress();
	Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
	return clientNode;
}
 
Example 11
Source File: ServerChannelContext.java    From talent-aio with GNU Lesser General Public License v2.1 3 votes vote down vote up
/** 
 * @see com.talent.aio.common.ChannelContext#createClientNode(java.nio.channels.AsynchronousSocketChannel)
 * 
 * @param asynchronousSocketChannel
 * @return
 * @throws IOException 
 * @重写人: tanyaowu
 * @重写时间: 2016年12月6日 下午12:18:08
 * 
 */
@Override
public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException
{
	InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getRemoteAddress();
	Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
	return clientNode;
}