131 localHost = localAddr.getHostAddress();
132 }
133 } catch (Exception e) {
134 localHostKnown = false;
135 localHost = null;
136 }
137 }
138
139 if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
140 TCPTransport.tcpLog.log(Log.BRIEF,
141 "localHostKnown = " + localHostKnown +
142 ", localHost = " + localHost);
143 }
144 }
145
146 /** maps an endpoint key containing custom socket factories to
147 * their own unique endpoint */
148 // TBD: should this be a weak hash table?
149 private static final
150 Map<TCPEndpoint,LinkedList<TCPEndpoint>> localEndpoints =
151 new HashMap<TCPEndpoint,LinkedList<TCPEndpoint>>();
152
153 /**
154 * Create an endpoint for a specified host and port.
155 * This should not be used by external classes to create endpoints
156 * for servers in this VM; use getLocalEndpoint instead.
157 */
158 public TCPEndpoint(String host, int port) {
159 this(host, port, null, null);
160 }
161
162 /**
163 * Create a custom socket factory endpoint for a specified host and port.
164 * This should not be used by external classes to create endpoints
165 * for servers in this VM; use getLocalEndpoint instead.
166 */
167 public TCPEndpoint(String host, int port, RMIClientSocketFactory csf,
168 RMIServerSocketFactory ssf)
169 {
170 if (host == null)
171 host = "";
606 Socket socket;
607
608 try {
609 RMIClientSocketFactory clientFactory = csf;
610 if (clientFactory == null) {
611 clientFactory = chooseFactory();
612 }
613 socket = clientFactory.createSocket(host, port);
614
615 } catch (java.net.UnknownHostException e) {
616 throw new java.rmi.UnknownHostException(
617 "Unknown host: " + host, e);
618 } catch (java.net.ConnectException e) {
619 throw new java.rmi.ConnectException(
620 "Connection refused to host: " + host, e);
621 } catch (IOException e) {
622 // We might have simply run out of file descriptors
623 try {
624 TCPEndpoint.shedConnectionCaches();
625 // REMIND: should we retry createSocket?
626 } catch (OutOfMemoryError mem) {
627 // don't quit if out of memory
628 } catch (Exception ex) {
629 // don't quit if shed fails non-catastrophically
630 }
631
632 throw new ConnectIOException("Exception creating connection to: " +
633 host, e);
634 }
635
636 // set socket to disable Nagle's algorithm (always send immediately)
637 // TBD: should this be left up to socket factory instead?
638 try {
639 socket.setTcpNoDelay(true);
640 } catch (Exception e) {
641 // if we fail to set this, ignore and proceed anyway
642 }
643
644 // fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
645 try {
646 socket.setKeepAlive(true);
647 } catch (Exception e) {
648 // ignore and proceed
649 }
|
131 localHost = localAddr.getHostAddress();
132 }
133 } catch (Exception e) {
134 localHostKnown = false;
135 localHost = null;
136 }
137 }
138
139 if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
140 TCPTransport.tcpLog.log(Log.BRIEF,
141 "localHostKnown = " + localHostKnown +
142 ", localHost = " + localHost);
143 }
144 }
145
146 /** maps an endpoint key containing custom socket factories to
147 * their own unique endpoint */
148 // TBD: should this be a weak hash table?
149 private static final
150 Map<TCPEndpoint,LinkedList<TCPEndpoint>> localEndpoints =
151 new HashMap<>();
152
153 /**
154 * Create an endpoint for a specified host and port.
155 * This should not be used by external classes to create endpoints
156 * for servers in this VM; use getLocalEndpoint instead.
157 */
158 public TCPEndpoint(String host, int port) {
159 this(host, port, null, null);
160 }
161
162 /**
163 * Create a custom socket factory endpoint for a specified host and port.
164 * This should not be used by external classes to create endpoints
165 * for servers in this VM; use getLocalEndpoint instead.
166 */
167 public TCPEndpoint(String host, int port, RMIClientSocketFactory csf,
168 RMIServerSocketFactory ssf)
169 {
170 if (host == null)
171 host = "";
606 Socket socket;
607
608 try {
609 RMIClientSocketFactory clientFactory = csf;
610 if (clientFactory == null) {
611 clientFactory = chooseFactory();
612 }
613 socket = clientFactory.createSocket(host, port);
614
615 } catch (java.net.UnknownHostException e) {
616 throw new java.rmi.UnknownHostException(
617 "Unknown host: " + host, e);
618 } catch (java.net.ConnectException e) {
619 throw new java.rmi.ConnectException(
620 "Connection refused to host: " + host, e);
621 } catch (IOException e) {
622 // We might have simply run out of file descriptors
623 try {
624 TCPEndpoint.shedConnectionCaches();
625 // REMIND: should we retry createSocket?
626 } catch (OutOfMemoryError | Exception mem) {
627 // don't quit if out of memory
628 // or shed fails non-catastrophically
629 }
630
631 throw new ConnectIOException("Exception creating connection to: " +
632 host, e);
633 }
634
635 // set socket to disable Nagle's algorithm (always send immediately)
636 // TBD: should this be left up to socket factory instead?
637 try {
638 socket.setTcpNoDelay(true);
639 } catch (Exception e) {
640 // if we fail to set this, ignore and proceed anyway
641 }
642
643 // fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
644 try {
645 socket.setKeepAlive(true);
646 } catch (Exception e) {
647 // ignore and proceed
648 }
|