OSDN Git Service

scene2d.ui Disableable interface.
[mikumikustudio/libgdx-mikumikustudio.git] / backends / gdx-backend-iosmonotouch / src / com / badlogic / gdx / backends / ios / IOSSocket.java
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  * 
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
7  * 
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  * 
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;
17
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.net.InetSocketAddress;
21
22 import cli.System.Net.Sockets.LingerOption;
23 import cli.System.Net.Sockets.NetworkStream;
24 import cli.System.Net.Sockets.TcpClient;
25
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;
30
31 /**
32  * The iOS socket implementation using System.Net.Sockets.TcpClient (Microsoft).
33  * 
34  * @author noblemaster
35  */
36 public class IOSSocket implements Socket {
37
38         /** Our server if the socket was created via server socket. null if it's a client socket only. */
39         private IOSServerSocket server;
40
41         /** Our client or null for disposed, aka closed. */
42         private TcpClient client;
43         
44         private NetworkStream stream;
45         private IOSStreamInput inputStream;
46         private IOSStreamOutput outputStream;
47         
48         
49         public IOSSocket(Protocol protocol, String host, int port, SocketHints hints) {
50                 if (protocol == Protocol.TCP) {
51                         try {
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);
56                                 
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);
63                         }
64                         catch (Exception e) {
65                                 throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e);
66                         }
67                 }
68                 else {
69                         throw new GdxRuntimeException("Socket protocol " + protocol + " is not supported under iOS backend.");
70                 }
71         }
72         
73         public IOSSocket(IOSServerSocket server, TcpClient client, SocketHints hints) {
74                 this.server = server;
75                 this.client = client;
76                 setupConnection(hints);
77         }
78         
79         private void setupConnection(SocketHints hints) {
80                 // apply hints as needed
81                 if (hints != null) {
82                         try {   
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));
90                         }
91                         catch (Exception e) {
92                                 throw new GdxRuntimeException("Error setting socket hints." , e);
93                         }
94                 }
95                 
96                 // create our streams!
97                 stream = client.GetStream();
98                 inputStream = new IOSStreamInput(stream, false);
99                 outputStream = new IOSStreamOutput(stream, false);
100         }
101         
102         @Override
103         public boolean isConnected () {
104                 if (client != null) {
105                         return client.get_Connected();
106                 }
107                 else {
108                         return false;
109                 }
110         }
111
112         @Override
113         public InputStream getInputStream () {
114                 return inputStream;
115         }
116
117         @Override
118         public OutputStream getOutputStream () {
119                 return outputStream;
120         }
121
122         @Override
123         public void dispose() {
124                 // close stream
125            if (stream != null) {
126                         try {
127                                 stream.Close();
128                         }
129                         catch (Exception e) {
130                                 throw new GdxRuntimeException("Error closing stream.", e);
131                         }
132                         stream = null;
133            }
134                    
135                 // remove from server as needed
136                 if (server != null) {
137                         try {
138                                 server.dispose(this);
139                         }
140                         catch (Exception e) {
141                                 throw new GdxRuntimeException("Error closing socket on server.", e);
142                         }
143                         server = null;
144                 }
145                         
146                 // dispose client
147                 if (client != null) {
148                         try {
149                                 client.Close();
150                                 client.Dispose();
151                         }
152                         catch (Exception e) {
153                                 throw new GdxRuntimeException("Error closing socket.", e);
154                         }
155                         client = null;
156                 }
157         }
158 }