Examples of DatagramChannel


Examples of java.nio.channels.DatagramChannel

        if (config == null) {
            config = getDefaultConfig();
        }

        DatagramChannel ch = null;
        boolean initialized = false;
        try {
            ch = DatagramChannel.open();
            DatagramSessionConfig cfg;
            if (config.getSessionConfig() instanceof DatagramSessionConfig) {
                cfg = (DatagramSessionConfig) config.getSessionConfig();
            } else {
                cfg = getDefaultConfig().getSessionConfig();
            }

            ch.socket().setReuseAddress(cfg.isReuseAddress());
            ch.socket().setBroadcast(cfg.isBroadcast());
            ch.socket().setReceiveBufferSize(cfg.getReceiveBufferSize());
            ch.socket().setSendBufferSize(cfg.getSendBufferSize());

            if (ch.socket().getTrafficClass() != cfg.getTrafficClass()) {
                ch.socket().setTrafficClass(cfg.getTrafficClass());
            }

            if (localAddress != null) {
                ch.socket().bind(localAddress);
            }
            ch.connect(address);
            ch.configureBlocking(false);
            initialized = true;
        } catch (IOException e) {
            return DefaultConnectFuture.newFailedFuture(e);
        } finally {
            if (!initialized && ch != null) {
                try {
                    ch.disconnect();
                    ch.close();
                } catch (IOException e) {
                    ExceptionMonitor.getInstance().exceptionCaught(e);
                }
            }
        }

        RegistrationRequest request = new RegistrationRequest(ch, handler,
                config);
        synchronized (lock) {
            try {
                startupWorker();
            } catch (IOException e) {
                try {
                    ch.disconnect();
                    ch.close();
                } catch (IOException e2) {
                    ExceptionMonitor.getInstance().exceptionCaught(e2);
                }
   
                return DefaultConnectFuture.newFailedFuture(e);
View Full Code Here

Examples of java.nio.channels.DatagramChannel

        if (!key.isValid()) {
            return false;
        }
        key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));

        DatagramChannel ch = session.getChannel();
        Queue<WriteRequest> writeRequestQueue = session.getWriteRequestQueue();

        int writtenBytes = 0;
        int maxWrittenBytes = ((DatagramSessionConfig) session.getConfig()).getSendBufferSize() << 1;
        try {
            for (;;) {
                WriteRequest req = writeRequestQueue.peek();
       
                if (req == null)
                    break;
       
                ByteBuffer buf = (ByteBuffer) req.getMessage();
                if (buf.remaining() == 0) {
                    // pop and fire event
                    writeRequestQueue.poll();
       
                    buf.reset();
                   
                    if (!buf.hasRemaining()) {
                        session.increaseWrittenMessages();
                    }

                    session.getFilterChain().fireMessageSent(session, req);
                    continue;
                }
       
                int localWrittenBytes = ch.write(buf.buf());
                writtenBytes += localWrittenBytes;
   
                if (localWrittenBytes == 0 || writtenBytes >= maxWrittenBytes) {
                    // Kernel buffer is full or wrote too much
                    key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
View Full Code Here

Examples of java.nio.channels.DatagramChannel

            if (session == null)
                break;
            else {
                SelectionKey key = session.getSelectionKey();
                DatagramChannel ch = (DatagramChannel) key.channel();
                try {
                    ch.disconnect();
                    ch.close();
                } catch (IOException e) {
                    ExceptionMonitor.getInstance().exceptionCaught(e);
                }

                getListeners().fireSessionDestroyed(session);
View Full Code Here

Examples of java.nio.channels.DatagramChannel

        }
    }

    private void flush( DatagramSessionImpl session ) throws IOException
    {
        DatagramChannel ch = session.getChannel();

        Queue writeRequestQueue = session.getWriteRequestQueue();

        WriteRequest req;
        for( ;; )
        {
            synchronized( writeRequestQueue )
            {
                req = ( WriteRequest ) writeRequestQueue.first();
            }

            if( req == null )
                break;

            ByteBuffer buf = ( ByteBuffer ) req.getMessage();
            if( buf.remaining() == 0 )
            {
                // pop and fire event
                synchronized( writeRequestQueue )
                {
                    writeRequestQueue.pop();
                }

                session.increaseWrittenMessages();
                buf.reset();
                session.getFilterChain().fireMessageSent( session, req );
                continue;
            }

            SelectionKey key = session.getSelectionKey();
            if( key == null )
            {
                scheduleFlush( session );
                break;
            }
            if( !key.isValid() )
            {
                continue;
            }

            int writtenBytes = ch.write( buf.buf() );

            if( writtenBytes == 0 )
            {
                // Kernel buffer is full
                key.interestOps( key.interestOps() | SelectionKey.OP_WRITE );
View Full Code Here

Examples of java.nio.channels.DatagramChannel

            if( session == null )
                break;
            else
            {
                SelectionKey key = session.getSelectionKey();
                DatagramChannel ch = ( DatagramChannel ) key.channel();
                try
                {
                    ch.disconnect();
                    ch.close();
                }
                catch( IOException e )
                {
                    ExceptionMonitor.getInstance().exceptionCaught( e );
                }
View Full Code Here

Examples of java.nio.channels.DatagramChannel

        if( config == null )
        {
            config = getDefaultConfig();
        }
       
        DatagramChannel ch = null;
        boolean initialized = false;
        try
        {
            ch = DatagramChannel.open();
            DatagramSessionConfig cfg;
            if( config.getSessionConfig() instanceof DatagramSessionConfig )
            {
                cfg = ( DatagramSessionConfig ) config.getSessionConfig();
            }
            else
            {
                cfg = ( DatagramSessionConfig ) getDefaultConfig().getSessionConfig();
            }
           
            ch.socket().setReuseAddress( cfg.isReuseAddress() );
            ch.socket().setBroadcast( cfg.isBroadcast() );
            ch.socket().setReceiveBufferSize( cfg.getReceiveBufferSize() );
            ch.socket().setSendBufferSize( cfg.getSendBufferSize() );

            if( ch.socket().getTrafficClass() != cfg.getTrafficClass() )
            {
                ch.socket().setTrafficClass( cfg.getTrafficClass() );
            }

            if( localAddress != null )
            {
                ch.socket().bind( localAddress );
            }
            ch.connect( address );
            ch.configureBlocking( false );
            initialized = true;
        }
        catch( IOException e )
        {
            return DefaultConnectFuture.newFailedFuture( e );
        }
        finally
        {
            if( !initialized && ch != null )
            {
                try
                {
                    ch.disconnect();
                    ch.close();
                }
                catch( IOException e )
                {
                    ExceptionMonitor.getInstance().exceptionCaught( e );
                }
            }
        }

        RegistrationRequest request = new RegistrationRequest( ch, handler, config );
        synchronized( this )
        {
            try
            {
                startupWorker();
            }
            catch( IOException e )
            {
                try
                {
                    ch.disconnect();
                    ch.close();
                }
                catch( IOException e2 )
                {
                    ExceptionMonitor.getInstance().exceptionCaught( e2 );
                }
View Full Code Here

Examples of org.jboss.netty.channel.socket.DatagramChannel

      }
    }
    else
    {
      //UDP
      DatagramChannel udpChannel = (DatagramChannel)channel;
      buf = ChannelBuffers.buffer(1 + 8);
      buf.writeByte(Events.SESSION_MESSAGE);
      buf.writeInt(type);
      buf.writeInt(operation);
      for(int i =0; i < 10;i++){
        udpChannel.write(buf, remoteAddress);
      }
    }
   
  }
View Full Code Here

Examples of org.jboss.netty.channel.socket.DatagramChannel

   * @throws UnknownHostException
   */
  public DatagramChannel createDatagramChannel()  throws UnknownHostException
  {
    String localHost = InetAddress.getLocalHost().getHostAddress();
    DatagramChannel c = (DatagramChannel) b.bind( new InetSocketAddress(localHost,0));
    return c;
  }
View Full Code Here

Examples of org.jboss.netty.channel.socket.DatagramChannel

  }

  public InetSocketAddress connectLocal() throws UnknownHostException
  {
    DatagramChannel c = udpClient.createDatagramChannel();
    InetSocketAddress localAddress = udpClient.getLocalAddress(c);
    CLIENTS.put(localAddress, c);
    return localAddress;
  }
View Full Code Here

Examples of org.jboss.netty.channel.socket.DatagramChannel

 
  public void connectUDP(Channel channel)
  {
    InetSocketAddress address = ZombieClient.CHANNEL_ID_ADDRESS_MAP.get(channel.getId());
    System.out.println("UDP address for connect UDP: " + address);
    final DatagramChannel c = CLIENTS.get(address);
    if ((udpClient != null) && (c != null))
    {
      // Connect the UDP
      System.out.println("Going to connect UDP in ZombieHandler");
      Runnable runnable = new Runnable()
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.