Exemplo n.º 1
0
        /// <summary>
        /// Connects to the server. Normally there is no need to call this method as
        /// the driver will connect to the server automatically when needed.
        /// </summary>
        /// <param name="timeout">How long to wait before timing out.</param>
        public virtual void Connect(
            TimeSpan timeout
            )
        {
            lock (serverLock) {
                if (state == MongoServerState.Disconnected)
                {
                    switch (settings.ConnectionMode)
                    {
                    case ConnectionMode.Direct:
                        var directConnector = new DirectConnector(this);
                        directConnector.Connect(timeout);
                        break;

                    case ConnectionMode.ReplicaSet:
                        var replicaSetConnector = new ReplicaSetConnector(this);
                        replicaSetConnector.Connect(timeout);
                        break;

                    default:
                        throw new MongoInternalException("Invalid ConnectionMode");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void DirectConnectorTests_BasicConnectPipes()
        {
            var pipe1 = new DelegatePipe(load => load["pipe1"] = true);
            var pipe2 = new DelegatePipe(load => load["pipe2"] = true);
            var pipe3 = new DelegatePipe(load => load["pipe3"] = true);

            var startConnector = new DirectConnector();
            var connector1     = new DirectConnector();
            var connector2     = new DirectConnector();

            var testLoad = new Dictionary <string, object>();

            startConnector.ReceivePipe(null, pipe1);
            connector1.ReceivePipe(pipe1, pipe2);
            connector2.ReceivePipe(pipe2, pipe3);

            Assert.AreEqual(startConnector, pipe1.InputConnector);
            Assert.AreEqual(connector1, pipe1.OutputConnector);
            Assert.AreEqual(connector1, pipe2.InputConnector);
            Assert.AreEqual(connector2, pipe2.OutputConnector);
            Assert.AreEqual(connector2, pipe3.InputConnector);
            Assert.IsNull(pipe3.OutputConnector);

            startConnector
            .RunPipes(testLoad)      // pipe1
            .RunPipes(testLoad)      // pipe2
            .RunPipes(testLoad);     // pipe3

            Assert.AreEqual(testLoad["pipe1"], true);
            Assert.AreEqual(testLoad["pipe2"], true);
            Assert.AreEqual(testLoad["pipe3"], true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Connects to the server. Normally there is no need to call this method as
        /// the driver will connect to the server automatically when needed.
        /// </summary>
        /// <param name="timeout">How long to wait before timing out.</param>
        public virtual void Connect(
            TimeSpan timeout
            )
        {
            lock (serverLock) {
                if (state != MongoServerState.Connected)
                {
                    state = MongoServerState.Connecting;
                    try {
                        switch (settings.ConnectionMode)
                        {
                        case ConnectionMode.Direct:
                            var directConnector = new DirectConnector(this);
                            directConnector.Connect(timeout);
                            primaryConnectionPool    = new MongoConnectionPool(this, directConnector.Connection);
                            secondaryConnectionPools = null;
                            replicaSet       = null;
                            maxDocumentSize  = directConnector.MaxDocumentSize;
                            maxMessageLength = directConnector.MaxMessageLength;
                            break;

                        case ConnectionMode.ReplicaSet:
                            var replicaSetConnector = new ReplicaSetConnector(this);
                            replicaSetConnector.Connect(timeout);
                            primaryConnectionPool = new MongoConnectionPool(this, replicaSetConnector.PrimaryConnection);
                            if (settings.SlaveOk && replicaSetConnector.SecondaryConnections.Count > 0)
                            {
                                secondaryConnectionPools = new List <MongoConnectionPool>();
                                foreach (var connection in replicaSetConnector.SecondaryConnections)
                                {
                                    var secondaryConnectionPool = new MongoConnectionPool(this, connection);
                                    secondaryConnectionPools.Add(secondaryConnectionPool);
                                }
                            }
                            else
                            {
                                secondaryConnectionPools = null;
                            }
                            replicaSet       = replicaSetConnector.ReplicaSet;
                            maxDocumentSize  = replicaSetConnector.MaxDocumentSize;
                            maxMessageLength = replicaSetConnector.MaxMessageLength;
                            break;

                        default:
                            throw new MongoInternalException("Invalid ConnectionMode");
                        }
                        state = MongoServerState.Connected;
                    } catch {
                        state = MongoServerState.Disconnected;
                        throw;
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void DirectConnectorTests_ThrowIfAttemptingToConnectMoreThanOnePipeToThisConnector()
        {
            var pipe1 = new DelegatePipe(load => load["pipe1"] = true);
            var pipe2 = new DelegatePipe(load => load["pipe2"] = true);
            var pipe3 = new DelegatePipe(load => load["pipe3"] = true);

            var connector = new DirectConnector();

            connector.ReceivePipe(pipe1, pipe2);
            connector.ReceivePipe(pipe1, pipe3);
        }
Exemplo n.º 5
0
 private void Awake()
 {
     this._playerName = PlayerPrefs.GetString ("playerName");
     this._connector = base.GetComponent<DirectConnector> ();
 }
        protected virtual void SetupData()
        {
            Random = new Random();

            _serverEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
            ConnectionInfo  = new ConnectionInfo(
                _serverEndPoint.Address.ToString(),
                _serverEndPoint.Port,
                "user",
                new PasswordAuthenticationMethod("user", "password"))
            {
                Timeout = TimeSpan.FromSeconds(20)
            };
            _keyExchangeAlgorithm = Random.Next().ToString(CultureInfo.InvariantCulture);
            SessionId             = new byte[10];
            Random.NextBytes(SessionId);
            DisconnectedRegister        = new List <EventArgs>();
            DisconnectReceivedRegister  = new List <MessageEventArgs <DisconnectMessage> >();
            ErrorOccurredRegister       = new List <ExceptionEventArgs>();
            ServerBytesReceivedRegister = new List <byte[]>();
            ServerIdentification        = new SshIdentification("2.0", "OurServerStub");
            _authenticationStarted      = false;
            _disconnectMessage          = new DisconnectMessage(DisconnectReason.ServiceNotAvailable, "Not today!");
            _socketFactory = new SocketFactory();

            Session = new Session(ConnectionInfo, _serviceFactoryMock.Object, _socketFactoryMock.Object);
            Session.Disconnected            += (sender, args) => DisconnectedRegister.Add(args);
            Session.DisconnectReceived      += (sender, args) => DisconnectReceivedRegister.Add(args);
            Session.ErrorOccured            += (sender, args) => ErrorOccurredRegister.Add(args);
            Session.KeyExchangeInitReceived += (sender, args) =>
            {
                var newKeysMessage = new NewKeysMessage();
                var newKeys        = newKeysMessage.GetPacket(8, null);
                ServerSocket.Send(newKeys, 4, newKeys.Length - 4, SocketFlags.None);
            };

            ServerListener            = new AsyncSocketListener(_serverEndPoint);
            ServerListener.Connected += socket =>
            {
                ServerSocket = socket;

                // Since we're mocking the protocol version exchange, we'll immediately stat KEX upon
                // having established the connection instead of when the client has been identified

                var keyExchangeInitMessage = new KeyExchangeInitMessage
                {
                    CompressionAlgorithmsClientToServer = new string[0],
                    CompressionAlgorithmsServerToClient = new string[0],
                    EncryptionAlgorithmsClientToServer  = new string[0],
                    EncryptionAlgorithmsServerToClient  = new string[0],
                    KeyExchangeAlgorithms       = new[] { _keyExchangeAlgorithm },
                    LanguagesClientToServer     = new string[0],
                    LanguagesServerToClient     = new string[0],
                    MacAlgorithmsClientToServer = new string[0],
                    MacAlgorithmsServerToClient = new string[0],
                    ServerHostKeyAlgorithms     = new string[0]
                };
                var keyExchangeInit = keyExchangeInitMessage.GetPacket(8, null);
                ServerSocket.Send(keyExchangeInit, 4, keyExchangeInit.Length - 4, SocketFlags.None);
            };
            ServerListener.BytesReceived += (received, socket) =>
            {
                ServerBytesReceivedRegister.Add(received);

                if (!_authenticationStarted)
                {
                    var serviceAcceptMessage = ServiceAcceptMessageBuilder.Create(ServiceName.UserAuthentication).Build();
                    ServerSocket.Send(serviceAcceptMessage, 0, serviceAcceptMessage.Length, SocketFlags.None);
                    _authenticationStarted = true;
                }
            };

            ServerListener.Start();

            ClientSocket = new DirectConnector(_socketFactory).Connect(ConnectionInfo);
        }
Exemplo n.º 7
0
 protected virtual void SetupData()
 {
     Connector     = new DirectConnector(SocketFactoryMock.Object);
     SocketFactory = new SocketFactory();
 }