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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/server/http_server_request_info.h" | 11 #include "net/server/http_server_request_info.h" |
12 #include "net/server/http_server_response_info.h" | 12 #include "net/server/http_server_response_info.h" |
13 #include "net/socket/tcp_listen_socket.h" | 13 #include "net/socket/tcp_server_socket.h" |
14 | 14 |
15 namespace spy { | 15 namespace spy { |
16 | 16 |
17 const int kNotConnected = -1; | 17 const int kNotConnected = -1; |
18 | 18 |
19 WebSocketServer::WebSocketServer(int port) | 19 WebSocketServer::WebSocketServer(int port) |
20 : port_(port), connection_id_(kNotConnected) { | 20 : port_(port), connection_id_(kNotConnected) { |
21 } | 21 } |
22 | 22 |
23 WebSocketServer::~WebSocketServer() { | 23 WebSocketServer::~WebSocketServer() { |
24 } | 24 } |
25 | 25 |
26 bool WebSocketServer::Start() { | 26 bool WebSocketServer::Start() { |
27 net::TCPListenSocketFactory factory("0.0.0.0", port_); | 27 scoped_ptr<net::ServerSocket> server_socket( |
28 server_ = new net::HttpServer(factory, this); | 28 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 29 server_socket->ListenWithAddressAndPort("0.0.0.0", 0, 1); |
| 30 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
29 net::IPEndPoint address; | 31 net::IPEndPoint address; |
30 int error = server_->GetLocalAddress(&address); | 32 int error = server_->GetLocalAddress(&address); |
31 port_ = address.port(); | 33 port_ = address.port(); |
32 return (error == net::OK); | 34 return (error == net::OK); |
33 } | 35 } |
34 | 36 |
35 void WebSocketServer::OnHttpRequest( | 37 void WebSocketServer::OnHttpRequest( |
36 int connection_id, | 38 int connection_id, |
37 const net::HttpServerRequestInfo& info) { | 39 const net::HttpServerRequestInfo& info) { |
38 server_->Send500(connection_id, "websockets protocol only"); | 40 server_->Send500(connection_id, "websockets protocol only"); |
39 } | 41 } |
40 | 42 |
41 void WebSocketServer::OnWebSocketRequest( | 43 void WebSocketServer::OnWebSocketRequest( |
42 int connection_id, | 44 int connection_id, |
43 const net::HttpServerRequestInfo& info) { | 45 const net::HttpServerRequestInfo& info) { |
44 if (connection_id_ != kNotConnected) { | 46 if (connection_id_ != kNotConnected) { |
45 // Reject connection since we already have our client. | 47 server_->Close(connection_id); |
46 base::MessageLoop::current()->PostTask( | |
47 FROM_HERE, | |
48 base::Bind(&net::HttpServer::Close, server_, connection_id)); | |
49 return; | 48 return; |
50 } | 49 } |
51 // Accept the connection. | 50 // Accept the connection. |
52 server_->AcceptWebSocket(connection_id, info); | 51 server_->AcceptWebSocket(connection_id, info); |
53 connection_id_ = connection_id; | 52 connection_id_ = connection_id; |
54 } | 53 } |
55 | 54 |
56 void WebSocketServer::OnWebSocketMessage( | 55 void WebSocketServer::OnWebSocketMessage( |
57 int connection_id, | 56 int connection_id, |
58 const std::string& data) { | 57 const std::string& data) { |
59 // TODO(cpu): remove this test code soon. | 58 // TODO(cpu): remove this test code soon. |
60 if (data == "\"hello\"") | 59 if (data == "\"hello\"") |
61 server_->SendOverWebSocket(connection_id, "\"hi there!\""); | 60 server_->SendOverWebSocket(connection_id, "\"hi there!\""); |
62 } | 61 } |
63 | 62 |
64 void WebSocketServer::OnClose( | 63 void WebSocketServer::OnClose( |
65 int connection_id) { | 64 int connection_id) { |
66 if (connection_id == connection_id_) | 65 if (connection_id == connection_id_) |
67 connection_id_ = kNotConnected; | 66 connection_id_ = kNotConnected; |
68 } | 67 } |
69 | 68 |
70 } // namespace spy | 69 } // namespace spy |
OLD | NEW |