public TcpDefaultAuthRequest(TcpConnectionManager manager, Guid correlationId, UserCredentials userCredentials)
     : base(userCredentials.Login, userCredentials.Password)
 {
     _manager         = manager;
     _correlationId   = correlationId;
     _userCredentials = userCredentials;
 }
Beispiel #2
0
        public Message UnwrapPackage(TcpPackage package, IEnvelope envelope, IPrincipal user, string login, string pass,
                                     TcpConnectionManager connection, byte version)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException("envelope");
            }

            var unwrapper = _unwrappers[version][(byte)package.Command];

            if (unwrapper == null)
            {
                unwrapper = _unwrappers[_unwrappers.Length - 1][(byte)package.Command];
            }

            if (unwrapper != null)
            {
                try {
                    return(unwrapper(package, envelope, user, login, pass, connection));
                } catch (Exception exc) {
                    Log.ErrorException(exc, "Error while unwrapping TcpPackage with command {command}.",
                                       package.Command);
                }
            }

            return(null);
        }
 public SendOverTcpEnvelope(TcpConnectionManager manager, IPublisher networkSendQueue)
 {
     Ensure.NotNull(manager, "manager");
     Ensure.NotNull(networkSendQueue, "networkSendQueue");
     _networkSendQueue = networkSendQueue;
     _manager          = manager;
 }
Beispiel #4
0
        private void OnConnectionAccepted(IPEndPoint endPoint, Socket socket)
        {
            var conn = _securityType == TcpSecurityType.Secure
                                ? TcpConnectionSsl.CreateServerFromSocket(Guid.NewGuid(), endPoint, socket, _certificate, verbose: true)
                                : TcpConnection.CreateAcceptedTcpConnection(Guid.NewGuid(), endPoint, socket, verbose: true);

            Log.Information(
                "{serviceType} TCP connection accepted: [{securityType}, {remoteEndPoint}, L{localEndPoint}, {connectionId:B}].",
                _serviceType, _securityType, conn.RemoteEndPoint, conn.LocalEndPoint, conn.ConnectionId);

            var dispatcher = _dispatcherFactory(conn.ConnectionId, _serverEndPoint);
            var manager    = new TcpConnectionManager(
                string.Format("{0}-{1}", _serviceType.ToString().ToLower(), _securityType.ToString().ToLower()),
                _serviceType,
                dispatcher,
                _publisher,
                conn,
                _networkSendQueue,
                _authProvider,
                _heartbeatInterval,
                _heartbeatTimeout,
                (m, e) => _publisher.Publish(new TcpMessage.ConnectionClosed(m, e)),
                _connectionPendingSendBytesThreshold,
                _connectionQueueSizeThreshold);                 // TODO AN: race condition

            _publisher.Publish(new TcpMessage.ConnectionEstablished(manager));
            manager.StartReceiving();
        }
Beispiel #5
0
        private ClientMessage.UpdatePersistentSubscription UnwrapUpdatePersistentSubscription(
            TcpPackage package, IEnvelope envelope, IPrincipal user, string username, string password,
            TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.UpdatePersistentSubscription>();

            if (dto == null)
            {
                return(null);
            }

            var namedConsumerStrategy = dto.NamedConsumerStrategy;

            if (string.IsNullOrEmpty(namedConsumerStrategy))
            {
                namedConsumerStrategy = dto.PreferRoundRobin
                  ? SystemConsumerStrategies.RoundRobin
                  : SystemConsumerStrategies.DispatchToSingle;
            }

            return(new ClientMessage.UpdatePersistentSubscription(Guid.NewGuid(), package.CorrelationId, envelope,
                                                                  dto.EventStreamId, dto.SubscriptionGroupName, dto.ResolveLinkTos, dto.StartFrom,
                                                                  dto.MessageTimeoutMilliseconds,
                                                                  dto.RecordStatistics, dto.MaxRetryCount, dto.BufferSize, dto.LiveBufferSize,
                                                                  dto.ReadBatchSize, dto.CheckpointAfterTime, dto.CheckpointMinCount,
                                                                  dto.CheckpointMaxCount, dto.SubscriberMaxCount, namedConsumerStrategy,
                                                                  user, username, password));
        }
Beispiel #6
0
 public SendOverTcpEnvelope(TcpConnectionManager manager, IPublisher networkSendQueue)
 {
     _networkSendQueue = networkSendQueue;
     Ensure.NotNull(manager, "manager");
     Ensure.NotNull(networkSendQueue, "networkSendQueue");
     _manager = new WeakReference(manager);
 }
 public SendOverTcpEnvelope(TcpConnectionManager manager)
 {
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     _manager = new WeakReference(manager);
 }
 public TcpDefaultAuthRequest(TcpConnectionManager manager, Guid correlationId,
                              UserCredentials userCredentials)
     : base($"(TCP) {manager.RemoteEndPoint}", userCredentials.Tokens)
 {
     _manager         = manager;
     _correlationId   = correlationId;
     _userCredentials = userCredentials;
 }
Beispiel #9
0
        public Message UnwrapPackage(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            if (envelope == null)
                throw new ArgumentNullException("envelope");

            Func<TcpPackage, IEnvelope, TcpConnectionManager, Message> unwrapper = _unwrappers[(byte)package.Command];
            return unwrapper == null ? null : unwrapper(package, envelope, connection);
        }
Beispiel #10
0
        private void OnConnectionAccepted(TcpConnection connection)
        {
            Log.Info("Client TCP connection accepted: [{0}]", connection.EffectiveEndPoint);
            var manager = new TcpConnectionManager("REPLICA", _tcpDispatcher, _publisher, connection);

            manager.ConnectionClosed += OnConnectionClosed;

            _publisher.Publish(new TcpMessage.ConnectionEstablished(manager));
            manager.StartReceiving();
        }
        private ReplicationMessage.ReplicaSubscribed UnwrapReplicaSubscribed(TcpPackage package, IEnvelope envelope,
                                                                             TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.ReplicaSubscribed>();

            return(new ReplicationMessage.ReplicaSubscribed(new Guid(dto.LeaderId),
                                                            new Guid(dto.SubscriptionId),
                                                            dto.SubscriptionPosition,
                                                            connection.RemoteEndPoint));
        }
Beispiel #12
0
        private ClientMessage.DeletePersistentSubscription UnwrapDeletePersistentSubscription(
            TcpPackage package, IEnvelope envelope, ClaimsPrincipal user, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.CreatePersistentSubscription>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.DeletePersistentSubscription(Guid.NewGuid(), package.CorrelationId, envelope,
                                                                  dto.EventStreamId, dto.SubscriptionGroupName, user));
        }
        //private static TcpPackage WrapReadEventsFromBeginning(ClientMessage.ReadEventsForward msg)
        //{
        //    var dto = new ClientMessageDto.ReadEventsForward(msg.CorrelationId,
        //                                                           msg.EventStreamId,
        //                                                           msg.EventNumber,
        //                                                           msg.MaxCount);
        //    return new TcpPackage(TcpCommand.ReadEventsForward, dto.Serialize());
        //}

        private static ClientMessage.ReadEventsForward UnwrapReadEventsForward(TcpPackage package,
                                                                               IEnvelope envelope,
                                                                               TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.ReadEventsForward>();

            return(new ClientMessage.ReadEventsForward(new Guid(dto.CorrelationId),
                                                       envelope,
                                                       dto.EventStreamId,
                                                       dto.StartIndex,
                                                       dto.MaxCount,
                                                       dto.ResolveLinktos));
        }
Beispiel #14
0
        private ClientMessage.PersistentSubscriptionAckEvents UnwrapPersistentSubscriptionAckEvents(
            TcpPackage package, IEnvelope envelope, ClaimsPrincipal user, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.PersistentSubscriptionAckEvents>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.PersistentSubscriptionAckEvents(
                       Guid.NewGuid(), package.CorrelationId, envelope, dto.SubscriptionId,
                       dto.ProcessedEventIds.Select(x => new Guid(x)).ToArray(), user));
        }
Beispiel #15
0
        private ClientMessage.ConnectToPersistentSubscription UnwrapConnectToPersistentSubscription(
            TcpPackage package, IEnvelope envelope, IPrincipal user, string login, string pass,
            TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.ConnectToPersistentSubscription>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ConnectToPersistentSubscription(Guid.NewGuid(), package.CorrelationId, envelope,
                                                                     connection.ConnectionId, dto.SubscriptionId, dto.EventStreamId, dto.AllowedInFlightMessages, connection.RemoteEndPoint.ToString(), user));
        }
Beispiel #16
0
        private ClientMessage.SubscribeToStream UnwrapSubscribeToStream(TcpPackage package,
                                                                        IEnvelope envelope,
                                                                        ClaimsPrincipal user,
                                                                        TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.SubscribeToStream>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.SubscribeToStream(Guid.NewGuid(), package.CorrelationId, envelope,
                                                       connection.ConnectionId, dto.EventStreamId, dto.ResolveLinkTos, user));
        }
        public Message UnwrapPackage(TcpPackage package, IEnvelope envelope, ClaimsPrincipal user,
                                     IReadOnlyDictionary <string, string> tokens, TcpConnectionManager connection, byte version)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException(nameof(envelope));
            }

            var unwrapper = _unwrappers[version][(byte)package.Command] ??
                            _unwrappers[^ 1][(byte)package.Command];

            try {
                return(unwrapper?.Invoke(package, envelope, user, tokens, connection));
            } catch (Exception exc) {
                Log.Error(exc, "Error while unwrapping TcpPackage with command {command}.",
                          package.Command);
            }

            return(null);
        }
Beispiel #18
0
        public Message UnwrapPackage(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            if (envelope == null)
            {
                throw new ArgumentNullException("envelope");
            }

            Func <TcpPackage, IEnvelope, TcpConnectionManager, Message> unwrapper = _unwrappers[(byte)package.Command];

            if (unwrapper != null)
            {
                try
                {
                    return(unwrapper(package, envelope, connection));
                }
                catch (Exception exc)
                {
                    Log.ErrorException(exc, "Error while unwrapping TcpPackage with command {0}.", package.Command);
                }
            }
            return(null);
        }
        private ClientMessage.UnsubscribeFromStream UnwrapUnsubscribeFromStream(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.UnsubscribeFromStream>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.UnsubscribeFromStream(connection, package.CorrelationId, dto.EventStreamId));
        }
        private ReplicationMessage.ReplicaLogPositionAck UnwrapReplicaLogPositionAck(TcpPackage package,
                                                                                     IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.ReplicaLogPositionAck>();

            return(new ReplicationMessage.ReplicaLogPositionAck(new Guid(dto.SubscriptionId),
                                                                dto.ReplicationLogPosition));
        }
        private ReplicationMessage.ReplicaSubscriptionRequest UnwrapReplicaSubscriptionRequest(TcpPackage package,
                                                                                               IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.SubscribeReplica>();
            var vnodeTcpEndPoint = new IPEndPoint(new IPAddress(dto.Ip), dto.Port);
            var lastEpochs       = dto.LastEpochs.Safe()
                                   .Select(x => new Epoch(x.EpochPosition, x.EpochNumber, new Guid(x.EpochId))).ToArray();

            return(new ReplicationMessage.ReplicaSubscriptionRequest(package.CorrelationId,
                                                                     envelope,
                                                                     connection,
                                                                     dto.LogPosition,
                                                                     new Guid(dto.ChunkId),
                                                                     lastEpochs,
                                                                     vnodeTcpEndPoint,
                                                                     new Guid(dto.LeaderId),
                                                                     new Guid(dto.SubscriptionId),
                                                                     dto.IsPromotable));
        }
 private ClientMessage.SubscribeToAllStreams UnwrapSubscribeToAllStreams(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     //var dto = package.Data.Deserialize<HttpClientMessageDto.SubscribeToAllStreams>();
     return(new ClientMessage.SubscribeToAllStreams(connection, package.CorrelationId));
 }
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package, string login, string password)
     : base(login, password)
 {
     _manager = manager;
     _package = package;
 }
        //private static ClientMessage.ReadEventsForwardCompleted UnwrapReadEventsFromBeginningCompleted(
        //                                                                            TcpPackage package,
        //                                                                            IEnvelope envelope,
        //                                                                            TcpConnectionManager connection)
        //{
        //    var dto = package.Data.Deserialize<ClientMessageDto.ReadEventsForwardCompleted>();

        //    return new ClientMessage.ReadEventsForwardCompleted(new Guid(dto.CorrelationId),
        //                                                              dto.EventStreamId,
        //                                                              dto.Events,
        //                                                              dto.LinkToEvents,
        //                                                              (RangeReadResult)dto.Result,
        //                                                              dto.LastCommitPosition);
        //}

        private ClientMessage.SubscribeToStream UnwrapSubscribeToStream(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.SubscribeToStream>();

            return(new ClientMessage.SubscribeToStream(connection, new Guid(dto.CorrelationId), dto.EventStreamId));
        }
Beispiel #25
0
 private void OnConnectionClosed(TcpConnectionManager manager, SocketError socketError)
 {
     manager.ConnectionClosed -= OnConnectionClosed;
     _publisher.Publish(new TcpMessage.ConnectionClosed(manager, socketError));
 }
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package, string login, string password)
     : base($"(TCP) {manager.RemoteEndPoint}", login, password)
 {
     _manager = manager;
     _package = package;
 }
Beispiel #27
0
        private ClientMessage.FilteredSubscribeToStream UnwrapFilteredSubscribeToStream(TcpPackage package,
                                                                                        IEnvelope envelope,
                                                                                        ClaimsPrincipal user,
                                                                                        TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.FilteredSubscribeToStream>();

            if (dto == null)
            {
                return(null);
            }

            IEventFilter eventFilter = EventFilter.Get(dto.Filter);

            return(new ClientMessage.FilteredSubscribeToStream(Guid.NewGuid(), package.CorrelationId, envelope,
                                                               connection.ConnectionId, dto.EventStreamId, dto.ResolveLinkTos, user, eventFilter,
                                                               dto.CheckpointInterval));
        }
        private static ClientMessage.ReadAllEventsBackward UnwrapReadAllEventsBackward(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.ReadAllEventsBackward>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ReadAllEventsBackward(package.CorrelationId,
                                                           envelope,
                                                           dto.CommitPosition,
                                                           dto.PreparePosition,
                                                           dto.MaxCount,
                                                           dto.ResolveLinkTos));
        }
        private ClientMessage.UnsubscribeFromAllStreams UnwrapUnsubscribeFromAllStreams(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.UnsubscribeFromAllStreams>();

            return(new ClientMessage.UnsubscribeFromAllStreams(connection, new Guid(dto.CorrelationId)));
        }
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package)
     : base($"(TCP) {manager.RemoteEndPoint}", package.Tokens)
 {
     _manager = manager;
     _package = package;
 }