private void Send(BluetoothClient xClient, ProtoBufExample.Header xHeader)
        {
            if (xClient == null)
            {
                return;
            }
            if (xHeader == null)
            {
                return;
            }

            lock (xClient)
            {
                try
                {
                    NetworkStream lNetworkStream = xClient.GetStream();

                    // send header (most likely a simple feedback)
                    ProtoBuf.Serializer.SerializeWithLengthPrefix <ProtoBufExample.Header>(lNetworkStream, xHeader, ProtoBuf.PrefixStyle.Fixed32);

                    // send errors
                    if (xHeader.objectType != ProtoBufExample.eType.eError)
                    {
                        return;
                    }
                    ProtoBuf.Serializer.SerializeWithLengthPrefix <ProtoBufExample.ErrorMessage>(lNetworkStream, (ProtoBufExample.ErrorMessage)xHeader.data, ProtoBuf.PrefixStyle.Fixed32);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        } //

        private void LoopRead(object xClient)
        {
            BluetoothClient lClient        = xClient as BluetoothClient;
            NetworkStream   lNetworkStream = lClient.GetStream();

            while (!_ExitLoop)
            {
                try {
                    ProtoBufExample.Header lHeader = ProtoBuf.Serializer.DeserializeWithLengthPrefix <ProtoBufExample.Header>(lNetworkStream, ProtoBuf.PrefixStyle.Fixed32);
                    if (lHeader == null)
                    {
                        break;              // happens during shutdown process
                    }
                    switch (lHeader.objectType)
                    {
                    case ProtoBufExample.eType.eBook:
                        ProtoBufExample.Book lBook = ProtoBuf.Serializer.DeserializeWithLengthPrefix <ProtoBufExample.Book>(lNetworkStream, ProtoBuf.PrefixStyle.Fixed32);
                        if (lBook == null)
                        {
                            break;
                        }
                        lHeader.data = lBook; // not necessary, but nicer

                        OnMessageBookReceived(new BtChatBookEventArgs(lHeader, lBook));
                        break;

                    case ProtoBufExample.eType.eFable:
                        ProtoBufExample.Fable lFable = ProtoBuf.Serializer.DeserializeWithLengthPrefix <ProtoBufExample.Fable>(lNetworkStream, ProtoBuf.PrefixStyle.Fixed32);
                        if (lFable == null)
                        {
                            break;
                        }
                        lHeader.data = lFable; // not necessary, but nicer

                        OnMessageFableReceived(new BtChatFableEventArgs(lHeader, lFable));
                        break;

                    default:
                        Console.WriteLine("Mayday, mayday, we are in big trouble.");
                        break;
                    }
                }
                catch (System.IO.IOException) {
                    if (_ExitLoop)
                    {
                        Console.WriteLine("user requested client shutdown");
                    }
                    else
                    {
                        Console.WriteLine("disconnected");
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
            Console.WriteLine("server: listener is shutting down");
        } //
        public void SendFable(int iClient)
        {
            if (iClient < 0 || _Clients.Count < iClient)
            {
                return;
            }
            BluetoothClient xClient = _Clients[iClient];

            if (xClient == null)
            {
                return;
            }
            ProtoBufExample.Header xHeader;
            ProtoBufExample.Book   lBook = ProtoBufExample.GetData();
            xHeader = new ProtoBufExample.Header(lBook.stories[1], ProtoBufExample.eType.eFable);

            Send(xClient, xHeader);
        }
예제 #4
0
 public BtChatFableEventArgs(ProtoBufExample.Header xHeader, ProtoBufExample.Fable xFable)
 {
     _xHeader = xHeader;
     _xFable  = xFable;
 }
예제 #5
0
 public BtChatBookEventArgs(ProtoBufExample.Header xHeader, ProtoBufExample.Book xBook)
 {
     _xHeader = xHeader;
     _xBook   = xBook;
 }