RpcStreamConnection extends RpcConnection { void calculatePreambleBytesAndSendtoDownstream( @Nonnull final RpcPacketSupplier argsSupplier) throws ConnectionException { byte[] sendBytes = argsSupplier.sendBytes(); int sendPos = argsSupplier.sendPos(); byte[] preambleBytes = RpcPacketPreamble .constructPreamble(sendPos - RpcPacketPreamble.RPC_PREAMBLE_SIZE).marshalAsBytes(); System.arraycopy(preambleBytes, 0, sendBytes, 0, preambleBytes.length); try { topOutputStream.write(sendBytes, 0, sendPos); topOutputStream.flush(); stats.streamSends.incrementAndGet(); stats.totalBytesSent.getAndAdd(sendPos); stats.packetsSent.incrementAndGet(); if (stats.largestRpcPacketSent.get() < sendPos) { stats.largestRpcPacketSent.set(sendPos); } } catch (IOException exc) { Log.exception(exc); StringBuilder message = new StringBuilder(); if (exc instanceof SocketTimeoutException && secure) { message.append(MessageFormat.format( "SSL connect to ssl:{0}:{1,number,#} failed.\nRemove SSL protocol prefix.\n", hostName, hostPort)); } else { message.append("Unable to send command to Perforce server: "); } message.append(exc.getMessage()); throwConnectionException(exc, message.toString()); } } RpcStreamConnection(String serverHost, int serverPort, Properties props,
                               ServerStats stats, P4Charset p4Charset, boolean secure); private  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, P4Charset p4Charset, Socket socket, boolean secure); private  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, P4Charset p4Charset, Socket socket, RpcSocketPool pool, boolean secure);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, P4Charset p4Charset, Socket socket, RpcSocketPool pool, boolean secure,
            String rsh); String getServerIpPort(); String getClientIpPort(); void disconnect(final RpcPacketDispatcher dispatcher); RpcPacket getRpcPacket(); RpcPacket getRpcPacket(final RpcPacketFieldRule fieldRule,
            final IFilterCallback filterCallback); int getSystemRecvBufferSize(); int getSystemSendBufferSize(); long putRpcPackets(@Nonnull RpcPacket[] packets); long putRpcPacket(@Nonnull RpcPacket packet); @Override void useConnectionCompression(); static final String TRACE_PREFIX; }
@Test public void calculatePreambleBytesAndSendtoDownstream() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ConnectionException { RpcStreamConnection.RpcPacketSupplier supplier = createMockRpcPacketSupplier(); mockConnection.calculatePreambleBytesAndSendtoDownstream(supplier); verify(topOutputStream).flush(); verify(topOutputStream).write(any(byte[].class), eq(0), anyInt()); }
@Test(expected=ConnectionException.class) public void calculatePreambleBytesAndSendtoDownstream_with_SocketTimeoutException() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ConnectionException { RpcStreamConnection.RpcPacketSupplier supplier = createMockRpcPacketSupplier(); doThrow(SocketTimeoutException.class).when(topOutputStream).write(any(), eq(0), anyInt()); mockConnection.calculatePreambleBytesAndSendtoDownstream(supplier); }