コード例 #1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task <byte[]> Send(TcpClientEndpoint endpoint, byte[] data)
        {
            //初始化终结点
            await InitEndpoint(endpoint);

            TcpClientContext context = null;

            //从池中获取Tcp客户端上下文
            try
            {
                context = await _tcpClientContextPool.GetAsync(true);

                //判断是否已经断开连接,如果已经断开连接,则直接抛出异常
                if (context.ConnectionIsClose)
                {
                    throw context.MainException;
                }

                return(await context.Send(data));
            }
            finally
            {
                if (context != null)
                {
                    _tcpClientContextPool.Return(context);
                }
            }
        }
コード例 #2
0
        public async Task Dispose(TcpClientEndpoint endpoint)
        {
            //将池中的每个上下文都执行Dispose操作
            _start = false;
            var contextItems = _tcpClientContextPool.Items;

            foreach (var item in contextItems)
            {
                if (!item.Value.ConnectionIsClose)
                {
                    await item.Value.Dispose();
                }
            }
        }
コード例 #3
0
        public TcpClientContext(TcpClientEndpoint endpoint, ILoggerFactory loggerFactory, string logCategoryName)
        {
            _loggerFactory   = loggerFactory;
            _logCategoryName = logCategoryName;
            _endpoint        = endpoint;
            IPEndPoint serverPoint = new IPEndPoint(IPAddress.Parse(_endpoint.ServerAddress), _endpoint.ServerPort);

            _tcpClient = new Socket(SocketType.Stream, ProtocolType.Tcp);

            _receiveBytes = new List <byte>();

            _args = new SocketAsyncEventArgs()
            {
                UserToken      = this,
                RemoteEndPoint = serverPoint
            };

            _args.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);

            _connectSemaphore = new SemaphoreSlim(0, 1);
            _sendSemaphore    = new SemaphoreSlim(0, 1);
            _receiveSemaphore = new SemaphoreSlim(0, 1);
            ConnectionIsClose = false;
        }
コード例 #4
0
        /// <summary>
        /// 初始化终结点
        /// </summary>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        private async Task InitEndpoint(TcpClientEndpoint endpoint)
        {
            if (!_start)
            {
                _start    = true;
                _endpoint = endpoint;
                try
                {
                    _tcpClientContextPool = new Pool <TcpClientContext>($"TcpClient-{endpoint.Name}",
                                                                        null,
                                                                        null,
                                                                        null,
                                                                        null,
                                                                        async() =>
                    {
                        TcpClientContext context = new TcpClientContext(endpoint, _loggerFactory, _logCategoryName);
                        await context.Init();
                        return(context);
                    },
                                                                        async(context) =>
                    {
                        if (context.ConnectionIsClose)
                        {
                            return(await Task.FromResult(false));
                        }
                        else
                        {
                            try
                            {
                                await context.Send(UTF8Encoding.UTF8.GetBytes(_endpoint.HeartBeatSendData));
                            }
                            catch (Exception ex)
                            {
                                LoggerHelper.LogError(_logCategoryName, $"TcpClientEndpoint {_endpoint.Name} send HeartBeat error,server address:{_endpoint.ServerAddress},port:{_endpoint.ServerPort.ToString()},error message:{ex.Message},stack:{ex.StackTrace}");
                            }

                            if (context.ConnectionIsClose)
                            {
                                return(await Task.FromResult(false));
                            }
                            else
                            {
                                return(await Task.FromResult(true));
                            }
                        }
                    },
                                                                        null,
                                                                        null,
                                                                        endpoint.PoolMaxSize
                                                                        );

                    if (_endpoint.KeepAlive)
                    {
                        //启动一个循环执行的方法,每隔一秒为所有可用的连接发送心跳信息
                        var t = Task.Run(async() =>
                        {
                            while (_start)
                            {
                                await _tcpClientContextPool.InvokeEveryUnUseItem(async(context) =>
                                {
                                    if (!context.ConnectionIsClose)
                                    {
                                        try
                                        {
                                            await context.Send(UTF8Encoding.UTF8.GetBytes(_endpoint.HeartBeatSendData));
                                        }
                                        catch (Exception ex)
                                        {
                                            LoggerHelper.LogError(_logCategoryName, $"TcpClientEndpoint {_endpoint.Name} send HeartBeat error,server address:{_endpoint.ServerAddress},port:{_endpoint.ServerPort.ToString()},error message:{ex.Message},stack:{ex.StackTrace}");
                                        }
                                    }
                                });

                                System.Threading.Thread.Sleep(1000);
                            }
                        });
                    }
                }
                catch
                {
                    _start = false;
                    throw;
                }
            }

            await Task.FromResult(0);
        }
コード例 #5
0
 public async Task Update(TcpClientEndpoint endpoint)
 {
     await _tcpClientEndpointStore.Update(endpoint);
 }
コード例 #6
0
 public async Task Delete(TcpClientEndpoint endpoint)
 {
     await _tcpClientEndpointStore.Delete(endpoint.ID);
 }
コード例 #7
0
 public async Task Add(TcpClientEndpoint endpoint)
 {
     await _tcpClientEndpointStore.Add(endpoint);
 }