示例#1
0
		public static IPEndPoint LanDiscovery(ushort port, int listenWaitTime = 10000, TransportationProtocolType protocol = TransportationProtocolType.UDP, bool winRT = false)
#endif
		{
#if !NETFX_CORE && !UNITY_WEBPLAYER
			UdpClient Client = new UdpClient();
			IPEndPoint foundEndpoint = new IPEndPoint(IPAddress.Any, 0);
			bool found = false;

			List<string> localSubNet = new List<string>();

			foreach (System.Net.NetworkInformation.NetworkInterface f in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
			{
				if (f.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
				{
					foreach (System.Net.NetworkInformation.GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
					{
						if (d.Address.ToString() == "0.0.0.0") continue;

						if (d.Address.ToString().Contains("."))
							localSubNet.Add(d.Address.ToString().Remove(d.Address.ToString().LastIndexOf('.')));
					}
				}
			}

			foreach (string s in localSubNet)
				Client.Send(new byte[1], 1, new IPEndPoint(IPAddress.Parse(s + ".255"), port));

			int counter = 0;
			do
			{
				if (Client.Available != 0)
				{
					Client.Receive(ref foundEndpoint);
					found = true;
					break;
				}

				if (counter++ > listenWaitTime / 50)
					break;

				System.Threading.Thread.Sleep(50);
				foreach (string s in localSubNet)
					Client.Send(new byte[1], 1, new IPEndPoint(IPAddress.Parse(s + ".255"), port));
			} while (true);

			Client.Close();

			if (found)
				return foundEndpoint;
#elif NETFX_CORE
			// TODO:  Implement
#elif UNITY_WEBPLAYER
			Debug.LogError("Unable to find local at this time for webplayer");
#endif

			return null;
		}
示例#2
0
		/// <summary>
		/// Finds the first host on the network on the specified port number in the local area network and makes a connection to it
		/// </summary>
		/// <param name="port">The port to connect to</param>
		/// <param name="listenWaitTime">The time in milliseconds to wait for a discovery</param>
		/// <param name="protocol">The protocol type for the server</param>
		/// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param>
		/// <returns>The <see cref="NetWorker"/> that has been bound for this communication, null if none were found</returns>
#if NETFX_CORE
		public static IPEndPointWinRT LanDiscovery(ushort port, int listenWaitTime = 10000, TransportationProtocolType protocol = TransportationProtocolType.UDP, bool winRT = false)
示例#3
0
		/// <summary>
		/// Create and connect a client to the specified server ip and port
		/// </summary>
		/// <param name="ip">The host (usually ip address or domain name) to connect to</param>
		/// <param name="port">The port for the particular server that this connection is attempting</param>
		/// <param name="comType">The transportation protocol type that is to be used <see cref="Networking.TransportationProtocolType"/></param>
		/// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param>
		/// <returns>The NetWorker client that was created (Which may not have established a connection yet <see cref="NetWorker.connected"/></returns>
		/// <example>
		/// public string host = "127.0.0.1";																		// IP address
		/// public int port = 15937;																				// Port number
		/// public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP;	// Communication protocol
		/// 
		/// #if NETFX_CORE && !UNITY_EDITOR
		///		private bool isWinRT = true;
		/// #else
		///		private bool isWinRT = false;
		/// #endif
		/// public void StartServer()
		/// {
		///		NetWorker socket = Networking.Connect(host, (ushort)port, protocolType, isWinRT);	
		///	}
		/// </example>
		public static NetWorker Connect(string ip, ushort port, TransportationProtocolType comType, bool winRT = false)
		{
			Unity.MainThreadManager.Create();

			if (Sockets == null) Sockets = new Dictionary<ushort, NetWorker>();

			if (Sockets.ContainsKey(port))
			{
				if (Sockets[port].Connected)
					throw new NetworkException(8, "Socket has already been initialized on that port");
				else if (Sockets[port].Disconnected)
					Sockets.Remove(port);
				else
					return Sockets[port];	// It has not finished connecting yet
			}

			if (comType == TransportationProtocolType.UDP)
				Sockets.Add(port, new CrossPlatformUDP(false, 0));
			else
			{
				if (winRT)
					Sockets.Add(port, new WinMobileClient());
				else
					Sockets.Add(port, new DefaultClientTCP());
			}

			Sockets[port].connected += delegate()
			{
				if (connectedInvoker != null)
					connectedInvoker();
			};

			Sockets[port].Connect(ip, port);

			return Sockets[port];
		}
示例#4
0
		/// <summary>
		/// Will setup a new server on this machine
		/// </summary>
		/// <param name="port">This is the port you want to bind the server to</param>
		/// <param name="comType">The particular transportation protocol <see cref="Networking.TransportationProtocolType"/> you wish to be used for this server</param>
		/// <param name="maxConnections">The maximum connections (players) allowed on the server at one point in time</param>
		/// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param>
		/// <param name="allowWebplayerConnection">Allow web player connections to server</param>
		/// <param name="relayToAll">Used to determine if messages should be relayed to client (normally true) - Mainly internal</param>
		/// <returns>The NetWorker server that was created (Which may not have established a connection yet <see cref="NetWorker.connected"/></returns>
		/// <example>
		/// public int port = 15937;																				// Port number
		/// public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP;	// Communication protocol
		/// public int playerCount = 31;
		/// 
		/// #if NETFX_CORE && !UNITY_EDITOR
		///		private bool isWinRT = true;
		/// #else
		///		private bool isWinRT = false;
		/// #endif
		/// public void StartServer()
		/// {
		///		NetWorker socket = Networking.Host((ushort)port, protocolType, playerCount, isWinRT);	
		///	}
		/// </example>
		public static NetWorker Host(ushort port, TransportationProtocolType comType, int maxConnections, bool winRT = false, string overrideIP = null, bool allowWebplayerConnection = false, bool relayToAll = true)
		{
			Unity.MainThreadManager.Create();

			if (Sockets == null) Sockets = new Dictionary<ushort, NetWorker>();

			if (Sockets.ContainsKey(port) && Sockets[port].Connected)
				throw new NetworkException(8, "Socket has already been initialized on that port");

			if (comType == TransportationProtocolType.UDP)
				Sockets.Add(port, new CrossPlatformUDP(true, maxConnections));
			else
			{
				if (winRT)
					Sockets.Add(port, new WinMobileServer(maxConnections));
				else
				{
					Sockets.Add(port, new DefaultServerTCP(maxConnections));
					((DefaultServerTCP)Sockets[port]).RelayToAll = relayToAll;
				}
			}

			Sockets[port].connected += delegate()
			{
				Sockets[port].AssignUniqueId(0);

				if (connectedInvoker != null)
					connectedInvoker();
			};

			Sockets[port].Connect(overrideIP, port);

#if !NETFX_CORE
			// TODO:  Allow user to pass in the variables needed to pass into this begin function
			if (allowWebplayerConnection)
				SocketPolicyServer.Begin();
#endif

			return Sockets[port];
		}
示例#5
0
		/// <summary>
		/// Create and connect a client to the specified server ip and port
		/// </summary>
		/// <param name="ip">The host (usually ip address or domain name) to connect to</param>
		/// <param name="port">The port for the particular server that this connection is attempting</param>
		/// <param name="comType">The transportation protocol type that is to be used <see cref="Networking.TransportationProtocolType"/></param>
		/// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param>
		/// <returns>The NetWorker client that was created (Which may not have established a connection yet <see cref="NetWorker.connected"/></returns>
		/// <example>
		/// public string host = "127.0.0.1";																		// IP address
		/// public int port = 15937;																				// Port number
		/// public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP;	// Communication protocol
		/// 
		/// #if NETFX_CORE && !UNITY_EDITOR
		///		private bool isWinRT = true;
		/// #else
		///		private bool isWinRT = false;
		/// #endif
		/// public void StartServer()
		/// {
		///		NetWorker socket = Networking.Connect(host, (ushort)port, protocolType, isWinRT);	
		///	}
		/// </example>
		public static NetWorker Connect(string ip, ushort port, TransportationProtocolType comType, bool winRT = false, bool useNat = false, bool standAlone = false)
		{
			Threading.ThreadManagement.Initialize();
			Unity.MainThreadManager.Create();

			if (Sockets == null) Sockets = new Dictionary<ushort, NetWorker>();

			if (Sockets.ContainsKey(port))
			{
#if UNITY_IOS || UNITY_IPHONE
				if (comType == TransportationProtocolType.UDP)
					Sockets[port] = new CrossPlatformUDP(false, 0);
				else
					Sockets[port] = new DefaultClientTCP();
#else
				if (Sockets[port].Connected)
					throw new NetworkException(8, "Socket has already been initialized on that port");
				else if (Sockets[port].Disconnected)
					Sockets.Remove(port);
				else
					return Sockets[port];	// It has not finished connecting yet
#endif
			}
			else if (comType == TransportationProtocolType.UDP)
				Sockets.Add(port, new CrossPlatformUDP(false, 0));
			else
			{
				if (winRT)
					Sockets.Add(port, new WinMobileClient());
				else
					Sockets.Add(port, new DefaultClientTCP());
			}

			Sockets[port].connected += delegate()
			{
				if (connectedInvoker != null)
					connectedInvoker(Sockets[port]);
			};

			Sockets[port].UseNatHolePunch = useNat;
			Sockets[port].Connect(ip, port);

			if (!standAlone)
				SimpleNetworkedMonoBehavior.Initialize(Sockets[port]);

			return Sockets[port];
		}
示例#6
0
		/// <summary>
		/// Will setup a new server on this machine
		/// </summary>
		/// <param name="port">This is the port you want to bind the server to</param>
		/// <param name="comType">The particular transportation protocol <see cref="Networking.TransportationProtocolType"/> you wish to be used for this server</param>
		/// <param name="maxConnections">The maximum connections (players) allowed on the server at one point in time</param>
		/// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param>
		/// <param name="allowWebplayerConnection">Allow web player connections to server</param>
		/// <param name="relayToAll">Used to determine if messages should be relayed to client (normally true) - Mainly internal</param>
		/// <returns>The NetWorker server that was created (Which may not have established a connection yet <see cref="NetWorker.connected"/></returns>
		/// <example>
		/// public int port = 15937;																				// Port number
		/// public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP;	// Communication protocol
		/// public int playerCount = 31;
		/// 
		/// #if NETFX_CORE && !UNITY_EDITOR
		///		private bool isWinRT = true;
		/// #else
		///		private bool isWinRT = false;
		/// #endif
		/// public void StartServer()
		/// {
		///		NetWorker socket = Networking.Host((ushort)port, protocolType, playerCount, isWinRT);	
		///	}
		/// </example>
		public static NetWorker Host(ushort port, TransportationProtocolType comType, int maxConnections, bool winRT = false, string overrideIP = null, bool allowWebplayerConnection = false, bool relayToAll = true, bool useNat = false, NetWorker.NetworkErrorEvent errorCallback = null)
		{
			Threading.ThreadManagement.Initialize();
			Unity.MainThreadManager.Create();

			if (Sockets == null) Sockets = new Dictionary<ushort, NetWorker>();

			if (Sockets.ContainsKey(port) && Sockets[port].Connected)
				throw new NetworkException(8, "Socket has already been initialized on that port");

			if (comType == TransportationProtocolType.UDP)
				Sockets.Add(port, new CrossPlatformUDP(true, maxConnections));
			else
			{
				if (winRT)
					Sockets.Add(port, new WinMobileServer(maxConnections));
				else
				{
					Sockets.Add(port, new DefaultServerTCP(maxConnections));
					((DefaultServerTCP)Sockets[port]).RelayToAll = relayToAll;
				}
			}

			// JM: added error callback in args in case Connect() below fails
			if (errorCallback != null) {
				Sockets [port].error += errorCallback;
			}

			Sockets[port].connected += delegate()
			{
				Sockets[port].AssignUniqueId(0);

				if (connectedInvoker != null)
					connectedInvoker(Sockets[port]);
			};

			Sockets[port].UseNatHolePunch = useNat;
			Sockets[port].Connect(overrideIP, port);

#if !NETFX_CORE
			// TODO:  Allow user to pass in the variables needed to pass into this begin function
			if (allowWebplayerConnection)
				SocketPolicyServer.Begin();
#endif

			SimpleNetworkedMonoBehavior.Initialize(Sockets[port]);
			return Sockets[port];
		}