| 34 | /// |
| 35 | /// @author Shai Almog |
| 36 | public final class Socket { |
| 37 | private Socket() { |
| 38 | } |
| 39 | |
| 40 | /// Returns true if sockets are supported in this port, false otherwise |
| 41 | /// |
| 42 | /// #### Returns |
| 43 | /// |
| 44 | /// true if sockets are supported in this port, false otherwise |
| 45 | public static boolean isSupported() { |
| 46 | return Util.getImplementation().isSocketAvailable(); |
| 47 | } |
| 48 | |
| 49 | /// Returns true if server sockets are supported in this port, if this method returns |
| 50 | /// false invocations of listen will always fail |
| 51 | /// |
| 52 | /// #### Returns |
| 53 | /// |
| 54 | /// true if server sockets are supported in this port, false otherwise |
| 55 | /// |
| 56 | /// #### Deprecated |
| 57 | /// |
| 58 | /// @deprecated server sockets are only supported on Android and Desktop and as such |
| 59 | /// we recommend against using them |
| 60 | public static boolean isServerSocketSupported() { |
| 61 | return Util.getImplementation().isServerSocketAvailable(); |
| 62 | } |
| 63 | |
| 64 | /// Connect to a remote host |
| 65 | /// |
| 66 | /// #### Parameters |
| 67 | /// |
| 68 | /// - `host`: the host |
| 69 | /// |
| 70 | /// - `port`: the connection port |
| 71 | /// |
| 72 | /// - `sc`: callback for when the connection is established or fails |
| 73 | public static void connect(final String host, final int port, final SocketConnection sc) { |
| 74 | if (host.indexOf('.') > -1 && host.indexOf(':') > -1) { |
| 75 | throw new IllegalArgumentException("Port should be provided separately"); |
| 76 | } |
| 77 | Display.getInstance().startThread(new Runnable() { |
| 78 | @Override |
| 79 | public void run() { |
| 80 | Object connection = Util.getImplementation().connectSocket(host, port, sc.getConnectTimeout()); |
| 81 | if (connection != null) { |
| 82 | sc.setConnected(true); |
| 83 | sc.input = new SocketInputStream(connection, sc); |
| 84 | sc.output = new SocketOutputStream(connection, sc); |
| 85 | sc.connectionEstablished(sc.input, sc.output); |
| 86 | } else { |
| 87 | sc.setConnected(false); |
| 88 | if (connection == null) { |
| 89 | sc.connectionError(-1, "Failed to connect"); |
| 90 | } else { |
| 91 | sc.connectionError(Util.getImplementation().getSocketErrorCode(connection), Util.getImplementation().getSocketErrorMessage(connection)); |
| 92 | } |
| 93 | } |
nothing calls this directly
no outgoing calls
no test coverage detected