NAME

Protocol::TLS::Server - pure Perl TLS Server

SYNOPSIS

use Protocol::TLS::Server;

# Create server object.
# Load X509 certificate and private key
my $server = Protocol::TLS::Server->new(
    cert_file => 'server.crt',
    key_file  => 'server.key',
);

# You must create tcp server yourself
my $cv = AE::cv;
tcp_server undef, 4443, sub {
    my ( $fh, $host, $port ) = @_ or do {
        warn "Client error\n";
        $cv->send;
        return;
    };

    # Create new TLS-connection object
    my $con = $server->new_connection(

        # Callback executed when TLS-handshake finished
        on_handshake_finish => sub {
            my ($tls) = @_;
            
            # send application data
            $tls->send("hello");
        },

        # Callback executed when application data received
        on_data => sub {
            my ( $tls, $data ) = @_;
            print $data;

            # send close notify and close application level connection
            $tls->close;
        }
    );

    # socket handling
    my $h;
    $h = AnyEvent::Handle->new(
        fh       => $fh,
        on_error => sub {
            $_[0]->destroy;
            print "connection error\n";
            $cv->send;
        },
        on_eof => sub {
            $h->destroy;
            print "that's all folks\n";
            $cv->send;
        },
    );

    # low level socket operations (read/write)
    $h->on_read(
        sub {
            my $handle = shift;

            # read TLS records from socket and put them to $con object
            $con->feed( $handle->{rbuf} );
            $handle->{rbuf} = '';

            # write TLS records to socket
            while ( my $record = $con->next_record ) {
                $handle->push_write($record);
            }

            # Terminate connection if all done
            $handle->push_shutdown if $con->shutdown;
            ();
        }
    );
    ()
};

# finish
$cv->recv;

DESCRIPTION

Protocol::TLS::Server is TLS server library. It's intended to make TLS-server implementations on top of your favorite event loop.

LICENSE

Copyright (C) Vladimir Lettiev.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Vladimir Lettiev <thecrux@gmail.com>