//Example if you want to subscribe to an event fired by Wyphon
        //partner.OnMessage += new MessageReceivedHandler(DoubleBufferChanged);



        public LocalMessageBroadcastPartner(string partnerName, string channel)
        {
            partnerJoinedCallbackDelegate   = PartnerJoinedCallback;
            partnerLeftCallbackDelegate     = PartnerLeftCallback;
            messageReceivedCallbackDelegate = MessageReceivedCallback;

            localMessageBroadcastPartnerHandle = CreateLocalMessageBroadcastPartner(channel, partnerName, IntPtr.Zero,
                                                                                    Marshal.GetFunctionPointerForDelegate(partnerJoinedCallbackDelegate),
                                                                                    Marshal.GetFunctionPointerForDelegate(partnerLeftCallbackDelegate),
                                                                                    Marshal.GetFunctionPointerForDelegate(messageReceivedCallbackDelegate)
                                                                                    );
        }
        //Example if you want to subscribe to an event fired by Wyphon
        //partner.OnMessage += new MessageReceivedHandler(DoubleBufferChanged);
        public LocalMessageBroadcastPartner(string partnerName, string channel)
        {
            partnerJoinedCallbackDelegate = PartnerJoinedCallback;
            partnerLeftCallbackDelegate = PartnerLeftCallback;
            messageReceivedCallbackDelegate = MessageReceivedCallback;

            localMessageBroadcastPartnerHandle = CreateLocalMessageBroadcastPartner(channel, partnerName, IntPtr.Zero,
                                                      Marshal.GetFunctionPointerForDelegate(partnerJoinedCallbackDelegate),
                                                      Marshal.GetFunctionPointerForDelegate(partnerLeftCallbackDelegate),
                                                      Marshal.GetFunctionPointerForDelegate(messageReceivedCallbackDelegate)
                                                     );
        }
        public void DoTestLocalMessageBroadcast()
        {
            Console.Write("What's your name: ");
            string name = Console.ReadLine();
            Console.Write("Which channel (0-9) do you want to use: ");
            char input = Console.ReadKey(true).KeyChar;//.ReadLine();
            while ( input != '0' && input != '1' && input != '2' && input != '3' && input != '4' && input != '5' && input != '6' && input != '7' && input != '8' && input != '9' ) {
                input = Console.ReadKey(true).KeyChar;
            }
            Console.WriteLine("\n");
            string channel = input.ToString();

            joinedDelegate = BroadcastMessagePartnerJoinedCallback;
            leftDelegate = BroadcastMessagePartnerLeftCallback;
            receivedDelegate = BroadcastMessageReceivedCallback;
            UInt32 hLocalMessageBroadcastPartner = CreateLocalMessageBroadcastPartner(
                    "BroadcastTest/channel" + channel,
                    name,
                    IntPtr.Zero,
                    Marshal.GetFunctionPointerForDelegate(joinedDelegate),
                    Marshal.GetFunctionPointerForDelegate(leftDelegate),
                    Marshal.GetFunctionPointerForDelegate(receivedDelegate)
                );

            if ( hLocalMessageBroadcastPartner > 0 ) {
                try {
                    Console.WriteLine("");
                    Console.WriteLine("Welcome " + name + "! You can talk to the others on channel " + channel + " by typing s, then your message and pressing <enter>");

                    input = Console.ReadKey(true).KeyChar;//.ReadLine();
                    while ( input != 'q' ) {
                        if ( input == 's' ) {
                            Console.Write( name + " says: ");
                            string s = Console.ReadLine();
                            byte[] stringBytes = new System.Text.UTF8Encoding().GetBytes(s);
                            BroadcastMessage( hLocalMessageBroadcastPartner, stringBytes, (UInt32)(stringBytes.GetLength(0)) );
                        }

                        input = Console.ReadKey(true).KeyChar;
                    }
                }
                finally {
                    if ( DestroyLocalMessageBroadcastPartner(hLocalMessageBroadcastPartner) ) {
                        Console.WriteLine( "SUCCESSFULLY destroyed LocalMessageBroadcast Partner" );
                    }
                    else {
                        Console.WriteLine( "FAILED to destroy LocalMessageBroadcast Partner" );
                    }
                }
            }
            else {
                Console.WriteLine( "CreateLocalMessageBroadcastPartner FAILED!!!" );
            }

            Console.WriteLine( "\nPress 'q' again to really quit..." );
            char input2 = Console.ReadKey(true).KeyChar;//.ReadLine();
            while ( input2 != 'q' ) {
            }
            //Thread.Sleep(8000);
        }
Esempio n. 4
0
        public void DoTestLocalMessageBroadcast()
        {
            Console.Write("What's your name: ");
            string name = Console.ReadLine();

            Console.Write("Which channel (0-9) do you want to use: ");
            char input = Console.ReadKey(true).KeyChar;            //.ReadLine();

            while (input != '0' && input != '1' && input != '2' && input != '3' && input != '4' && input != '5' && input != '6' && input != '7' && input != '8' && input != '9')
            {
                input = Console.ReadKey(true).KeyChar;
            }
            Console.WriteLine("\n");
            string channel = input.ToString();

            joinedDelegate   = BroadcastMessagePartnerJoinedCallback;
            leftDelegate     = BroadcastMessagePartnerLeftCallback;
            receivedDelegate = BroadcastMessageReceivedCallback;
            UInt32 hLocalMessageBroadcastPartner = CreateLocalMessageBroadcastPartner(
                "BroadcastTest/channel" + channel,
                name,
                IntPtr.Zero,
                Marshal.GetFunctionPointerForDelegate(joinedDelegate),
                Marshal.GetFunctionPointerForDelegate(leftDelegate),
                Marshal.GetFunctionPointerForDelegate(receivedDelegate)
                );

            if (hLocalMessageBroadcastPartner > 0)
            {
                try {
                    Console.WriteLine("");
                    Console.WriteLine("Welcome " + name + "! You can talk to the others on channel " + channel + " by typing s, then your message and pressing <enter>");

                    input = Console.ReadKey(true).KeyChar;                    //.ReadLine();
                    while (input != 'q')
                    {
                        if (input == 's')
                        {
                            Console.Write(name + " says: ");
                            string s           = Console.ReadLine();
                            byte[] stringBytes = new System.Text.UTF8Encoding().GetBytes(s);
                            BroadcastMessage(hLocalMessageBroadcastPartner, stringBytes, (UInt32)(stringBytes.GetLength(0)));
                        }

                        input = Console.ReadKey(true).KeyChar;
                    }
                }
                finally {
                    if (DestroyLocalMessageBroadcastPartner(hLocalMessageBroadcastPartner))
                    {
                        Console.WriteLine("SUCCESSFULLY destroyed LocalMessageBroadcast Partner");
                    }
                    else
                    {
                        Console.WriteLine("FAILED to destroy LocalMessageBroadcast Partner");
                    }
                }
            }
            else
            {
                Console.WriteLine("CreateLocalMessageBroadcastPartner FAILED!!!");
            }

            Console.WriteLine("\nPress 'q' again to really quit...");
            char input2 = Console.ReadKey(true).KeyChar;            //.ReadLine();

            while (input2 != 'q')
            {
            }
            //Thread.Sleep(8000);
        }