예제 #1
0
        //数据发送处理
        private async Task ProcessSend(SocketAsyncEventArgs e)
        {
            TcpDuplexAcceptContext token = (TcpDuplexAcceptContext)e.UserToken;

            if (e.SocketError == SocketError.Success)
            {
                token.LatestRunning = DateTime.UtcNow;
                lock (token.LockObj)
                {
                    token.Status = 1;
                }

                token.ReleaseSendSemaphore();
            }
            else
            {
                lock (token.LockObj)
                {
                    token.Status = -1;
                }
                token.ReleaseReceiveSemaphore();
                token.ReleaseSendSemaphore();
            }

            await Task.FromResult(0);
        }
예제 #2
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            TcpDuplexAcceptContext token = (TcpDuplexAcceptContext)e.UserToken;

            lock (token)
            {
                Socket currentSocket = e.AcceptSocket;

                try
                {
                    if (currentSocket != null)
                    {
                        currentSocket.Close();
                    }
                }
                catch
                {
                }

                //重新设置连接上下文
                token.SocketAsyncEventArgs = null;
                token.Status = 0;
                token.RequestData.Clear();
                token.ReleaseReceiveSemaphore();
                token.ReleaseSendSemaphore();
                _tcpDuplexAcceptContextPool.Return(token);
            }
        }
예제 #3
0
        private async Task DoProcessReceive(SocketAsyncEventArgs e, bool useSocket = false, byte[] buffer = null)
        {
            TcpDuplexAcceptContext token = (TcpDuplexAcceptContext)e.UserToken;
            var innerResult = await InnerDoProcessReceive(e, useSocket, buffer);

            while (!innerResult.Complete)
            {
                innerResult = await InnerDoProcessReceive(e, true, innerResult.Buffer);
            }



            //如果收到的是空字节,表示客户端已经关闭,服务端也需要关闭
            if (token.RequestData.Count == 0)
            {
                lock (token.LockObj)
                {
                    token.Status = -1;
                }
                token.ReleaseReceiveSemaphore();
                token.ReleaseSendSemaphore();
                return;
            }

            if (!_tcpDuplexDataExecuteFactories.TryGetValue(_listener.ExecuteDataFactoryType, out IFactory <ITcpDuplexDataExecute> tcpDataExecuteFactory))
            {
                lock (_tcpDuplexDataExecuteFactories)
                {
                    Type tcpDataExecuteFactoryType = Type.GetType(_listener.ExecuteDataFactoryType);

                    if (!_tcpDuplexDataExecuteFactories.TryGetValue(_listener.ExecuteDataFactoryType, out tcpDataExecuteFactory))
                    {
                        object objTcpDataExecuteFactory;
                        if (_listener.ExecuteDataFactoryTypeUseDI == true)
                        {
                            //通过DI容器创建
                            objTcpDataExecuteFactory = DIContainerContainer.Get(tcpDataExecuteFactoryType);
                        }
                        else
                        {
                            //通过反射创建
                            objTcpDataExecuteFactory = tcpDataExecuteFactoryType.Assembly.CreateInstance(tcpDataExecuteFactoryType.FullName);
                        }

                        if (!(objTcpDataExecuteFactory is IFactory <ITcpDataExecute>))
                        {
                            var fragment = new TextFragment()
                            {
                                Code = TextCodes.TcpDuplexDataExecuteTypeError,
                                DefaultFormatting = "双工Tcp监听{0}中,数据处理的工厂类型{1}未实现接口IFactory<ITcpDuplexDataExecute>",
                                ReplaceParameters = new List <object>()
                                {
                                    _listener.Name, _listener.ExecuteDataFactoryType
                                }
                            };

                            throw new UtilityException((int)Errors.TcpDuplexDataExecuteTypeError, fragment);
                        }

                        tcpDataExecuteFactory = (IFactory <ITcpDuplexDataExecute>)objTcpDataExecuteFactory;
                        _tcpDuplexDataExecuteFactories.Add(_listener.ExecuteDataFactoryType, tcpDataExecuteFactory);
                    }
                }
            }

            var tcpDataExecute = tcpDataExecuteFactory.Create();

            var byteResult = await tcpDataExecute.Execute(token.RequestData.ToArray(), token.Connection, GetConnections());

            //标识处理状态为成功,并停止计时
            token.ExecuteSuccess = true;
            token.Watch.Stop();

            await AddLog(token);

            //更新状态,解除接收锁
            token.LatestRunning = DateTime.UtcNow;
            token.RequestData.Clear();
            token.RequestData.AddRange(byteResult.RestRequestData);
            lock (token.LockObj)
            {
                token.Status = 1;
            }
            token.ReleaseReceiveSemaphore();



            //向要发送的连接发送返回信息
            if (byteResult.ResponseData != null && byteResult.ResponseData.Length > 0)
            {
                List <TcpDuplexAcceptContext> sendContexts = null;
                if (byteResult.ResponseAll)
                {
                    sendContexts = GetAllAcceptContext();
                }
                else
                {
                    sendContexts = GetAcceptContext(byteResult.ResponseConnections);
                }

                if (sendContexts != null)
                {
                    foreach (var itemSendContext in sendContexts)
                    {
                        await itemSendContext.ReceiveSemaphore.WaitAsync();

                        itemSendContext.SocketAsyncEventArgs.SetBuffer(byteResult.ResponseData, 0, byteResult.ResponseData.Length);

                        try
                        {
                            var willRaiseEvent = itemSendContext.SocketAsyncEventArgs.AcceptSocket.SendAsync(itemSendContext.SocketAsyncEventArgs);
                            if (!willRaiseEvent)
                            {
                                await ProcessSend(itemSendContext.SocketAsyncEventArgs);
                            }
                        }
                        catch (Exception ex)
                        {
                            CloseClientSocket(itemSendContext.SocketAsyncEventArgs);
                            await AddLog($"Send Error,message:{ex.Message},stack:{ex.StackTrace}", DateTime.UtcNow);

                            LoggerHelper.LogError(LogCategoryName, $"TcpDuplexListener {_listener.Name},Send Error,message:{ex.Message},stack:{ex.StackTrace}");
                        }
                    }
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 数据接收处理
        /// </summary>
        /// <param name="e"></param>
        private async Task ProcessReceive(SocketAsyncEventArgs e)
        {
            TcpDuplexAcceptContext token = (TcpDuplexAcceptContext)e.UserToken;



            //从开始接受数据计时,默认执行状态设置为false
            token.ExecuteSuccess  = false;
            token.ExecuteError    = string.Empty;
            token.RequestDateTime = DateTime.UtcNow;
            token.Watch           = new Stopwatch();
            token.Watch.Start();



            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                //判断是否已经处于关闭状态,是,直接关闭,否,将状态改为正在处理
                bool needClose = false;
                lock (token.LockObj)
                {
                    if (token.Status == -1)
                    {
                        needClose = true;
                    }
                    else
                    {
                        token.Status        = 2;
                        token.LatestRunning = DateTime.UtcNow;
                    }
                }

                if (needClose)
                {
                    token.ReleaseReceiveSemaphore();
                    token.ReleaseSendSemaphore();
                    return;
                }

                var t = Task.Run(async() =>
                {
                    try
                    {
                        await DoProcessReceive(e);
                    }
                    catch (Exception ex)
                    {
                        token.ExecuteError = $"error message:{ex.Message},stack:{ex.StackTrace}";
                        token.Watch.Stop();
                        lock (token.LockObj)
                        {
                            token.Status = -1;
                        }
                        await AddLog(token);
                        token.ReleaseReceiveSemaphore();
                        token.ReleaseSendSemaphore();
                    }
                }
                                 );
            }
            else
            {
                lock (token.LockObj)
                {
                    token.Status = -1;
                }
                token.ReleaseReceiveSemaphore();
                token.ReleaseSendSemaphore();
            }

            await Task.FromResult(0);
        }