Exemplo n.º 1
0
        public static void SendMCL()
        {
            Console.WriteLine("Enter Port: ");
            int port = int.Parse(Console.ReadLine());

            NetPeerConfiguration config = new NetPeerConfiguration("InduZtry");
            config.Port = port;
            config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
            config.EnableMessageType(NetIncomingMessageType.Data);
            config.AcceptIncomingConnections = true;
            //config.EnableUPnP = true;
            peer = new NetPeer(config);
            peer.Start();
            //Console.WriteLine("UPnP IP: " + peer.UPnP.GetExternalIP());

            Thread t = new Thread(RecvMCL);
            t.IsBackground = true;
            t.Start();

            //Console.WriteLine("Enter Server IP: ");
            //string sip = Console.ReadLine();
            Console.WriteLine("Enter Server Port: ");
            port = int.Parse(Console.ReadLine());
            //peer.DiscoverKnownPeer(sip, port);
            peer.DiscoverLocalPeers(port);
            //while(true) {
            //Thread.Sleep(10);
            //Console.WriteLine("Port: ");
            //int rport = int.Parse(Console.ReadLine());
            //peer.DiscoverLocalPeers(rport);
            //}
        }
Exemplo n.º 2
0
		internal static void Find (NetworkSessionType sessionType)
		{
			NetPeerConfiguration config = new NetPeerConfiguration (applicationIdentifier);			
			if (sessionType == NetworkSessionType.PlayerMatch) {
				config.EnableMessageType (NetIncomingMessageType.UnconnectedData);
				config.EnableMessageType (NetIncomingMessageType.NatIntroductionSuccess);
			} else {
				config.EnableMessageType (NetIncomingMessageType.DiscoveryRequest);
			}
            if (MonoGameNetworkConfiguration.Broadcast != IPAddress.None)
            {
                config.BroadcastAddress = MonoGameNetworkConfiguration.Broadcast;
            }
			netPeer = new NetPeer (config);
			netPeer.Start ();

			if (sessionType == NetworkSessionType.PlayerMatch) {
				GetServerList (netPeer);
			} else {
				netPeer.DiscoverLocalPeers (port);
			}

			DateTime now = DateTime.Now;

			discoveryMsgs = new List<NetIncomingMessage> ();

			do {
				NetIncomingMessage msg;
				while ((msg = netPeer.ReadMessage ()) != null) {
					switch (msg.MessageType) {
					case NetIncomingMessageType.DiscoveryResponse:
						discoveryMsgs.Add (msg);
						break;
					case NetIncomingMessageType.UnconnectedData:
						if (msg.SenderEndpoint.Equals (m_masterServer)) {
							discoveryMsgs.Add (msg);
							/*
				* // it's from the master server - must be a host
				IPEndPoint hostInternal = msg.ReadIPEndpoint();
				IPEndPoint hostExternal = msg.ReadIPEndpoint();

				m_hostList.Add(new IPEndPoint[] { hostInternal, hostExternal });                            
				*/
						}
						break;
					case NetIncomingMessageType.VerboseDebugMessage:
					case NetIncomingMessageType.DebugMessage:
					case NetIncomingMessageType.WarningMessage:
					case NetIncomingMessageType.ErrorMessage:
						//
						// Just print diagnostic messages to console
						//
#if DEBUG
						Console.WriteLine ("Find: " + msg.ReadString ());
#endif
						break;
					}
				}
			} while ((DateTime.Now - now).Seconds <= 2);

			netPeer.Shutdown ("Find shutting down");
		}
        public PentagoNetwork(string playerName)
        {
            lastPingTime = DateTime.Now;
            nextPingTime = lastPingTime.AddSeconds(1);

            availablePeers = new List<peerType>();

            config = new NetPeerConfiguration("Pentago");
            // Enable DiscoveryResponse messages
            config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
            config.EnableMessageType(NetIncomingMessageType.UnconnectedData);
            config.EnableMessageType(NetIncomingMessageType.Data);
            config.ConnectionTimeout = 5;
            config.AcceptIncomingConnections = true;
            config.MaximumConnections = 32;
            config.Port = PORT_NUMBER;

            peer = new NetPeer(config);
            try
            {
                peer.Start();
                peerOn = true;

                Console.WriteLine("Peer Started");

                Thread t = new Thread(waitForMessages);
                t.SetApartmentState(ApartmentState.STA);
                peerName = playerName;
                t.Start();

                Console.WriteLine(peerName);

                peer.DiscoverLocalPeers(PORT_NUMBER);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Caught exception: " + ex.Data);
            }
        }