You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.1 KiB

  1. # P2P Multiplex Connection
  2. ## MConnection
  3. `MConnection` is a multiplex connection that supports multiple independent streams
  4. with distinct quality of service guarantees atop a single TCP connection.
  5. Each stream is known as a `Channel` and each `Channel` has a globally unique _byte id_.
  6. Each `Channel` also has a relative priority that determines the quality of service
  7. of the `Channel` compared to other `Channel`s.
  8. The _byte id_ and the relative priorities of each `Channel` are configured upon
  9. initialization of the connection.
  10. The `MConnection` supports three packet types:
  11. - Ping
  12. - Pong
  13. - Msg
  14. ### Ping and Pong
  15. The ping and pong messages consist of writing a single byte to the connection; 0x1 and 0x2, respectively.
  16. When we haven't received any messages on an `MConnection` in time `pingTimeout`, we send a ping message.
  17. When a ping is received on the `MConnection`, a pong is sent in response only if there are no other messages
  18. to send and the peer has not sent us too many pings (TODO).
  19. If a pong or message is not received in sufficient time after a ping, the peer is disconnected from.
  20. ### Msg
  21. Messages in channels are chopped into smaller `msgPacket`s for multiplexing.
  22. ```
  23. type msgPacket struct {
  24. ChannelID byte
  25. EOF byte // 1 means message ends here.
  26. Bytes []byte
  27. }
  28. ```
  29. The `msgPacket` is serialized using [go-amino](https://github.com/tendermint/go-amino) and prefixed with 0x3.
  30. The received `Bytes` of a sequential set of packets are appended together
  31. until a packet with `EOF=1` is received, then the complete serialized message
  32. is returned for processing by the `onReceive` function of the corresponding channel.
  33. ### Multiplexing
  34. Messages are sent from a single `sendRoutine`, which loops over a select statement and results in the sending
  35. of a ping, a pong, or a batch of data messages. The batch of data messages may include messages from multiple channels.
  36. Message bytes are queued for sending in their respective channel, with each channel holding one unsent message at a time.
  37. Messages are chosen for a batch one at a time from the channel with the lowest ratio of recently sent bytes to channel priority.
  38. ## Sending Messages
  39. There are two methods for sending messages:
  40. ```go
  41. func (m MConnection) Send(chID byte, msg interface{}) bool {}
  42. func (m MConnection) TrySend(chID byte, msg interface{}) bool {}
  43. ```
  44. `Send(chID, msg)` is a blocking call that waits until `msg` is successfully queued
  45. for the channel with the given id byte `chID`. The message `msg` is serialized
  46. using the `tendermint/go-amino` submodule's `WriteBinary()` reflection routine.
  47. `TrySend(chID, msg)` is a nonblocking call that queues the message msg in the channel
  48. with the given id byte chID if the queue is not full; otherwise it returns false immediately.
  49. `Send()` and `TrySend()` are also exposed for each `Peer`.
  50. ## Peer
  51. Each peer has one `MConnection` instance, and includes other information such as whether the connection
  52. was outbound, whether the connection should be recreated if it closes, various identity information about the node,
  53. and other higher level thread-safe data used by the reactors.
  54. ## Switch/Reactor
  55. The `Switch` handles peer connections and exposes an API to receive incoming messages
  56. on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  57. or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  58. incoming messages are received on the reactor.
  59. ```go
  60. // Declare a MyReactor reactor that handles messages on MyChannelID.
  61. type MyReactor struct{}
  62. func (reactor MyReactor) GetChannels() []*ChannelDescriptor {
  63. return []*ChannelDescriptor{ChannelDescriptor{ID:MyChannelID, Priority: 1}}
  64. }
  65. func (reactor MyReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {
  66. r, n, err := bytes.NewBuffer(msgBytes), new(int64), new(error)
  67. msgString := ReadString(r, n, err)
  68. fmt.Println(msgString)
  69. }
  70. // Other Reactor methods omitted for brevity
  71. ...
  72. switch := NewSwitch([]Reactor{MyReactor{}})
  73. ...
  74. // Send a random message to all outbound connections
  75. for _, peer := range switch.Peers().List() {
  76. if peer.IsOutbound() {
  77. peer.Send(MyChannelID, "Here's a random message")
  78. }
  79. }
  80. ```