|
1 | 1 | # -*- coding: utf-8; -*- |
| 2 | +"""Pieces common to the REPL server and client.""" |
2 | 3 |
|
3 | 4 | import json |
4 | 5 |
|
|
9 | 10 | class ApplevelProtocol: |
10 | 11 | """Application-level communication protocol. |
11 | 12 |
|
12 | | - We encode the payload as JSON encoded dictionaries, then encoded as utf-8 |
13 | | - text. The bytes are stuffed into a message using the `unpythonic.net.msg` |
| 13 | + We encode the payload dictionary as JSON, then encode the text as utf-8. |
| 14 | + The resulting bytes are stuffed into a message using the `unpythonic.net.msg` |
14 | 15 | low-level message protocol. |
15 | 16 |
|
16 | 17 | This format was chosen instead of pickle to ensure the client and server |
17 | 18 | can talk to each other regardless of the Python versions on each end of the |
18 | 19 | connection. |
19 | 20 |
|
20 | 21 | Transmission is synchronous; when one end is sending, the other one must be |
21 | | - receiving. Both sending and receiving will block until success, or until |
22 | | - the socket is closed. |
| 22 | + receiving. Receiving will block until success, or until the socket is closed. |
23 | 23 |
|
24 | 24 | This can be used as a common base class for server/client object pairs. |
| 25 | + It can also be used as a mixin. |
25 | 26 |
|
26 | | - **NOTE**: The derived class must define two attributes: |
| 27 | + **NOTE**: The derived class (or class mixing this in) must define two |
| 28 | + attributes: |
27 | 29 |
|
28 | | - - `sock`: an open TCP socket connected to the peer to communicate with. |
| 30 | + - `sock`: a TCP socket. When `_send` or `_recv` is called, the socket |
| 31 | + must be open and connected to the peer to communicate with. |
29 | 32 |
|
30 | 33 | - `decoder`: `unpythonic.net.msg.MessageDecoder` instance for receiving |
31 | 34 | messages. Typically this is connected to `sock` using an |
32 | 35 | `unpythonic.net.msg.socketsource`, like |
33 | 36 | `MessageDecoder(socketsource(sock))`. |
34 | 37 |
|
35 | 38 | These are left to the user code to define, because typically the client and |
36 | | - server sides must handle this differently. The client can create `sock` and |
| 39 | + server sides must handle them differently. The client can create `sock` and |
37 | 40 | `decoder` in its constructor, whereas a TCP server typically inherits from |
38 | 41 | `socketserver.BaseRequestHandler`, and receives an incoming connection in |
39 | 42 | its `handle` method (which is then the official place to create any |
|
0 commit comments