1 /*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 package com.badlogic.gdx.backends.ios;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.net.InetSocketAddress;
22 import cli.System.Net.Sockets.LingerOption;
23 import cli.System.Net.Sockets.NetworkStream;
24 import cli.System.Net.Sockets.TcpClient;
26 import com.badlogic.gdx.Net.Protocol;
27 import com.badlogic.gdx.net.Socket;
28 import com.badlogic.gdx.net.SocketHints;
29 import com.badlogic.gdx.utils.GdxRuntimeException;
32 * The iOS socket implementation using System.Net.Sockets.TcpClient (Microsoft).
36 public class IOSSocket implements Socket {
38 /** Our server if the socket was created via server socket. null if it's a client socket only. */
39 private IOSServerSocket server;
41 /** Our client or null for disposed, aka closed. */
42 private TcpClient client;
44 private NetworkStream stream;
45 private IOSStreamInput inputStream;
46 private IOSStreamOutput outputStream;
49 public IOSSocket(Protocol protocol, String host, int port, SocketHints hints) {
50 if (protocol == Protocol.TCP) {
52 // create and connect the socket
53 // NOTE: there is no connection timeout setting available - will assume there is some sort of default!?
54 client = new TcpClient(host, port);
55 setupConnection(hints);
57 // Hack to catch socket exception
58 // Keep compiler happy and catch the Mono SocketException explicitly.
59 // This Mono exception is not caught by the java Exception catch.
60 if (false) throw new cli.System.Net.Sockets.SocketException();
61 } catch(cli.System.Net.Sockets.SocketException e) {
62 throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e);
65 throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e);
69 throw new GdxRuntimeException("Socket protocol " + protocol + " is not supported under iOS backend.");
73 public IOSSocket(IOSServerSocket server, TcpClient client, SocketHints hints) {
76 setupConnection(hints);
79 private void setupConnection(SocketHints hints) {
80 // apply hints as needed
83 // NOTE: traffic parameter settings/class cannot be set via iOS
84 client.set_NoDelay(hints.tcpNoDelay);
85 client.set_SendTimeout(hints.keepAlive ? 0 : 30000); // milliseconds -> 0=no timeout
86 client.set_SendBufferSize(hints.sendBufferSize);
87 client.set_ReceiveTimeout(hints.keepAlive ? 0 : 30000); // milliseconds -> 0=no timeout
88 client.set_ReceiveBufferSize(hints.receiveBufferSize);
89 client.set_LingerState(new LingerOption(hints.linger, hints.lingerDuration));
92 throw new GdxRuntimeException("Error setting socket hints." , e);
96 // create our streams!
97 stream = client.GetStream();
98 inputStream = new IOSStreamInput(stream, false);
99 outputStream = new IOSStreamOutput(stream, false);
103 public boolean isConnected () {
104 if (client != null) {
105 return client.get_Connected();
113 public InputStream getInputStream () {
118 public OutputStream getOutputStream () {
123 public void dispose() {
125 if (stream != null) {
129 catch (Exception e) {
130 throw new GdxRuntimeException("Error closing stream.", e);
135 // remove from server as needed
136 if (server != null) {
138 server.dispose(this);
140 catch (Exception e) {
141 throw new GdxRuntimeException("Error closing socket on server.", e);
147 if (client != null) {
152 catch (Exception e) {
153 throw new GdxRuntimeException("Error closing socket.", e);