Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/SendUrgentData.java
38828 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 807159925* @run main/othervm SendUrgentData26* @run main/othervm SendUrgentData -inline27* @summary Test sending of urgent data.28*/2930import java.io.IOException;31import java.net.InetSocketAddress;32import java.net.SocketAddress;33import java.net.SocketException;34import java.nio.ByteBuffer;35import java.nio.channels.ServerSocketChannel;36import java.nio.channels.SocketChannel;3738public class SendUrgentData {3940/**41* The arguments may be one of the following:42* <ol>43* <li>-server</li>44* <li>-client host port [-inline]</li>45* <li>[-inline]</li>46* </ol>47* The first option creates a standalone server, the second a standalone48* client, and the third a self-contained server-client pair on the49* local host.50*51* @param args52* @throws Exception53*/54public static void main(String[] args) throws Exception {5556ServerSocketChannelThread serverThread57= new ServerSocketChannelThread("SendUrgentDataServer");58serverThread.start();59boolean b = serverThread.isAlive();6061String host = null;62int port = 0;63boolean inline = false;64if (args.length > 0 && args[0].equals("-server")) {65System.out.println(serverThread.getAddress());66Thread.currentThread().suspend();67} else {68if (args.length > 0 && args[0].equals("-client")) {69host = args[1];70port = Integer.parseInt(args[2]);71if (args.length > 3) {72inline = args[2].equals("-inline");73}74} else {75host = "localhost";76port = serverThread.getAddress().getPort();77if (args.length > 0) {78inline = args[0].equals("-inline");79}80}81}8283System.out.println("OOB Inline : "+inline);8485SocketAddress sa = new InetSocketAddress(host, port);8687try (SocketChannel sc = SocketChannel.open(sa)) {88sc.configureBlocking(false);89sc.socket().setOOBInline(inline);9091sc.socket().sendUrgentData(0);92System.out.println("wrote 1 OOB byte");9394ByteBuffer bb = ByteBuffer.wrap(new byte[100 * 1000]);9596int blocked = 0;97long total = 0;9899int n;100do {101n = sc.write(bb);102if (n == 0) {103System.out.println("blocked, wrote " + total + " so far");104if (++blocked == 10) {105break;106}107Thread.sleep(100);108} else {109total += n;110bb.rewind();111}112} while (n > 0);113114long attempted = 0;115while (attempted < total) {116bb.rewind();117n = sc.write(bb);118System.out.println("wrote " + n + " normal bytes");119attempted += bb.capacity();120121String osName = System.getProperty("os.name").toLowerCase();122123try {124sc.socket().sendUrgentData(0);125} catch (IOException ex) {126if (osName.contains("linux")) {127if (!ex.getMessage().contains("Socket buffer full")) {128throw new RuntimeException("Unexpected message", ex);129}130} else if (osName.contains("os x") || osName.contains("mac")) {131if (!ex.getMessage().equals("No buffer space available")) {132throw new RuntimeException("Unexpected message", ex);133}134} else if (osName.contains("windows")) {135if (!(ex instanceof SocketException)) {136throw new RuntimeException("Unexpected exception", ex);137} else if (!ex.getMessage().contains("Resource temporarily unavailable")) {138throw new RuntimeException("Unexpected message", ex);139}140} else {141throw new RuntimeException("Unexpected IOException", ex);142}143}144145try {146Thread.sleep(100);147} catch (InterruptedException ex) {148// don't want to fail on this so just print trace and break149ex.printStackTrace();150break;151}152}153} finally {154serverThread.close();155}156}157158static class ServerSocketChannelThread extends Thread {159160private ServerSocketChannel ssc;161162private ServerSocketChannelThread(String name) {163super(name);164try {165ssc = ServerSocketChannel.open();166ssc.bind(new InetSocketAddress((0)));167} catch (IOException ex) {168throw new RuntimeException(ex);169}170}171172public void run() {173while (ssc.isOpen()) {174try {175Thread.sleep(100);176} catch (InterruptedException ex) {177throw new RuntimeException(ex);178}179}180try {181ssc.close();182} catch (IOException ex) {183throw new RuntimeException(ex);184}185System.out.println("ServerSocketChannelThread exiting ...");186}187188public InetSocketAddress getAddress() throws IOException {189if (ssc == null) {190throw new IllegalStateException("ServerSocketChannel not created");191}192193return (InetSocketAddress) ssc.getLocalAddress();194}195196public void close() {197try {198ssc.close();199} catch (IOException ex) {200throw new RuntimeException(ex);201}202}203}204}205206207