Beispiel #1
0
        private void ProcessDisconnectCallback(SocketAsyncEventArgs e)
        {
            SocketToken sToken = e.UserToken as SocketToken;

            if (sToken == null)
            {
                return;// throw new Exception("空异常");
            }

            try
            {
                sToken.Close();
                //递减信号量
                maxNumberAcceptedClients.Release();

                Interlocked.Decrement(ref numberOfConnections);
                if (sToken.TokenId != 0)
                {
                    //将断开的对象重新放回复用队列
                    acceptTokenManager.Set(e);
                }

                DisconnectedCallback?.Invoke(sToken);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理接收的数据
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            try
            {
                if (stoped)
                {
                    if (e.ConnectSocket != null)
                    {
                        e.ConnectSocket.Close();
                    }
                    if (e.AcceptSocket != null)
                    {
                        e.AcceptSocket.Close();
                    }
                    acceptPool.Push(e);
                    return;
                }

                SocketToken sToken = e.UserToken as SocketToken;

                if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
                {
                    ReceiveOffsetCallback?.Invoke(sToken, e.Buffer, e.Offset, e.BytesTransferred);

                    //处理接收到的数据
                    if (ReceivedCallback != null)
                    {
                        if (e.Offset == 0 && e.BytesTransferred == e.Buffer.Length)
                        {
                            ReceivedCallback(sToken, e.Buffer);
                        }
                        else
                        {
                            byte[] realBytes = new byte[e.BytesTransferred];
                            Buffer.BlockCopy(e.Buffer, e.Offset, realBytes, 0, e.BytesTransferred);
                            ReceivedCallback(sToken, realBytes);
                        }
                    }

                    //继续投递下一个接受请求
                    if (!sToken.TokenSocket.ReceiveAsync(e))
                    {
                        this.ProcessReceive(e);
                    }
                }
                else
                {
                    //关闭异常对象
                    CloseClientSocket(sToken);

                    DisconnectedCallback?.Invoke(sToken);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 处理接收数据之后的事件
        /// </summary>
        /// <param name="e"></param>
        private void ProcessReceiveHandler(SocketAsyncEventArgs e)
        {
            try
            {
                if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
                {
                    ReceiveOffsetCallback?.Invoke(e.UserToken as SocketToken, e.Buffer, e.Offset, e.BytesTransferred);

                    if (RecievedCallback != null)
                    {
                        if (e.Offset == 0 && e.BytesTransferred == e.Buffer.Length)
                        {
                            RecievedCallback(e.UserToken as SocketToken, e.Buffer);
                        }
                        else
                        {
                            byte[] realBytes = new byte[e.BytesTransferred];

                            Buffer.BlockCopy(e.Buffer, e.Offset, realBytes, 0, e.BytesTransferred);
                            RecievedCallback(e.UserToken as SocketToken, realBytes);
                        }
                    }


                    if (!isConnected)
                    {
                        return;
                    }

                    if (!clientSocket.ReceiveAsync(e))
                    {
                        ProcessReceiveHandler(e);
                    }
                }
                else
                {
                    isConnected = false;

                    DisconnectedCallback?.Invoke(e.UserToken as SocketToken);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Disconnects from the assigned port.
        /// </summary>
        /// <exception cref="IOException">If disconnecting fails.</exception>
        override public void Disconnect()
        {
            if (!isConnected)
            {
                return;
            }

            isConnected = false;
            try
            {
                arduinoSerial.Close();
            }
            finally
            {
                DisconnectedCallback?.Invoke();
            }
        }
Beispiel #5
0
        private void CheckConnection()
        {
            runChecker       = true;
            refreshRequested = true;

            while (runChecker)
            {
                if (Monitor.TryEnter(arduinoConnection))
                {
                    try
                    {
                        if (!Send("1000"))
                        {
                            DisconnectedCallback?.Invoke();
                            TryConnect();
                            IsConnected = false;
                        }
                        else
                        {
                            ConnectedCallback?.Invoke();
                            IsConnected = true;
                        }
                    }
                    finally
                    {
                        if (Monitor.IsEntered(arduinoConnection))
                        {
                            Monitor.Exit(arduinoConnection);
                        }
                    }
                }

                int sleeptFor = 0;
                while (++sleeptFor < 2000)
                {
                    if (refreshRequested)
                    {
                        refreshRequested = false;
                        break;
                    }
                    Thread.Sleep(1);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// 处理断开连接
        /// </summary>
        /// <param name="e"></param>
        private void ProcessDisconnect(SocketAsyncEventArgs e)
        {
            try
            {
                Interlocked.Decrement(ref numberOfConnections);

                //递减信号量
                acceptSemphoreClients.Release();

                //将断开的对象重新放回复用队列
                acceptPool.Push(e);

                DisconnectedCallback?.Invoke(e.UserToken as SocketToken);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }