Beispiel #1
0
        public static void Test()
        {
            NetworkListener lServer = new NetworkListener("127.0.0.1", 65432, "Server");
            NetworkClient   lClient = new NetworkClient("127.0.0.1", 65432, "Client");

            lServer.Connect();
            lServer.OnMessageBook  += new NetworkListener.dOnMessageBook(OnMessageBook);
            lServer.OnMessageFable += new NetworkListener.dOnMessageFable(OnMessageFable);

            lClient.Connect();

            ProtoBufExample.Header lHeader;

            // send a book across the network
            ProtoBufExample.Book lBook = ProtoBufExample.GetData();
            lHeader = new ProtoBufExample.Header(lBook, ProtoBufExample.eType.eBook);
            lClient.Send(lHeader);

            System.Threading.Thread.Sleep(1000); // remove this to see the asynchonous processing (the output will look terrible)

            // send a fable across the network
            lHeader = new ProtoBufExample.Header(lBook.stories[1], ProtoBufExample.eType.eFable);
            lClient.Send(lHeader);

            System.Threading.Thread.Sleep(1000);

            lClient.Disconnect();
            lServer.Disconnect();

            Console.ReadLine();
        }
Beispiel #2
0
        // demo: synchronous processing
        static void OnMessageFable(TcpClient xSender, ProtoBufExample.Header xHeader, ProtoBufExample.Fable xFable)
        {
            Console.WriteLine(Environment.NewLine + "received a fable: ");
            Console.WriteLine(xFable.ToString());

            // demo: we tell the server that something went wrong
            ProtoBufExample.ErrorMessage lErrorMessage = new ProtoBufExample.ErrorMessage()
            {
                Text = "The fable was rejected. It is far too short."
            };
            ProtoBufExample.Header lErrorHeader = new ProtoBufExample.Header(lErrorMessage, ProtoBufExample.eType.eError, xHeader.serialMessageId);
            NetworkListener.Send(xSender, lErrorHeader);
        } //
Beispiel #3
0
        } //

        // demo: asynchronous processing
        static void OnMessageBook(TcpClient xSender, ProtoBufExample.Header xHeader, ProtoBufExample.Book xBook)
        {
            Task.Factory.StartNew(() => {
                Console.WriteLine(Environment.NewLine + "received a book: ");
                Console.WriteLine(xBook.ToString());

                // send a feedback without any body to signal all was ok
                ProtoBufExample.Header lFeedback = new ProtoBufExample.Header(null, ProtoBufExample.eType.eFeedback, xHeader.serialMessageId);
                NetworkListener.Send(xSender, lFeedback);
                return;
            });

            Console.WriteLine("Book event was raised");
        } //