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

Side by Side Diff: remoting/host/websocket_listener.cc

Issue 11358190: Add simple WebSocket server implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "remoting/host/websocket_listener.h"
6
7 #include <vector>
8
9 #include "base/compiler_specific.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/stl_util.h"
13 #include "base/sys_byteorder.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/net_errors.h"
16 #include "net/socket/stream_socket.h"
17 #include "remoting/host/websocket_connection.h"
18
19 namespace remoting {
20
21 namespace {
22 const int kTcpListenBacklog = 2;
Wez 2012/11/15 02:28:37 nit: The conventional value for this is 5. Does n
Sergey Ulanov 2012/11/15 21:01:03 Done.
23 } // namespace
24
25 WebsocketListener::WebsocketListener()
26 : tcp_socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())),
27 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
28 }
29
30 WebsocketListener::~WebsocketListener() {
31 STLDeleteElements(&pending_connections_);
32 }
33
34 bool WebsocketListener::Listen(const net::IPEndPoint& address,
35 const NewConnectionCallback& callback) {
36 new_connection_callback_ = callback;
Wez 2012/11/15 02:28:37 nit: Blank line after this.
Sergey Ulanov 2012/11/15 21:01:03 Done.
37 int result = tcp_socket_->Listen(address, kTcpListenBacklog);
38 if (result != net::OK) {
39 return false;
40 }
41
42 DoAccept();
43 return true;
44 }
45
46 void WebsocketListener::DoAccept() {
47 while (true) {
48 int result = tcp_socket_->Accept(
49 &accepted_socket_, base::Bind(&WebsocketListener::OnAccepted,
50 base::Unretained(this)));
51 if (result != net::ERR_IO_PENDING) {
52 HandleAcceptResult(result);
53 }
54 if (result != net::OK) {
55 break;
56 }
57 }
58 }
59
60 void WebsocketListener::OnAccepted(int result) {
61 DCHECK_NE(result, net::ERR_IO_PENDING);
62 if (result == net::OK) {
63 // The call for WebsocketConnection::Start() may result in delegate being
64 // called which is allowed to destroyed this object, so we need to post a
65 // task to call DoAccept() again asynchronously.
66 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE, base::Bind(&WebsocketListener::DoAccept,
68 weak_factory_.GetWeakPtr()));
69 }
70 HandleAcceptResult(result);
71 }
72
73 void WebsocketListener::HandleAcceptResult(int result) {
74 if (result == net::OK) {
75 WebsocketConnection* connection = new WebsocketConnection();
76 pending_connections_.insert(connection);
77 connection->Start(
78 accepted_socket_.Pass(),
79 base::Bind(&WebsocketListener::OnConnected, weak_factory_.GetWeakPtr(),
80 connection));
81 } else {
82 LOG(ERROR) << "Error when trying to accept WebSocket connection: "
83 << result;
84 }
85 }
86
87 void WebsocketListener::OnConnected(WebsocketConnection* connection_ptr,
88 bool handshake_result) {
89 scoped_ptr<WebsocketConnection> connection(connection_ptr);
90 pending_connections_.erase(connection_ptr);
91 if (handshake_result) {
92 new_connection_callback_.Run(connection.Pass());
93 }
94 }
95
96 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698