Chromium Code Reviews| Index: net/server/http_connection.h |
| diff --git a/net/server/http_connection.h b/net/server/http_connection.h |
| index 17faa46eb6b77304085635364f4a73b65c3c7821..e196847f5d750669065cd0c204fc3de89703812d 100644 |
| --- a/net/server/http_connection.h |
| +++ b/net/server/http_connection.h |
| @@ -5,43 +5,121 @@ |
| #ifndef NET_SERVER_HTTP_CONNECTION_H_ |
| #define NET_SERVER_HTTP_CONNECTION_H_ |
| +#include <queue> |
| #include <string> |
| #include "base/basictypes.h" |
| #include "base/memory/scoped_ptr.h" |
| -#include "net/http/http_status_code.h" |
| +#include "net/base/io_buffer.h" |
| namespace net { |
| -class HttpServer; |
| -class HttpServerResponseInfo; |
| -class StreamListenSocket; |
| +class StreamSocket; |
| class WebSocket; |
| +// A container which has all information of an http connection. It includes |
| +// id, underlying socket, and pending read/write data. |
| class HttpConnection { |
| public: |
| - ~HttpConnection(); |
| + // IOBuffer for data read. Not to move data when part of data has been |
| + // consumed, It has 3 parts: |
| + // 1) consumed data: [base_->StartOfBuffer(), data()] |
| + // 2) unconsumed data: [data(), base_->data()] |
| + // 3) not used buffer: [base_->data(), |
| + // base_->data() + base_->RemainingCapacity()] |
| + class ReadIOBuffer : public IOBuffer { |
|
mmenke
2014/06/04 16:29:50
Why can't we just use GrowableIOBuffer?
byungchul
2014/06/04 17:41:28
GrowableIOBuffer can append more data at the end o
mmenke
2014/06/04 19:27:50
Growable IOBuffer has an "offset" which is the "be
byungchul
2014/06/04 20:51:26
Sure, I know. It moves the offset/pointer up to wh
byungchul
2014/06/06 19:17:35
Revised comment.
|
| + public: |
| + ReadIOBuffer(); |
| + |
| + // Returns IOBuffer to append more data read. |
| + IOBuffer* GetUnusedIOBuffer() const; |
| + // Returns capacity not used yet. |
| + int GetUnusedCapacity() const; |
| + |
| + // Capacity of |base_|. |
| + int GetCapacity() const; |
| + void SetCapacity(int capacity); |
| + // Increases capacity and returns true if capacity is not beyond the limit. |
| + bool IncreaseCapacity(); |
| + |
| + // More data is appended. Refresh |data_| if base_->data() has changed. |
| + void DidRead(int bytes); |
| + |
| + // Returns the bytes of unconsumed data. |
| + int GetUnconsumedSize() const; |
| + |
| + // Changes |data_| so that |data_| always points to the first unconsumed |
| + // byte. |
| + void DidConsume(int bytes); |
| + |
| + // Limit of how much internal capacity can increase. |
| + int capacity_limit() const { return capacity_limit_; } |
| + void set_capacity_limit(int limit) { capacity_limit_ = limit; } |
| + |
| + private: |
| + virtual ~ReadIOBuffer(); |
| + |
| + scoped_refptr<GrowableIOBuffer> base_; |
| + int capacity_limit_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ReadIOBuffer); |
| + }; |
| + |
| + // IOBuffer of pending data to write which has a queue of pending data. Each |
| + // pending data is stored in std::string. data() is the data of first |
| + // std::string stored. |
| + class PendingWriteIOBuffer : public IOBuffer { |
|
mmenke
2014/06/04 19:28:52
This class really should be unit tested independen
byungchul
2014/06/04 20:51:26
Sure, I added TODO in commit message. Will do TODO
|
| + public: |
| + PendingWriteIOBuffer(); |
| - void Send(const std::string& data); |
| - void Send(const char* bytes, int len); |
| - void Send(const HttpServerResponseInfo& response); |
| + // Whether or not pending data exists. |
| + bool IsEmpty() const; |
| - void Shift(int num_bytes); |
| + // Appends new pending data and returns true if total size doesn't exceed |
| + // the limit, |total_size_limit_|. It would change data() if new data is |
| + // the first pending data. |
| + bool Append(const std::string& data); |
| + |
| + // Consumes data and changes data() accordingly. |
| + void DidConsume(int size); |
| + |
| + // Gets size of data to write this time. It is NOT total data size. |
| + int GetSizeToWrite() const; |
| + |
| + int total_size() const { return total_size_; } |
| + // Limit of how much total_size can increase. |
| + int total_size_limit() const { return total_size_limit_; } |
| + void set_total_size_limit(int limit) { total_size_limit_ = limit; } |
| + |
| + private: |
| + virtual ~PendingWriteIOBuffer(); |
| + |
| + std::queue<std::string> pending_data_; |
| + int total_size_; |
| + int total_size_limit_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PendingWriteIOBuffer); |
| + }; |
| + |
| + HttpConnection(int id, scoped_ptr<StreamSocket> socket); |
| + ~HttpConnection(); |
| - const std::string& recv_data() const { return recv_data_; } |
| int id() const { return id_; } |
| + StreamSocket* socket() const { return socket_.get(); } |
| + ReadIOBuffer* read_buf() const { return read_buf_.get(); } |
| + PendingWriteIOBuffer* write_buf() const { return write_buf_.get(); } |
| - private: |
| - friend class HttpServer; |
| - static int last_id_; |
| + WebSocket* web_socket() const { return web_socket_.get(); } |
| + void SetWebSocket(scoped_ptr<WebSocket> web_socket); |
| - HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); |
| + private: |
| + const int id_; |
| + const scoped_ptr<StreamSocket> socket_; |
| + const scoped_refptr<ReadIOBuffer> read_buf_; |
| + const scoped_refptr<PendingWriteIOBuffer> write_buf_; |
| - HttpServer* server_; |
| - scoped_ptr<StreamListenSocket> socket_; |
| scoped_ptr<WebSocket> web_socket_; |
| - std::string recv_data_; |
| - int id_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
| }; |