Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Unified Diff: mojo/examples/http_server/http_server.cc

Issue 634713002: Implement parts of the Mojo TCP interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: missing file in gyp Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/examples/http_server/BUILD.gn ('k') | mojo/mojo_examples.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/examples/http_server/http_server.cc
diff --git a/mojo/examples/http_server/http_server.cc b/mojo/examples/http_server/http_server.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cc8df2063f1f326852d8c68e3d98a5cf501de6f8
--- /dev/null
+++ b/mojo/examples/http_server/http_server.cc
@@ -0,0 +1,74 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdio.h>
+
+#include "base/bind.h"
+#include "mojo/public/c/system/main.h"
+#include "mojo/public/cpp/application/application_delegate.h"
+#include "mojo/public/cpp/application/application_impl.h"
+#include "mojo/public/cpp/application/application_runner.h"
+#include "mojo/services/public/interfaces/network/network_service.mojom.h"
+
+namespace mojo {
+namespace examples {
+
+class HttpServerApp : public ApplicationDelegate {
+ public:
+ virtual void Initialize(ApplicationImpl* app) override {
+ app->ConnectToService("mojo:mojo_network_service", &network_service_);
+ Start();
+ }
+
+ private:
+ void OnListeningStarted(NetworkErrorPtr err) {
+ }
+
+ void OnSocketBound(NetworkErrorPtr err, NetAddressPtr bound_address) {
+ if (err->code != 0) {
+ printf("Bound err = %d\n", err->code);
+ return;
+ }
+
+ printf("Got address = %d.%d.%d.%d:%d\n",
+ (int)bound_address->ipv4->addr[0],
+ (int)bound_address->ipv4->addr[1],
+ (int)bound_address->ipv4->addr[2],
+ (int)bound_address->ipv4->addr[3],
+ (int)bound_address->ipv4->port);
+
+ bound_socket_->StartListening(
+ GetProxy(&server_socket_),
+ base::Bind(&HttpServerApp::OnListeningStarted,
+ base::Unretained(this)));
+ }
+
+ void Start() {
+ NetAddressPtr net_address(NetAddress::New());
+ net_address->family = NET_ADDRESS_FAMILY_IPV4;
+ net_address->ipv4 = NetAddressIPv4::New();
+ net_address->ipv4->addr.resize(4);
+ net_address->ipv4->addr[0] = 0;
+ net_address->ipv4->addr[1] = 0;
+ net_address->ipv4->addr[2] = 0;
+ net_address->ipv4->addr[3] = 0;
+
+ network_service_->CreateTCPBoundSocket(
+ net_address.Pass(),
+ GetProxy(&bound_socket_),
+ base::Bind(&HttpServerApp::OnSocketBound, base::Unretained(this)));
+ }
+
+ NetworkServicePtr network_service_;
+ TCPBoundSocketPtr bound_socket_;
+ TCPServerSocketPtr server_socket_;
+};
+
+} // namespace examples
+} // namespace mojo
+
+MojoResult MojoMain(MojoHandle shell_handle) {
+ mojo::ApplicationRunner runner(new mojo::examples::HttpServerApp);
+ return runner.Run(shell_handle);
+}
« no previous file with comments | « mojo/examples/http_server/BUILD.gn ('k') | mojo/mojo_examples.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698