Chapter 13. Networking: Implementing RPC

I waited and waited and waited, and when no message came, I knew it must have been from you.

Ashleigh Brilliant

In this chapter, we build on the lessons learned in the preceding chapter and implement two layers on top of sockets. The first is an asynchronous message-passing system, Msg, which takes advantage of nonblocking I/O where available. We then build a remote procedure call module, RPC, on top of the Msg substrate. RPC offers the convenience of synchronous procedure calls and accounts for exceptions, wantarray, parameter marshalling, and so on.

Before we proceed, let us get one basic definition out of the way. In Chapter 12, we glossed over the definition of a "message.” A socket connection is simply a stream of bytes and leaves it to the application to define message boundaries, so the receiver can tell when one message ends and another starts. Some protocols insert an end-of-message character, an arbitrarily chosen byte such as ASCII 4 (Ctrl-D), or a lone period on a line, and some prepend message lengths so that the receiver knows how much to expect. We use the latter option in this chapter.

Msg: Messaging Toolkit

In this section, we implement a module called Msg, an event-driven, client-server, messaging framework,[51] using the IO::Select and IO::Socket modules. These are its key characteristics:

Queued messages

You can instruct Msg to either send a message right away or queue it for later delivery.

Nonblocking I/O

Msg checks to see ...

Get Advanced Perl Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.