コード例 #1
0
        protected void Connected(Connection.Tcp connection)
        {
            try
            {
                // We should be connected
                if (connection.client.Connected)
                {
                    // Get the stream associated with this connection
                    connection.stream = connection.client.GetStream();

                    Message.Connection.Parameters header = new Message.Connection.Parameters
                    {
                        version   = maxSupportedVersion,
                        encrypted = (serverCertificate != null)
                    };

                    // Send greating message with protocol version and parameters
                    connection.Send(header);

                    if (serverCertificate != null)
                    {
                        // Create the SSL wraping stream
                        connection.stream = new SslStream(connection.stream, false, new RemoteCertificateValidationCallback(
                                                              (sender, certificate, chain, sslPolicyErrors) => (sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable
                                                              ), null, EncryptionPolicy.RequireEncryption);

                        // Authenticate with the client
                        ((SslStream)connection.stream).BeginAuthenticateAsServer(serverCertificate, Authenticated, connection);
                    }
                    else
                    {
                        // No encryption, the channel stay as is
                        ValidateCredentials(connection);
                    }
                }
                else
                {
                    Debug.LogError("The connection from the client failed.");
                }
            }
            catch (DropException)
            {
                throw;
            }
            catch (Exception exception)
            {
                Debug.LogErrorFormat("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: asheesh1996/clarte-utils
        protected void Connected(IAsyncResult async_result)
        {
            try
            {
                // Finalize connection to server
                Connection.Tcp connection = (Connection.Tcp)async_result.AsyncState;

                connection.client.EndConnect(async_result);

                // We should be connected
                if (connection.client.Connected)
                {
                    // Get the stream associated with this connection
                    connection.stream = connection.client.GetStream();

                    Message.Base h;

                    if (connection.Receive(out h) && h.IsType <Message.Connection.Parameters>())
                    {
                        Message.Connection.Parameters header = (Message.Connection.Parameters)h;

                        connection.version = header.version;

                        if (connection.version > maxSupportedVersion)
                        {
                            Debug.LogWarningFormat("Usupported protocol version '{0}'. Using version '{1}' instead.", connection.version, maxSupportedVersion);

                            connection.version = maxSupportedVersion;
                        }

                        if (header.encrypted)
                        {
                            // Create the SSL wraping stream
                            connection.stream = new SslStream(connection.stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null, EncryptionPolicy.RequireEncryption);

                            // Authenticate with the server
                            ((SslStream)connection.stream).BeginAuthenticateAsClient(hostname, Authenticated, connection);
                        }
                        else
                        {
                            // No encryption, the channel stay as is
                            ValidateCredentials(connection);
                        }
                    }
                    else
                    {
                        Drop(connection, "Expected to receive connection greetings and parameters.");
                    }
                }
                else
                {
                    Drop(connection, "The connection to {0}:{1} failed.", hostname, port);
                }
            }
            catch (DropException)
            {
                throw;
            }
            catch (Exception exception)
            {
                Debug.LogErrorFormat("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
            }
        }