.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Event::RPC::Server 3pm" .TH Event::RPC::Server 3pm "2022-12-12" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Event::RPC::Server \- Simple API for event driven RPC servers .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Event::RPC::Server; \& use My::TestModule; \& \& my $server = Event::RPC::Server\->new ( \& #\-\- Required arguments \& port => 8888, \& classes => { \& "My::TestModule" => { \& new => "_constructor", \& get_data => 1, \& set_data => 1, \& clone => "_object", \& }, \& }, \& \& #\-\- Optional arguments \& name => "Test server", \& logger => Event::RPC::Logger\->new(), \& start_log_listener => 1, \& \& ssl => 1 \& ssl_key_file => "server.key", \& ssl_cert_file => "server.crt", \& ssl_passwd_cb => sub { "topsecret" }, \& ssl_opts => { ... }, \& \& auth_required => 1, \& auth_passwd_href => { $user => Event::RPC\->crypt($user,$pass) }, \& auth_module => Your::Own::Auth::Module\->new(...), \& \& loop => Event::RPC::Loop::Event\->new(), \& \& host => "localhost", \& load_modules => 1, \& auto_reload_modules => 1, \& connection_hook => sub { ... }, \& \& message_formats => [qw/ SERL CBOR JSON STOR /], \& insecure_msg_fmt_ok => 1, \& ); \& \& $server\->set_max_packet_size(2*1024*1024*1024); \& \& # start server and event loop \& $server\->start; \& \& # or prepare server start if you like to control event loop by yourself \& $server\->prepare; \& \& # and later from inside your server implementation \& Event::RPC::Server\->instance\->stop; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Use this module to add a simple to use \s-1RPC\s0 mechanism to your event driven server application. .PP Just create an instance of the Event::RPC::Server class with a bunch of required settings. Then enter the main event loop through it, or take control over the main loop on your own if you like (refer to the \s-1MAINLOOP\s0 chapter for details). .PP General information about the architecture of Event::RPC driven applications is collected in the Event::RPC manpage. .SH "CONFIGURATION OPTIONS" .IX Header "CONFIGURATION OPTIONS" All options described here may be passed to the \fBnew()\fR constructor of Event::RPC::Server. As well you may set or modify them using set_OPTION style mutators, but not after \fBstart()\fR or \fBsetup_listeners()\fR was called! All options may be read using get_OPTION style accessors. .SS "\s-1REQUIRED OPTIONS\s0" .IX Subsection "REQUIRED OPTIONS" If you just pass the required options listed beyond you have a \s-1RPC\s0 server which listens to a network port and allows everyone connecting to it to access a well defined list of classes and methods resp. using the correspondent server objects. .PP There is no authentication or encryption active in this minimal configuration, so aware that this may be a big security risk! Adding security is easy, refer to the chapters about \s-1SSL\s0 and authentication. .PP These are the required options: .IP "\fBport\fR" 4 .IX Item "port" \&\s-1TCP\s0 port number of the \s-1RPC\s0 listener. .IP "\fBclasses\fR" 4 .IX Item "classes" This is a hash ref with the following structure: .Sp .Vb 9 \& classes => { \& "Class1" => { \& new => "_constructor", \& simple_method => 1, \& object_returner => "_object", \& }, \& "Class2" => { ... }, \& ... \& }, .Ve .Sp Each class which should be accessible for clients needs to be listed here at the first level, assigned a hash of methods allowed to be called. Event::RPC disuinguishes three types of methods by classifying their return value: .RS 4 .IP "\fBConstructors\fR" 4 .IX Item "Constructors" A constructor method creates a new object of the corresponding class and returns it. You need to assign the string \*(L"_constructor\*(R" to the method entry to mark a method as a constructor. .IP "\fBSingleton constructors\fR" 4 .IX Item "Singleton constructors" For singleton classes the method which returns the singleton instance should be declared with \*(L"_singleton\*(R". This way the server takes care that references get never destroyed on server side. .IP "\fBSimple methods\fR" 4 .IX Item "Simple methods" What's simple about these methods is their return value: it's a scalar, array, hash or even any complex reference structure (Ok, not simple anymore ;), but in particular it returns \fB\s-1NO\s0\fR objects, because this needs to handled specially (see below). .Sp Declare simple methods by assigning 1 in the method declaration. .IP "\fBObject returners\fR" 4 .IX Item "Object returners" Methods which return objects need to be declared by assigning \&\*(L"_object\*(R" to the method name here. They're not bound to return just one scalar object reference and may return an array or list reference with a bunch of objects as well. .RE .RS 4 .RE .SS "\s-1SSL OPTIONS\s0" .IX Subsection "SSL OPTIONS" The client/server protocol of Event::RPC is not encrypted by default, so everyone listening on your network can read or even manipulate data. To prevent this efficiently you can enable \s-1SSL\s0 encryption. Event::RPC uses the IO::Socket::SSL Perl module for this. .PP First you need to generate a server key and certificate for your server using the openssl command which is part of the OpenSSL distribution, e.g. by issuing these commands (please refer to the manpage of openssl for details \- this is a very rough example, which works in general, but probably you want to tweak some parameters): .PP .Vb 4 \& % openssl genrsa \-des3 \-out server.key 1024 \& % openssl req \-new \-key server.key \-out server.csr \& % openssl x509 \-req \-days 3600 \-in server.csr \e \& \-signkey server.key \-out server.crt .Ve .PP After executing these commands you have the following files .PP .Vb 3 \& server.crt \& server.key \& server.csr .Ve .PP Event::RPC needs the first two of them to operate with \s-1SSL\s0 encryption. .PP To enable \s-1SSL\s0 encryption you need to pass the following options to the constructor: .IP "\fBssl\fR" 4 .IX Item "ssl" The ssl option needs to be set to 1. .IP "\fBssl_key_file\fR" 4 .IX Item "ssl_key_file" This is the filename of the server.key you generated with the openssl command. .IP "\fBssl_cert_file\fR" 4 .IX Item "ssl_cert_file" This is the filename of the server.crt file you generated with the openssl command. .IP "\fBssl_passwd_cb\fR" 4 .IX Item "ssl_passwd_cb" Your server key is encrypted with a password you entered during the key creation process described above. This callback must return it. Depending on how critical your application is you probably must request the password from the user during server startup or place it into a more or less secured file. For testing purposes you can specify a simple anonymous sub here, which just returns the password, e.g. .Sp .Vb 1 \& ssl_passwd_cb => sub { return "topsecret" } .Ve .Sp But note: having the password in plaintext in your program code is insecure! .IP "\fBssl_opts\fR" 4 .IX Item "ssl_opts" This optional parameter takes a hash reference of options passed to IO::Socket::SSL\->new(...) to have more control over the server \s-1SSL\s0 listener. .SS "\s-1AUTHENTICATION OPTIONS\s0" .IX Subsection "AUTHENTICATION OPTIONS" \&\s-1SSL\s0 encryption is fine, now it's really hard for an attacker to listen or modify your network communication. But without any further configuration any user on your network is able to connect to your server. To prevent this users resp. connections to your server needs to be authenticated somehow. .PP Since version 0.87 Event::RPC has an \s-1API\s0 to delegate authentication tasks to a module, which can be implemented outside Event::RPC. To be compatible with prior releases it ships the module Event::RPC::AuthPasswdHash which implements the old behaviour transparently. .PP This default implementation is a simple user/password based model. For now this controls just the right to connect to your server, so knowing one valid user/password pair is enough to access all exported methods of your server. Probably a more differentiated model will be added later which allows granting access to a subset of exported methods only for each user who is allowed to connect. .PP The following options control the authentication: .IP "\fBauth_required\fR" 4 .IX Item "auth_required" Set this to 1 to enable authentication and nobody can connect your server until he passes a valid user/password pair. .IP "\fBauth_passwd_href\fR" 4 .IX Item "auth_passwd_href" If you like to use the builtin Event::RPC::AuthPasswdHash module simply set this attribute. If you decide to use \fBauth_module\fR (explained beyound) it's not necessary. .Sp \&\fBauth_passwd_href\fR is a hash of valid user/password pairs. The password stored here needs to be encrypted using Perl's \fBcrypt()\fR function, using the username as the salt. .Sp Event::RPC has a convenience function for generating such a crypted password, although it's currently just a 1:1 wrapper around Perl's builtin \fBcrypt()\fR function, but probably this changes someday, so better use this method: .Sp .Vb 1 \& $crypted_pass = Event::RPC\->crypt($user, $pass); .Ve .Sp This is a simple example of setting up a proper \fBauth_passwd_href\fR with two users: .Sp .Vb 4 \& auth_passwd_href => { \& fred => Event::RPC\->crypt("fred", $freds_password), \& nick => Event::RPC\->crypt("nick", $nicks_password), \& }, .Ve .IP "\fBauth_module\fR" 4 .IX Item "auth_module" If you like to implement a more complex authentication method yourself you may set the \fBauth_module\fR attribute to an instance of your class. For now your implementation just needs to have this method: .Sp .Vb 1 \& $auth_module\->check_credentials($user, $pass) .Ve .Sp Aware that \f(CW$pass\fR is encrypted as explained above, so your original password needs to by crypted using Event::RPC\->crypt as well, at least for the comparison itself. .PP \&\fBNote:\fR you can use the authentication module without \s-1SSL\s0 but aware that an attacker listening to the network connection will be able to grab the encrypted password token and authenticate himself with it to the server (replay attack). Probably a more sophisticated challenge/response mechanism will be added to Event::RPC to prevent this. But you definitely should use \s-1SSL\s0 encryption in a critical environment anyway, which renders grabbing the password from the net impossible. .SS "\s-1LOGGING OPTIONS\s0" .IX Subsection "LOGGING OPTIONS" Event::RPC has some logging abilities, primarily for debugging purposes. It uses a \fBlogger\fR for this, which is an object implementing the Event::RPC::Logger interface. The documentation of Event::RPC::Logger describes this interface and Event::RPC's logging facilities in general. .IP "\fBlogger\fR" 4 .IX Item "logger" To enable logging just pass such an Event::RPC::Logger object to the constructor. .IP "\fBstart_log_listener\fR" 4 .IX Item "start_log_listener" Additionally Event::RPC can start a log listener on the server's port number incremented by 1. All clients connected to this port (e.g. by using telnet) get the server's log output. .Sp Note: currently the logging port supports neither \s-1SSL\s0 nor authentication, so be careful enabling the log listener in critical environments. .SS "\s-1MAINLOOP OPTIONS\s0" .IX Subsection "MAINLOOP OPTIONS" Event::RPC derived it's name from the fact that it follows the event driven paradigm. There are several toolkits for Perl which allow event driven software development. Event::RPC has an abstraction layer for this and thus should be able to work with any toolkit. .IP "\fBloop\fR" 4 .IX Item "loop" This option takes an object of the loop abstraction layer you want to use. Currently the following modules are implemented: .Sp .Vb 3 \& Event::RPC::Loop::AnyEvent Use the AnyEvent module \& Event::RPC::Loop::Event Use the Event module \& Event::RPC::Loop::Glib Use the Glib module .Ve .Sp If \fBloop\fR isn't set, Event::RPC::Server tries all supported modules in a row and aborts the program, if no module was found. .Sp More modules will be added in the future. If you want to implement one just take a look at the code in the modules above: it's really easy and I appreciate your patch. The interface is roughly described in the documentation of Event::RPC::Loop. .PP If you use the Event::RPC\->\fBstart()\fR method as described in the \s-1SYNOPSIS\s0 Event::RPC will enter the correspondent main loop for you. If you want to have full control over the main loop, use this method to setup all necessary Event::RPC listeners: .PP .Vb 1 \& $rpc_server\->setup_listeners(); .Ve .PP and manage the main loop stuff on your own. .SS "\s-1MESSAGE FORMAT OPTIONS\s0" .IX Subsection "MESSAGE FORMAT OPTIONS" Event::RPC supports different \s-1CPAN\s0 modules for data serialisation, named \*(L"message formats\*(R" here: .PP .Vb 4 \& SERL \-\- Sereal::Encoder, Sereal::Decoder \& CBOR \-\- CBOR::XS \& JSON \-\- JSON::XS \& STOR \-\- Storable .Ve .PP Server and client negotiate automatically which format is best to use but you can manipulate this behaviour with the following options: .IP "\fBmessage_formats\fR" 4 .IX Item "message_formats" This takes an array of format identifiers from the list above. Event::RPC::Server will only use / accept these formats. .IP "\fBinsecure_msg_fmt_ok\fR" 4 .IX Item "insecure_msg_fmt_ok" The Storable module is known to be insecure. But for backward compatibility reasons Event::RPC::Server accepts clients which can't offer anything but Storable. You can prevent that by setting this option explicitly to 0. It's enabled by default. .SS "\s-1MISCELLANEOUS OPTIONS\s0" .IX Subsection "MISCELLANEOUS OPTIONS" .IP "\fBhost\fR" 4 .IX Item "host" By default the network listeners are bound to all interfaces in the system. Use the host option to bind to a specific interface, e.g. \*(L"localhost\*(R" if you efficiently want to prevent network clients from accessing your server. .IP "\fBload_modules\fR" 4 .IX Item "load_modules" Control whether the class module files should be loaded automatically when first accesed by a client. This options defaults to true, for backward compatibility reasons. .IP "\fBauto_reload_modules\fR" 4 .IX Item "auto_reload_modules" If this option is set Event::RPC::Server will check on each method call if the corresponding module changed on disk and reloads it automatically. Of course this has an effect on performance, but it's very useful during development. You probably shouldn't enable this in production environments. .IP "\fBconnection_hook\fR" 4 .IX Item "connection_hook" This callback is called on each connection / disconnection with two arguments: the Event::RPC::Connection object and a string containing either \*(L"connect\*(R" or \*(L"disconnect\*(R" depending what's currently happening with this connection. .SH "METHODS" .IX Header "METHODS" The following methods are publically available: .IP "Event::RPC::Server\->\fBinstance\fR" 4 .IX Item "Event::RPC::Server->instance" This returns the latest created Event::RPC::Server instance (usually you have only one instance in one program). .ie n .IP "$rpc_server\->\fBstart\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBstart\fR" 4 .IX Item "$rpc_server->start" Start the mainloop of your Event::RPC::Server. .ie n .IP "$rpc_server\->\fBstop\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBstop\fR" 4 .IX Item "$rpc_server->stop" Stops the mainloop which usually means, that the server exits, as long you don't do more sophisticated mainloop stuff by your own. .ie n .IP "$rpc_server\->\fBsetup_listeners\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBsetup_listeners\fR" 4 .IX Item "$rpc_server->setup_listeners" This method initializes all networking listeners needed for Event::RPC::Server to work, using the configured loop module. Use this method if you don't use the \fBstart()\fR method but manage the mainloop on your own. .ie n .IP "$rpc_server\->\fBlog\fR ( [$level,] $msg )" 4 .el .IP "\f(CW$rpc_server\fR\->\fBlog\fR ( [$level,] \f(CW$msg\fR )" 4 .IX Item "$rpc_server->log ( [$level,] $msg )" Convenience method for logging. It simply passes the arguments to the configured logger's \fBlog()\fR method. .ie n .IP "$rpc_server\->\fBget_clients_connected\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBget_clients_connected\fR" 4 .IX Item "$rpc_server->get_clients_connected" Returns the number of currently connected Event::RPC clients. .ie n .IP "$rpc_server\->\fBget_log_clients_connected\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBget_log_clients_connected\fR" 4 .IX Item "$rpc_server->get_log_clients_connected" Returns the number of currently connected logging clients. .ie n .IP "$rpc_server\->\fBget_active_connection\fR" 4 .el .IP "\f(CW$rpc_server\fR\->\fBget_active_connection\fR" 4 .IX Item "$rpc_server->get_active_connection" This returns the currently active Event::RPC::Connection object representing the connection resp. the client which currently requests method invocation. This is undef if no client call is active. .ie n .IP "$rpc_client\->\fBset_max_packet_size\fR ( $bytes )" 4 .el .IP "\f(CW$rpc_client\fR\->\fBset_max_packet_size\fR ( \f(CW$bytes\fR )" 4 .IX Item "$rpc_client->set_max_packet_size ( $bytes )" By default Event::RPC does not handle network packages which exceed 2 \s-1GB\s0 in size (was 4 \s-1MB\s0 with version 1.04 and earlier). .Sp You can change this value using this method at any time, but 4 \s-1GB\s0 is the maximum. An attempt of the server to send a bigger packet will be aborted and reported as an exception on the client and logged as an error message on the server. .Sp Note: you have to set the same value on client and server side! .ie n .IP "$rpc_client\->\fBget_max_packet_size\fR" 4 .el .IP "\f(CW$rpc_client\fR\->\fBget_max_packet_size\fR" 4 .IX Item "$rpc_client->get_max_packet_size" Returns the currently active max packet size. .SH "AUTHORS" .IX Header "AUTHORS" .Vb 1 \& Jörn Reder .Ve .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2005\-2015 by Jörn Reder . .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.