| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <limits> | 6 #include <limits> |
| 7 | 7 |
| 8 #include "net/websockets/websocket.h" | 8 #include "net/websockets/websocket.h" |
| 9 | 9 |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 DCHECK(ready_state_ == OPEN); | 75 DCHECK(ready_state_ == OPEN); |
| 76 DCHECK(MessageLoop::current() == origin_loop_); | 76 DCHECK(MessageLoop::current() == origin_loop_); |
| 77 | 77 |
| 78 IOBufferWithSize* buf = new IOBufferWithSize(msg.size() + 2); | 78 IOBufferWithSize* buf = new IOBufferWithSize(msg.size() + 2); |
| 79 char* p = buf->data(); | 79 char* p = buf->data(); |
| 80 *p = '\0'; | 80 *p = '\0'; |
| 81 memcpy(p + 1, msg.data(), msg.size()); | 81 memcpy(p + 1, msg.data(), msg.size()); |
| 82 *(p + 1 + msg.size()) = '\xff'; | 82 *(p + 1 + msg.size()) = '\xff'; |
| 83 pending_write_bufs_.push_back(buf); | 83 pending_write_bufs_.push_back(make_scoped_refptr(buf)); |
| 84 SendPending(); | 84 SendPending(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void WebSocket::Close() { | 87 void WebSocket::Close() { |
| 88 DCHECK(MessageLoop::current() == origin_loop_); | 88 DCHECK(MessageLoop::current() == origin_loop_); |
| 89 | 89 |
| 90 // If connection has not yet started, do nothing. | 90 // If connection has not yet started, do nothing. |
| 91 if (ready_state_ == INITIALIZED) { | 91 if (ready_state_ == INITIALIZED) { |
| 92 DCHECK(!socket_stream_); | 92 DCHECK(!socket_stream_); |
| 93 ready_state_ = CLOSED; | 93 ready_state_ = CLOSED; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 request_->url(), request_->origin(), request_->location(), | 165 request_->url(), request_->origin(), request_->location(), |
| 166 request_->protocol())); | 166 request_->protocol())); |
| 167 break; | 167 break; |
| 168 default: | 168 default: |
| 169 NOTREACHED() << "Unexpected protocol version:" << request_->version(); | 169 NOTREACHED() << "Unexpected protocol version:" << request_->version(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 const std::string msg = handshake_->CreateClientHandshakeMessage(); | 172 const std::string msg = handshake_->CreateClientHandshakeMessage(); |
| 173 IOBufferWithSize* buf = new IOBufferWithSize(msg.size()); | 173 IOBufferWithSize* buf = new IOBufferWithSize(msg.size()); |
| 174 memcpy(buf->data(), msg.data(), msg.size()); | 174 memcpy(buf->data(), msg.data(), msg.size()); |
| 175 pending_write_bufs_.push_back(buf); | 175 pending_write_bufs_.push_back(make_scoped_refptr(buf)); |
| 176 origin_loop_->PostTask(FROM_HERE, | 176 origin_loop_->PostTask(FROM_HERE, |
| 177 NewRunnableMethod(this, &WebSocket::SendPending)); | 177 NewRunnableMethod(this, &WebSocket::SendPending)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void WebSocket::OnSentData(SocketStream* socket_stream, int amount_sent) { | 180 void WebSocket::OnSentData(SocketStream* socket_stream, int amount_sent) { |
| 181 DCHECK(socket_stream == socket_stream_); | 181 DCHECK(socket_stream == socket_stream_); |
| 182 DCHECK(current_write_buf_); | 182 DCHECK(current_write_buf_); |
| 183 current_write_buf_->DidConsume(amount_sent); | 183 current_write_buf_->DidConsume(amount_sent); |
| 184 DCHECK_GE(current_write_buf_->BytesRemaining(), 0); | 184 DCHECK_GE(current_write_buf_->BytesRemaining(), 0); |
| 185 if (current_write_buf_->BytesRemaining() == 0) { | 185 if (current_write_buf_->BytesRemaining() == 0) { |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 // 4.2 *start the WebSocket closing handshake*. | 423 // 4.2 *start the WebSocket closing handshake*. |
| 424 if (closing_handshake_started_ || client_closing_handshake_) { | 424 if (closing_handshake_started_ || client_closing_handshake_) { |
| 425 // 1. If the WebSocket closing handshake has started, then abort these | 425 // 1. If the WebSocket closing handshake has started, then abort these |
| 426 // steps. | 426 // steps. |
| 427 return; | 427 return; |
| 428 } | 428 } |
| 429 // 2.,3. Send a 0xFF and 0x00 byte to the server. | 429 // 2.,3. Send a 0xFF and 0x00 byte to the server. |
| 430 client_closing_handshake_ = true; | 430 client_closing_handshake_ = true; |
| 431 IOBufferWithSize* buf = new IOBufferWithSize(2); | 431 IOBufferWithSize* buf = new IOBufferWithSize(2); |
| 432 memcpy(buf->data(), kClosingFrame, 2); | 432 memcpy(buf->data(), kClosingFrame, 2); |
| 433 pending_write_bufs_.push_back(buf); | 433 pending_write_bufs_.push_back(make_scoped_refptr(buf)); |
| 434 SendPending(); | 434 SendPending(); |
| 435 } | 435 } |
| 436 | 436 |
| 437 void WebSocket::DoForceCloseConnection() { | 437 void WebSocket::DoForceCloseConnection() { |
| 438 // 4.2 *start the WebSocket closing handshake* | 438 // 4.2 *start the WebSocket closing handshake* |
| 439 // 6. If the WebSocket connection is not already closed, then close the | 439 // 6. If the WebSocket connection is not already closed, then close the |
| 440 // WebSocket connection. (If this happens, then the closing handshake | 440 // WebSocket connection. (If this happens, then the closing handshake |
| 441 // doesn't finish.) | 441 // doesn't finish.) |
| 442 DCHECK(MessageLoop::current() == origin_loop_); | 442 DCHECK(MessageLoop::current() == origin_loop_); |
| 443 force_close_task_ = NULL; | 443 force_close_task_ = NULL; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 Release(); | 475 Release(); |
| 476 } | 476 } |
| 477 | 477 |
| 478 void WebSocket::DoSocketError(int error) { | 478 void WebSocket::DoSocketError(int error) { |
| 479 DCHECK(MessageLoop::current() == origin_loop_); | 479 DCHECK(MessageLoop::current() == origin_loop_); |
| 480 if (delegate_) | 480 if (delegate_) |
| 481 delegate_->OnSocketError(this, error); | 481 delegate_->OnSocketError(this, error); |
| 482 } | 482 } |
| 483 | 483 |
| 484 } // namespace net | 484 } // namespace net |
| OLD | NEW |