<commit_before>package com.badlogic.gdx.backends.ios;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cli.System.AsyncCallback;
import cli.System.IAsyncResult;
import cli.System.Net.Dns;
import cli.System.Net.IPAddress;
import cli.System.Net.Sockets.AddressFamily;
import cli.System.Net.Sockets.TcpClient;
import cli.System.Net.Sockets.TcpListener;
import cli.System.Threading.ManualResetEvent;
import cli.System.Threading.Timeout;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.Protocol;
import com.badlogic.gdx.net.ServerSocket;
import com.badlogic.gdx.net.ServerSocketHints;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.net.SocketHints;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
 * iOS server socket implementation using System.Net.Sockets.TcpListener (Microsoft).
 * 
 * @author noblemaster
 */
public class IOSServerSocket implements ServerSocket {

	private Protocol protocol;
	public int port;
	private ServerSocketHints hints;
	
	/** Our listeners we have created (1 per port). Will prevent "address already in use" errors if used more than once. */
	static Map<Integer, TcpListener> listeners = new HashMap<Integer, TcpListener>();
	/** Our clients that we have accepted or null for none (1 per port). We'll constantly poll in background. */
	static Map<Integer, TcpClient> clients = new HashMap<Integer, TcpClient>();
	/** Our accept callbacks instantiated (1 per port). */
	static Map<Integer, AsyncCallback> clientCallbacks = new HashMap<Integer, AsyncCallback>();
	

	/** The sockets that are connected. Will need to keep them to dispose. */
	private List<IOSSocket> sockets = new ArrayList<IOSSocket>();
	
	
	public IOSServerSocket(Protocol protocol, final int port, final ServerSocketHints hints) {
		if (protocol == Protocol.TCP) {
			this.protocol = protocol;
			this.port = port;
			this.hints = hints;
			
			// create the server socket
			try {
				// we only create a new listener if one does not exist yet
				if (listeners.get(port) == null) {
					// get local IP to connect with
					IPAddress ipAddress = null;
					String host = Dns.GetHostName(); 
					host = host + ".local"; // the ".local" might not be needed on the simulator!?
					IPAddress[] ips = Dns.GetHostAddresses(host);
					for (int i = 0; i < ips.length; i++) {
						IPAddress ip = ips[i];
						if (ip.get_AddressFamily().Value == AddressFamily.InterNetwork) {
							ipAddress = ip;
							break;
						}
					}
					Gdx.app.debug("IOSServerSocket", "Binding server to " + ipAddress.ToString() + ":" + port);
					
					// initialize server		<commit_msg>iOS: bugfix local IP detection (simulator vs. device different).<commit_after>package com.badlogic.gdx.backends.ios;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cli.MonoTouch.ObjCRuntime.Arch;
import cli.System.AsyncCallback;
import cli.System.IAsyncResult;
import cli.System.Net.Dns;
import cli.System.Net.IPAddress;
import cli.System.Net.Sockets.AddressFamily;
import cli.System.Net.Sockets.TcpClient;
import cli.System.Net.Sockets.TcpListener;
import cli.System.Threading.ManualResetEvent;
import cli.System.Threading.Timeout;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.Protocol;
import com.badlogic.gdx.net.ServerSocket;
import com.badlogic.gdx.net.ServerSocketHints;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.net.SocketHints;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
 * iOS server socket implementation using System.Net.Sockets.TcpListener (Microsoft).
 * 
 * @author noblemaster
 */
public class IOSServerSocket implements ServerSocket {

	private Protocol protocol;
	public int port;
	private ServerSocketHints hints;
	
	/** Our listeners we have created (1 per port). Will prevent "address already in use" errors if used more than once. */
	static Map<Integer, TcpListener> listeners = new HashMap<Integer, TcpListener>();
	/** Our clients that we have accepted or null for none (1 per port). We'll constantly poll in background. */
	static Map<Integer, TcpClient> clients = new HashMap<Integer, TcpClient>();
	/** Our accept callbacks instantiated (1 per port). */
	static Map<Integer, AsyncCallback> clientCallbacks = new HashMap<Integer, AsyncCallback>();
	

	/** The sockets that are connected. Will need to keep them to dispose. */
	private List<IOSSocket> sockets = new ArrayList<IOSSocket>();
	
	
	public IOSServerSocket(Protocol protocol, final int port, final ServerSocketHints hints) {
		if (protocol == Protocol.TCP) {
			this.protocol = protocol;
			this.port = port;
			this.hints = hints;
			
			// create the server socket
			try {
				// we only create a new listener if one does not exist yet
				if (listeners.get(port) == null) {
					// get local IP to connect with
					IPAddress ipAddress = null;
					String host = Dns.GetHostName(); 
					if (cli.MonoTouch.ObjCRuntime.Runtime.Arch.Value == Arch.DEVICE) {
						// the ".local" is not added on the simulator!
						host = host + ".local"; 
					}
					IPAddress[] ips = Dns.GetHostAddresses(host);
					for (int i = 0; i < ips.length; i++) {
						IPAddress ip = ips[i];
						if (ip.get_AddressFamily().Value == AddressFamily.InterNetwork) {
							ipAddress = ip;
							break;
						}
					}
					Gdx.app.debug("IOSServerSocket", "Binding server to " + ipAddress.ToString() + ":" + port);
					
					// initialize server		
{'fraction_non_alphanumeric': 0.11190345584201865, 'fraction_numerical': 0.0014627902724446882, 'mean_word_length': 3.8024582967515363, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144394', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1653, 'n_words': 486}