OLD | NEW |
1 Example Echo Client & Server | 1 Example Echo Client & Server |
2 ==== | 2 ==== |
3 | 3 |
4 This echo client/server demonstrate how to create and use a mojom interface, | 4 This echo client/server demonstrate how to create and use a mojom interface, |
5 as well as demonstrating one way to communicate between mojo applications. | 5 as well as demonstrating one way to communicate between mojo applications. |
6 | 6 |
7 For a deeper dive into this code, refer to the [Mojo | 7 For a deeper dive into this code, refer to the [Mojo |
8 Tutorial](https://docs.google.com/document/d/1mufrtxTk8w9qa3jcnlgqsYkWlyhwEpc7aW
NaSOks7ug). | 8 Tutorial](https://docs.google.com/document/d/1mufrtxTk8w9qa3jcnlgqsYkWlyhwEpc7aW
NaSOks7ug). |
9 | 9 |
10 ## Running the Echo Client & Server | 10 ## Running the Echo Client & Server |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 callback implementations being passed where necessary. | 120 callback implementations being passed where necessary. |
121 | 121 |
122 ## Echo Server Structure | 122 ## Echo Server Structure |
123 | 123 |
124 The echo server, like the echo client, is implemented as an application. This | 124 The echo server, like the echo client, is implemented as an application. This |
125 means it has a `MojoMain` function, an `ApplicationRunner`, and an | 125 means it has a `MojoMain` function, an `ApplicationRunner`, and an |
126 `ApplicationDelegate` actually implementing the core application. | 126 `ApplicationDelegate` actually implementing the core application. |
127 | 127 |
128 echo_server.cc contains three different types of servers, though only one can be | 128 echo_server.cc contains three different types of servers, though only one can be |
129 used at a time. To try changing the server, uncomment one of the lines in | 129 used at a time. To try changing the server, uncomment one of the lines in |
130 MojoMain. These different `ApplicationDelegate` derivations demonstrate how | 130 MojoMain. These different `ApplicationDelegate` derivations demonstrate |
131 differently the `InterfaceFactory` can be created. Each server derives from | 131 different ways in which incoming requests can be handled. |
132 `InterfaceFactory`, which implements an interface and binds it to incoming | |
133 requests. | |
134 | 132 |
135 All three servers, being `ApplicationDelegate` derivations, implement | 133 All three servers, being `ApplicationDelegate` derivations, implement |
136 `ConfigureIncomingConnection` in the same way: | 134 `ConfigureIncomingConnection` in the same way: |
137 | 135 |
138 ``` | 136 ``` |
139 connection->AddService<Echo>(this); | 137 connection->GetServiceProviderImpl().AddService<Echo>( |
| 138 [this](const mojo::ConnectionContext& connection_context, |
| 139 mojo::InterfaceRequest<Echo> echo_request) { |
| 140 ... |
| 141 }); |
140 ``` | 142 ``` |
141 | 143 |
142 This should be read as "For any incoming connections to this server, use `this` | 144 This should be read as "For any incoming connections to this server, use the |
143 as a factory to create the Echo interface". | 145 given lambda function use `this` to create the Echo interface". |
144 | |
145 Each server's `Create` method will now be called when a new connection wants | |
146 to access the Echo interface. | |
147 | 146 |
148 ### EchoImpl: The Interface Implementation | 147 ### EchoImpl: The Interface Implementation |
149 | 148 |
150 All three implementations use the `EchoImpl` class, implementing the `Echo` | 149 All three implementations use the `EchoImpl` class, implementing the `Echo` |
151 interface we defined in our mojom file, which does what you would expect of an | 150 interface we defined in our mojom file, which does what you would expect of an |
152 echo server: it sends back the supplied value String back to the client. | 151 echo server: it sends back the supplied value String back to the client. |
153 | 152 |
154 ``` | 153 ``` |
155 callback.Run(value); | 154 callback.Run(value); |
156 ``` | 155 ``` |
(...skipping 20 matching lines...) Expand all Loading... |
177 This server creates an `EchoImpl` object, like the `SingletonServer`, but uses a | 176 This server creates an `EchoImpl` object, like the `SingletonServer`, but uses a |
178 single `Binding`, rather than a `BindingSet`. This means that when a new client | 177 single `Binding`, rather than a `BindingSet`. This means that when a new client |
179 connects to the OneAtATimeServer, the previous binding is closed, and a new | 178 connects to the OneAtATimeServer, the previous binding is closed, and a new |
180 binding is made between the new client and the interface implementation. | 179 binding is made between the new client and the interface implementation. |
181 | 180 |
182 The OneAtATimeServer demonstrates a pattern that should be avoided because it | 181 The OneAtATimeServer demonstrates a pattern that should be avoided because it |
183 contains a race condition for multiple clients. If a new client binds to the | 182 contains a race condition for multiple clients. If a new client binds to the |
184 server before the first client managed to call EchoString, the first client's | 183 server before the first client managed to call EchoString, the first client's |
185 call would cause an error. Unless you have a specific use case for this | 184 call would cause an error. Unless you have a specific use case for this |
186 behavior, it is advised to avoid creating a server like this. | 185 behavior, it is advised to avoid creating a server like this. |
OLD | NEW |