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
|
// Copyright (c) 2011 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.
#ifndef NET_WEBSOCKETS_WEBSOCKET_FRAME_HANDLER_H_
#define NET_WEBSOCKETS_WEBSOCKET_FRAME_HANDLER_H_
#pragma once
#include <deque>
#include <vector>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
namespace net {
class IOBuffer;
class IOBufferWithSize;
// Handles WebSocket frame messages.
class WebSocketFrameHandler {
public:
struct FrameInfo {
const char* frame_start;
int frame_length;
const char* message_start;
int message_length;
};
WebSocketFrameHandler();
~WebSocketFrameHandler();
// Appends WebSocket raw data on connection.
// For sending, this is data from WebKit.
// For receiving, this is data from network.
void AppendData(const char* data, int len);
// Updates current IOBuffer.
// If |buffered| is true, it tries to find WebSocket frames.
// Otherwise, it just picks the first buffer in |pending_buffers_|.
// Returns available size of data, 0 if no more data or current buffer was
// not released, and negative if some error occurred.
int UpdateCurrentBuffer(bool buffered);
// Gets current IOBuffer.
// For sending, this is data to network.
// For receiving, this is data to WebKit.
// Returns NULL just after ReleaseCurrentBuffer() was called.
IOBuffer* GetCurrentBuffer() { return current_buffer_.get(); }
int GetCurrentBufferSize() const { return current_buffer_size_; }
// Returns original buffer size of current IOBuffer.
// This might differ from GetCurrentBufferSize() if frame message is
// compressed or decompressed.
int GetOriginalBufferSize() const { return original_current_buffer_size_; }
// Releases current IOBuffer.
void ReleaseCurrentBuffer();
// Parses WebSocket frame in [|buffer|, |buffer|+|size|), fills frame
// information in |frame_info|, and returns number of bytes for
// complete WebSocket frames.
static int ParseWebSocketFrame(const char* buffer, int size,
std::vector<FrameInfo>* frame_info);
private:
typedef std::deque< scoped_refptr<IOBufferWithSize> > PendingDataQueue;
scoped_refptr<IOBuffer> current_buffer_;
int current_buffer_size_;
int original_current_buffer_size_;
// Deque of IOBuffers in pending.
PendingDataQueue pending_buffers_;
DISALLOW_COPY_AND_ASSIGN(WebSocketFrameHandler);
};
} // namespace net
#endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_HANDLER_H_
|