예제 #1
0
파일: TcpListener.cs 프로젝트: rhw1111/DCEM
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            TcpAcceptContext token = (TcpAcceptContext)e.UserToken;

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

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

                //重新设置连接上下文
                token.SocketAsyncEventArgs = null;
                token.Status = 0;
                token.ReleaseReceiveSemaphore();
                _tcpAcceptContextPool.Return(token);
            }
        }
예제 #2
0
파일: TcpListener.cs 프로젝트: rhw1111/DCEM
        //数据发送处理
        private async Task ProcessSend(SocketAsyncEventArgs e)
        {
            TcpAcceptContext token = (TcpAcceptContext)e.UserToken;

            if (e.SocketError == SocketError.Success)
            {
                if (!_listener.KeepAlive)
                {
                    lock (token.LockObj)
                    {
                        token.Status = -1;
                    }
                }
                else
                {
                    token.LatestRunning = DateTime.UtcNow;
                    lock (token.LockObj)
                    {
                        token.Status = 1;
                    }
                }
            }
            else
            {
                lock (token.LockObj)
                {
                    token.Status = -1;
                }
            }
            token.ReleaseReceiveSemaphore();


            await Task.FromResult(0);
        }
예제 #3
0
파일: TcpListener.cs 프로젝트: rhw1111/DCEM
        private async Task DoProcessReceive(SocketAsyncEventArgs e, bool useSocket = false, byte[] buffer = null)
        {
            TcpAcceptContext token = (TcpAcceptContext)e.UserToken;
            var innerResult        = await InnerDoProcessReceive(e, useSocket, buffer);

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

            bool willRaiseEvent;
            //处理接收到的数据
            var strData = UTF8Encoding.UTF8.GetString(token.RetrieveBytes.ToArray());


            //创建接收处理上下文

            //检查是否是心跳包
            //心跳包的格式固定为字符串“HeartBeat”
            if (strData.ToLower() == _listener.HeartBeatSendData.ToLower())
            {
                //标识处理状态为成功,并停止计时
                token.ExecuteSuccess = true;
                token.Watch.Stop();
                await AddLog(token);


                token.LatestRunning = DateTime.UtcNow;

                var responseBytes = UTF8Encoding.UTF8.GetBytes(_emptyResponse);

                e.SetBuffer(responseBytes, 0, responseBytes.Length);

                willRaiseEvent = e.AcceptSocket.SendAsync(e);
                if (!willRaiseEvent)
                {
                    await ProcessSend(e);
                }

                return;
            }

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

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

                    if (!_tcpDataExecuteFactories.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.TcpDataExecuteTypeError,
                                DefaultFormatting = "Tcp监听{0}中,数据处理的工厂类型{1}未实现接口IFactory<ITcpDataExecute>",
                                ReplaceParameters = new List <object>()
                                {
                                    _listener.Name, _listener.ExecuteDataFactoryType
                                }
                            };

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

                        tcpDataExecuteFactory = (IFactory <ITcpDataExecute>)objTcpDataExecuteFactory;
                        _tcpDataExecuteFactories.Add(_listener.ExecuteDataFactoryType, tcpDataExecuteFactory);
                    }
                }
            }

            var tcpDataExecute = tcpDataExecuteFactory.Create();

            var byteResult = await tcpDataExecute.Execute(token.RetrieveBytes.ToArray());

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

            if (byteResult != null)
            {
                token.ResponseBytes = byteResult.ToList();
            }

            await AddLog(token);

            //发送返回值

            if (byteResult != null)
            {
                /*var bytes = UTF8Encoding.UTF8.GetBytes(strResult);
                 *
                 * var totalByteList = bytes.Length.ToBytes().ToList();
                 *
                 * totalByteList.AddRange(bytes);
                 *
                 * var totalBytes = totalByteList.ToArray();*/

                e.SetBuffer(byteResult, 0, byteResult.Length);
            }
            else
            {
                var responseBytes = UTF8Encoding.UTF8.GetBytes(_emptyResponse);
                e.SetBuffer(responseBytes, 0, responseBytes.Length);
            }


            willRaiseEvent = e.AcceptSocket.SendAsync(e);
            if (!willRaiseEvent)
            {
                await ProcessSend(e);
            }
        }
예제 #4
0
파일: TcpListener.cs 프로젝트: rhw1111/DCEM
        /// <summary>
        /// 数据接收处理
        /// </summary>
        /// <param name="e"></param>
        private async Task ProcessReceive(SocketAsyncEventArgs e)
        {
            TcpAcceptContext token = (TcpAcceptContext)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();
                    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();
                    }
                }
                                 );
            }
            else
            {
                lock (token.LockObj)
                {
                    token.Status = -1;
                }
                token.ReleaseReceiveSemaphore();
            }

            await Task.FromResult(0);
        }