socket.h 4.6 KB

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