예제 #1
0
        static void Main(string[] args)
        {
            //create a new server
            var server = new UdpListener();

            //start listening for messages and copy the messages back to the client
            Task.Factory.StartNew(async() =>
            {
                while (true)
                {
                    var received = await server.Receive();
                    server.Reply("copy " + received.Message, received.Sender);
                    if (received.Message == "quit")
                    {
                        break;
                    }
                }
            });

            //create a new client
            var client = UdpUser.ConnectTo("127.0.0.1", 32123);

            //wait for reply messages from server and send them to console
            Task.Factory.StartNew(async() =>
            {
                while (true)
                {
                    try
                    {
                        var received = await client.Receive();
                        Console.WriteLine(received.Message);
                        if (received.Message.Contains("quit"))
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Write(ex);
                    }
                }
            });

            //type ahead :-)
            string read;

            do
            {
                read = Console.ReadLine();
                client.Send(read);
            } while (read != "quit");
        }
예제 #2
0
        static void Main(string[] args)
        {
            //create multi session
            List <NDS_MULTI_SESSION> nds_session_list = new List <NDS_MULTI_SESSION>();
            int server_port = 8888;
            //create a new server
            var server = new UdpListener();

            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.WriteLine(" Inter-DS UDP Companion Program. \n Usage: Connect this computer to the same network a DS is setup. \n Any connected DS through the DSWNIFI library should connect automatically.\n Developed by coto. Press ESC to quit");
            //start listening for messages and copy the messages back to the client
            Task.Factory.StartNew(async() =>
            {
                while (true)
                {
                    var received = await server.Receive();
                    server.Reply(received.Message, received.Sender);
                    string[] str_list = received.Message.Split('-');

                    //for assigning ids
                    Random random_inst = new Random();

                    //Console.WriteLine("UDPMSG:" + received.Message+" \n");
                    //dsnotaware-NIFINintendoDS-IP-PORT
                    //Console.WriteLine("RECVUDPMSG + \n");

                    //Console.WriteLine(str_list[0]+" \n");             //cmd
                    //Console.WriteLine(str_list[1] + " \n");           //header
                    //Console.WriteLine(str_list[2] + " \n");           //IP
                    //Console.WriteLine(str_list[3] + " \n end msg");   //NDS MULTI PORT
                    string incomingnds_cmd = str_list[0].Trim();
                    string incomingnds_ip  = str_list[2].Trim();
                    string incomingnds_multi_port;

                    if (str_list[0] == "dsnotaware")
                    {
                        bool rec_already_exists = false;

                        int index_found = 0;
                        foreach (NDS_MULTI_SESSION entry in nds_session_list)
                        {
                            if (entry.ip.Equals(incomingnds_ip))
                            {
                                rec_already_exists = true;
                                break;
                            }
                            index_found++;
                        }

                        if (rec_already_exists == false)
                        {
                            nds_session_list.Add(new NDS_MULTI_SESSION {
                                ip = incomingnds_ip, status_multiplay = false, server_port_cmd = server_port.ToString().Trim(), ds_status = DS_STAT_MODES.idle
                            });
                            Console.WriteLine("DS's detected so far:" + nds_session_list.Count() + "\n");
                        }
                        else
                        {
                            //Console.Write("DS [" + nds_session_list[index_found].ip + ":" + nds_session_list[index_found].port + "] already registered");
                        }

                        //2 ds are connected and waiting
                        if (nds_session_list.Count() > 1)
                        {
                            int nds1_index = 0;
                            int nds2_index = 0;

                            foreach (NDS_MULTI_SESSION entry in nds_session_list)
                            {
                                if (entry.status_multiplay == false)
                                {
                                    break;
                                }
                                nds1_index++;
                            }

                            foreach (NDS_MULTI_SESSION entry in nds_session_list)
                            {
                                if ((entry.status_multiplay == false) && !nds_session_list[nds1_index].Equals(entry))
                                {
                                    break;
                                }
                                nds2_index++;
                            }

                            //found them
                            if ((nds1_index != nds2_index) && (nds_session_list[nds1_index].status_multiplay == false) && (nds_session_list[nds2_index].status_multiplay == false))
                            {
                                string ds1_mode = "notassigned";
                                string ds2_mode = "notassigned";

                                // ind_token : 0 == guest  | 1 == host
                                //id 0 is null / id 1 == host / id 2 == guest
                                int ind_token = random_inst.Next(1);
                                if (ind_token == 0)
                                {
                                    //nds1_index[console1] is guest
                                    nds_session_list[nds1_index].mode = ds1_mode = "guest";
                                    //nds2_index[console2] is host
                                    nds_session_list[nds2_index].mode = ds2_mode = "host";
                                }
                                else
                                {
                                    //nds1_index[console1] is host
                                    nds_session_list[nds1_index].mode = ds1_mode = "host";
                                    //nds2_index[console2] is guest
                                    nds_session_list[nds2_index].mode = ds2_mode = "guest";
                                }

                                //assign ports
                                int LISTENER_PORT = 0;
                                int SENDER_PORT   = 0;
                                if (ds1_mode == "host")
                                {
                                    LISTENER_PORT = 8889;   //NDSMULTI_UDP_PORT_HOST 8889
                                    SENDER_PORT   = 8890;   //NDSMULTI_UDP_PORT_GUEST 8890

                                    nds_session_list[nds1_index].port_listener = LISTENER_PORT.ToString();
                                    nds_session_list[nds1_index].port_sender   = SENDER_PORT.ToString();

                                    nds_session_list[nds2_index].port_listener = SENDER_PORT.ToString();
                                    nds_session_list[nds2_index].port_sender   = LISTENER_PORT.ToString();
                                }
                                else if (ds2_mode == "host")
                                {
                                    LISTENER_PORT = 8889;   //NDSMULTI_UDP_PORT_HOST 8889
                                    SENDER_PORT   = 8890;   //NDSMULTI_UDP_PORT_GUEST 8890

                                    nds_session_list[nds2_index].port_listener = LISTENER_PORT.ToString();
                                    nds_session_list[nds2_index].port_sender   = SENDER_PORT.ToString();

                                    nds_session_list[nds1_index].port_listener = SENDER_PORT.ToString();
                                    nds_session_list[nds1_index].port_sender   = LISTENER_PORT.ToString();
                                }


                                //Console.Clear();

                                //send to DS's: MULTI IP of each counterpart DS, assign the new host-guest mode for each
                                string ip_dest_ds1 = String.Empty;
                                ip_dest_ds1        = nds_session_list[nds2_index].ip; //guest <- ip host

                                string ip_dest_ds2 = String.Empty;
                                ip_dest_ds2        = nds_session_list[nds1_index].ip; //guest <- ip host

                                nds_session_list[nds1_index].status_multiplay = true;
                                nds_session_list[nds2_index].status_multiplay = true;

                                //DS1
                                sendaware_req("srvaware", nds_session_list[nds1_index].ip, ip_dest_ds1, ds1_mode);

                                Thread.Sleep(3000);  //3s

                                //DS2
                                sendaware_req("srvaware", nds_session_list[nds2_index].ip, ip_dest_ds2, ds2_mode);
                                Console.WriteLine("DS's are binded correctly :)");
                            }
                            else
                            {
                                //Console.WriteLine("not enough NDSs available for multi IDLE'd");
                            }
                        }

                        //Console.WriteLine("DSCONSOLEWANTSTOSIGN-IN. DS's so far:"+nds_session_list.Count()+"\n");
                    }



                    if (str_list[0] == "dsaware")
                    {
                        //ds cmd: dsaware-%s-bindOK-%d-%s- (IP of NDS that sent this request)
                        //str_list[0] //cmd
                        //str_list[1] //host-guest assigned
                        //str_list[2] //binding status
                        //str_list[3] //DS listener port for recv cmds
                        //str_list[4] //IP
                        //Console.WriteLine("DSOK:[" + str_list[1] + "]-[" + str_list[2] + "]-[" + str_list[4] + "]:[" + str_list[3] + "] \n");


                        int nds_index   = 0;
                        string found_ip = String.Empty;
                        //get index(DS Console that sent this msg) from IP
                        foreach (NDS_MULTI_SESSION reg in nds_session_list)
                        {
                            if (((reg.ip + "\n") == str_list[4]) && (nds_session_list[nds_index].ds_status != DS_STAT_MODES.handshake))  //lazy I know but works anyway
                            {
                                //nds_session_list[nds_index].ds_status = DS_STAT_MODES.handshake;
                                found_ip = reg.ip;
                                break;
                            }
                            nds_index++;
                        }

                        //is this DS actually registered earlier? If so, proceed to connect
                        if (found_ip != String.Empty)
                        {
                            //send "dsconnect-" //where IP sent is the one this DS must connect to.
                            sendudpmsg("dsconnect-", nds_session_list[nds_index].ip.Trim(), nds_session_list[nds_index].server_port_cmd.Trim());
                        }
                    }

                    if (str_list[0] == "dsconnected")
                    {
                        //update lib status here
                    }

                    if (str_list[0] == "debug")
                    {
                        Console.WriteLine("DEBUG:" + received.Message + " \n");
                    }

                    if (received.Message == "quit")
                    {
                        break;
                    }
                }
            });


            while (ConsoleKey.Escape != Console.ReadKey().Key)
            {
                Thread.Sleep(100);  //100ms
            }
        }