Exemplo n.º 1
0
        private async void ReceiveDataLoop(DataReader chatReader)
        {
            try
            {
                uint size = await chatReader.LoadAsync(sizeof(uint));

                if (size < sizeof(uint))
                {
                    Disconnect("Remote device terminated connection - make sure only one instance of server is running on remote device");
                    return;
                }

                var type = DataHandler.RecognizeDataType(chatReader);
                switch (type)
                {
                case DataHandlerTypes.ByteArray:
                    DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadByteArrayAsync(chatReader), typeof(byte[]))); break;

                case DataHandlerTypes.Int32:
                    DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadInt32Async(chatReader), typeof(Int32))); break;

                case DataHandlerTypes.String:
                    DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadStringAsync(chatReader), typeof(string))); break;

                case DataHandlerTypes.IBuffer:
                    DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadIBufferAsync(chatReader), typeof(IBuffer))); break;
                }

                ReceiveDataLoop(chatReader);
            }
            catch (Exception ex)
            {
                lock (this)
                {
                    if (chatSocket == null)
                    {
                        // Do not print anything here -  the user closed the socket.
                        //if ((uint)ex.HResult == 0x80072745)
                        //rootPage.NotifyUser("Disconnect triggered by remote device", NotifyType.StatusMessage);
                        //else if ((uint)ex.HResult == 0x800703E3)
                        //rootPage.NotifyUser("The I/O operation has been aborted because of either a thread exit or an application request.", NotifyType.StatusMessage);
                    }
                    else
                    {
                        Disconnect("Read stream failed with error: " + ex.Message);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Invoked when the socket listener accepts an incoming Bluetooth connection.
        /// </summary>
        /// <param name="sender">The socket listener that accepted the connection.</param>
        /// <param name="args">The connection accept parameters, which contain the connected socket.</param>
        private async void OnConnectionReceived(
            StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            // Don't need the listener anymore
            socketListener.Dispose();
            socketListener = null;

            try
            {
                socket = args.Socket;
            }
            catch (Exception)
            {
                Disconnect();
                return;
            }

            // Note - this is the supported way to get a Bluetooth device from a given socket
            var remoteDevice = await BluetoothDevice.FromHostNameAsync(socket.Information.RemoteHostName);

            writer = new DataWriter(socket.OutputStream);
            var  reader = new DataReader(socket.InputStream);
            bool remoteDisconnection = false;

            // TODO: notify connected
            //await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            //{
            //    rootPage.NotifyUser("Connected to Client: " + remoteDevice.Name, NotifyType.StatusMessage);
            //});

            // Infinite read buffer loop
            while (true)
            {
                try
                {
                    uint size = await reader.LoadAsync(sizeof(uint));

                    if (size < sizeof(uint))
                    {
                        remoteDisconnection = true;
                        return;
                    }

                    var type = DataHandler.RecognizeDataType(reader);
                    switch (type)
                    {
                    case DataHandlerTypes.ByteArray:
                        DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadByteArrayAsync(reader), typeof(byte[]))); break;

                    case DataHandlerTypes.Int32:
                        DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadInt32Async(reader), typeof(Int32))); break;

                    case DataHandlerTypes.String:
                        DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadStringAsync(reader), typeof(string))); break;

                    case DataHandlerTypes.IBuffer:
                        DataReceived?.Invoke(this, new DataReceivedEventArgs(await DataHandler.ReadIBufferAsync(reader), typeof(IBuffer))); break;
                    }
                }
                // Catch exception HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED).
                catch (Exception ex) when((uint)ex.HResult == 0x800703E3)
                {
                    // Client Disconnected Successfully
                    break;
                }
            }

            reader.DetachStream();
            if (remoteDisconnection)
            {
                Disconnect();
                // Client Disconnected Successfully
            }
        }