private void sendVolumeToClient( SocketChannel lSocketChannel, Volume lVolumeToSend, boolean pReleaseOrForward) throws IOException { mByteBuffer = ClearVolumeSerialization.serialize(lVolumeToSend, mByteBuffer); mByteBuffer.rewind(); if (lSocketChannel.isConnected() && lSocketChannel.isOpen()) { while (mByteBuffer.hasRemaining()) lSocketChannel.write(mByteBuffer); if (pReleaseOrForward) { if (mClearVolumeTCPServerSink.getRelaySink() == null) lVolumeToSend.makeAvailableToManager(); else mClearVolumeTCPServerSink.getRelaySink().sendVolume(lVolumeToSend); } } }
@Override public void run() { try { while (!mStopSignal) { final SocketChannel lSocketChannel = mServerSocketChannel.accept(); // System.out.println("connection accepted"); lSocketChannel.setOption( StandardSocketOptions.SO_SNDBUF, ClearVolumeTCPClient.cSocketBufferLength); try { if (lSocketChannel.isOpen() && lSocketChannel.isConnected() && mClearVolumeTCPServerSink.getLastVolumeSeen() != null) { // System.out.println("sending last seen volume: " + // mClearVolumeTCPServerSink.getLastVolumeSeen()); sendVolumeToClient( lSocketChannel, mClearVolumeTCPServerSink.getLastVolumeSeen(), false); } while (lSocketChannel.isOpen() && lSocketChannel.isConnected() && !mStopSignal) { final Volume lVolumeToSend = mVolumeSource.requestVolumeAndWait(10, TimeUnit.MILLISECONDS); if (lVolumeToSend != null) sendVolumeToClient(lSocketChannel, lVolumeToSend, true); } } catch (final java.io.IOException e1) { continue; } catch (final Throwable e) { e.printStackTrace(); } } } catch (final java.nio.channels.AsynchronousCloseException e) { } catch (final Throwable e) { handleError(e); } finally { mStoppedSignal = true; } }