Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stdio.h> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "mojo/public/c/system/main.h" | |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | |
| 10 #include "mojo/public/cpp/application/application_impl.h" | |
| 11 #include "mojo/public/cpp/application/application_runner.h" | |
| 12 #include "mojo/public/cpp/utility/run_loop.h" | |
|
yzshen1
2014/10/07 20:14:34
nit: this is not needed.
| |
| 13 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace examples { | |
| 17 | |
| 18 class HttpServerApp : public ApplicationDelegate { | |
| 19 public: | |
| 20 virtual void Initialize(ApplicationImpl* app) override { | |
| 21 app->ConnectToService("mojo:mojo_network_service", &network_service_); | |
| 22 Start(); | |
| 23 } | |
| 24 | |
| 25 private: | |
| 26 void OnListeningStarted(NetworkErrorPtr err) { | |
| 27 } | |
| 28 | |
| 29 void OnSocketBound(NetworkErrorPtr err, NetAddressPtr bound_address) { | |
| 30 if (err->code != 0) { | |
| 31 printf("Bound err = %d\n", err->code); | |
| 32 return; | |
| 33 } | |
| 34 | |
| 35 printf("Got address = %d.%d.%d.%d:%d\n", | |
| 36 (int)bound_address->ipv4->addr[0], | |
| 37 (int)bound_address->ipv4->addr[1], | |
| 38 (int)bound_address->ipv4->addr[2], | |
| 39 (int)bound_address->ipv4->addr[3], | |
| 40 (int)bound_address->ipv4->port); | |
| 41 | |
| 42 bound_socket_->StartListening( | |
| 43 GetProxy(&server_socket_), | |
| 44 base::Bind(&HttpServerApp::OnListeningStarted, | |
| 45 base::Unretained(this))); | |
| 46 } | |
| 47 | |
| 48 void Start() { | |
| 49 NetAddressPtr net_address(NetAddress::New()); | |
| 50 net_address->family = NET_ADDRESS_FAMILY_IPV4; | |
| 51 net_address->ipv4 = NetAddressIPv4::New().Pass(); | |
|
yzshen1
2014/10/07 20:14:34
nit:Pass() is not needed.
| |
| 52 net_address->ipv4->addr.resize(4); | |
| 53 net_address->ipv4->addr[0] = 0; | |
| 54 net_address->ipv4->addr[1] = 0; | |
| 55 net_address->ipv4->addr[2] = 0; | |
| 56 net_address->ipv4->addr[3] = 0; | |
| 57 | |
| 58 network_service_->CreateTCPBoundSocket( | |
| 59 net_address.Pass(), | |
| 60 GetProxy(&bound_socket_), | |
| 61 base::Bind(&HttpServerApp::OnSocketBound, base::Unretained(this))); | |
|
yzshen1
2014/10/07 20:14:34
nit: It seems you probably don't need to depend on
| |
| 62 } | |
| 63 | |
| 64 NetworkServicePtr network_service_; | |
| 65 TCPBoundSocketPtr bound_socket_; | |
| 66 TCPServerSocketPtr server_socket_; | |
| 67 }; | |
| 68 | |
| 69 } // namespace examples | |
| 70 } // namespace mojo | |
| 71 | |
| 72 MojoResult MojoMain(MojoHandle shell_handle) { | |
| 73 mojo::ApplicationRunner runner(new mojo::examples::HttpServerApp); | |
| 74 return runner.Run(shell_handle); | |
| 75 } | |
| OLD | NEW |