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 3 // found in the LICENSE file. 4 5 #ifndef NET_SOCKET_SOCKET_H_ 6 #define NET_SOCKET_SOCKET_H_ 7 8 #include <stdint.h> 9 10 #include "net/base/completion_once_callback.h" 11 #include "net/base/net_export.h" 12 #include "net/traffic_annotation/network_traffic_annotation.h" 13 14 namespace net { 15 16 class IOBuffer; 17 18 // Represents a read/write socket. 19 class NET_EXPORT Socket { 20 public: ~Socket()21 virtual ~Socket() {} 22 23 // Reads data, up to |buf_len| bytes, from the socket. The number of bytes 24 // read is returned, or an error is returned upon failure. 25 // ERR_SOCKET_NOT_CONNECTED should be returned if the socket is not currently 26 // connected. Zero is returned once to indicate end-of-file; the return value 27 // of subsequent calls is undefined, and may be OS dependent. ERR_IO_PENDING 28 // is returned if the operation could not be completed synchronously, in which 29 // case the result will be passed to the callback when available. If the 30 // operation is not completed immediately, the socket acquires a reference to 31 // the provided buffer until the callback is invoked or the socket is 32 // closed. If the socket is Disconnected before the read completes, the 33 // callback will not be invoked. 34 virtual int Read(IOBuffer* buf, 35 int buf_len, 36 CompletionOnceCallback callback) = 0; 37 38 // Reads data, up to |buf_len| bytes, into |buf| without blocking. Default 39 // implementation returns ERR_READ_IF_READY_NOT_IMPLEMENTED. Caller should 40 // fall back to Read() if receives ERR_READ_IF_READY_NOT_IMPLEMENTED. 41 // Upon synchronous completion, returns the number of bytes read, or 0 on EOF, 42 // or an error code if an error happens. If read cannot be completed 43 // synchronously, returns ERR_IO_PENDING and does not hold on to |buf|. 44 // |callback| will be invoked with OK when data can be read, at which point, 45 // caller can call ReadIfReady() again. If an error occurs asynchronously, 46 // |callback| will be invoked with the error code. 47 virtual int ReadIfReady(IOBuffer* buf, 48 int buf_len, 49 CompletionOnceCallback callback); 50 51 // Cancels a pending ReadIfReady(). May only be called when a ReadIfReady() is 52 // pending. Returns net::OK or an error code. ERR_READ_IF_READY_NOT_SUPPORTED 53 // is returned if ReadIfReady() is not supported. 54 virtual int CancelReadIfReady(); 55 56 // Writes data, up to |buf_len| bytes, to the socket. Note: data may be 57 // written partially. The number of bytes written is returned, or an error 58 // is returned upon failure. ERR_SOCKET_NOT_CONNECTED should be returned if 59 // the socket is not currently connected. The return value when the 60 // connection is closed is undefined, and may be OS dependent. ERR_IO_PENDING 61 // is returned if the operation could not be completed synchronously, in which 62 // case the result will be passed to the callback when available. If the 63 // operation is not completed immediately, the socket acquires a reference to 64 // the provided buffer until the callback is invoked or the socket is 65 // closed. Implementations of this method should not modify the contents 66 // of the actual buffer that is written to the socket. If the socket is 67 // Disconnected before the write completes, the callback will not be invoked. 68 // |traffic_annotation| provides the required description for auditing. Please 69 // refer to //docs/network_traffic_annotations.md for more details. 70 virtual int Write(IOBuffer* buf, 71 int buf_len, 72 CompletionOnceCallback callback, 73 const NetworkTrafficAnnotationTag& traffic_annotation) = 0; 74 75 // Set the receive buffer size (in bytes) for the socket. 76 // Note: changing this value can affect the TCP window size on some platforms. 77 // Returns a net error code. 78 virtual int SetReceiveBufferSize(int32_t size) = 0; 79 80 // Set the send buffer size (in bytes) for the socket. 81 // Note: changing this value can affect the TCP window size on some platforms. 82 // Returns a net error code. 83 virtual int SetSendBufferSize(int32_t size) = 0; 84 }; 85 86 } // namespace net 87 88 #endif // NET_SOCKET_SOCKET_H_ 89