#ifndef NET_SOCKET_TCP_SOCKET_WIN_H_
#define NET_SOCKET_TCP_SOCKET_WIN_H_
#include <stdint.h>
#include <winsock2.h>
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/threading/thread_checker.h"
#include "base/win/object_watcher.h"
#include "net/base/address_family.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_export.h"
#include "net/base/network_handle.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/socket_descriptor.h"
#include "net/socket/socket_performance_watcher.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
class AddressList;
class IOBuffer;
class IPEndPoint;
class NetLog;
struct NetLogSource;
class SocketTag;
class NET_EXPORT TCPSocketWin : public base::win::ObjectWatcher::Delegate {
public:
TCPSocketWin(
std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
NetLog* net_log,
const NetLogSource& source);
TCPSocketWin(
std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
NetLogWithSource net_log_source);
TCPSocketWin(const TCPSocketWin&) = delete;
TCPSocketWin& operator=(const TCPSocketWin&) = delete;
~TCPSocketWin() override;
int Open(AddressFamily family);
int AdoptConnectedSocket(SocketDescriptor socket,
const IPEndPoint& peer_address);
int AdoptUnconnectedSocket(SocketDescriptor socket);
int Bind(const IPEndPoint& address);
int Listen(int backlog);
int Accept(std::unique_ptr<TCPSocketWin>* socket,
IPEndPoint* address,
CompletionOnceCallback callback);
int Connect(const IPEndPoint& address, CompletionOnceCallback callback);
bool IsConnected() const;
bool IsConnectedAndIdle() const;
int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
int ReadIfReady(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
int CancelReadIfReady();
int Write(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation);
int GetLocalAddress(IPEndPoint* address) const;
int GetPeerAddress(IPEndPoint* address) const;
int SetDefaultOptionsForServer();
void SetDefaultOptionsForClient();
int SetExclusiveAddrUse();
int SetReceiveBufferSize(int32_t size);
int SetSendBufferSize(int32_t size);
bool SetKeepAlive(bool enable, int delay);
bool SetNoDelay(bool no_delay);
int SetIPv6Only(bool ipv6_only);
[[nodiscard]] bool GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const;
void Close();
bool IsValid() const { return socket_ != INVALID_SOCKET; }
void DetachFromThread();
void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
void EndLoggingMultipleConnectAttempts(int net_error);
const NetLogWithSource& net_log() const { return net_log_; }
SocketDescriptor ReleaseSocketDescriptorForTesting();
SocketDescriptor SocketDescriptorForTesting() const;
void ApplySocketTag(const SocketTag& tag);
int BindToNetwork(handles::NetworkHandle network);
SocketPerformanceWatcher* socket_performance_watcher() const {
return socket_performance_watcher_.get();
}
private:
class Core;
void OnObjectSignaled(HANDLE object) override;
int AcceptInternal(std::unique_ptr<TCPSocketWin>* socket,
IPEndPoint* address);
int DoConnect();
void DoConnectComplete(int result);
void LogConnectBegin(const AddressList& addresses);
void LogConnectEnd(int net_error);
void RetryRead(int rv);
void DidCompleteConnect();
void DidCompleteWrite();
void DidSignalRead();
SOCKET socket_;
std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_;
HANDLE accept_event_;
base::win::ObjectWatcher accept_watcher_;
raw_ptr<std::unique_ptr<TCPSocketWin>> accept_socket_ = nullptr;
raw_ptr<IPEndPoint> accept_address_ = nullptr;
CompletionOnceCallback accept_callback_;
bool waiting_connect_ = false;
bool waiting_read_ = false;
bool waiting_write_ = false;
scoped_refptr<Core> core_;
CompletionOnceCallback read_callback_;
CompletionOnceCallback read_if_ready_callback_;
CompletionOnceCallback write_callback_;
std::unique_ptr<IPEndPoint> peer_address_;
int connect_os_error_ = 0;
bool logging_multiple_connect_attempts_ = false;
NetLogWithSource net_log_;
THREAD_CHECKER(thread_checker_);
};
}
#endif