ConnectionHandler.java revision 56d7e400ece64591685c8a21dbb82a94a7bd8010
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.dumprendertree2.forwarder; 18 19import android.util.Log; 20 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.net.Socket; 25 26/** 27 * Worker class for {@link Forwarder}. A ConnectionHandler will be created once the Forwarder 28 * accepts an incoming connection, and it will then forward the incoming/outgoing streams to a 29 * connection already proxied by adb networking (see also {@link AdbUtils}). 30 */ 31public class ConnectionHandler { 32 33 private static final String LOG_TAG = "ConnectionHandler"; 34 35 private class SocketPipeThread extends Thread { 36 37 private Socket mInSocket, mOutSocket; 38 39 public SocketPipeThread(Socket inSocket, Socket outSocket) { 40 mInSocket = inSocket; 41 mOutSocket = outSocket; 42 } 43 44 @Override 45 public void run() { 46 InputStream is; 47 OutputStream os; 48 try { 49 synchronized (this) { 50 is = mInSocket.getInputStream(); 51 os = mOutSocket.getOutputStream(); 52 } 53 } catch (IOException e) { 54 Log.w(LOG_TAG, this.toString(), e); 55 return; 56 } 57 58 byte[] buffer = new byte[4096]; 59 int length; 60 while (true) { 61 try { 62 synchronized (this) { 63 if ((length = is.read(buffer)) <= 0) { 64 break; 65 } 66 os.write(buffer, 0, length); 67 } 68 } catch (IOException e) { 69 /** This exception means one of the streams is closed */ 70 Log.v(LOG_TAG, this.toString(), e); 71 break; 72 } 73 } 74 } 75 76 @Override 77 public String toString() { 78 return "SocketPipeThread:\n" + mInSocket + "\n=>\n" + mOutSocket; 79 } 80 } 81 82 private Socket mFromSocket, mToSocket; 83 private SocketPipeThread mFromToPipe, mToFromPipe; 84 85 public ConnectionHandler(Socket fromSocket, Socket toSocket) { 86 mFromSocket = fromSocket; 87 mToSocket = toSocket; 88 mFromToPipe = new SocketPipeThread(mFromSocket, mToSocket); 89 mToFromPipe = new SocketPipeThread(mToSocket, mFromSocket); 90 } 91 92 public void start() { 93 mFromToPipe.start(); 94 mToFromPipe.start(); 95 } 96 97 public void stop() { 98 shutdown(mFromSocket); 99 shutdown(mToSocket); 100 } 101 102 private void shutdown(Socket socket) { 103 try { 104 synchronized (mFromToPipe) { 105 synchronized (mToFromPipe) { 106 /** This will stop the while loop in the run method */ 107 socket.shutdownInput(); 108 socket.shutdownOutput(); 109 socket.close(); 110 } 111 } 112 } catch (IOException e) { 113 Log.e(LOG_TAG, "mFromToPipe=" + mFromToPipe + " mToFromPipe=" + mToFromPipe, e); 114 } 115 } 116}