Java Code Examples for java.net.Socket#setPerformancePreferences()

The following examples show how to use java.net.Socket#setPerformancePreferences() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: SoloPi   File: AdbConnection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a AdbConnection object associated with the socket and
 * crypto object specified.
 * @param socket The socket that the connection will use for communcation.
 * @param crypto The crypto object that stores the key pair for authentication.
 * @return A new AdbConnection object.
 * @throws IOException If there is a socket error
 */
public static AdbConnection create(Socket socket, AdbCrypto crypto) throws IOException
{
	AdbConnection newConn = new AdbConnection();
	
	newConn.crypto = crypto;
	
	newConn.socket = socket;

	// 试试bufferedStream
	newConn.inputStream = socket.getInputStream();
	newConn.outputStream = socket.getOutputStream();

	/* Disable Nagle because we're sending tiny packets */
	socket.setTcpNoDelay(true);
	socket.setTrafficClass(0x10);
	socket.setPerformancePreferences(1, 3, 2);
	return newConn;
}
 
Example 2
Source Project: Tomcat7.0.67   File: SocketProperties.java    License: Apache License 2.0 6 votes vote down vote up
public void setProperties(Socket socket) throws SocketException{
    if (rxBufSize != null)
        socket.setReceiveBufferSize(rxBufSize.intValue());
    if (txBufSize != null)
        socket.setSendBufferSize(txBufSize.intValue());
    if (ooBInline !=null)
        socket.setOOBInline(ooBInline.booleanValue());
    if (soKeepAlive != null)
        socket.setKeepAlive(soKeepAlive.booleanValue());
    if (performanceConnectionTime != null && performanceLatency != null &&
            performanceBandwidth != null)
        socket.setPerformancePreferences(
                performanceConnectionTime.intValue(),
                performanceLatency.intValue(),
                performanceBandwidth.intValue());
    if (soReuseAddress != null)
        socket.setReuseAddress(soReuseAddress.booleanValue());
    if (soLingerOn != null && soLingerTime != null)
        socket.setSoLinger(soLingerOn.booleanValue(),
                soLingerTime.intValue());
    if (soTimeout != null && soTimeout.intValue() >= 0)
        socket.setSoTimeout(soTimeout.intValue());
    if (tcpNoDelay != null)
        socket.setTcpNoDelay(tcpNoDelay.booleanValue());
}
 
Example 3
Source Project: tomcatsrc   File: SocketProperties.java    License: Apache License 2.0 6 votes vote down vote up
public void setProperties(Socket socket) throws SocketException{
    if (rxBufSize != null)
        socket.setReceiveBufferSize(rxBufSize.intValue());
    if (txBufSize != null)
        socket.setSendBufferSize(txBufSize.intValue());
    if (ooBInline !=null)
        socket.setOOBInline(ooBInline.booleanValue());
    if (soKeepAlive != null)
        socket.setKeepAlive(soKeepAlive.booleanValue());
    if (performanceConnectionTime != null && performanceLatency != null &&
            performanceBandwidth != null)
        socket.setPerformancePreferences(
                performanceConnectionTime.intValue(),
                performanceLatency.intValue(),
                performanceBandwidth.intValue());
    if (soReuseAddress != null)
        socket.setReuseAddress(soReuseAddress.booleanValue());
    if (soLingerOn != null && soLingerTime != null)
        socket.setSoLinger(soLingerOn.booleanValue(),
                soLingerTime.intValue());
    if (soTimeout != null && soTimeout.intValue() >= 0)
        socket.setSoTimeout(soTimeout.intValue());
    if (tcpNoDelay != null)
        socket.setTcpNoDelay(tcpNoDelay.booleanValue());
}
 
Example 4
/**
 * 获取并配置SocketChannel
 *
 * @return 返回SocketChannel
 * @throws IOException
 */
private SocketChannel getAndConfigSocketChannel() throws IOException {
    final SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    // config the socket
    final Socket socket = socketChannel.socket();
    socket.setTcpNoDelay(options.isClientTcpNoDelay());
    socket.setReceiveBufferSize(options.getClientSocketReceiverBufferSize());
    socket.setSendBufferSize(options.getClientSocketSendBufferSize());
    socket.setSoTimeout(options.getClientSocketTimeout());
    socket.setPerformancePreferences(
            options.getClientPerformancePreferences()[0],
            options.getClientPerformancePreferences()[1],
            options.getClientPerformancePreferences()[2]);
    socket.setTrafficClass(options.getClientTrafficClass());
    return socketChannel;
}
 
Example 5
Source Project: Tomcat8-Source-Read   File: SocketProperties.java    License: MIT License 5 votes vote down vote up
public void setProperties(Socket socket) throws SocketException{
    if (rxBufSize != null)
        socket.setReceiveBufferSize(rxBufSize.intValue());
    if (txBufSize != null)
        socket.setSendBufferSize(txBufSize.intValue());
    if (ooBInline !=null)
        socket.setOOBInline(ooBInline.booleanValue());
    if (soKeepAlive != null)
        socket.setKeepAlive(soKeepAlive.booleanValue());
    if (performanceConnectionTime != null && performanceLatency != null &&
            performanceBandwidth != null)
        socket.setPerformancePreferences(
                performanceConnectionTime.intValue(),
                performanceLatency.intValue(),
                performanceBandwidth.intValue());
    if (soReuseAddress != null)
        socket.setReuseAddress(soReuseAddress.booleanValue());
    if (soLingerOn != null && soLingerTime != null)
        socket.setSoLinger(soLingerOn.booleanValue(),
                soLingerTime.intValue());
    if (soTimeout != null && soTimeout.intValue() >= 0)
        socket.setSoTimeout(soTimeout.intValue());
    if (tcpNoDelay != null) {
        try {
            socket.setTcpNoDelay(tcpNoDelay.booleanValue());
        } catch (SocketException e) {
            // Some socket types may not support this option which is set by default
        }
    }
}
 
Example 6
/**
 * 配置SocketChannel
 *
 * @throws IOException
 */
private void configSocketChannel(SocketChannel socketChannel) throws IOException {
    socketChannel.configureBlocking(false);
    // config the socket
    final Socket socket = socketChannel.socket();
    socket.setTcpNoDelay(options.isServerChildTcpNoDelay());
    socket.setReceiveBufferSize(options.getServerChildSocketReceiverBufferSize());
    socket.setSendBufferSize(options.getServerChildSocketSendBufferSize());
    socket.setSoTimeout(options.getServerChildSocketTimeout());
    socket.setPerformancePreferences(
            options.getServerChildPerformancePreferences()[0],
            options.getServerChildPerformancePreferences()[1],
            options.getServerChildPerformancePreferences()[2]);
    socket.setTrafficClass(options.getServerChildTrafficClass());
}
 
Example 7
Source Project: Cangol-appcore   File: SocketThread.java    License: Apache License 2.0 5 votes vote down vote up
private void makeRequest() throws IOException {
    Log.d(TAG, "socket connect executionCount=" + executionCount);
    if (!Thread.currentThread().isInterrupted()) {
        socket = new Socket();
        socket.setTrafficClass(0x04);//低成本0x02;高可靠性0x04;最高吞吐量;0x08;小延迟0x10
        socket.setTcpNoDelay(true);
        socket.setSoTimeout(isLong ? CONNECT_TIME_OUT * 3 : timeout);
        socket.setKeepAlive(isLong);
        socket.setPerformancePreferences(3, 2, 1);//相对重要性 (connectionTime:表示用最少时间建立连接;latency:表示最小延迟;bandwidth:表示最高带宽)
        socket.setReuseAddress(false);
        socket.setSoLinger(true, LINGER_TIME);//linger=0:当close时立即关闭,丢弃剩余数据
        Log.d(TAG, "socket " + host + ":" + port + " connect...");
        socket.connect(new InetSocketAddress(host, port), CONNECT_TIME_OUT);
        isConnecting = true;
        Log.d(TAG, "socket is connected. " + socket.getLocalPort());
        socketHandler.sendConnectedMessage();

        inputStream = new DataInputStream(socket.getInputStream());
        outputStream = new DataOutputStream(socket.getOutputStream());
        Log.d(TAG, "socket is " + (isLong ? "isLong" : "") + " connect.");
        if (!isLong) {
            handleSocketWrite();
            handleSocketRead();
            disconnect();
        } else {
            executorService.submit(new SocketWriteThread(socketHandler, outputStream));
            executorService.submit(new SocketReadThread(socketHandler, inputStream));
        }
    } else {
        isConnecting = false;
        Log.d(TAG, "Thread.isInterrupted");
    }
}
 
Example 8
Source Project: nettythrift   File: HTTPUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static void configSocket(Socket socket) throws SocketException {
    /**
     * 设置Socket属性
     */
    // true表示关闭Socket的缓冲,立即发送数据..其默认值为false
    // 若Socket的底层实现不支持TCP_NODELAY选项,则会抛出SocketException
    socket.setTcpNoDelay(true);
    // 表示是否允许重用Socket所绑定的本地地址
    socket.setReuseAddress(true);
    // 表示接收数据时的等待超时时间,单位毫秒..其默认值为0,表示会无限等待,永远不会超时
    // 当通过Socket的输入流读数据时,如果还没有数据,就会等待
    // 超时后会抛出SocketTimeoutException,且抛出该异常后Socket仍然是连接的,可以尝试再次读数据
    socket.setSoTimeout(5000);
    // 表示当执行Socket.close()时,是否立即关闭底层的Socket
    // 这里设置为当Socket关闭后,底层Socket延迟5秒后再关闭,而5秒后所有未发送完的剩余数据也会被丢弃
    // 默认情况下,执行Socket.close()方法,该方法会立即返回,但底层的Socket实际上并不立即关闭
    // 它会延迟一段时间,直到发送完所有剩余的数据,才会真正关闭Socket,断开连接
    // Tips:当程序通过输出流写数据时,仅仅表示程序向网络提交了一批数据,由网络负责输送到接收方
    // Tips:当程序关闭Socket,有可能这批数据还在网络上传输,还未到达接收方
    // Tips:这里所说的"未发送完的剩余数据"就是指这种还在网络上传输,未被接收方接收的数据
    socket.setSoLinger(true, 5);
    // 表示发送数据的缓冲区的大小
    socket.setSendBufferSize(1024);
    // 表示接收数据的缓冲区的大小
    socket.setReceiveBufferSize(1024);
    // 表示对于长时间处于空闲状态(连接的两端没有互相传送数据)的Socket,是否要自动把它关闭,true为是
    // 其默认值为false,表示TCP不会监视连接是否有效,不活动的客户端可能会永久存在下去,而不会注意到服务器已经崩溃
    socket.setKeepAlive(true);
    // 表示是否支持发送一个字节的TCP紧急数据,socket.sendUrgentData(data)用于发送一个字节的TCP紧急数据
    // 其默认为false,即接收方收到紧急数据时不作任何处理,直接将其丢弃..若用户希望发送紧急数据,则应设其为true
    // 设为true后,接收方会把收到的紧急数据与普通数据放在同样的队列中
    socket.setOOBInline(true);
    // 该方法用于设置服务类型,以下代码请求高可靠性和最小延迟传输服务(把0x04与0x10进行位或运算)
    // Socket类用4个整数表示服务类型
    // 0x02:低成本(二进制的倒数第二位为1)
    // 0x04:高可靠性(二进制的倒数第三位为1)
    // 0x08:最高吞吐量(二进制的倒数第四位为1)
    // 0x10:最小延迟(二进制的倒数第五位为1)
    socket.setTrafficClass(0x04 | 0x10);
    // 该方法用于设定连接时间,延迟,带宽的相对重要性(该方法的三个参数表示网络传输数据的3项指标)
    // connectionTime--该参数表示用最少时间建立连接
    // latency---------该参数表示最小延迟
    // bandwidth-------该参数表示最高带宽
    // 可以为这些参数赋予任意整数值,这些整数之间的相对大小就决定了相应参数的相对重要性
    // 如这里设置的就是---最高带宽最重要,其次是最小连接时间,最后是最小延迟
    socket.setPerformancePreferences(2, 1, 3);
}
 
Example 9
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 10
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 11
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 12
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 13
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 14
Source Project: p4ic4idea   File: RpcSocketHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Configure a socket with specified properties.
 */
public static void configureSocket(Socket socket, Properties properties) {
	if (socket == null || properties == null) {
		return;
	}

	try {
		// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
		boolean tcpNoDelay = RpcPropertyDefs.getPropertyAsBoolean(properties,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_NICK,
				RpcPropertyDefs.RPC_SOCKET_TCP_NO_DELAY_DEFAULT);
		socket.setTcpNoDelay(tcpNoDelay);
		
		String keepAlive = RpcPropertyDefs.getProperty(properties,
				RpcPropertyDefs.RPC_SOCKET_USE_KEEPALIVE_NICK);

		int timeouts = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK,
				RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_DEFAULT);

		int[] perfPrefs = RpcPropertyDefs.getPropertyAsIntArray(properties,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_NICK,
				RpcPropertyDefs.RPC_DEFAULT_PROPERTY_DELIMITER,
				RpcPropertyDefs.RPC_SOCKET_PERFORMANCE_PREFERENCES_DEFAULT);

		// Setting the socket performance preferences, described by three
		// integers whose values indicate the relative importance of short
		// connection time, low latency, and high bandwidth.
		// Socket.setPerformancePreferences(int connectionTime, int latency, int bandwidth)
		// The default values is (1, 2, 0), assume no one changes them.
		// This gives the highest importance to low latency, followed by
		// short connection time, and least importance to high bandwidth.
		if (perfPrefs != null && perfPrefs.length == 3) {
			socket.setPerformancePreferences(
					perfPrefs[0],
					perfPrefs[1],
					perfPrefs[2]);
		}
		
		socket.setSoTimeout(timeouts);

		if ((keepAlive != null)
				&& (keepAlive.startsWith("n") || keepAlive.startsWith("N"))) {
			socket.setKeepAlive(false);
		} else {
			socket.setKeepAlive(true);
		}

		int sockRecvBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_RECV_BUF_SIZE_NICK, 0);
		int sockSendBufSize = RpcPropertyDefs.getPropertyAsInt(properties,
				RpcPropertyDefs.RPC_SOCKET_SEND_BUF_SIZE_NICK, 0);

		if (sockRecvBufSize != 0) {
			socket.setReceiveBufferSize(sockRecvBufSize);
		}

		if (sockSendBufSize != 0) {
			socket.setSendBufferSize(sockSendBufSize);
		}
	} catch (Throwable exc) {
		Log
				.warn("Unexpected exception while setting Perforce RPC socket options: "
						+ exc.getLocalizedMessage());
		Log.exception(exc);
	}
}
 
Example 15
/**
 * Test method for {@link TransportConnection#TransportConnection} .
 *
 * @throws IOException
 *             On a failure connecting to the Mock MongoDB server.
 */
@Test
public void testSocketOptionsFailureFromUnixDomainSockets()
        throws IOException {
    final MongoClientConfiguration config = new MongoClientConfiguration();

    final SocketFactory mockFactory = createMock(SocketFactory.class);
    final Socket mockSocket = createMock(Socket.class);

    final SocketException thrown = new AFUNIXSocketException();
    expect(mockFactory.createSocket()).andReturn(mockSocket);

    mockSocket.connect(ourServer.getInetSocketAddress(),
            config.getConnectTimeout());
    expectLastCall();

    mockSocket.setKeepAlive(config.isUsingSoKeepalive());
    expectLastCall();
    mockSocket.setSoTimeout(config.getReadTimeout());
    expectLastCall();
    mockSocket.setTcpNoDelay(true);
    expectLastCall().andThrow(thrown);
    mockSocket.setPerformancePreferences(1, 5, 6);
    expectLastCall();
    expect(mockSocket.getInputStream()).andReturn(
            new ByteArrayInputStream(new byte[0]));
    expect(mockSocket.getOutputStream()).andReturn(
            new ByteArrayOutputStream());
    expect(mockSocket.getLocalPort()).andReturn(12345);
    expect(mockSocket.getRemoteSocketAddress()).andReturn(
            ourServer.getInetSocketAddress());

    mockSocket.close();
    expectLastCall();

    replay(mockFactory, mockSocket);

    config.setSocketFactory(mockFactory);

    connect(config);

    myTestConnection.close();
    myTestConnection = null;

    verify(mockFactory, mockSocket);
}
 
Example 16
public void apply(final Socket socket) {
    socket.setPerformancePreferences(connectionTime, latency, bandwidth);
}