| 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/services/public/interfaces/network/network_service.mojom.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace examples { |
| 16 |
| 17 class HttpServerApp : public ApplicationDelegate { |
| 18 public: |
| 19 virtual void Initialize(ApplicationImpl* app) override { |
| 20 app->ConnectToService("mojo:mojo_network_service", &network_service_); |
| 21 Start(); |
| 22 } |
| 23 |
| 24 private: |
| 25 void OnListeningStarted(NetworkErrorPtr err) { |
| 26 } |
| 27 |
| 28 void OnSocketBound(NetworkErrorPtr err, NetAddressPtr bound_address) { |
| 29 if (err->code != 0) { |
| 30 printf("Bound err = %d\n", err->code); |
| 31 return; |
| 32 } |
| 33 |
| 34 printf("Got address = %d.%d.%d.%d:%d\n", |
| 35 (int)bound_address->ipv4->addr[0], |
| 36 (int)bound_address->ipv4->addr[1], |
| 37 (int)bound_address->ipv4->addr[2], |
| 38 (int)bound_address->ipv4->addr[3], |
| 39 (int)bound_address->ipv4->port); |
| 40 |
| 41 bound_socket_->StartListening( |
| 42 GetProxy(&server_socket_), |
| 43 base::Bind(&HttpServerApp::OnListeningStarted, |
| 44 base::Unretained(this))); |
| 45 } |
| 46 |
| 47 void Start() { |
| 48 NetAddressPtr net_address(NetAddress::New()); |
| 49 net_address->family = NET_ADDRESS_FAMILY_IPV4; |
| 50 net_address->ipv4 = NetAddressIPv4::New(); |
| 51 net_address->ipv4->addr.resize(4); |
| 52 net_address->ipv4->addr[0] = 0; |
| 53 net_address->ipv4->addr[1] = 0; |
| 54 net_address->ipv4->addr[2] = 0; |
| 55 net_address->ipv4->addr[3] = 0; |
| 56 |
| 57 network_service_->CreateTCPBoundSocket( |
| 58 net_address.Pass(), |
| 59 GetProxy(&bound_socket_), |
| 60 base::Bind(&HttpServerApp::OnSocketBound, base::Unretained(this))); |
| 61 } |
| 62 |
| 63 NetworkServicePtr network_service_; |
| 64 TCPBoundSocketPtr bound_socket_; |
| 65 TCPServerSocketPtr server_socket_; |
| 66 }; |
| 67 |
| 68 } // namespace examples |
| 69 } // namespace mojo |
| 70 |
| 71 MojoResult MojoMain(MojoHandle shell_handle) { |
| 72 mojo::ApplicationRunner runner(new mojo::examples::HttpServerApp); |
| 73 return runner.Run(shell_handle); |
| 74 } |
| OLD | NEW |