Inheritance: TempestMessage
        public void ConnectMessage()
        {
            var msg = new ConnectMessage();
            msg.Protocols = new[] { MockProtocol.Instance };
            msg.SignatureHashAlgorithms = new[] { "SHA1" };

            var result = msg.AssertLengthMatches();
            Assert.IsNotNull (result.Protocols);
            Assert.AreEqual (msg.Protocols.Single(), result.Protocols.Single());
            Assert.IsNotNull (result.SignatureHashAlgorithms);
            Assert.AreEqual ("SHA1", result.SignatureHashAlgorithms.Single());
        }
        private void ConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            int c = GetNextCallId();
            Trace.WriteLineIf (NTrace.TraceVerbose, "Entering", String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));

            this.serializer = new ClientMessageSerializer (this, Protocols);

            int p;

            if (e.SocketError != SocketError.Success)
            {
                p = Interlocked.Decrement (ref this.pendingAsync);
                Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Decrement pending: {0}", p), String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));
                DisconnectAsync (ConnectionResult.ConnectionFailed);
                OnConnectionFailed (new ClientConnectionEventArgs (this));

                var tcs = Interlocked.Exchange (ref this.connectCompletion, null);
                if (tcs != null)
                {
                    ConnectionResult result = GetConnectFromError (e.SocketError);
                    tcs.TrySetResult (new ClientConnectionResult (result, null));
                }

                return;
            }

            e.Completed -= ConnectCompleted;
            e.Completed += ReliableReceiveCompleted;
            e.SetBuffer (this.rmessageBuffer, 0, this.rmessageBuffer.Length);
            this.rreader = new BufferValueReader (this.rmessageBuffer);

            bool received;
            lock (this.stateSync)
            {
                if (!IsConnected)
                {
                    Trace.WriteLineIf (NTrace.TraceVerbose, "Already disconnected", String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));
                    p = Interlocked.Decrement (ref this.pendingAsync);
                    Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Decrement pending: {0}", p), String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));
                    return;
                }

                //p = Interlocked.Increment (ref this.pendingAsync);
                //Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Increment pending: {0}", p), String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));

                received = !this.reliableSocket.ReceiveAsync (e);
            }

            //p = Interlocked.Decrement (ref this.pendingAsync);
            //Trace.WriteLineIf (NTrace.TraceVerbose, String.Format ("Decrement pending: {0}", p), String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));

            if (received)
                ReliableReceiveCompleted (this.reliableSocket, e);

            var connectMsg = new ConnectMessage { Protocols = this.protocols.Values };
            if (this.requiresHandshake)
            {
                while (!this.authReady)
                    Thread.Sleep (0);

                connectMsg.SignatureHashAlgorithms = this.pkAuthentication.SupportedHashAlgs;
            }

            SendAsync (connectMsg);

            Trace.WriteLineIf (NTrace.TraceVerbose, "Exiting", String.Format ("{2}:{3} {4}:ConnectCompleted({0},{1})", e.BytesTransferred, e.SocketError, this.typeName, connectionId, c));
        }
Exemplo n.º 3
0
        private void OnConnectMessage(ConnectMessage msg)
        {
            if (!msg.Protocols.Any())
            {
                DisconnectAsync (ConnectionResult.FailedHandshake);
                return;
            }

            string signingHashAlgorithm = null;
            if (this.requiresHandshake)
            {
                bool foundHashAlg = false;
                if (this.localCrypto != null) {
                    foreach (string hashAlg in this.localCrypto.SupportedHashAlgs) {
                        if (msg.SignatureHashAlgorithms.Contains (hashAlg)) {
                            signingHashAlgorithm = hashAlg;
                            foundHashAlg = true;
                            break;
                        }
                    }
                }

                if (!foundHashAlg)
                {
                    this.provider.SendConnectionlessMessageAsync (new DisconnectMessage { Reason = ConnectionResult.IncompatibleVersion }, RemoteTarget);
                    Disconnect (ConnectionResult.FailedHandshake);
                    return;
                }
            }

            foreach (Protocol protocol in msg.Protocols)
            {
                Protocol lp;
                if (!this.provider.protocols.TryGetValue (protocol.id, out lp) || !lp.CompatibleWith (protocol))
                {
                    this.provider.SendConnectionlessMessageAsync (new DisconnectMessage { Reason = ConnectionResult.IncompatibleVersion }, RemoteTarget);
                    Disconnect (ConnectionResult.IncompatibleVersion);
                    return;
                }
            }

            var protocols = this.provider.protocols.Values.Intersect (msg.Protocols);
            this.serializer = new ServerMessageSerializer (this, protocols);
            this.serializer.SigningHashAlgorithm = signingHashAlgorithm;
            this.receivedProtocols = true;

            if (!this.requiresHandshake)
            {
                this.formallyConnected = true;
                this.provider.Connect (this);

                SendAsync (new ConnectedMessage { ConnectionId = ConnectionId });
            }
            else
            {
                SendAsync (new AcknowledgeConnectMessage
                {
                    SignatureHashAlgorithm = this.serializer.SigningHashAlgorithm,
                    EnabledProtocols = Protocols,
                    ConnectionId = ConnectionId,
                    PublicAuthenticationKey = this.provider.PublicKey,
                    PublicEncryptionKey = this.provider.PublicKey
                });
            }
        }