1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/spdy/spdy_websocket_stream.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "googleurl/src/gurl.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/spdy/spdy_framer.h"
#include "net/spdy/spdy_protocol.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_stream.h"
namespace net {
SpdyWebSocketStream::SpdyWebSocketStream(
SpdySession* spdy_session, Delegate* delegate)
: weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
stream_(NULL),
spdy_session_(spdy_session),
delegate_(delegate) {
DCHECK(spdy_session_);
DCHECK(delegate_);
}
SpdyWebSocketStream::~SpdyWebSocketStream() {
if (stream_) {
// If Close() has not already been called, DetachDelegate() will send a
// SPDY RST_STREAM. Deleting SpdyWebSocketStream is good enough to initiate
// graceful shutdown, so we call Close() to avoid sending a RST_STREAM.
// For safe, we should eliminate |delegate_| for OnClose() calback.
delegate_ = NULL;
stream_->Close();
// The call to Close() should call into OnClose(), which should
// set |stream_| to NULL.
DCHECK(!stream_.get());
}
}
int SpdyWebSocketStream::InitializeStream(const GURL& url,
RequestPriority request_priority,
const BoundNetLog& net_log) {
if (spdy_session_->IsClosed())
return ERR_SOCKET_NOT_CONNECTED;
int rv = stream_request_.StartRequest(
spdy_session_, url, request_priority, net_log,
base::Bind(&SpdyWebSocketStream::OnSpdyStreamCreated,
weak_ptr_factory_.GetWeakPtr()));
if (rv == OK) {
stream_ = stream_request_.ReleaseStream();
DCHECK(stream_);
stream_->SetDelegate(this);
}
return rv;
}
int SpdyWebSocketStream::SendRequest(scoped_ptr<SpdyHeaderBlock> headers) {
if (!stream_) {
NOTREACHED();
return ERR_UNEXPECTED;
}
stream_->set_spdy_headers(headers.Pass());
int result = stream_->SendRequest(true);
if (result < OK && result != ERR_IO_PENDING)
Close();
return result;
}
int SpdyWebSocketStream::SendData(const char* data, int length) {
if (!stream_) {
NOTREACHED();
return ERR_UNEXPECTED;
}
scoped_refptr<IOBuffer> buf(new IOBuffer(length));
memcpy(buf->data(), data, length);
stream_->QueueStreamData(buf.get(), length, DATA_FLAG_NONE);
return ERR_IO_PENDING;
}
void SpdyWebSocketStream::Close() {
if (stream_)
stream_->Close();
}
SpdySendStatus SpdyWebSocketStream::OnSendHeadersComplete() {
DCHECK(delegate_);
delegate_->OnSentSpdyHeaders();
return NO_MORE_DATA_TO_SEND;
}
int SpdyWebSocketStream::OnSendBody() {
NOTREACHED();
return ERR_UNEXPECTED;
}
SpdySendStatus SpdyWebSocketStream::OnSendBodyComplete(size_t bytes_sent) {
NOTREACHED();
return NO_MORE_DATA_TO_SEND;
}
int SpdyWebSocketStream::OnResponseReceived(
const SpdyHeaderBlock& response,
base::Time response_time, int status) {
DCHECK(delegate_);
return delegate_->OnReceivedSpdyResponseHeader(response, status);
}
void SpdyWebSocketStream::OnHeadersSent() {
// This will be called when WebSocket over SPDY supports new framing.
NOTREACHED();
}
int SpdyWebSocketStream::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {
DCHECK(delegate_);
delegate_->OnReceivedSpdyData(buffer.Pass());
return OK;
}
void SpdyWebSocketStream::OnDataSent(size_t bytes_sent) {
DCHECK(delegate_);
delegate_->OnSentSpdyData(bytes_sent);
}
void SpdyWebSocketStream::OnClose(int status) {
stream_ = NULL;
// Destruction without Close() call OnClose() with delegate_ being NULL.
if (!delegate_)
return;
Delegate* delegate = delegate_;
delegate_ = NULL;
delegate->OnCloseSpdyStream();
}
void SpdyWebSocketStream::OnSpdyStreamCreated(int result) {
DCHECK_NE(ERR_IO_PENDING, result);
if (result == OK) {
stream_ = stream_request_.ReleaseStream();
DCHECK(stream_);
stream_->SetDelegate(this);
}
DCHECK(delegate_);
delegate_->OnCreatedSpdyStream(result);
}
} // namespace net
|