예제 #1
0
        public Task <Response> Send(Request request)
        {
            if (_sendQueue.IsAddingCompleted)
            {
                return(Task.FromCanceled <Response>(CancellationToken.None));
            }
            var resultTask = RequestTasks.NewRequest(request);

            if (_sendQueue.TryAdd(request))
            {
                return(resultTask);
            }
            RequestTasks.GetRequestTask(request.RequestId);
            return(Task.FromCanceled <Response>(CancellationToken.None));
        }
예제 #2
0
        private void Start()
        {
            _sendTask = Task.Factory.StartNew(() =>
            {
                while (!_isClosed && !_sendQueue.IsAddingCompleted)
                {
                    Request request = null;
                    try
                    {
                        request = _sendQueue.Take();
                        Codec.EncodeRequest(request, _rwStream);
                        _rwStream.Flush();
                        LastWriteTime = DateTime.UtcNow;
                    }
                    catch (Exception ex)
                    {
                        switch (ex)
                        {
                        case IOException _:
                        case ObjectDisposedException _:
                            RequestTask task;
                            if (request != null && (task = RequestTasks.GetRequestTask(request.RequestId)) != null)
                            {
                                task.Task.TrySetException(new RemotingException($"message can not send, because connection is closed . address:{_host}:{_port.ToString()}", ex));
                            }
                            Reconect();
                            log.Warn("An error happend when send message", ex);
                            break;

                        case InvalidOperationException _:
                            log.Warn("the connection has been closed", ex);
                            break;
                        }
                    }
                }

                while (_sendQueue.TryTake(out var request))
                {
                    var task = RequestTasks.GetRequestTask(request.RequestId);
                    task?.Task.TrySetException(new RemotingException($"message can not send, because connection is closed . address:{_host}:{_port.ToString()}"));
                }
            }, TaskCreationOptions.LongRunning);
            _receiveTask = Task.Factory.StartNew(() =>
            {
                while (!_isClosed)
                {
                    try
                    {
                        var response = Codec.DecodeResponse(_rwStream);
                        if (response.IsEvent)
                        {
                            log.Debug("Receive HeartBeat Response");
                        }
                        LastReadTime = DateTime.UtcNow;
                    }
                    catch (Exception ex)
                    {
                        if (ex is IOException || ex is ObjectDisposedException)
                        {
                            if (_isClosed)
                            {
                                return;
                            }
                            Reconect();
                        }
                        log.Warn("An error happend when receive message", ex);
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }