Skip to content

Commit

Permalink
Socket improvements from bitcoinj fork.
Browse files Browse the repository at this point in the history
  * Allow creation of unconnected sockets
  * Avoid exceptions for unrecognized socket options.
  • Loading branch information
brl committed May 2, 2015
1 parent 7fc020c commit 82d3834
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 62 deletions.
21 changes: 11 additions & 10 deletions src/com/subgraph/orchid/sockets/OrchidSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ public OrchidSocketFactory(TorClient torClient, boolean exceptionOnLocalBind) {
this.exceptionOnLocalBind = exceptionOnLocalBind;
}

@Override
@Override
public Socket createSocket() throws IOException {
return createSocketInstance();
}

@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return createOrchidSocket(host, port);
final Socket s = createSocketInstance();
return connectOrchidSocket(s, host, port);
}

@Override
Expand All @@ -43,7 +49,8 @@ public Socket createSocket(String host, int port, InetAddress localHost,

@Override
public Socket createSocket(InetAddress address, int port) throws IOException {
return createOrchidSocket(address.getHostAddress(), port);
final Socket s = createSocketInstance();
return connectOrchidSocket(s, address.getHostAddress(), port);
}

@Override
Expand All @@ -55,13 +62,7 @@ public Socket createSocket(InetAddress address, int port,
return createSocket(address, port);
}

@Override
public Socket createSocket() throws SocketException {
return createSocketInstance();
}

private Socket createOrchidSocket(String host, int port) throws IOException {
final Socket s = createSocketInstance();
private Socket connectOrchidSocket(Socket s, String host, int port) throws IOException {
final SocketAddress endpoint = InetSocketAddress.createUnresolved(host, port);
s.connect(endpoint);
return s;
Expand Down
121 changes: 69 additions & 52 deletions src/com/subgraph/orchid/sockets/OrchidSocketImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.subgraph.orchid.sockets;

import com.subgraph.orchid.OpenFailedException;
import com.subgraph.orchid.Stream;
import com.subgraph.orchid.Threading;
import com.subgraph.orchid.TorClient;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -13,16 +18,12 @@
import java.net.SocketOptions;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

import com.subgraph.orchid.OpenFailedException;
import com.subgraph.orchid.Stream;
import com.subgraph.orchid.TorClient;
import java.util.concurrent.locks.Lock;

public class OrchidSocketImpl extends SocketImpl {

private final TorClient torClient;
private final Object streamLock = new Object();

private Lock streamLock = Threading.lock("stream");
private Stream stream;

OrchidSocketImpl(TorClient torClient) {
Expand All @@ -31,7 +32,7 @@ public class OrchidSocketImpl extends SocketImpl {
}

public void setOption(int optID, Object value) throws SocketException {
throw new UnsupportedOperationException();
// Ignored.
}

public Object getOption(int optID) throws SocketException {
Expand All @@ -53,13 +54,16 @@ protected void create(boolean stream) throws IOException {

@Override
protected void connect(String host, int port) throws IOException {
throw new UnsupportedOperationException();
SocketAddress endpoint =
InetSocketAddress.createUnresolved(host, port);
connect(endpoint, 0);
}

@Override
protected void connect(InetAddress address, int port) throws IOException {
throw new UnsupportedOperationException();

SocketAddress endpoint =
InetSocketAddress.createUnresolved(address.getHostAddress(), port);
connect(endpoint, 0);
}

@Override
Expand All @@ -82,20 +86,36 @@ private String addressToName(InetSocketAddress address) {
}

private void doConnect(String host, int port) throws IOException {
synchronized(streamLock) {
if(stream != null) {
throw new SocketException("Already connected");
}
try {
stream = torClient.openExitStreamTo(host, port);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SocketException("connect() interrupted");
} catch (TimeoutException e) {
throw new SocketTimeoutException();
} catch (OpenFailedException e) {
throw new ConnectException(e.getMessage());
}
Stream stream;

// Try to avoid holding the stream lock here whilst calling into torclient to avoid accidental inversions.

streamLock.lock();
stream = this.stream;
streamLock.unlock();

if (stream != null)
throw new SocketException("Already connected");

try {
stream = torClient.openExitStreamTo(host, port);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SocketException("connect() interrupted");
} catch (TimeoutException e) {
throw new SocketTimeoutException();
} catch (OpenFailedException e) {
throw new ConnectException(e.getMessage());
}

streamLock.lock();
if (this.stream != null) {
// Raced with another concurrent call.
streamLock.unlock();
stream.close();
} else {
this.stream = stream;
streamLock.unlock();
}
}

Expand All @@ -114,44 +134,41 @@ protected void accept(SocketImpl s) throws IOException {
throw new UnsupportedOperationException();
}

@Override
protected InputStream getInputStream() throws IOException {
synchronized (streamLock) {
if(stream == null) {
private Stream getStream() throws IOException {
streamLock.lock();
try {
if (stream == null)
throw new IOException("Not connected");
}
return stream.getInputStream();
return stream;
} finally {
streamLock.unlock();
}
}

@Override
protected InputStream getInputStream() throws IOException {
return getStream().getInputStream();
}

@Override
protected OutputStream getOutputStream() throws IOException {
synchronized (streamLock) {
if(stream == null) {
throw new IOException("Not connected");
}
return stream.getOutputStream();
}
return getStream().getOutputStream();
}

@Override
protected int available() throws IOException {
synchronized(streamLock) {
if(stream == null) {
throw new IOException("Not connected");
}
return stream.getInputStream().available();
}
return getStream().getInputStream().available();
}

@Override
protected void close() throws IOException {
synchronized (streamLock) {
if(stream != null) {
stream.close();
stream = null;
}
}
Stream toClose;
streamLock.lock();
toClose = this.stream;
this.stream = null;
streamLock.unlock();
if (toClose != null)
toClose.close();
}

@Override
Expand All @@ -160,10 +177,10 @@ protected void sendUrgentData(int data) throws IOException {
}

protected void shutdownInput() throws IOException {
//throw new IOException("Method not implemented!");
}
//throw new IOException("Method not implemented!");
}

protected void shutdownOutput() throws IOException {
//throw new IOException("Method not implemented!");
}
//throw new IOException("Method not implemented!");
}
}

0 comments on commit 82d3834

Please sign in to comment.