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 "net/base/completion_callback.h" | 10 #include "net/base/completion_callback.h" |
11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 class IOBuffer; | 15 class IOBuffer; |
16 | 16 |
17 // Represents a read/write socket. | 17 // Represents a read/write socket. |
18 class NET_EXPORT Socket { | 18 class NET_EXPORT Socket { |
19 public: | 19 public: |
| 20 // Name of the field trial for using ReadyIfReady() instead of Read(). |
| 21 static const char kReadIfReadyTrialName[]; |
| 22 |
20 virtual ~Socket() {} | 23 virtual ~Socket() {} |
21 | 24 |
22 // Reads data, up to |buf_len| bytes, from the socket. The number of bytes | 25 // Reads data, up to |buf_len| bytes, from the socket. The number of bytes |
23 // read is returned, or an error is returned upon failure. | 26 // read is returned, or an error is returned upon failure. |
24 // ERR_SOCKET_NOT_CONNECTED should be returned if the socket is not currently | 27 // 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 | 28 // 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 | 29 // 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 | 30 // 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 | 31 // 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 | 32 // operation is not completed immediately, the socket acquires a reference to |
30 // the provided buffer until the callback is invoked or the socket is | 33 // the provided buffer until the callback is invoked or the socket is |
31 // closed. If the socket is Disconnected before the read completes, the | 34 // closed. If the socket is Disconnected before the read completes, the |
32 // callback will not be invoked. | 35 // callback will not be invoked. |
33 virtual int Read(IOBuffer* buf, int buf_len, | 36 virtual int Read(IOBuffer* buf, int buf_len, |
34 const CompletionCallback& callback) = 0; | 37 const CompletionCallback& callback) = 0; |
35 | 38 |
| 39 // Reads as much data as possible into |buf| without blocking. Default |
| 40 // implementation returns ERR_READ_IF_READY_NOT_IMPLEMENTED. Caller should |
| 41 // fall back to Read() if receives ERR_READ_IF_READY_NOT_IMPLEMENTED. |
| 42 // Upon synchronous completion, returns the number of bytes read or an error |
| 43 // code if an error happens; If read cannot be completed synchronously, |
| 44 // returns ERR_IO_PENDING and does not hold on to |buf|. |callback| will be |
| 45 // invoked with OK when data can be read, at which point, caller can call |
| 46 // ReadIfReady() again; if an error occurs asynchronously, |callback| will be |
| 47 // invoked with the error code. |
| 48 virtual int ReadIfReady(IOBuffer* buf, |
| 49 int buf_len, |
| 50 const CompletionCallback& callback); |
| 51 |
36 // Writes data, up to |buf_len| bytes, to the socket. Note: data may be | 52 // 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 | 53 // 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 | 54 // is returned upon failure. ERR_SOCKET_NOT_CONNECTED should be returned if |
39 // the socket is not currently connected. The return value when the | 55 // 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 | 56 // 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 | 57 // 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 | 58 // 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 | 59 // operation is not completed immediately, the socket acquires a reference to |
44 // the provided buffer until the callback is invoked or the socket is | 60 // the provided buffer until the callback is invoked or the socket is |
45 // closed. Implementations of this method should not modify the contents | 61 // 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 | 62 // 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. | 63 // Disconnected before the write completes, the callback will not be invoked. |
48 virtual int Write(IOBuffer* buf, int buf_len, | 64 virtual int Write(IOBuffer* buf, int buf_len, |
49 const CompletionCallback& callback) = 0; | 65 const CompletionCallback& callback) = 0; |
50 | 66 |
51 // Set the receive buffer size (in bytes) for the socket. | 67 // Set the receive buffer size (in bytes) for the socket. |
52 // Note: changing this value can affect the TCP window size on some platforms. | 68 // Note: changing this value can affect the TCP window size on some platforms. |
53 // Returns a net error code. | 69 // Returns a net error code. |
54 virtual int SetReceiveBufferSize(int32_t size) = 0; | 70 virtual int SetReceiveBufferSize(int32_t size) = 0; |
55 | 71 |
56 // Set the send buffer size (in bytes) for the socket. | 72 // Set the send buffer size (in bytes) for the socket. |
57 // Note: changing this value can affect the TCP window size on some platforms. | 73 // Note: changing this value can affect the TCP window size on some platforms. |
58 // Returns a net error code. | 74 // Returns a net error code. |
59 virtual int SetSendBufferSize(int32_t size) = 0; | 75 virtual int SetSendBufferSize(int32_t size) = 0; |
60 }; | 76 }; |
61 | 77 |
62 } // namespace net | 78 } // namespace net |
63 | 79 |
64 #endif // NET_SOCKET_SOCKET_H_ | 80 #endif // NET_SOCKET_SOCKET_H_ |
OLD | NEW |