Esempio n. 1
0
        public async Task Connect()
        {
            int origin = Interlocked.Exchange(ref _state, _connecting);

            if (!(origin == _none || origin == _closed))
            {
                await Close(false);

                throw new InvalidOperationException("This tcp socket client is in invalid state when connecting.");
            }

            try
            {
                Clean();

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

                if (_localEndPoint != null)
                {
                    _socket.Bind(_localEndPoint);
                }

                var saea = _saeaPool.Take();
                saea.Saea.RemoteEndPoint = _remoteEndPoint;

                var socketError = await _socket.ConnectAsync(saea);

                if (socketError != SocketError.Success)
                {
                    throw new SocketException((int)socketError);
                }

                if (_receiveBuffer == default(ArraySegment <byte>))
                {
                    _receiveBuffer = _configuration.BufferManager.BorrowBuffer();
                }
                _receiveBufferOffset = 0;

                if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting)
                {
                    await Close(false);

                    throw new InvalidOperationException("This tcp socket client is in invalid state when connected.");
                }

                Loger.DebugFormat("Connected to server [{0}] with dispatcher [{1}] on [{2}].",
                                  this.RemoteEndPoint,
                                  _dispatcher.GetType().Name,
                                  DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
                bool isErrorOccurredInUserSide = false;
                try
                {
                    await _dispatcher.OnServerConnected(this);
                }
                catch (Exception ex)
                {
                    isErrorOccurredInUserSide = true;
                    HandleUserSideError(ex);
                }

                if (!isErrorOccurredInUserSide)
                {
                    Task.Factory.StartNew(async() =>
                    {
                        await Process();
                    },
                                          TaskCreationOptions.LongRunning)
                    .Forget();
                }
                else
                {
                    await Close();
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex.Message, ex);
                throw;
            }
        }
Esempio n. 2
0
        public async Task Connect()
        {
            int origin = Interlocked.CompareExchange(ref _state, _connecting, _none);

            if (origin == _disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            else if (origin != _none)
            {
                throw new InvalidOperationException("This tcp socket client has already connected to server.");
            }

            try
            {
                _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                if (_localEndPoint != null)
                {
                    _socket.Bind(_localEndPoint);
                }

                var saea = _saeaPool.Take();
                saea.Saea.RemoteEndPoint = _remoteEndPoint;

                var socketError = await _socket.ConnectAsync(saea);

                if (socketError != SocketError.Success)
                {
                    throw new SocketException((int)socketError);
                }

                ConfigureSocket();

                _receiveBuffer       = _bufferManager.BorrowBuffer();
                _receiveBufferOffset = 0;

                if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting)
                {
                    throw new ObjectDisposedException(GetType().FullName);
                }

                _log.DebugFormat("Connected to server [{0}] with dispatcher [{1}] on [{2}].",
                                 this.RemoteEndPoint,
                                 _dispatcher.GetType().Name,
                                 DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"));
                bool isErrorOccurredInUserSide = false;
                try
                {
                    await _dispatcher.OnServerConnected(this);
                }
                catch (Exception ex)
                {
                    isErrorOccurredInUserSide = true;
                    HandleUserSideError(ex);
                }

                if (!isErrorOccurredInUserSide)
                {
                    Task.Run(async() =>
                    {
                        await Process();
                    })
                    .Forget();
                }
                else
                {
                    await Close();
                }
            }
            catch (ObjectDisposedException) { }
            catch (Exception ex)
                when(ex is TimeoutException)
                {
                    _log.Error(ex.Message, ex);
                    await Close();
                }
        }