| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/spdy/spdy_websocket_stream.h" | 5 #include "net/spdy/spdy_websocket_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/spdy/spdy_framer.h" | 13 #include "net/spdy/spdy_framer.h" |
| 14 #include "net/spdy/spdy_protocol.h" | 14 #include "net/spdy/spdy_protocol.h" |
| 15 #include "net/spdy/spdy_session.h" | 15 #include "net/spdy/spdy_session.h" |
| 16 #include "net/spdy/spdy_stream.h" | 16 #include "net/spdy/spdy_stream.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 SpdyWebSocketStream::SpdyWebSocketStream( | 20 SpdyWebSocketStream::SpdyWebSocketStream( |
| 21 SpdySession* spdy_session, Delegate* delegate) | 21 SpdySession* spdy_session, Delegate* delegate) |
| 22 : weak_ptr_factory_(this), | 22 : weak_ptr_factory_(this), |
| 23 spdy_session_(spdy_session), | 23 spdy_session_(spdy_session), |
| 24 pending_send_data_length_(0), |
| 24 delegate_(delegate) { | 25 delegate_(delegate) { |
| 25 DCHECK(spdy_session_); | 26 DCHECK(spdy_session_); |
| 26 DCHECK(delegate_); | 27 DCHECK(delegate_); |
| 27 } | 28 } |
| 28 | 29 |
| 29 SpdyWebSocketStream::~SpdyWebSocketStream() { | 30 SpdyWebSocketStream::~SpdyWebSocketStream() { |
| 30 delegate_ = NULL; | 31 delegate_ = NULL; |
| 31 Close(); | 32 Close(); |
| 32 } | 33 } |
| 33 | 34 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 60 if (result < OK && result != ERR_IO_PENDING) | 61 if (result < OK && result != ERR_IO_PENDING) |
| 61 Close(); | 62 Close(); |
| 62 return result; | 63 return result; |
| 63 } | 64 } |
| 64 | 65 |
| 65 int SpdyWebSocketStream::SendData(const char* data, int length) { | 66 int SpdyWebSocketStream::SendData(const char* data, int length) { |
| 66 if (!stream_) { | 67 if (!stream_) { |
| 67 NOTREACHED(); | 68 NOTREACHED(); |
| 68 return ERR_UNEXPECTED; | 69 return ERR_UNEXPECTED; |
| 69 } | 70 } |
| 71 DCHECK_GE(length, 0); |
| 72 pending_send_data_length_ = static_cast<size_t>(length); |
| 70 scoped_refptr<IOBuffer> buf(new IOBuffer(length)); | 73 scoped_refptr<IOBuffer> buf(new IOBuffer(length)); |
| 71 memcpy(buf->data(), data, length); | 74 memcpy(buf->data(), data, length); |
| 72 stream_->QueueStreamData(buf.get(), length, DATA_FLAG_NONE); | 75 stream_->SendStreamData(buf.get(), length, DATA_FLAG_NONE); |
| 73 return ERR_IO_PENDING; | 76 return ERR_IO_PENDING; |
| 74 } | 77 } |
| 75 | 78 |
| 76 void SpdyWebSocketStream::Close() { | 79 void SpdyWebSocketStream::Close() { |
| 77 if (stream_) { | 80 if (stream_) { |
| 78 stream_->Close(); | 81 stream_->Close(); |
| 79 DCHECK(!stream_); | 82 DCHECK(!stream_); |
| 80 } | 83 } |
| 81 } | 84 } |
| 82 | 85 |
| 83 SpdySendStatus SpdyWebSocketStream::OnSendHeadersComplete() { | 86 SpdySendStatus SpdyWebSocketStream::OnSendHeadersComplete() { |
| 84 DCHECK(delegate_); | 87 DCHECK(delegate_); |
| 85 delegate_->OnSentSpdyHeaders(); | 88 delegate_->OnSentSpdyHeaders(); |
| 86 return NO_MORE_DATA_TO_SEND; | 89 return NO_MORE_DATA_TO_SEND; |
| 87 } | 90 } |
| 88 | 91 |
| 89 void SpdyWebSocketStream::OnSendBody() { | 92 void SpdyWebSocketStream::OnSendBody() { |
| 90 CHECK(false); | 93 CHECK(false); |
| 91 } | 94 } |
| 92 | 95 |
| 93 SpdySendStatus SpdyWebSocketStream::OnSendBodyComplete(size_t bytes_sent) { | 96 SpdySendStatus SpdyWebSocketStream::OnSendBodyComplete() { |
| 94 CHECK(false); | 97 CHECK(false); |
| 95 return NO_MORE_DATA_TO_SEND; | 98 return NO_MORE_DATA_TO_SEND; |
| 96 } | 99 } |
| 97 | 100 |
| 98 int SpdyWebSocketStream::OnResponseReceived( | 101 int SpdyWebSocketStream::OnResponseReceived( |
| 99 const SpdyHeaderBlock& response, | 102 const SpdyHeaderBlock& response, |
| 100 base::Time response_time, int status) { | 103 base::Time response_time, int status) { |
| 101 DCHECK(delegate_); | 104 DCHECK(delegate_); |
| 102 return delegate_->OnReceivedSpdyResponseHeader(response, status); | 105 return delegate_->OnReceivedSpdyResponseHeader(response, status); |
| 103 } | 106 } |
| 104 | 107 |
| 105 void SpdyWebSocketStream::OnHeadersSent() { | 108 void SpdyWebSocketStream::OnHeadersSent() { |
| 106 // This will be called when WebSocket over SPDY supports new framing. | 109 // This will be called when WebSocket over SPDY supports new framing. |
| 107 NOTREACHED(); | 110 NOTREACHED(); |
| 108 } | 111 } |
| 109 | 112 |
| 110 int SpdyWebSocketStream::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { | 113 int SpdyWebSocketStream::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) { |
| 111 DCHECK(delegate_); | 114 DCHECK(delegate_); |
| 112 delegate_->OnReceivedSpdyData(buffer.Pass()); | 115 delegate_->OnReceivedSpdyData(buffer.Pass()); |
| 113 return OK; | 116 return OK; |
| 114 } | 117 } |
| 115 | 118 |
| 116 void SpdyWebSocketStream::OnDataSent(size_t bytes_sent) { | 119 void SpdyWebSocketStream::OnDataSent() { |
| 117 DCHECK(delegate_); | 120 DCHECK(delegate_); |
| 118 delegate_->OnSentSpdyData(bytes_sent); | 121 delegate_->OnSentSpdyData(pending_send_data_length_); |
| 122 pending_send_data_length_ = 0; |
| 119 } | 123 } |
| 120 | 124 |
| 121 void SpdyWebSocketStream::OnClose(int status) { | 125 void SpdyWebSocketStream::OnClose(int status) { |
| 122 stream_.reset(); | 126 stream_.reset(); |
| 123 | 127 |
| 124 // Destruction without Close() call OnClose() with delegate_ being NULL. | 128 // Destruction without Close() call OnClose() with delegate_ being NULL. |
| 125 if (!delegate_) | 129 if (!delegate_) |
| 126 return; | 130 return; |
| 127 Delegate* delegate = delegate_; | 131 Delegate* delegate = delegate_; |
| 128 delegate_ = NULL; | 132 delegate_ = NULL; |
| 129 delegate->OnCloseSpdyStream(); | 133 delegate->OnCloseSpdyStream(); |
| 130 } | 134 } |
| 131 | 135 |
| 132 void SpdyWebSocketStream::OnSpdyStreamCreated(int result) { | 136 void SpdyWebSocketStream::OnSpdyStreamCreated(int result) { |
| 133 DCHECK_NE(ERR_IO_PENDING, result); | 137 DCHECK_NE(ERR_IO_PENDING, result); |
| 134 if (result == OK) { | 138 if (result == OK) { |
| 135 stream_ = stream_request_.ReleaseStream(); | 139 stream_ = stream_request_.ReleaseStream(); |
| 136 DCHECK(stream_); | 140 DCHECK(stream_); |
| 137 stream_->SetDelegate(this); | 141 stream_->SetDelegate(this); |
| 138 } | 142 } |
| 139 DCHECK(delegate_); | 143 DCHECK(delegate_); |
| 140 delegate_->OnCreatedSpdyStream(result); | 144 delegate_->OnCreatedSpdyStream(result); |
| 141 } | 145 } |
| 142 | 146 |
| 143 } // namespace net | 147 } // namespace net |
| OLD | NEW |