Examples of SocketChannel


Examples of java.nio.channels.SocketChannel

    /** We handle only non-SSL connections */
    void loop(Selector selector) {
        Set                 ready_keys;
        SelectionKey        key;
        ServerSocketChannel srv_sock;
        SocketChannel       in_sock, out_sock;
        InetSocketAddress   src, dest;

        while (true) {
            if (verbose)
                log("[Proxy] ready to accept connection");

            // 4. Call Selector.select()
            try {
                selector.select();

                // get set of ready objects
                ready_keys=selector.selectedKeys();
                for (Iterator it=ready_keys.iterator(); it.hasNext();) {
                    key=(SelectionKey) it.next();
                    it.remove();

                    if (key.isAcceptable()) {
                        srv_sock=(ServerSocketChannel) key.channel();
                        // get server socket and attachment
                        src=(InetSocketAddress) key.attachment();
                        in_sock=srv_sock.accept(); // accept request
                        if (verbose)
                            log("Proxy.loop()", "accepted connection from " + toString(in_sock));
                        dest=(InetSocketAddress) mappings.get(src);
                        // find corresponding dest
                        if (dest == null) {
                            in_sock.close();
                            log("Proxy.loop()", "did not find a destination host for " + src);
                            continue;
                        }
                        else {
                            if (verbose)
                                log("Proxy.loop()", "relaying traffic from " + toString(src) + " to " + toString(dest));
                        }

                        // establish connection to destination host
                        try {
                            out_sock=SocketChannel.open(dest);
                            // uses thread pool (Executor) to handle request, closes socks at end
                            handleConnection(in_sock, out_sock);
                        }
                        catch (Exception ex) {
                            in_sock.close();
                            throw ex;
                        }
                    }
                }
            }
View Full Code Here

Examples of java.nio.channels.SocketChannel

   
    void _handleConnection(final SocketChannel in_channel, final SocketChannel out_channel) throws Exception {
        executor.execute(new Runnable() {
                public void run() {
                    Selector sel=null;
                    SocketChannel tmp;
                    Set ready_keys;
                    SelectionKey key;
                    ByteBuffer transfer_buf=ByteBuffer.allocate(BUFSIZE);

                    try {
View Full Code Here

Examples of java.nio.channels.SocketChannel

   
      throws IOException
    {
      connection.setTimeStamp();
     
      SocketChannel  chan1 = sc;
      SocketChannel  chan2 = sc==source_channel?target_channel:source_channel;
     
      DirectByteBuffer  read_buffer = sc==source_channel?source_buffer:target_buffer;
                 
      int  len = read_buffer.read( DirectByteBuffer.SS_PROXY, chan1 );
     
View Full Code Here

Examples of java.nio.channels.SocketChannel

      throws IOException
    {
        // socket SX -> SY via BX
        // so if SX = source_channel then BX is target buffer
     
      SocketChannel  chan1 = sc;
      SocketChannel  chan2 = sc==source_channel?target_channel:source_channel;
     
      DirectByteBuffer  read_buffer = sc==source_channel?target_buffer:source_buffer;
     
      int written = read_buffer.write( DirectByteBuffer.SS_PROXY, chan1 );
           
View Full Code Here

Examples of java.nio.channels.SocketChannel

 
  protected void
  outgoing()
  {
    try{     
      final SocketChannel  channel = SocketChannel.open();
     
      channel.configureBlocking( false );
   
      if ( channel.connect( new InetSocketAddress("localhost", 8765 ))){
             
        outgoing( channel );
       
      }else{
       
        connect_selector.register(
          channel,
          new VirtualSelectorListener()
          {
            public boolean
            selectSuccess(
              VirtualChannelSelector selector, SocketChannel sc, Object attachment)
            {
              try{
                if ( channel.finishConnect()){
                 
                  outgoing( channel );
                 
                  return( true );
                }else{
View Full Code Here

Examples of java.nio.channels.SocketChannel

 
  private final TransportEndpoint      transport_endpoint;
  private final TransportHelperFilter   filter; 
 
  public LightweightTCPTransport( ProtocolEndpoint  pe, TransportHelperFilter filter ) {
    SocketChannel channel = ((TCPTransportHelper)filter.getHelper()).getSocketChannel();
    transport_endpoint  = new TransportEndpointTCP( pe, channel );
    this.filter = filter;
  }
View Full Code Here

Examples of java.nio.channels.SocketChannel

  int            local_port,
  TransportHelperFilter   filter )

  {
    SocketChannel  channel = ((TCPTransportHelper)filter.getHelper()).getSocketChannel();
      
    //set advanced socket options
    try {
      int so_sndbuf_size = COConfigurationManager.getIntParameter( "network.tcp.socket.SO_SNDBUF" );
      if( so_sndbuf_size > 0 channel.socket().setSendBufferSize( so_sndbuf_size );
     
      String ip_tos = COConfigurationManager.getStringParameter( "network.tcp.socket.IPDiffServ" );
      if( ip_tos.length() > 0 channel.socket().setTrafficClass( Integer.decode( ip_tos ).intValue() );
    }
    catch( Throwable t ) {
      t.printStackTrace();
    }
   
  InetSocketAddress tcp_address = new InetSocketAddress( channel.socket().getInetAddress(), channel.socket().getPort());

  ConnectionEndpoint  co_ep = new ConnectionEndpoint(tcp_address);

  ProtocolEndpointTCP  pe_tcp = (ProtocolEndpointTCP)ProtocolEndpointFactory.createEndpoint( ProtocolEndpoint.PROTOCOL_TCP, co_ep, tcp_address );
View Full Code Here

Examples of java.nio.channels.SocketChannel

        }
      }, null );
    }
    catch( Throwable t ) {
      Debug.out( t );
      SocketChannel chan = proxy_connection.getSocketChannel();
      if ( chan != null ){
        TCPNetworkManager.getSingleton().getReadSelector().cancel( chan );
      }
      proxy_listener.connectFailure( t );
    }
View Full Code Here

Examples of java.nio.channels.SocketChannel

      Debug.out( "socket_channel == null" );
      return;
    }
   
    try{
      SocketChannel  channel = getSocketChannel();
     
      channel.socket().setSendBufferSize( size_in_bytes );
      channel.socket().setReceiveBufferSize( size_in_bytes );
     
      int snd_real = channel.socket().getSendBufferSize();
      int rcv_real = channel.socket().getReceiveBufferSize();
     
      if (Logger.isEnabled())
        Logger.log(new LogEvent(LOGID, "Setting new transport [" + description
          + "] buffer sizes: SND=" + size_in_bytes + " [" + snd_real
          + "] , RCV=" + size_in_bytes + " [" + rcv_real + "]"));
View Full Code Here

Examples of java.nio.channels.SocketChannel

       
        if( waiting_time > request.connect_timeout ) {
         
          i.remove();
 
          SocketChannel channel = request.channel;
         
          connect_selector.cancel( channel );
 
          closeConnection( channel );
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.