示例#1
0
        /// <summary>
        /// If the server IP address changes during a session, we use the broadcast to try to adjust.
        /// If the server broadcast does not reach our subnet, and the IP address changes, we won't be
        /// able to reconnect.
        /// </summary>
        private void UpdateRemoteEndpoint()
        {
            if (m_ServerId.Equals(Guid.Empty))
            {
                return;
            }

            IPEndPoint[] endpoints = BroadcastListener.GuidToEndPoints(m_ServerId);

            if (endpoints.Length == 0)
            {
                return;
            }

            foreach (IPEndPoint ep in endpoints)
            {
                if (m_RemoteEP.Equals(ep))
                {
                    //Old endpoint is still valid
                    return;
                }
            }

            if (endpoints.Length > 1)
            {
                Trace.WriteLine("Warning: Updating server endpoint by arbitrarily picking the first one of " +
                                endpoints.Length.ToString(), this.GetType().ToString());
            }

            m_RemoteEP = endpoints[0];
            Trace.WriteLine("TCPClient updating server endpoint to: " + m_RemoteEP.ToString(), this.GetType().ToString());
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BluetoothLE.Droid.Adapter"/> class.
        /// </summary>
        public Adapter()
        {
            var appContext = Android.App.Application.Context;

            _manager = (BluetoothManager)appContext.GetSystemService(Context.BluetoothService);
            _adapter = _manager.Adapter;

            _callback = new GattCallback();
            _callback.DeviceConnected    += BluetoothGatt_DeviceConnected;
            _callback.DeviceDisconnected += BluetoothGatt_DeviceDisconnected;

            _advertiseCallback = new AdvertiseCallback();
            _advertiseCallback.AdvertiseStartFailed  += BluetoothGatt_AdvertiseStartFailed;
            _advertiseCallback.AdvertiseStartSuccess += Bluetooth_AdvertiseStartSuccess;

            var callback = new GattServerCallback();

            _gattServer        = _manager.OpenGattServer(appContext, callback);
            callback.Server    = _gattServer;
            _broadcastListener = new BroadcastListener();

            CharacteristicsFactory = new CharacteristicsFactory();
            ServiceFactory         = new ServiceFactory();

            PeripheralStateChanged = _broadcastListener.StateUpdatedSubject.Select(state =>
            {
                if (state == ManagerState.PoweredOn)
                {
                    if (_adapter.BluetoothLeAdvertiser == null)
                    {
                        return(ManagerState.Unsupported);
                    }
                    if (!_adapter.IsMultipleAdvertisementSupported)
                    {
                        return(ManagerState.PartialSupport);
                    }
                }
                return(state);
            }).AsObservable();

            CentralStateChanged = _broadcastListener.StateUpdatedSubject.Select(state =>
            {
                if (state == ManagerState.PoweredOn)
                {
                    if (_adapter.BluetoothLeScanner == null)
                    {
                        return(ManagerState.Unsupported);
                    }
                    if (!(_adapter.IsOffloadedFilteringSupported && _adapter.IsOffloadedScanBatchingSupported))
                    {
                        return(ManagerState.PartialSupport);
                    }
                }

                return(state);
            }).AsObservable();

            _peripheralStateDisposable = PeripheralStateChanged.Subscribe(state => _peripheralState = state);
            _centralStateDisposable    = CentralStateChanged.Subscribe(state => _centralState = state);
        }
 public BroadcastConnectionEstablishmentStrategy(int localPort)
 {
     LocalPort                = localPort;
     broadcastClient          = new BroadcastClient(localPort);
     listener                 = new BroadcastListener(broadcastClient);
     listener.OnDataReceived += OnDataReceived;
     broadcaster              = new BroadcastTransmitter(broadcastClient);
 }
        static void Main(string[] args)
        {
            BroadcastListener listener = new BroadcastListener(8409, Global.MsgSplitter, BoardcastMsgAnalysis.GetReply,
                                                               new Func <string, string>(t => { Console.WriteLine(t); return(string.Empty); }));

            listener.StartListening();

            Console.ReadKey();
        }
示例#5
0
    public static void addListener(Action<object> action, string messageName)
    {
        if(action == null || messageName == null) return;

        BroadcastListener bl = new BroadcastListener();
        bl.action = action;
        bl.messageName = messageName;
        bl.shouldRemove = false;
        broadcastListeners.Add(bl);
        Debug.Log("Listener added for message: " + messageName + " " + action.ToString());
    }
 private void Awake()
 {
     if (Instance == null)
     {
         Instance = this;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }
示例#7
0
 public void Register(BroadcastListener o)
 {
     m_Listeners.Add(o);
 }
 void Start()
 {
     //StartCoroutine(TestBroadcast(2f));
     listener = BroadcastListener.Instance;
 }