예제 #1
0
        /// <summary>
        /// Ends a pending asynchronous recieve.
        /// </summary>
        /// <param name="asyncResult">An System.IAsyncResult that stores state information
        /// for this asynchronous operation.</param>
        /// <exception cref="System.ArgumentException">
        /// asyncResult was not returned by a call to OpenWiz.WizSocket.BeginRecieve(System.AsyncCallback,System.Object)
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// OpenWiz.WizSocket.EndReceive(System.IAsyncResult) was previously called
        /// for the asynchronous receive.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// asyncResult or handle is null.
        /// </exception>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been closed.
        /// </exception>
        /// <returns>If successful, the OpenWiz.WizState;
        /// otherwise, an invalid System.Net.Sockets.Socket error.</returns>
        ///
        public WizState EndReceiveFrom(WizHandle handle, IAsyncResult result)
        {
            EndPoint ep   = new IPEndPoint(handle.Ip, 0);
            int      rLen = socket.EndReceiveFrom(result, ref ep);

            byte[] buffer = bufferMap[handle.Ip.ToString()];
            bufferMap.Remove(handle.Ip.ToString());

            return(WizState.Parse(new ArraySegment <byte>(buffer, 0, rLen)));
        }
예제 #2
0
        /// <summary>
        /// Receives data from a bound OpenWiz.WizSocket.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        /// handle is null.
        /// </exception>
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access the underlying socket.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        /// The underlying socket has been disposed.
        /// </exception>
        /// <exception cref="System.Security.SecurityException">
        /// A caller in the call stack does not have the required permissions.
        /// </exception>
        /// <returns>The remote state.</returns>
        ///
        public WizState ReceiveFrom(WizHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException("handle cannot be null");
            }
            byte[] buffer = new byte[BUFFER_SIZE];

            EndPoint ep   = new IPEndPoint(handle.Ip, 0);
            int      rLen = socket.ReceiveFrom(buffer, ref ep);

            return(WizState.Parse(new ArraySegment <byte>(buffer, 0, rLen)));
        }
        /// Responsible for handling responses from Wiz lights
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (!KeepAlive)
            {
                return;
            }

            IPEndPoint ep = new IPEndPoint(IPAddress.Any, PORT_DISCOVERY);

            byte[]   data       = discoveryClient.EndReceive(ar, ref ep);
            string   jsonString = Encoding.UTF8.GetString(data);
            WizState wState     = WizState.Parse(data);

            Action <WizHandle> discoveryAction = (Action <WizHandle>)ar.AsyncState;

            if (wState == null)
            {
                Debug.WriteLine($"[WARNING] WizDiscoveryService@{hostIp}: Got bad json message:");
                Debug.WriteLine($"\t{jsonString}");
            }
            else if (wState.Error != null)
            {
                Debug.Write($"[WARNING] WizDiscoveryService@{hostIp}: Encountered ");

                if (wState.Error.Code == null)
                {
                    Debug.Write("unknwon error");
                }
                else
                {
                    Debug.Write($"error {wState.Error.Code}");
                }
                if (wState.Error.Message != null)
                {
                    Debug.Write($" -- {wState.Error.Message}");
                }
                Debug.WriteLine($" from {ep.Address}");
                Debug.WriteLine($"\t{jsonString}");
            }
            else if (wState.Result != null)
            {
                Debug.WriteLine($"[INFO] WizDiscoveryService@{hostIp}: Got response:");
                Debug.WriteLine($"\t{jsonString}");
                WizHandle handle = new WizHandle(wState.Result.Mac, ep.Address);
                discoveryAction.Invoke(handle);
            }

            discoveryClient.BeginReceive(new AsyncCallback(ReceiveCallback), discoveryAction);
        }