Search in sources :

Example 6 with SctpChannel

use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.

the class Util method isSCTPSupported.

static boolean isSCTPSupported() {
    try {
        SctpChannel c = SctpChannel.open();
        c.close();
        return true;
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (UnsupportedOperationException e) {
        out.println(e);
    }
    return false;
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) IOException(java.io.IOException)

Example 7 with SctpChannel

use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.

the class SocketOptionTests method sctpPrimaryAddr.

/* SCTP_PRIMARY_ADDR */
void sctpPrimaryAddr() throws IOException {
    SocketAddress addrToSet = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
    System.out.println("TESTING SCTP_PRIMARY_ADDR");
    /* create listening channel */
    SctpServerChannel ssc = SctpServerChannel.open().bind(null);
    Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
    if (addrs.isEmpty())
        debug("addrs should not be empty");
    InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();
    /* setup an association implicitly by sending a small message */
    int streamNumber = 0;
    debug("sending to " + serverAddr + " on stream number: " + streamNumber);
    MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);
    buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
    buffer.flip();
    debug("sending small message: " + buffer);
    SctpMultiChannel smc = SctpMultiChannel.open();
    int sent = smc.send(buffer, info);
    /* Receive the COMM_UP */
    buffer.clear();
    SOTNotificationHandler handler = new SOTNotificationHandler();
    info = smc.receive(buffer, null, handler);
    check(handler.receivedCommUp(), "COMM_UP no received");
    Set<Association> associations = smc.associations();
    check(!associations.isEmpty(), "There should be some associations");
    Association assoc = associations.iterator().next();
    SctpChannel peerChannel = ssc.accept();
    ssc.close();
    Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();
    debug("Peer local Addresses: ");
    for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
        InetSocketAddress addr = (InetSocketAddress) it.next();
        debug("\t" + addr);
        // any of the peer addresses will do!
        addrToSet = addr;
    }
    /* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */
    if ("SunOS".equals(osName)) {
        //smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        return;
    } else {
        /* Linux */
        SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        /* Verify that this is one of the peer addresses */
        boolean found = false;
        // may not have more than one addr
        addrToSet = primaryAddr;
        for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
            InetSocketAddress addr = (InetSocketAddress) it.next();
            if (addr.equals(primaryAddr)) {
                found = true;
            }
            addrToSet = addr;
        }
        check(found, "SCTP_PRIMARY_ADDR returned bogus address!");
        System.out.println("Try SCTP_PRIMARY_ADDR set to: " + addrToSet);
        smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);
        primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        check(addrToSet.equals(primaryAddr), "SCTP_PRIMARY_ADDR not set correctly");
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) InetSocketAddress(java.net.InetSocketAddress) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo) Association(com.sun.nio.sctp.Association) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel)

Example 8 with SctpChannel

use of com.sun.nio.sctp.SctpChannel in project netty by netty.

the class NioSctpChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
        int pos = data.position();
        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return 0;
        }
        allocHandle.lastBytesRead(data.position() - pos);
        buf.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            buffer.release();
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) SctpMessage(io.netty.channel.sctp.SctpMessage) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 9 with SctpChannel

use of com.sun.nio.sctp.SctpChannel in project netty by netty.

the class NioSctpServerChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel().accept();
    if (ch == null) {
        return 0;
    }
    buf.add(new NioSctpChannel(this, ch));
    return 1;
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel)

Example 10 with SctpChannel

use of com.sun.nio.sctp.SctpChannel in project jdk8u_jdk by JetBrains.

the class SctpServerChannelImpl method accept.

@Override
public SctpChannel accept() throws IOException {
    synchronized (lock) {
        if (!isOpen())
            throw new ClosedChannelException();
        if (!isBound())
            throw new NotYetBoundException();
        SctpChannel sc = null;
        int n = 0;
        FileDescriptor newfd = new FileDescriptor();
        InetSocketAddress[] isaa = new InetSocketAddress[1];
        try {
            begin();
            if (!isOpen())
                return null;
            thread = NativeThread.current();
            for (; ; ) {
                n = accept0(fd, newfd, isaa);
                if ((n == IOStatus.INTERRUPTED) && isOpen())
                    continue;
                break;
            }
        } finally {
            acceptCleanup();
            end(n > 0);
            assert IOStatus.check(n);
        }
        if (n < 1)
            return null;
        IOUtil.configureBlocking(newfd, true);
        InetSocketAddress isa = isaa[0];
        sc = new SctpChannelImpl(provider(), newfd);
        SecurityManager sm = System.getSecurityManager();
        if (sm != null)
            sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
        return sc;
    }
}
Also used : NotYetBoundException(java.nio.channels.NotYetBoundException) ClosedChannelException(java.nio.channels.ClosedChannelException) SctpChannel(com.sun.nio.sctp.SctpChannel) InetSocketAddress(java.net.InetSocketAddress) FileDescriptor(java.io.FileDescriptor)

Aggregations

SctpChannel (com.sun.nio.sctp.SctpChannel)17 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 MessageInfo (com.sun.nio.sctp.MessageInfo)7 InetSocketAddress (java.net.InetSocketAddress)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)3 SocketAddress (java.net.SocketAddress)3 Association (com.sun.nio.sctp.Association)2 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)2 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)2 AlreadyBoundException (java.nio.channels.AlreadyBoundException)2 AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 SelectionKey (java.nio.channels.SelectionKey)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 ByteBuf (io.netty.buffer.ByteBuf)1 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)1 SctpMessage (io.netty.channel.sctp.SctpMessage)1 FileDescriptor (java.io.FileDescriptor)1