Search in sources :

Example 1 with NioException

use of org.webpieces.nio.api.exceptions.NioException in project webpieces by deanhiller.

the class UDPChannelImpl method disconnect.

public synchronized void disconnect() {
    apiLog.trace(() -> this + "Basic.disconnect called");
    try {
        isConnected = false;
        channel.disconnect();
    } catch (IOException e) {
        throw new NioException(e);
    }
}
Also used : IOException(java.io.IOException) NioException(org.webpieces.nio.api.exceptions.NioException)

Example 2 with NioException

use of org.webpieces.nio.api.exceptions.NioException in project webpieces by deanhiller.

the class BasTCPServerChannel method registerServerSocketChannel.

public void registerServerSocketChannel(ConnectionListener cb) {
    if (!isBound())
        throw new IllegalArgumentException("Only bound sockets can be registered or selector doesn't work");
    try {
        CompletableFuture<Void> future = getSelectorManager().registerServerSocketChannel(this, cb);
        future.get();
    } catch (IOException e) {
        throw new NioException(e);
    } catch (InterruptedException e) {
        throw new NioException(e);
    } catch (ExecutionException e) {
        throw new NioException(e);
    }
}
Also used : IOException(java.io.IOException) NioException(org.webpieces.nio.api.exceptions.NioException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with NioException

use of org.webpieces.nio.api.exceptions.NioException in project webpieces by deanhiller.

the class Helper method read.

private static void read(SelectionKey key, SelectorManager2 mgr, BufferPool pool) throws IOException {
    log.trace(() -> key.attachment() + "reading data");
    WrapperAndListener struct = (WrapperAndListener) key.attachment();
    DataListener in = struct.getDataHandler();
    BasChannelImpl channel = (BasChannelImpl) struct.getChannel();
    //pressure in RAM so just wait until they re-registerForReads and they will get the data then
    if (!channel.isRegisteredForReads()) {
        //do not process reads if we were unregistered
        return;
    }
    ByteBuffer chunk = pool.nextBuffer(512);
    try {
        if (logBufferNextRead)
            log.info(channel + "buffer=" + chunk);
        int bytes = channel.readImpl(chunk);
        if (logBufferNextRead) {
            logBufferNextRead = false;
            log.info(channel + "buffer2=" + chunk);
        }
        processBytes(key, chunk, bytes, mgr);
    } catch (PortUnreachableException e) {
        //this is a normal occurence when some writes out udp to a port that is not
        //listening for udp!!!  log as finer and fire to client to deal with it.
        log.trace(() -> "Client sent data to a host or port that is not listening " + "to udp, or udp can't get through to that machine", e);
        in.failure(channel, null, e);
    } catch (NotYetConnectedException e) {
        //this happens in udp when I disconnect after someone had already been streaming me
        //data.  It is supposed to stop listening but selector keeps firing.
        log.error("Can't read until UDPChannel is connected", e);
        in.failure(channel, null, e);
    } catch (IOException e) {
        //kept getting the following exception so I added this as protection
        //NOTE: this exceptionn should never occur.  read should be returning -1
        //but for some reason is returning hte exception instead.
        //        WARNING: [server] Processing of key failed, closing channel
        //        java.io.IOException: An established connection was aborted by the software in your host machine
        //            at sun.nio.ch.SocketDispatcher.read0(Native Method)
        //            at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
        //            at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
        //            at sun.nio.ch.IOUtil.read(IOUtil.java:206)
        //            at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:207)
        //            at biz.xsoftware.impl.nio.cm.basic.TCPChannelImpl.readImpl(TCPChannelImpl.java:168)
        //            at biz.xsoftware.impl.nio.cm.basic.Helper.read(Helper.java:128)
        //            at biz.xsoftware.impl.nio.cm.basic.Helper.processKey(Helper.java:77)
        //            at biz.xsoftware.impl.nio.cm.basic.Helper.processKeys(Helper.java:43)
        //            at biz.xsoftware.impl.nio.cm.basic.SelectorManager2.runLoop(SelectorManager2.java:262)
        //            at biz.xsoftware.impl.nio.cm.basic.SelectorManager2$PollingThread.run
        //        (SelectorManager2.java:224)     
        //another one that landes here is the Connection reset by peer"....
        //			Jan 18, 2012 1:00:42 PM biz.xsoftware.impl.nio.cm.basic.Helper read
        //			INFO: [stserver] Exception
        //			java.io.IOException: Connection reset by peer
        //			        at sun.nio.ch.FileDispatcher.read0(Native Method)
        //			        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
        //			        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:251)
        //			        at sun.nio.ch.IOUtil.read(IOUtil.java:224)
        //			        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:254)
        //			        at biz.xsoftware.impl.nio.cm.basic.chanimpl.SocketChannelImpl.read(SocketChannelImpl.java:65)
        //			        at biz.xsoftware.impl.nio.cm.basic.BasTCPChannel.readImpl(BasTCPChannel.java:108)
        //			        at biz.xsoftware.impl.nio.cm.basic.Helper.read(Helper.java:162)
        //			        at biz.xsoftware.impl.nio.cm.basic.Helper.processKey(Helper.java:104)
        //			        at biz.xsoftware.impl.nio.cm.basic.Helper.processKeys(Helper.java:51)
        //			        at biz.xsoftware.impl.nio.cm.basic.SelectorManager2.selectorFired(SelectorManager2.java:253)
        //			        at biz.xsoftware.impl.nio.cm.basic.nioimpl.SelectorImpl.runLoop(SelectorImpl.java:126)
        //			        at biz.xsoftware.impl.nio.cm.basic.nioimpl.SelectorImpl$PollingThread.run(SelectorImpl.java:107)
        //One other exception starts with "An existing connection was forcibly closed"
        //in the case of SSL over TCP only, sometimes instead of reading off a -1, this will
        //throw an IOException: "An existing connection was forcibly closed by the remote host"
        //we also close UDPChannels as well on IOException.  Not sure if this is good or not.
        process(key, mgr, in, channel, chunk, e);
    } catch (NioException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            IOException ioExc = (IOException) cause;
            process(key, mgr, in, channel, chunk, ioExc);
        } else
            throw e;
    }
}
Also used : PortUnreachableException(java.net.PortUnreachableException) DataListener(org.webpieces.nio.api.handlers.DataListener) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) NioException(org.webpieces.nio.api.exceptions.NioException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with NioException

use of org.webpieces.nio.api.exceptions.NioException in project webpieces by deanhiller.

the class BasTCPServerChannel method bind.

public void bind(SocketAddress srvrAddr) {
    try {
        bindImpl(srvrAddr);
        registerServerSocketChannel(connectionListener);
    } catch (IOException e) {
        throw new NioException(e);
    }
}
Also used : IOException(java.io.IOException) NioException(org.webpieces.nio.api.exceptions.NioException)

Example 5 with NioException

use of org.webpieces.nio.api.exceptions.NioException in project webpieces by deanhiller.

the class SelectorImpl method startPollingThread.

public void startPollingThread(SelectorListener l, String threadName) {
    if (running)
        throw new IllegalStateException("Already running, can't start again");
    this.listener = l;
    try {
        selector = provider.openSelector();
        thread = new PollingThread();
        thread.setDaemon(true);
        thread.setName(threadName);
        thread.start();
    } catch (IOException e) {
        throw new NioException(e);
    }
}
Also used : IOException(java.io.IOException) NioException(org.webpieces.nio.api.exceptions.NioException)

Aggregations

IOException (java.io.IOException)6 NioException (org.webpieces.nio.api.exceptions.NioException)6 DatagramSocket (java.net.DatagramSocket)1 PortUnreachableException (java.net.PortUnreachableException)1 ByteBuffer (java.nio.ByteBuffer)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 ExecutionException (java.util.concurrent.ExecutionException)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1