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

Side by Side Diff: mojo/spy/websocket_server.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 6 years, 4 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/spy/websocket_server.h" 5 #include "mojo/spy/websocket_server.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "mojo/public/cpp/bindings/message.h" 12 #include "mojo/public/cpp/bindings/message.h"
13 #include "net/base/ip_endpoint.h" 13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "net/server/http_server_request_info.h" 15 #include "net/server/http_server_request_info.h"
16 #include "net/server/http_server_response_info.h" 16 #include "net/server/http_server_response_info.h"
17 #include "net/socket/tcp_listen_socket.h" 17 #include "net/socket/tcp_server_socket.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 19
20 namespace mojo { 20 namespace mojo {
21 21
22 const int kNotConnected = -1; 22 const int kNotConnected = -1;
23 23
24 #define MOJO_DEBUGGER_MESSAGE_FORMAT "\"url: %s\n," \ 24 #define MOJO_DEBUGGER_MESSAGE_FORMAT "\"url: %s\n," \
25 "time: %02d:%02d:%02d\n,"\ 25 "time: %02d:%02d:%02d\n,"\
26 "bytes: %u\n,"\ 26 "bytes: %u\n,"\
27 "fields: %u\n,"\ 27 "fields: %u\n,"\
28 "name: %u\n,"\ 28 "name: %u\n,"\
29 "requestId: %llu\n,"\ 29 "requestId: %llu\n,"\
30 "type: %s\n,"\ 30 "type: %s\n,"\
31 "end:\n\"" 31 "end:\n\""
32 32
33 WebSocketServer::WebSocketServer(int port, 33 WebSocketServer::WebSocketServer(int port,
34 mojo::ScopedMessagePipeHandle server_pipe) 34 mojo::ScopedMessagePipeHandle server_pipe)
35 : port_(port), 35 : port_(port),
36 connection_id_(kNotConnected), 36 connection_id_(kNotConnected),
37 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) { 37 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) {
38 spy_server_.set_client(this); 38 spy_server_.set_client(this);
39 } 39 }
40 40
41 WebSocketServer::~WebSocketServer() { 41 WebSocketServer::~WebSocketServer() {
42 } 42 }
43 43
44 bool WebSocketServer::Start() { 44 bool WebSocketServer::Start() {
45 net::TCPListenSocketFactory factory("0.0.0.0", port_); 45 scoped_ptr<net::ServerSocket> server_socket(
46 web_server_ = new net::HttpServer(factory, this); 46 new net::TCPServerSocket(NULL, net::NetLog::Source()));
47 server_socket->ListenWithAddressAndPort("0.0.0.0", port_, 1);
48 web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
47 net::IPEndPoint address; 49 net::IPEndPoint address;
48 int error = web_server_->GetLocalAddress(&address); 50 int error = web_server_->GetLocalAddress(&address);
49 port_ = address.port(); 51 port_ = address.port();
50 return (error == net::OK); 52 return (error == net::OK);
51 } 53 }
52 54
53 void WebSocketServer::LogMessageInfo( 55 void WebSocketServer::LogMessageInfo(
54 const mojo::MojoRequestHeader& message_header, 56 const mojo::MojoRequestHeader& message_header,
55 const GURL& url, 57 const GURL& url,
56 const base::Time& message_time) { 58 const base::Time& message_time) {
(...skipping 26 matching lines...) Expand all
83 void WebSocketServer::OnHttpRequest( 85 void WebSocketServer::OnHttpRequest(
84 int connection_id, 86 int connection_id,
85 const net::HttpServerRequestInfo& info) { 87 const net::HttpServerRequestInfo& info) {
86 web_server_->Send500(connection_id, "websockets protocol only"); 88 web_server_->Send500(connection_id, "websockets protocol only");
87 } 89 }
88 90
89 void WebSocketServer::OnWebSocketRequest( 91 void WebSocketServer::OnWebSocketRequest(
90 int connection_id, 92 int connection_id,
91 const net::HttpServerRequestInfo& info) { 93 const net::HttpServerRequestInfo& info) {
92 if (connection_id_ != kNotConnected) { 94 if (connection_id_ != kNotConnected) {
93 // Reject connection since we already have our client. 95 web_server_->Close(connection_id);
94 base::MessageLoop::current()->PostTask(
95 FROM_HERE,
96 base::Bind(&net::HttpServer::Close, web_server_, connection_id));
97 return; 96 return;
98 } 97 }
99 // Accept the connection. 98 // Accept the connection.
100 web_server_->AcceptWebSocket(connection_id, info); 99 web_server_->AcceptWebSocket(connection_id, info);
101 connection_id_ = connection_id; 100 connection_id_ = connection_id;
102 } 101 }
103 102
104 void WebSocketServer::OnWebSocketMessage( 103 void WebSocketServer::OnWebSocketMessage(
105 int connection_id, 104 int connection_id,
106 const std::string& data) { 105 const std::string& data) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 149
151 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { 150 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) {
152 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); 151 web_server_->SendOverWebSocket(connection_id_, "\"ok start\"");
153 } 152 }
154 153
155 bool WebSocketServer::Connected() const { 154 bool WebSocketServer::Connected() const {
156 return connection_id_ != kNotConnected; 155 return connection_id_ != kNotConnected;
157 } 156 }
158 157
159 } // namespace mojo 158 } // namespace mojo
160
OLDNEW
« no previous file with comments | « mojo/spy/websocket_server.h ('k') | net/net.gypi » ('j') | net/server/http_connection.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698