/// <summary> /// Create a new Debugger object, configured to listen for connections /// on a specific port. /// </summary> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: Debugger(Client client, int listenPort) throws java.io.IOException internal Debugger(Client client, int listenPort) { mClient = client; mListenPort = listenPort; mListenChannel = ServerSocketChannel.open(); mListenChannel.configureBlocking(false); // required for Selector var addr = new DnsEndPoint("localhost", listenPort); //$NON-NLS-1$ mListenChannel.socket().ExclusiveAddressUse = false; // .reuseAddress = true; // enable SO_REUSEADDR mListenChannel.socket().Bind(addr); mReadBuffer = ByteBuffer.allocate(INITIAL_BUF_SIZE); mPreDataBuffer = ByteBuffer.allocate(PRE_DATA_BUF_SIZE); mConnState = ST_NOT_CONNECTED; Log.d("ddms", "Created: " + this.ToString()); }
/// <summary> /// Close the socket that's listening for new connections and (if /// we're connected) the debugger data socket. /// </summary> /*lock*/ internal void close() { try { if (mListenChannel != null) { mListenChannel.close(); } mListenChannel = null; closeData(); } catch (IOException) { Log.w("ddms", "Failed to close listener " + this); } }
/// <summary> /// Accept a new connection from the specified listen channel. This /// is so we can listen on a dedicated port for the "current" client, /// where "current" is constantly in flux. /// /// Must be synchronized with other uses of mChannel and mPreBuffer. /// /// Returns "null" if we're already talking to somebody. /// </summary> internal SocketChannel accept(ServerSocketChannel listenChan) { if(listenChan != null) { SocketChannel newChan; newChan = listenChan.accept(); if(mChannel != null) { Log.w("ddms", "debugger already talking to " + mClient + " on " + mListenPort); newChan.close(); return null; } mChannel = newChan; mChannel.configureBlocking(false); // required for Selector mConnState = ST_AWAIT_SHAKE; return mChannel; } return null; }
/// <summary> /// Opens (or reopens) the "debug selected" port and listen for connections. </summary> /// <returns> true if the port was opened successfully. </returns> /// <exception cref="IOException"> </exception> private bool reopenDebugSelectedPort() { Log.d("ddms", "reopen debug-selected port: " + mNewDebugSelectedPort); if (mDebugSelectedChan != null) { mDebugSelectedChan.close(); } mDebugSelectedChan = ServerSocketChannel.open(); mDebugSelectedChan.configureBlocking(false); // required for Selector var addr = new DnsEndPoint("localhost", mNewDebugSelectedPort); //$NON-NLS-1$ mDebugSelectedChan.socket().ExclusiveAddressUse = false; // enable SO_REUSEADDR try { mDebugSelectedChan.socket().Bind(addr); if (mSelectedClient != null) { mSelectedClient.update(Client.CHANGE_PORT); } mDebugSelectedChan.register(mSelector, SelectionKey.OP_ACCEPT, this); return true; } catch (Exception) { displayDebugSelectedBindError(mNewDebugSelectedPort); // do not attempt to reopen it. mDebugSelectedChan = null; mNewDebugSelectedPort = -1; return false; } }
/* * Accept a new connection from a debugger. If successful, register it with * the Selector. */ private void acceptNewDebugger(Debugger dbg, ServerSocketChannel acceptChan) { lock (mClientList) { SocketChannel chan; if (acceptChan == null) { chan = dbg.accept(); } else { chan = dbg.accept(acceptChan); } if (chan != null) { chan.socket().NoDelay = true; wakeup(); try { chan.register(mSelector, SelectionKey.OP_READ, dbg); } catch (IOException ioe) { // failed, drop the connection dbg.closeData(); throw ioe; } catch (Exception re) { // failed, drop the connection dbg.closeData(); throw re; } } else { Log.w("ddms", "ignoring duplicate debugger"); // new connection already closed } } }
/// <summary> /// Tell the thread to stop. Called from UI thread. /// </summary> internal void quit() { lock (this) { mQuit = true; wakeup(); Log.d("ddms", "Waiting for Monitor thread"); try { thread.Join(); // since we're quitting, lets drop all the client and disconnect // the DebugSelectedPort lock (mClientList) { foreach (Client c in mClientList) { c.close(false); // notify broadcast(CLIENT_DISCONNECTED, c); } mClientList.Clear(); } if (mDebugSelectedChan != null) { mDebugSelectedChan.close(); mDebugSelectedChan.socket().Close(); mDebugSelectedChan = null; } mSelector.close(); } catch (ThreadInterruptedException ie) { Console.WriteLine(ie.ToString()); Console.Write(ie.StackTrace); } catch (IOException e) { // TODO Auto-generated catch block Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } mInstance = null; } }