예제 #1
0
 protected void OnConnectionRequest(ConnectionRequestedEventArgs eventArgs)
 {
     byte[] bytes = eventArgs.bytes;
     if (bytes != null && bytes.Length == connectionRequestMessageLength)
     {
         try
         {
             Client.TYPE type      = Client.Identify((byte)(bytes[0]));
             string      name      = Client.typenames[type] + "_" + Encoding.ASCII.GetString(bytes, 1, 2);
             Client      newClient = Client.CreateNew(type, name, this);
             ClientConnectionRequestedEventArgs newClientEventArgs = new ClientConnectionRequestedEventArgs(newClient);
             OnClientConnectionRequested(newClientEventArgs);
         }
         catch (Exception e)
         {
             ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
             OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
             StartWaitingForConnectionRequest();
         }
     }
     else
     {
         StartWaitingForConnectionRequest();
     }
 }
예제 #2
0
        private async Task MonitorPort()
        {
            byte[] buffer    = new byte[connectionRequestMessageLength];
            byte[] data      = new byte[connectionRequestMessageLength];
            int    bytesRead = 0;

            while (bytesRead != connectionRequestMessageLength)
            {
                try
                {
                    if (!tcpClient.Connected)
                    {
                        throw new PortClosedException();
                    }
                    NetworkStream inputStream = tcpClient.GetStream();
                    int           dataLength  = 0;
                    dataLength = await inputStream.ReadAsync(buffer, 0, connectionRequestMessageLength);

                    if (dataLength == 0)
                    {
                        Close();
                        break;
                    }
                    else if (dataLength <= connectionRequestMessageLength - bytesRead)
                    {
                        Array.Copy(buffer, 0, data, bytesRead, dataLength);
                        bytesRead += dataLength;
                    }
                    else
                    {
                        Array.Copy(buffer, 0, data, bytesRead, connectionRequestMessageLength - bytesRead);
                        bytesRead = connectionRequestMessageLength;
                    }
                }
                catch (Exception e)
                {
                    ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
                    OnReadExceptionOccured(exceptionOccuredEventArgs);
                }
            }
            ConnectionRequestedEventArgs eventArgs = new ConnectionRequestedEventArgs(data);

            OnConnectionRequest(eventArgs);
        }
예제 #3
0
 private void WaitingForConnectionCallback(GattCharacteristic gattCharacteristic, GattValueChangedEventArgs eventArgs)
 {
     try
     {
         DataReader dataReader = DataReader.FromBuffer(eventArgs.CharacteristicValue);
         byte[]     data       = new byte[dataReader.UnconsumedBufferLength];
         dataReader.ReadBytes(data);
         if (data.Length == connectionRequestMessageLength)
         {
             ConnectionRequestedEventArgs conReqEventArgs = new ConnectionRequestedEventArgs(data);
             OnConnectionRequest(conReqEventArgs);
             characteristic.ValueChanged -= WaitingForConnectionCallback;
         }
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
         OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
     }
 }
예제 #4
0
        private async Task MonitorPort()
        {
            byte[] buffer    = new byte[port.ReadBufferSize];
            byte[] data      = new byte[connectionRequestMessageLength];
            int    bytesRead = 0;

            while (bytesRead != connectionRequestMessageLength)
            {
                try
                {
                    if (!port.IsOpen)
                    {
                        throw new PortClosedException();
                    }
                    int dataLength = await port.BaseStream.ReadAsync(buffer, 0, connectionRequestMessageLength);

                    if (dataLength <= connectionRequestMessageLength - bytesRead)
                    {
                        Array.Copy(buffer, 0, data, bytesRead, dataLength);
                        bytesRead += dataLength;
                    }
                    else
                    {
                        Array.Copy(buffer, 0, data, bytesRead, connectionRequestMessageLength - bytesRead);
                        bytesRead = connectionRequestMessageLength;
                    }
                }
                catch (Exception e)
                {
                    ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
                    OnWaitForConnectionRequestExceptionOccured(eventArgs);
                    break;
                }
            }

            if (bytesRead == connectionRequestMessageLength)
            {
                ConnectionRequestedEventArgs eventArgs = new ConnectionRequestedEventArgs(data);
                OnConnectionRequest(eventArgs);
            }
        }