Exemplo n.º 1
0
        /// <summary>
        /// Constructor for a single mikroBUS socket.
        /// </summary>
        /// <param name="num">Socket number.</param>
        /// <param name="shield">mikroBUS shield kind.
        /// <c>Shield.Kinds.Unknown</c> indicates automatic detection using
        /// using the <c>Shield.kind</c> property.</param>
        public Socket(int num, Shield.Kinds shield = Shield.Kinds.Unknown)
        {
            if (shield == Shield.Kinds.Unknown)
            {
                shield = Shield.kind;
            }

            // Search for matching shield and socket number

            for (int i = 0; i < SocketTable.Length; i++)
            {
                if ((SocketTable[i].shield == shield) &&
                    (SocketTable[i].num == num))
                {
                    myInfo = SocketTable[i];

                    if (myInfo.ShareI2C && (Shield.I2CBus == null))
                    {
                        // Initialize the shared I2C bus object

                        Shield.I2CBus =
                            new IO.Objects.libsimpleio.I2C.Bus(myInfo.I2CBus);
                    }

                    return;
                }
            }

            throw new System.Exception("Unable to find matching shield and socket number.");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor for a single mikroBUS socket.
        /// </summary>
        /// <param name="num">Socket number.</param>
        /// <param name="shield">mikroBUS shield kind.  Zero
        /// indicates automatic detection using the <c>Shield.kind</c>
        /// property.</param>
        public Socket(int num, Shield.Kinds shield = Shield.Kinds.Unknown)
        {
            if (shield == Shield.Kinds.Unknown)
            {
                shield = Shield.kind;
            }

            // Search for matching shield and socket number

            for (int i = 0; i < SocketTable.Length; i++)
            {
                if ((SocketTable[i].shield == shield) &&
                    (SocketTable[i].num == num))
                {
                    myInfo = SocketTable[i];
                    return;
                }
            }

            throw new System.Exception("Unable to find matching shield and socket number.");
        }
        // 新しいソケットをキューに入れてから、ソケットが切断されるまで待機する
        public async Task <bool> InjectAndWaitAsync(TSocket newSocket)
        {
            if (newSocket == null)
            {
                return(false);
            }

            SocketEntry?sockEntry = null;

            lock (Lock)
            {
                if (AcceptedQueue.Count >= this.BackLog)
                {
                    // バックログがいっぱいです
                    Dbg.Where($"Backlog exceeded: {AcceptedQueue.Count} >= {this.BackLog}");
                    return(false);
                }

                if (this.IsCanceled)
                {
                    // 終了されようとしている
                    return(false);
                }

                AcceptedQueue.Enqueue(sockEntry = new SocketEntry(newSocket));
            }

            AcceptedEvent.Set();

            if (sockEntry.Socket.IsDisposed)
            {
                // すでに Dispose されていた
                return(false);
            }

            // このソケットがユーザーによって Dispose されるまでの間待機する
            await sockEntry.DisconnectedEvent.WaitAsync(timeout : Timeout.Infinite, cancel : this.GrandCancel);

            return(true);
        }