| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/socket/web_socket_server_socket.h" | 5 #include "net/socket/web_socket_server_socket.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 WebSocketServerSocketImpl(net::Socket* transport_socket, Delegate* delegate) | 111 WebSocketServerSocketImpl(net::Socket* transport_socket, Delegate* delegate) |
| 112 : phase_(PHASE_NYMPH), | 112 : phase_(PHASE_NYMPH), |
| 113 frame_bytes_remaining_(0), | 113 frame_bytes_remaining_(0), |
| 114 transport_socket_(transport_socket), | 114 transport_socket_(transport_socket), |
| 115 delegate_(delegate), | 115 delegate_(delegate), |
| 116 handshake_buf_(new net::IOBuffer(kHandshakeLimitBytes)), | 116 handshake_buf_(new net::IOBuffer(kHandshakeLimitBytes)), |
| 117 fill_handshake_buf_(new net::DrainableIOBuffer( | 117 fill_handshake_buf_(new net::DrainableIOBuffer( |
| 118 handshake_buf_, kHandshakeLimitBytes)), | 118 handshake_buf_, kHandshakeLimitBytes)), |
| 119 process_handshake_buf_(new net::DrainableIOBuffer( | 119 process_handshake_buf_(new net::DrainableIOBuffer( |
| 120 handshake_buf_, kHandshakeLimitBytes)), | 120 handshake_buf_, kHandshakeLimitBytes)), |
| 121 transport_read_callback_(NewCallback( | |
| 122 this, &WebSocketServerSocketImpl::OnRead)), | |
| 123 transport_write_callback_(NewCallback( | |
| 124 this, &WebSocketServerSocketImpl::OnWrite)), | |
| 125 is_transport_read_pending_(false), | 121 is_transport_read_pending_(false), |
| 126 is_transport_write_pending_(false), | 122 is_transport_write_pending_(false), |
| 127 method_factory_(this) { | 123 method_factory_(this) { |
| 128 DCHECK(transport_socket); | 124 DCHECK(transport_socket); |
| 129 DCHECK(delegate); | 125 DCHECK(delegate); |
| 130 } | 126 } |
| 131 | 127 |
| 132 virtual ~WebSocketServerSocketImpl() { | 128 virtual ~WebSocketServerSocketImpl() { |
| 133 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_READ); | 129 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_READ); |
| 134 if (it != pending_reqs_.end() && | 130 if (it != pending_reqs_.end() && |
| 135 it->type == PendingReq::TYPE_READ && | 131 it->type == PendingReq::TYPE_READ && |
| 136 it->io_buf != NULL && | 132 it->io_buf != NULL && |
| 137 it->io_buf->data() != NULL && | 133 it->io_buf->data() != NULL && |
| 138 (it->old_callback || !it->callback.is_null())) { | 134 !it->callback.is_null()) { |
| 139 if (it->old_callback) | 135 it->callback.Run(0); // Report EOF. |
| 140 it->old_callback->Run(0); // Report EOF. | |
| 141 else | |
| 142 it->callback.Run(0); | |
| 143 } | 136 } |
| 144 } | 137 } |
| 145 | 138 |
| 146 private: | 139 private: |
| 147 enum Phase { | 140 enum Phase { |
| 148 // Before Accept() is called. | 141 // Before Accept() is called. |
| 149 PHASE_NYMPH, | 142 PHASE_NYMPH, |
| 150 | 143 |
| 151 // After Accept() is called and until handshake success/fail. | 144 // After Accept() is called and until handshake success/fail. |
| 152 PHASE_HANDSHAKE, | 145 PHASE_HANDSHAKE, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 168 // Read request. | 161 // Read request. |
| 169 TYPE_READ = 1 << 1, | 162 TYPE_READ = 1 << 1, |
| 170 // Write request. | 163 // Write request. |
| 171 TYPE_WRITE = 1 << 2, | 164 TYPE_WRITE = 1 << 2, |
| 172 | 165 |
| 173 TYPE_READ_METADATA = TYPE_READ | TYPE_METADATA, | 166 TYPE_READ_METADATA = TYPE_READ | TYPE_METADATA, |
| 174 TYPE_WRITE_METADATA = TYPE_WRITE | TYPE_METADATA | 167 TYPE_WRITE_METADATA = TYPE_WRITE | TYPE_METADATA |
| 175 }; | 168 }; |
| 176 | 169 |
| 177 PendingReq(Type type, net::DrainableIOBuffer* io_buf, | 170 PendingReq(Type type, net::DrainableIOBuffer* io_buf, |
| 178 net::OldCompletionCallback* callback) | |
| 179 : type(type), | |
| 180 io_buf(io_buf), | |
| 181 old_callback(callback) { | |
| 182 switch (type) { | |
| 183 case PendingReq::TYPE_READ: | |
| 184 case PendingReq::TYPE_WRITE: | |
| 185 case PendingReq::TYPE_READ_METADATA: | |
| 186 case PendingReq::TYPE_WRITE_METADATA: { | |
| 187 DCHECK(io_buf); | |
| 188 break; | |
| 189 } | |
| 190 default: { | |
| 191 NOTREACHED(); | |
| 192 break; | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 PendingReq(Type type, net::DrainableIOBuffer* io_buf, | |
| 197 const net::CompletionCallback& callback) | 171 const net::CompletionCallback& callback) |
| 198 : type(type), | 172 : type(type), |
| 199 io_buf(io_buf), | 173 io_buf(io_buf), |
| 200 old_callback(NULL), | |
| 201 callback(callback) { | 174 callback(callback) { |
| 202 switch (type) { | 175 switch (type) { |
| 203 case PendingReq::TYPE_READ: | 176 case PendingReq::TYPE_READ: |
| 204 case PendingReq::TYPE_WRITE: | 177 case PendingReq::TYPE_WRITE: |
| 205 case PendingReq::TYPE_READ_METADATA: | 178 case PendingReq::TYPE_READ_METADATA: |
| 206 case PendingReq::TYPE_WRITE_METADATA: { | 179 case PendingReq::TYPE_WRITE_METADATA: { |
| 207 DCHECK(io_buf); | 180 DCHECK(io_buf); |
| 208 break; | 181 break; |
| 209 } | 182 } |
| 210 default: { | 183 default: { |
| 211 NOTREACHED(); | 184 NOTREACHED(); |
| 212 break; | 185 break; |
| 213 } | 186 } |
| 214 } | 187 } |
| 215 } | 188 } |
| 216 | 189 |
| 217 Type type; | 190 Type type; |
| 218 scoped_refptr<net::DrainableIOBuffer> io_buf; | 191 scoped_refptr<net::DrainableIOBuffer> io_buf; |
| 219 net::OldCompletionCallback* old_callback; | |
| 220 net::CompletionCallback callback; | 192 net::CompletionCallback callback; |
| 221 }; | 193 }; |
| 222 | 194 |
| 223 // Socket implementation. | 195 // Socket implementation. |
| 224 virtual int Read(net::IOBuffer* buf, int buf_len, | 196 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 225 net::OldCompletionCallback* callback) OVERRIDE { | |
| 226 if (buf_len == 0) | |
| 227 return 0; | |
| 228 if (buf == NULL || buf_len < 0) { | |
| 229 NOTREACHED(); | |
| 230 return net::ERR_INVALID_ARGUMENT; | |
| 231 } | |
| 232 while (int bytes_remaining = fill_handshake_buf_->BytesConsumed() - | |
| 233 process_handshake_buf_->BytesConsumed()) { | |
| 234 DCHECK(!is_transport_read_pending_); | |
| 235 DCHECK(GetPendingReq(PendingReq::TYPE_READ) == pending_reqs_.end()); | |
| 236 switch (phase_) { | |
| 237 case PHASE_FRAME_OUTSIDE: | |
| 238 case PHASE_FRAME_INSIDE: | |
| 239 case PHASE_FRAME_LENGTH: | |
| 240 case PHASE_FRAME_SKIP: { | |
| 241 int n = std::min(bytes_remaining, buf_len); | |
| 242 int rv = ProcessDataFrames( | |
| 243 process_handshake_buf_->data(), n, buf->data(), buf_len); | |
| 244 process_handshake_buf_->DidConsume(n); | |
| 245 if (rv == 0) { | |
| 246 // ProcessDataFrames may return zero for non-empty buffer if it | |
| 247 // contains only frame delimiters without real data. In this case: | |
| 248 // try again and do not just return zero (zero stands for EOF). | |
| 249 continue; | |
| 250 } | |
| 251 return rv; | |
| 252 } | |
| 253 case PHASE_SHUT: { | |
| 254 return 0; | |
| 255 } | |
| 256 case PHASE_NYMPH: | |
| 257 case PHASE_HANDSHAKE: | |
| 258 default: { | |
| 259 NOTREACHED(); | |
| 260 return net::ERR_UNEXPECTED; | |
| 261 } | |
| 262 } | |
| 263 } | |
| 264 switch (phase_) { | |
| 265 case PHASE_FRAME_OUTSIDE: | |
| 266 case PHASE_FRAME_INSIDE: | |
| 267 case PHASE_FRAME_LENGTH: | |
| 268 case PHASE_FRAME_SKIP: { | |
| 269 pending_reqs_.push_back(PendingReq( | |
| 270 PendingReq::TYPE_READ, | |
| 271 new net::DrainableIOBuffer(buf, buf_len), | |
| 272 callback)); | |
| 273 ConsiderTransportRead(); | |
| 274 break; | |
| 275 } | |
| 276 case PHASE_SHUT: { | |
| 277 return 0; | |
| 278 } | |
| 279 case PHASE_NYMPH: | |
| 280 case PHASE_HANDSHAKE: | |
| 281 default: { | |
| 282 NOTREACHED(); | |
| 283 return net::ERR_UNEXPECTED; | |
| 284 } | |
| 285 } | |
| 286 return net::ERR_IO_PENDING; | |
| 287 } | |
| 288 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 289 const net::CompletionCallback& callback) OVERRIDE { | 197 const net::CompletionCallback& callback) OVERRIDE { |
| 290 if (buf_len == 0) | 198 if (buf_len == 0) |
| 291 return 0; | 199 return 0; |
| 292 if (buf == NULL || buf_len < 0) { | 200 if (buf == NULL || buf_len < 0) { |
| 293 NOTREACHED(); | 201 NOTREACHED(); |
| 294 return net::ERR_INVALID_ARGUMENT; | 202 return net::ERR_INVALID_ARGUMENT; |
| 295 } | 203 } |
| 296 while (int bytes_remaining = fill_handshake_buf_->BytesConsumed() - | 204 while (int bytes_remaining = fill_handshake_buf_->BytesConsumed() - |
| 297 process_handshake_buf_->BytesConsumed()) { | 205 process_handshake_buf_->BytesConsumed()) { |
| 298 DCHECK(!is_transport_read_pending_); | 206 DCHECK(!is_transport_read_pending_); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 case PHASE_HANDSHAKE: | 252 case PHASE_HANDSHAKE: |
| 345 default: { | 253 default: { |
| 346 NOTREACHED(); | 254 NOTREACHED(); |
| 347 return net::ERR_UNEXPECTED; | 255 return net::ERR_UNEXPECTED; |
| 348 } | 256 } |
| 349 } | 257 } |
| 350 return net::ERR_IO_PENDING; | 258 return net::ERR_IO_PENDING; |
| 351 } | 259 } |
| 352 | 260 |
| 353 virtual int Write(net::IOBuffer* buf, int buf_len, | 261 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 354 net::OldCompletionCallback* callback) OVERRIDE { | 262 const net::CompletionCallback& callback) OVERRIDE { |
| 355 if (buf_len == 0) | 263 if (buf_len == 0) |
| 356 return 0; | 264 return 0; |
| 357 if (buf == NULL || buf_len < 0) { | 265 if (buf == NULL || buf_len < 0) { |
| 358 NOTREACHED(); | 266 NOTREACHED(); |
| 359 return net::ERR_INVALID_ARGUMENT; | 267 return net::ERR_INVALID_ARGUMENT; |
| 360 } | 268 } |
| 361 DCHECK_EQ(std::find(buf->data(), buf->data() + buf_len, '\xff'), | 269 DCHECK_EQ(std::find(buf->data(), buf->data() + buf_len, '\xff'), |
| 362 buf->data() + buf_len); | 270 buf->data() + buf_len); |
| 363 switch (phase_) { | 271 switch (phase_) { |
| 364 case PHASE_FRAME_OUTSIDE: | 272 case PHASE_FRAME_OUTSIDE: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 375 default: { | 283 default: { |
| 376 NOTREACHED(); | 284 NOTREACHED(); |
| 377 return net::ERR_UNEXPECTED; | 285 return net::ERR_UNEXPECTED; |
| 378 } | 286 } |
| 379 } | 287 } |
| 380 | 288 |
| 381 net::IOBuffer* frame_start = new net::IOBuffer(1); | 289 net::IOBuffer* frame_start = new net::IOBuffer(1); |
| 382 frame_start->data()[0] = '\x00'; | 290 frame_start->data()[0] = '\x00'; |
| 383 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, | 291 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, |
| 384 new net::DrainableIOBuffer(frame_start, 1), | 292 new net::DrainableIOBuffer(frame_start, 1), |
| 385 NULL)); | 293 net::CompletionCallback())); |
| 386 | 294 |
| 387 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE, | 295 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE, |
| 388 new net::DrainableIOBuffer(buf, buf_len), | 296 new net::DrainableIOBuffer(buf, buf_len), |
| 389 callback)); | 297 callback)); |
| 390 | 298 |
| 391 net::IOBuffer* frame_end = new net::IOBuffer(1); | 299 net::IOBuffer* frame_end = new net::IOBuffer(1); |
| 392 frame_end->data()[0] = '\xff'; | 300 frame_end->data()[0] = '\xff'; |
| 393 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, | 301 pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, |
| 394 new net::DrainableIOBuffer(frame_end, 1), | 302 new net::DrainableIOBuffer(frame_end, 1), |
| 395 NULL)); | 303 net::CompletionCallback())); |
| 396 | 304 |
| 397 ConsiderTransportWrite(); | 305 ConsiderTransportWrite(); |
| 398 return net::ERR_IO_PENDING; | 306 return net::ERR_IO_PENDING; |
| 399 } | 307 } |
| 400 | 308 |
| 401 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { | 309 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { |
| 402 return transport_socket_->SetReceiveBufferSize(size); | 310 return transport_socket_->SetReceiveBufferSize(size); |
| 403 } | 311 } |
| 404 | 312 |
| 405 virtual bool SetSendBufferSize(int32 size) OVERRIDE { | 313 virtual bool SetSendBufferSize(int32 size) OVERRIDE { |
| 406 return transport_socket_->SetSendBufferSize(size); | 314 return transport_socket_->SetSendBufferSize(size); |
| 407 } | 315 } |
| 408 | 316 |
| 409 // WebSocketServerSocket implementation. | 317 // WebSocketServerSocket implementation. |
| 410 virtual int Accept(net::OldCompletionCallback* callback) { | 318 virtual int Accept(net::OldCompletionCallback* callback) { |
| 411 if (phase_ != PHASE_NYMPH) | 319 if (phase_ != PHASE_NYMPH) |
| 412 return net::ERR_UNEXPECTED; | 320 return net::ERR_UNEXPECTED; |
| 413 phase_ = PHASE_HANDSHAKE; | 321 phase_ = PHASE_HANDSHAKE; |
| 322 net::CompletionCallback cb; |
| 323 if (callback) { |
| 324 cb = base::Bind(&net::OldCompletionCallback::Run<int>, |
| 325 base::Unretained(callback)); |
| 326 } |
| 414 pending_reqs_.push_front(PendingReq( | 327 pending_reqs_.push_front(PendingReq( |
| 415 PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(), callback)); | 328 PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(), |
| 329 cb)); |
| 416 ConsiderTransportRead(); | 330 ConsiderTransportRead(); |
| 417 return net::ERR_IO_PENDING; | 331 return net::ERR_IO_PENDING; |
| 418 } | 332 } |
| 419 | 333 |
| 420 std::deque<PendingReq>::iterator GetPendingReq(PendingReq::Type type) { | 334 std::deque<PendingReq>::iterator GetPendingReq(PendingReq::Type type) { |
| 421 for (std::deque<PendingReq>::iterator it = pending_reqs_.begin(); | 335 for (std::deque<PendingReq>::iterator it = pending_reqs_.begin(); |
| 422 it != pending_reqs_.end(); ++it) { | 336 it != pending_reqs_.end(); ++it) { |
| 423 if (it->type & type) | 337 if (it->type & type) |
| 424 return it; | 338 return it; |
| 425 } | 339 } |
| 426 return pending_reqs_.end(); | 340 return pending_reqs_.end(); |
| 427 } | 341 } |
| 428 | 342 |
| 429 void ConsiderTransportRead() { | 343 void ConsiderTransportRead() { |
| 430 if (pending_reqs_.empty()) | 344 if (pending_reqs_.empty()) |
| 431 return; | 345 return; |
| 432 if (is_transport_read_pending_) | 346 if (is_transport_read_pending_) |
| 433 return; | 347 return; |
| 434 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_READ); | 348 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_READ); |
| 435 if (it == pending_reqs_.end()) | 349 if (it == pending_reqs_.end()) |
| 436 return; | 350 return; |
| 437 if (it->io_buf == NULL || it->io_buf->BytesRemaining() == 0) { | 351 if (it->io_buf == NULL || it->io_buf->BytesRemaining() == 0) { |
| 438 NOTREACHED(); | 352 NOTREACHED(); |
| 439 return; | 353 return; |
| 440 } | 354 } |
| 441 is_transport_read_pending_ = true; | 355 is_transport_read_pending_ = true; |
| 442 int rv = transport_socket_->Read( | 356 int rv = transport_socket_->Read( |
| 443 it->io_buf.get(), it->io_buf->BytesRemaining(), | 357 it->io_buf.get(), it->io_buf->BytesRemaining(), |
| 444 transport_read_callback_.get()); | 358 base::Bind(&WebSocketServerSocketImpl::OnRead, |
| 359 base::Unretained(this))); |
| 445 if (rv != net::ERR_IO_PENDING) { | 360 if (rv != net::ERR_IO_PENDING) { |
| 446 // PostTask rather than direct call in order to: | 361 // PostTask rather than direct call in order to: |
| 447 // (1) guarantee calling callback after returning from Read(); | 362 // (1) guarantee calling callback after returning from Read(); |
| 448 // (2) avoid potential stack overflow; | 363 // (2) avoid potential stack overflow; |
| 449 MessageLoop::current()->PostTask(FROM_HERE, | 364 MessageLoop::current()->PostTask(FROM_HERE, |
| 450 method_factory_.NewRunnableMethod( | 365 method_factory_.NewRunnableMethod( |
| 451 &WebSocketServerSocketImpl::OnRead, rv)); | 366 &WebSocketServerSocketImpl::OnRead, rv)); |
| 452 } | 367 } |
| 453 } | 368 } |
| 454 | 369 |
| 455 void ConsiderTransportWrite() { | 370 void ConsiderTransportWrite() { |
| 456 if (is_transport_write_pending_) | 371 if (is_transport_write_pending_) |
| 457 return; | 372 return; |
| 458 if (pending_reqs_.empty()) | 373 if (pending_reqs_.empty()) |
| 459 return; | 374 return; |
| 460 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_WRITE); | 375 std::deque<PendingReq>::iterator it = GetPendingReq(PendingReq::TYPE_WRITE); |
| 461 if (it == pending_reqs_.end()) | 376 if (it == pending_reqs_.end()) |
| 462 return; | 377 return; |
| 463 if (it->io_buf == NULL || it->io_buf->BytesRemaining() == 0) { | 378 if (it->io_buf == NULL || it->io_buf->BytesRemaining() == 0) { |
| 464 NOTREACHED(); | 379 NOTREACHED(); |
| 465 Shut(net::ERR_UNEXPECTED); | 380 Shut(net::ERR_UNEXPECTED); |
| 466 return; | 381 return; |
| 467 } | 382 } |
| 468 is_transport_write_pending_ = true; | 383 is_transport_write_pending_ = true; |
| 469 int rv = transport_socket_->Write( | 384 int rv = transport_socket_->Write( |
| 470 it->io_buf.get(), it->io_buf->BytesRemaining(), | 385 it->io_buf.get(), it->io_buf->BytesRemaining(), |
| 471 transport_write_callback_.get()); | 386 base::Bind(&WebSocketServerSocketImpl::OnWrite, |
| 387 base::Unretained(this))); |
| 472 if (rv != net::ERR_IO_PENDING) { | 388 if (rv != net::ERR_IO_PENDING) { |
| 473 // PostTask rather than direct call in order to: | 389 // PostTask rather than direct call in order to: |
| 474 // (1) guarantee calling callback after returning from Read(); | 390 // (1) guarantee calling callback after returning from Read(); |
| 475 // (2) avoid potential stack overflow; | 391 // (2) avoid potential stack overflow; |
| 476 MessageLoop::current()->PostTask(FROM_HERE, | 392 MessageLoop::current()->PostTask(FROM_HERE, |
| 477 method_factory_.NewRunnableMethod( | 393 method_factory_.NewRunnableMethod( |
| 478 &WebSocketServerSocketImpl::OnWrite, rv)); | 394 &WebSocketServerSocketImpl::OnWrite, rv)); |
| 479 } | 395 } |
| 480 } | 396 } |
| 481 | 397 |
| 482 void Shut(int result) { | 398 void Shut(int result) { |
| 483 if (result > 0 || result == net::ERR_IO_PENDING) | 399 if (result > 0 || result == net::ERR_IO_PENDING) |
| 484 result = net::ERR_UNEXPECTED; | 400 result = net::ERR_UNEXPECTED; |
| 485 if (result != 0) { | 401 if (result != 0) { |
| 486 while (!pending_reqs_.empty()) { | 402 while (!pending_reqs_.empty()) { |
| 487 PendingReq& req = pending_reqs_.front(); | 403 PendingReq& req = pending_reqs_.front(); |
| 488 if (req.old_callback) | 404 if (!req.callback.is_null()) |
| 489 req.old_callback->Run(result); | |
| 490 else if (!req.callback.is_null()) | |
| 491 req.callback.Run(result); | 405 req.callback.Run(result); |
| 492 pending_reqs_.pop_front(); | 406 pending_reqs_.pop_front(); |
| 493 } | 407 } |
| 494 transport_socket_.reset(); // terminate underlying connection. | 408 transport_socket_.reset(); // terminate underlying connection. |
| 495 } | 409 } |
| 496 phase_ = PHASE_SHUT; | 410 phase_ = PHASE_SHUT; |
| 497 } | 411 } |
| 498 | 412 |
| 499 // Callbacks for transport socket. | 413 // Callbacks for transport socket. |
| 500 void OnRead(int result) { | 414 void OnRead(int result) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 530 NOTREACHED(); | 444 NOTREACHED(); |
| 531 Shut(net::ERR_UNEXPECTED); | 445 Shut(net::ERR_UNEXPECTED); |
| 532 return; | 446 return; |
| 533 } | 447 } |
| 534 fill_handshake_buf_->DidConsume(result); | 448 fill_handshake_buf_->DidConsume(result); |
| 535 // ProcessHandshake invalidates iterators for |pending_reqs_| | 449 // ProcessHandshake invalidates iterators for |pending_reqs_| |
| 536 int rv = ProcessHandshake(); | 450 int rv = ProcessHandshake(); |
| 537 if (rv > 0) { | 451 if (rv > 0) { |
| 538 process_handshake_buf_->DidConsume(rv); | 452 process_handshake_buf_->DidConsume(rv); |
| 539 phase_ = PHASE_FRAME_OUTSIDE; | 453 phase_ = PHASE_FRAME_OUTSIDE; |
| 540 net::OldCompletionCallback* old_cb = | |
| 541 pending_reqs_.front().old_callback; | |
| 542 net::CompletionCallback cb = pending_reqs_.front().callback; | 454 net::CompletionCallback cb = pending_reqs_.front().callback; |
| 543 pending_reqs_.pop_front(); | 455 pending_reqs_.pop_front(); |
| 544 ConsiderTransportWrite(); // Schedule answer handshake. | 456 ConsiderTransportWrite(); // Schedule answer handshake. |
| 545 if (old_cb) | 457 if (!cb.is_null()) |
| 546 old_cb->Run(0); | |
| 547 else if (!cb.is_null()) | |
| 548 cb.Run(0); | 458 cb.Run(0); |
| 549 } else if (rv == net::ERR_IO_PENDING) { | 459 } else if (rv == net::ERR_IO_PENDING) { |
| 550 if (fill_handshake_buf_->BytesRemaining() < 1) | 460 if (fill_handshake_buf_->BytesRemaining() < 1) |
| 551 Shut(net::ERR_LIMIT_VIOLATION); | 461 Shut(net::ERR_LIMIT_VIOLATION); |
| 552 } else if (rv < 0) { | 462 } else if (rv < 0) { |
| 553 Shut(rv); | 463 Shut(rv); |
| 554 } else { | 464 } else { |
| 555 Shut(net::ERR_UNEXPECTED); | 465 Shut(net::ERR_UNEXPECTED); |
| 556 } | 466 } |
| 557 break; | 467 break; |
| 558 } | 468 } |
| 559 case PHASE_FRAME_OUTSIDE: | 469 case PHASE_FRAME_OUTSIDE: |
| 560 case PHASE_FRAME_INSIDE: | 470 case PHASE_FRAME_INSIDE: |
| 561 case PHASE_FRAME_LENGTH: | 471 case PHASE_FRAME_LENGTH: |
| 562 case PHASE_FRAME_SKIP: { | 472 case PHASE_FRAME_SKIP: { |
| 563 int rv = ProcessDataFrames( | 473 int rv = ProcessDataFrames( |
| 564 it->io_buf->data(), result, | 474 it->io_buf->data(), result, |
| 565 it->io_buf->data(), it->io_buf->BytesRemaining()); | 475 it->io_buf->data(), it->io_buf->BytesRemaining()); |
| 566 if (rv < 0) { | 476 if (rv < 0) { |
| 567 Shut(rv); | 477 Shut(rv); |
| 568 return; | 478 return; |
| 569 } | 479 } |
| 570 if (rv > 0 || phase_ == PHASE_SHUT) { | 480 if (rv > 0 || phase_ == PHASE_SHUT) { |
| 571 net::OldCompletionCallback* old_cb = it->old_callback; | |
| 572 net::CompletionCallback cb = it->callback; | 481 net::CompletionCallback cb = it->callback; |
| 573 pending_reqs_.erase(it); | 482 pending_reqs_.erase(it); |
| 574 if (old_cb) | 483 if (!cb.is_null()) |
| 575 old_cb->Run(rv); | |
| 576 else if (!cb.is_null()) | |
| 577 cb.Run(rv); | 484 cb.Run(rv); |
| 578 } | 485 } |
| 579 break; | 486 break; |
| 580 } | 487 } |
| 581 case PHASE_NYMPH: | 488 case PHASE_NYMPH: |
| 582 default: { | 489 default: { |
| 583 NOTREACHED(); | 490 NOTREACHED(); |
| 584 Shut(net::ERR_UNEXPECTED); | 491 Shut(net::ERR_UNEXPECTED); |
| 585 break; | 492 break; |
| 586 } | 493 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 605 if (it == pending_reqs_.end() || | 512 if (it == pending_reqs_.end() || |
| 606 it->io_buf == NULL || | 513 it->io_buf == NULL || |
| 607 it->io_buf->data() == NULL) { | 514 it->io_buf->data() == NULL) { |
| 608 NOTREACHED(); | 515 NOTREACHED(); |
| 609 Shut(net::ERR_UNEXPECTED); | 516 Shut(net::ERR_UNEXPECTED); |
| 610 return; | 517 return; |
| 611 } | 518 } |
| 612 DCHECK_LE(result, it->io_buf->BytesRemaining()); | 519 DCHECK_LE(result, it->io_buf->BytesRemaining()); |
| 613 it->io_buf->DidConsume(result); | 520 it->io_buf->DidConsume(result); |
| 614 if (it->io_buf->BytesRemaining() == 0) { | 521 if (it->io_buf->BytesRemaining() == 0) { |
| 615 net::OldCompletionCallback* old_cb = it->old_callback; | |
| 616 net::CompletionCallback cb = it->callback; | 522 net::CompletionCallback cb = it->callback; |
| 617 int bytes_written = it->io_buf->BytesConsumed(); | 523 int bytes_written = it->io_buf->BytesConsumed(); |
| 618 DCHECK_GT(bytes_written, 0); | 524 DCHECK_GT(bytes_written, 0); |
| 619 pending_reqs_.erase(it); | 525 pending_reqs_.erase(it); |
| 620 if (old_cb) | 526 if (!cb.is_null()) |
| 621 old_cb->Run(bytes_written); | |
| 622 else if (!cb.is_null()) | |
| 623 cb.Run(bytes_written); | 527 cb.Run(bytes_written); |
| 624 } | 528 } |
| 625 ConsiderTransportWrite(); | 529 ConsiderTransportWrite(); |
| 626 } | 530 } |
| 627 | 531 |
| 628 // Returns (positive) number of consumed bytes on success. | 532 // Returns (positive) number of consumed bytes on success. |
| 629 // Returns ERR_IO_PENDING in case of incomplete input. | 533 // Returns ERR_IO_PENDING in case of incomplete input. |
| 630 // Returns ERR_WS_PROTOCOL_ERROR or ERR_LIMIT_VIOLATION in case of failure to | 534 // Returns ERR_WS_PROTOCOL_ERROR or ERR_LIMIT_VIOLATION in case of failure to |
| 631 // reasonably parse input. | 535 // reasonably parse input. |
| 632 int ProcessHandshake() { | 536 int ProcessHandshake() { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 return net::ERR_LIMIT_VIOLATION; | 782 return net::ERR_LIMIT_VIOLATION; |
| 879 buffer.WriteLine(tmp); | 783 buffer.WriteLine(tmp); |
| 880 } | 784 } |
| 881 buffer.WriteLine(""); | 785 buffer.WriteLine(""); |
| 882 buffer.Write(&challenge_response, sizeof(challenge_response)); | 786 buffer.Write(&challenge_response, sizeof(challenge_response)); |
| 883 | 787 |
| 884 if (!buffer.is_ok()) | 788 if (!buffer.is_ok()) |
| 885 return net::ERR_LIMIT_VIOLATION; | 789 return net::ERR_LIMIT_VIOLATION; |
| 886 | 790 |
| 887 pending_reqs_.push_back(PendingReq( | 791 pending_reqs_.push_back(PendingReq( |
| 888 PendingReq::TYPE_WRITE_METADATA, buffer, NULL)); | 792 PendingReq::TYPE_WRITE_METADATA, buffer, net::CompletionCallback())); |
| 889 DCHECK_GT(term_pos - buf, 0); | 793 DCHECK_GT(term_pos - buf, 0); |
| 890 return term_pos - buf; | 794 return term_pos - buf; |
| 891 } | 795 } |
| 892 | 796 |
| 893 // Removes frame delimiters and returns net number of data bytes (or error). | 797 // Removes frame delimiters and returns net number of data bytes (or error). |
| 894 // |out| may be equal to |buf|, in that case it is in-place operation. | 798 // |out| may be equal to |buf|, in that case it is in-place operation. |
| 895 int ProcessDataFrames(char* buf, int buf_len, char* out, int out_len) { | 799 int ProcessDataFrames(char* buf, int buf_len, char* out, int out_len) { |
| 896 if (out_len < buf_len) { | 800 if (out_len < buf_len) { |
| 897 NOTREACHED(); | 801 NOTREACHED(); |
| 898 return net::ERR_UNEXPECTED; | 802 return net::ERR_UNEXPECTED; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 970 Delegate* delegate_; | 874 Delegate* delegate_; |
| 971 | 875 |
| 972 // IOBuffer used to communicate with transport at initial stage. | 876 // IOBuffer used to communicate with transport at initial stage. |
| 973 scoped_refptr<net::IOBuffer> handshake_buf_; | 877 scoped_refptr<net::IOBuffer> handshake_buf_; |
| 974 scoped_refptr<net::DrainableIOBuffer> fill_handshake_buf_; | 878 scoped_refptr<net::DrainableIOBuffer> fill_handshake_buf_; |
| 975 scoped_refptr<net::DrainableIOBuffer> process_handshake_buf_; | 879 scoped_refptr<net::DrainableIOBuffer> process_handshake_buf_; |
| 976 | 880 |
| 977 // Pending io requests we need to complete. | 881 // Pending io requests we need to complete. |
| 978 std::deque<PendingReq> pending_reqs_; | 882 std::deque<PendingReq> pending_reqs_; |
| 979 | 883 |
| 980 // Callbacks from transport to us. | |
| 981 scoped_ptr<net::OldCompletionCallback> transport_read_callback_; | |
| 982 scoped_ptr<net::OldCompletionCallback> transport_write_callback_; | |
| 983 | |
| 984 // Whether transport requests are pending. | 884 // Whether transport requests are pending. |
| 985 bool is_transport_read_pending_; | 885 bool is_transport_read_pending_; |
| 986 bool is_transport_write_pending_; | 886 bool is_transport_write_pending_; |
| 987 | 887 |
| 988 ScopedRunnableMethodFactory<WebSocketServerSocketImpl> method_factory_; | 888 ScopedRunnableMethodFactory<WebSocketServerSocketImpl> method_factory_; |
| 989 | 889 |
| 990 DISALLOW_COPY_AND_ASSIGN(WebSocketServerSocketImpl); | 890 DISALLOW_COPY_AND_ASSIGN(WebSocketServerSocketImpl); |
| 991 }; | 891 }; |
| 992 | 892 |
| 993 } // namespace | 893 } // namespace |
| 994 | 894 |
| 995 namespace net { | 895 namespace net { |
| 996 | 896 |
| 997 WebSocketServerSocket* CreateWebSocketServerSocket( | 897 WebSocketServerSocket* CreateWebSocketServerSocket( |
| 998 Socket* transport_socket, WebSocketServerSocket::Delegate* delegate) { | 898 Socket* transport_socket, WebSocketServerSocket::Delegate* delegate) { |
| 999 return new WebSocketServerSocketImpl(transport_socket, delegate); | 899 return new WebSocketServerSocketImpl(transport_socket, delegate); |
| 1000 } | 900 } |
| 1001 | 901 |
| 1002 WebSocketServerSocket::~WebSocketServerSocket() { | 902 WebSocketServerSocket::~WebSocketServerSocket() { |
| 1003 } | 903 } |
| 1004 | 904 |
| 1005 } // namespace net; | 905 } // namespace net; |
| OLD | NEW |