コード例 #1
0
        /// <summary>
        /// 使用底层的数据报文来通讯,传入需要发送的消息,返回最终的数据结果
        /// </summary>
        /// <param name="send">发送的数据</param>
        /// <returns>结果对象</returns>
        public OperateResult <byte[], byte[]> ReadFromCoreServer(byte[] send)
        {
            var result = new OperateResult <byte[], byte[]>();

            // LogNet?.WriteDebug( ToString( ), "Command: " + BasicFramework.SoftBasic.ByteToHexString( send ) );
            InteractiveLock.Enter( );

            // 获取有用的网络通道,如果没有,就建立新的连接
            OperateResult <Socket> resultSocket = GetAvailableSocket( );

            if (!resultSocket.IsSuccess)
            {
                IsSocketErrorState = false;
                InteractiveLock.Leave( );
                result.CopyErrorFromOther(resultSocket);
                return(result);
            }

            // 发送数据信息
            OperateResult resultSend = Send(resultSocket.Content, send);

            if (!resultSend.IsSuccess)
            {
                IsSocketErrorState = false;
                InteractiveLock.Leave( );
                resultSocket.Content?.Close( );
                result.CopyErrorFromOther(resultSend);
                return(result);
            }

            // 接收超时时间大于0时才允许接收远程的数据
            if (receiveTimeOut >= 0)
            {
                // 接收数据信息
                OperateResult <TNetMessage> resultReceive = ReceiveMessage(resultSocket.Content);
                if (!resultReceive.IsSuccess)
                {
                    IsSocketErrorState = false;
                    InteractiveLock.Leave();
                    resultSocket.Content?.Close();
                    result.CopyErrorFromOther(resultReceive);
                    return(result);
                }

                // 复制结果
                result.Content1 = resultReceive.Content.HeadBytes;
                result.Content2 = resultReceive.Content.ContentBytes;
            }

            if (!IsPersistentConn)
            {
                resultSocket.Content?.Close( );
            }
            InteractiveLock.Leave( );

            IsSocketErrorState = true;
            result.IsSuccess   = true;
            return(result);
        }
コード例 #2
0
        private void ThreadBackground()
        {
            while (true)
            {
                // 检测是否处于暂停的状态
                while (m_isRunningStop)
                {
                    ;
                }
                // 提取处理的任务
                int index = Interlocked.Decrement(ref m_opCount);
                if (index < 0)
                {
                    // 任务完成
                    break;
                }
                else
                {
                    T    item        = m_dataList[index];
                    bool result      = false;
                    bool isException = false;
                    try
                    {
                        if (!m_isQuit)
                        {
                            result = m_operater(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        isException = true;
                        // 此处必须吞噬所有异常
                        OnExceptionOccur?.Invoke(item, ex);

                        // 是否需要退出处理
                        if (m_isQuitAfterException)
                        {
                            EndedOperater();
                        }
                    }
                    finally
                    {
                        // 保证了报告进度时数据的正确性
                        HybirdLock.Enter();

                        if (result)
                        {
                            m_successCount++;
                        }
                        if (isException)
                        {
                            m_failedCount++;
                        }
                        m_finishCount++;
                        OnReportProgress?.Invoke(m_finishCount, m_dataList.Length, m_successCount, m_failedCount);

                        HybirdLock.Leave();
                    }
                }
            }
            JustEnded();
        }