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

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

Issue 258693011: First cut at mojo spy's websocket server (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « mojo/spy/websocket_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "mojo/spy/websocket_server.h"
6
7 #include "base/bind.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/base/ip_endpoint.h"
10 #include "net/base/net_errors.h"
11 #include "net/server/http_server_request_info.h"
12 #include "net/server/http_server_response_info.h"
13 #include "net/socket/tcp_listen_socket.h"
14
15 namespace spy {
16
17 const int kNotConnected = -1;
18
19 WebSocketServer::WebSocketServer(int port)
20 : port_(port), connection_id_(kNotConnected) {
21 }
22
23 WebSocketServer::~WebSocketServer() {
24 }
25
26 bool WebSocketServer::Start() {
27 net::TCPListenSocketFactory factory("0.0.0.0", port_);
28 server_ = new net::HttpServer(factory, this);
29 net::IPEndPoint address;
30 int error = server_->GetLocalAddress(&address);
31 port_ = address.port();
32 return (error == net::OK);
33 }
34
35 void WebSocketServer::OnHttpRequest(
36 int connection_id,
37 const net::HttpServerRequestInfo& info) {
38 server_->Send500(connection_id, "websockets protocol only");
39 }
40
41 void WebSocketServer::OnWebSocketRequest(
42 int connection_id,
43 const net::HttpServerRequestInfo& info) {
44 if (connection_id_ != kNotConnected) {
45 // Reject connection since we already have our client.
46 base::MessageLoop::current()->PostTask(
47 FROM_HERE,
48 base::Bind(&net::HttpServer::Close, server_, connection_id));
49 return;
50 }
51 // Accept the connection.
52 server_->AcceptWebSocket(connection_id, info);
53 connection_id_ = connection_id;
54 }
55
56 void WebSocketServer::OnWebSocketMessage(
57 int connection_id,
58 const std::string& data) {
59 // TODO(cpu): remove this test code soon.
60 if (data == "\"hello\"")
61 server_->SendOverWebSocket(connection_id, "\"hi there!\"");
62 }
63
64 void WebSocketServer::OnClose(
65 int connection_id) {
66 if (connection_id == connection_id_)
67 connection_id_ = kNotConnected;
68 }
69
70 } // namespace spy
OLDNEW
« no previous file with comments | « mojo/spy/websocket_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698