| OLD | NEW |
| 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_server_socket.h" | 17 #include "net/socket/tcp_listen_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 scoped_ptr<net::ServerSocket> server_socket( | 45 net::TCPListenSocketFactory factory("0.0.0.0", port_); |
| 46 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 46 web_server_ = new net::HttpServer(factory, this); |
| 47 server_socket->ListenWithAddressAndPort("0.0.0.0", port_, 1); | |
| 48 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | |
| 49 net::IPEndPoint address; | 47 net::IPEndPoint address; |
| 50 int error = web_server_->GetLocalAddress(&address); | 48 int error = web_server_->GetLocalAddress(&address); |
| 51 port_ = address.port(); | 49 port_ = address.port(); |
| 52 return (error == net::OK); | 50 return (error == net::OK); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void WebSocketServer::LogMessageInfo( | 53 void WebSocketServer::LogMessageInfo( |
| 56 const mojo::MojoRequestHeader& message_header, | 54 const mojo::MojoRequestHeader& message_header, |
| 57 const GURL& url, | 55 const GURL& url, |
| 58 const base::Time& message_time) { | 56 const base::Time& message_time) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 86 int connection_id, | 84 int connection_id, |
| 87 const net::HttpServerRequestInfo& info) { | 85 const net::HttpServerRequestInfo& info) { |
| 88 web_server_->Send500(connection_id, "websockets protocol only"); | 86 web_server_->Send500(connection_id, "websockets protocol only"); |
| 89 } | 87 } |
| 90 | 88 |
| 91 void WebSocketServer::OnWebSocketRequest( | 89 void WebSocketServer::OnWebSocketRequest( |
| 92 int connection_id, | 90 int connection_id, |
| 93 const net::HttpServerRequestInfo& info) { | 91 const net::HttpServerRequestInfo& info) { |
| 94 if (connection_id_ != kNotConnected) { | 92 if (connection_id_ != kNotConnected) { |
| 95 // Reject connection since we already have our client. | 93 // Reject connection since we already have our client. |
| 96 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; | 97 return; |
| 98 } | 98 } |
| 99 // Accept the connection. | 99 // Accept the connection. |
| 100 web_server_->AcceptWebSocket(connection_id, info); | 100 web_server_->AcceptWebSocket(connection_id, info); |
| 101 connection_id_ = connection_id; | 101 connection_id_ = connection_id; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void WebSocketServer::OnWebSocketMessage( | 104 void WebSocketServer::OnWebSocketMessage( |
| 105 int connection_id, | 105 int connection_id, |
| 106 const std::string& data) { | 106 const std::string& data) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { | 151 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { |
| 152 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); | 152 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); |
| 153 } | 153 } |
| 154 | 154 |
| 155 bool WebSocketServer::Connected() const { | 155 bool WebSocketServer::Connected() const { |
| 156 return connection_id_ != kNotConnected; | 156 return connection_id_ != kNotConnected; |
| 157 } | 157 } |
| 158 | 158 |
| 159 } // namespace mojo | 159 } // namespace mojo |
| 160 |
| OLD | NEW |