Chromium Code Reviews| 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 #ifndef NET_SOCKET_SOCKET_H_ | 5 #ifndef NET_SOCKET_SOCKET_H_ |
| 6 #define NET_SOCKET_SOCKET_H_ | 6 #define NET_SOCKET_SOCKET_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/feature_list.h" | |
| 10 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
| 11 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 class IOBuffer; | 16 class IOBuffer; |
| 16 | 17 |
| 17 // Represents a read/write socket. | 18 // Represents a read/write socket. |
| 18 class NET_EXPORT Socket { | 19 class NET_EXPORT Socket { |
| 19 public: | 20 public: |
| 21 // Name of the field trial for using ReadyIfReady() instead of Read(). | |
| 22 static const base::Feature kReadIfReadyExperiment; | |
| 23 | |
| 20 virtual ~Socket() {} | 24 virtual ~Socket() {} |
| 21 | 25 |
| 22 // Reads data, up to |buf_len| bytes, from the socket. The number of bytes | 26 // Reads data, up to |buf_len| bytes, from the socket. The number of bytes |
|
Bence
2017/03/03 16:33:40
Optional: add a note at the beginning of the comme
xunjieli
2017/03/03 19:41:05
There is a comment about this actually in the para
| |
| 23 // read is returned, or an error is returned upon failure. | 27 // read is returned, or an error is returned upon failure. |
| 24 // ERR_SOCKET_NOT_CONNECTED should be returned if the socket is not currently | 28 // ERR_SOCKET_NOT_CONNECTED should be returned if the socket is not currently |
| 25 // connected. Zero is returned once to indicate end-of-file; the return value | 29 // connected. Zero is returned once to indicate end-of-file; the return value |
| 26 // of subsequent calls is undefined, and may be OS dependent. ERR_IO_PENDING | 30 // of subsequent calls is undefined, and may be OS dependent. ERR_IO_PENDING |
| 27 // is returned if the operation could not be completed synchronously, in which | 31 // is returned if the operation could not be completed synchronously, in which |
| 28 // case the result will be passed to the callback when available. If the | 32 // case the result will be passed to the callback when available. If the |
| 29 // operation is not completed immediately, the socket acquires a reference to | 33 // operation is not completed immediately, the socket acquires a reference to |
| 30 // the provided buffer until the callback is invoked or the socket is | 34 // the provided buffer until the callback is invoked or the socket is |
| 31 // closed. If the socket is Disconnected before the read completes, the | 35 // closed. If the socket is Disconnected before the read completes, the |
| 32 // callback will not be invoked. | 36 // callback will not be invoked. |
| 33 virtual int Read(IOBuffer* buf, int buf_len, | 37 virtual int Read(IOBuffer* buf, int buf_len, |
|
Bence
2017/03/03 16:33:40
<rant>
I am unhappy with how a scoped_refptr is p
xunjieli
2017/03/03 19:41:05
Acknowledged. Rant acknowledged :)
| |
| 34 const CompletionCallback& callback) = 0; | 38 const CompletionCallback& callback) = 0; |
| 35 | 39 |
| 40 // Reads as much data as possible into |buf| without blocking. Default | |
|
Bence
2017/03/03 16:33:40
Optional: add a note at the beginning of the comme
Bence
2017/03/03 16:33:40
What happens if a caller calls ReadIfReady(), that
Bence
2017/03/03 16:33:40
If it reads as much data as possible, why does it
xunjieli
2017/03/03 19:41:05
I will keep it this way. I have a comment in the l
xunjieli
2017/03/03 19:41:05
Yep, it reads at most |buf_len_|. Clarified in the
xunjieli
2017/03/03 19:41:05
It should be the same as calling Read() twice when
| |
| 41 // implementation returns ERR_READ_IF_READY_NOT_IMPLEMENTED. Caller should | |
| 42 // fall back to Read() if receives ERR_READ_IF_READY_NOT_IMPLEMENTED. | |
| 43 // Upon synchronous completion, returns the number of bytes read or an error | |
| 44 // code if an error happens; If read cannot be completed synchronously, | |
|
Bence
2017/03/03 16:33:40
Optional: replace every semicolon with full stop t
xunjieli
2017/03/03 19:41:05
Done.
| |
| 45 // returns ERR_IO_PENDING and does not hold on to |buf|. |callback| will be | |
| 46 // invoked with OK when data can be read, at which point, caller can call | |
|
Bence
2017/03/03 16:33:40
Suppose ReadIfReady() returns ERR_IO_PENDING, late
xunjieli
2017/03/03 19:41:05
Nope. it's not guaranteed. It's legal for an imple
| |
| 47 // ReadIfReady() again; if an error occurs asynchronously, |callback| will be | |
| 48 // invoked with the error code. | |
| 49 // Note: if 0 is returned synchronously, it means that EOF is reached; if 0/OK | |
|
Bence
2017/03/03 16:33:40
Optional: move "if 0 is returned synchronously" in
xunjieli
2017/03/03 19:41:05
Done.
| |
| 50 // is returned asynchronously, it means that ReadIfReady() can be retried. | |
|
Bence
2017/03/03 16:33:40
Remove "0 is returned asynchronously" case because
xunjieli
2017/03/03 19:41:05
Done.
| |
| 51 virtual int ReadIfReady(IOBuffer* buf, | |
| 52 int buf_len, | |
| 53 const CompletionCallback& callback); | |
| 54 | |
| 36 // Writes data, up to |buf_len| bytes, to the socket. Note: data may be | 55 // Writes data, up to |buf_len| bytes, to the socket. Note: data may be |
| 37 // written partially. The number of bytes written is returned, or an error | 56 // written partially. The number of bytes written is returned, or an error |
| 38 // is returned upon failure. ERR_SOCKET_NOT_CONNECTED should be returned if | 57 // is returned upon failure. ERR_SOCKET_NOT_CONNECTED should be returned if |
| 39 // the socket is not currently connected. The return value when the | 58 // the socket is not currently connected. The return value when the |
| 40 // connection is closed is undefined, and may be OS dependent. ERR_IO_PENDING | 59 // connection is closed is undefined, and may be OS dependent. ERR_IO_PENDING |
| 41 // is returned if the operation could not be completed synchronously, in which | 60 // is returned if the operation could not be completed synchronously, in which |
| 42 // case the result will be passed to the callback when available. If the | 61 // case the result will be passed to the callback when available. If the |
| 43 // operation is not completed immediately, the socket acquires a reference to | 62 // operation is not completed immediately, the socket acquires a reference to |
| 44 // the provided buffer until the callback is invoked or the socket is | 63 // the provided buffer until the callback is invoked or the socket is |
| 45 // closed. Implementations of this method should not modify the contents | 64 // closed. Implementations of this method should not modify the contents |
| 46 // of the actual buffer that is written to the socket. If the socket is | 65 // of the actual buffer that is written to the socket. If the socket is |
| 47 // Disconnected before the write completes, the callback will not be invoked. | 66 // Disconnected before the write completes, the callback will not be invoked. |
| 48 virtual int Write(IOBuffer* buf, int buf_len, | 67 virtual int Write(IOBuffer* buf, int buf_len, |
| 49 const CompletionCallback& callback) = 0; | 68 const CompletionCallback& callback) = 0; |
| 50 | 69 |
| 51 // Set the receive buffer size (in bytes) for the socket. | 70 // Set the receive buffer size (in bytes) for the socket. |
| 52 // Note: changing this value can affect the TCP window size on some platforms. | 71 // Note: changing this value can affect the TCP window size on some platforms. |
| 53 // Returns a net error code. | 72 // Returns a net error code. |
| 54 virtual int SetReceiveBufferSize(int32_t size) = 0; | 73 virtual int SetReceiveBufferSize(int32_t size) = 0; |
| 55 | 74 |
| 56 // Set the send buffer size (in bytes) for the socket. | 75 // Set the send buffer size (in bytes) for the socket. |
| 57 // Note: changing this value can affect the TCP window size on some platforms. | 76 // Note: changing this value can affect the TCP window size on some platforms. |
| 58 // Returns a net error code. | 77 // Returns a net error code. |
| 59 virtual int SetSendBufferSize(int32_t size) = 0; | 78 virtual int SetSendBufferSize(int32_t size) = 0; |
| 60 }; | 79 }; |
| 61 | 80 |
| 62 } // namespace net | 81 } // namespace net |
| 63 | 82 |
| 64 #endif // NET_SOCKET_SOCKET_H_ | 83 #endif // NET_SOCKET_SOCKET_H_ |
| OLD | NEW |