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